-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathobjects.php
1193 lines (1107 loc) · 41.1 KB
/
objects.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
date_default_timezone_set('America/Puerto_Rico');
function br()
{return (PHP_SAPI === 'cli' ? "\n" : "<br />");}
class Util
{
public $client;
public $fio_public_key;
public $actor;
public $balance;
public $transfer_fee;
public $domain_fee;
public $address_fee;
public $transfer_domain_fee;
public $fio_addresses;
public function __construct($client)
{
$this->client = $client;
}
public function chainGet($endpoint, $params)
{
$results = [];
try {
$response = $this->client->post('/v1/chain/' . $endpoint, [
GuzzleHttp\RequestOptions::JSON => $params,
]);
$results = json_decode($response->getBody(), true);
} catch (Exception $e) {}
return $results;
}
public function getFIOPublicKey()
{
if ($this->fio_public_key) {
return $this->fio_public_key;
}
$params = array(
"account_name" => $this->actor,
);
try {
$response = $this->chainGet('get_account', $params);
//var_dump($response);
foreach ($response['permissions'] as $key => $permission) {
//var_dump($permission);
if ($permission['perm_name'] == "active") {
if (isset($permission['required_auth']['keys'][0])) {
$this->fio_public_key = $permission['required_auth']['keys'][0]['key'];
}
}
}
} catch (\Exception $e) {
//print $e->getMessage() . "\n";
}
return $this->fio_public_key;
}
public function getFIOBalance()
{
if ($this->balance) {
return $this->balance;
}
$this->getFIOPublicKey();
$params = ["fio_public_key" => $this->fio_public_key];
$fio_balance_results = $this->chainGet('get_fio_balance', $params);
$balance = $fio_balance_results['balance'];
$balance = $balance / 1000000000;
$this->balance = $balance;
return $this->balance;
}
public function getFIOAddresses()
{
if ($this->fio_addresses) {
return $this->fio_addresses;
}
$this->getFIOPublicKey();
$params = ["fio_public_key" => $this->fio_public_key];
$results = $this->chainGet('get_fio_names', $params);
if (count($results) != 0) {
$this->fio_addresses = $results['fio_addresses'];
} else {
$this->fio_addresses = [];
}
return $this->fio_addresses;
}
public function getDomainOwner($domain)
{
$hash = substr(sha1($domain), 0, 32);
$hashArray = array_reverse(str_split($hash, 2));
$hashHex = "0x" . implode($hashArray);
$params = [
"code" => 'fio.address',
"scope" => 'fio.address',
"table" => 'domains',
"lower_bound" => $hashHex,
"upper_bound" => $hashHex,
"key_type" => 'i128',
"index_position" => 4,
"json" => true,
];
$results = $this->chainGet('get_table_rows', $params);
$account = $results['rows'][0]["account"];
return $account;
}
public function getAccountPermissions($account)
{
$params = array(
"account_name" => $account,
);
try {
$accounts = array();
$response = $this->chainGet('get_account', $params);
//var_dump($response);
foreach ($response['permissions'] as $key => $permission) {
//var_dump($permission);
if ($permission['perm_name'] == "active") {
foreach ($permission['required_auth']['accounts'] as $key => $account) {
$accounts[] = $account['permission']['actor'];
}
}
}
} catch (\Exception $e) {
//print $e->getMessage() . "\n";
}
return $accounts;
}
public function isMemberOnChain($session, $domain)
{
$is_member = (isset($session["username"])
&& isset($session['member_status'])
&& isset($session['member_status'][$domain])
&& $session['member_status'][$domain]['owns_member_address']
);
return $is_member;
}
public function isAdminOnChain($session, $domain)
{
$is_admin = (isset($session["username"])
&& isset($session['member_status'])
&& isset($session['member_status'][$domain])
&& $session['member_status'][$domain]['owns_member_address']
&& $session['member_status'][$domain]['is_admin']
);
return $is_admin;
}
public function getMemberStatusOfDomain($domain, $fio_address = '')
{
$membership = array(
'fio_address' => $fio_address,
'domain' => $domain,
'domain_account' => '',
'is_admin' => false,
'owns_member_address' => false,
);
$fio_addresses = $this->getFIOAddresses();
foreach ($fio_addresses as $key => $on_chain_fio_address) {
if ($on_chain_fio_address['fio_address'] == $fio_address && strpos($fio_address, "@" . $domain) !== false) {
$membership['owns_member_address'] = true;
}
}
$membership['domain_account'] = $this->getDomainOwner($domain);
$accounts = $this->getAccountPermissions($membership['domain_account']);
if (in_array($this->actor, $accounts)) {
$membership['is_admin'] = true;
}
return $membership;
}
public function getFee($endpoint, $fio_address)
{
$fee = 0;
try {
$params = array(
"end_point" => $endpoint,
"fio_address" => $fio_address,
);
$results = $this->chainGet('get_fee', $params);
$fee = $results['fee'];
} catch (\Exception $e) {}
return $fee + ($fee * .1); // add 10% extra in case something changes between now and when it is executed.
}
public function getPendingMsig($account, $membership_proposal_name)
{
try {
$params = array(
"json" => true,
"code" => "eosio.msig",
"scope" => $account,
"table" => "proposal",
"lower_bound" => $membership_proposal_name,
"upper_bound" => "",
"index_position" => 1,
"key_type" => "name",
"limit" => 1,
);
$results = $this->chainGet('get_table_rows', $params);
} catch (\Exception $e) {}
return $results;
}
public function getTransferFee()
{
if ($this->transfer_fee) {
return $this->transfer_fee;
}
$this->transfer_fee = $this->getFee("transfer_tokens_pub_key", "faucet@stokes");
return $this->transfer_fee;
}
public function getRegisterDomainFee()
{
if ($this->domain_fee) {
return $this->domain_fee;
}
$this->domain_fee = $this->getFee("register_fio_domain", "faucet@stokes");
return $this->domain_fee;
}
public function getRegisterAddressFee()
{
if ($this->address_fee) {
return $this->address_fee;
}
$this->address_fee = $this->getFee("register_fio_address", "faucet@stokes");
return $this->address_fee;
}
public function getTransferDomainFee()
{
if ($this->transfer_domain_fee) {
return $this->transfer_domain_fee;
}
$this->transfer_domain_fee = $this->getFee("transfer_fio_domain", "faucet@stokes");
return $this->transfer_domain_fee;
}
public function FIOToSUF($amount)
{
return ($amount * 1000000000);
}
public function SUFToFIO($amount)
{
return ($amount / 1000000000);
}
public function getProposalName()
{
$proposal_name = "";
for ($i = 0; $i < 7; $i++) {
$proposal_name .= rand(1, 5);
}
return "apply"+$proposal_name;
}
}
class Factory
{
public $dataDir;
public function __construct($dataDir = null)
{
$this->dataDir = __DIR__ . "/data";
if ($dataDir) {
$this->dataDir = $dataDir;
}
}
function new ($object_type) {
return new $object_type($object_type, $this);
}
}
class BaseObject
{
public $_id;
public $internal_fields = array('internal_fields', 'non_printable_fields', 'sort_field', 'dataDir', 'dataStore', 'factory');
public $non_printable_fields = array();
public $sort_field = "_id";
public $dataDir;
public $dataStore;
public $factory;
public function __construct($object_type, $factory)
{
$this->dataDir = $factory->dataDir;
$this->dataStore = new \SleekDB\Store($object_type, $this->dataDir);
$this->factory = $factory;
$this->non_printable_fields = array_merge($this->internal_fields, $this->non_printable_fields);
}
public function getData()
{
$object_data = get_object_vars($this);
foreach ($this->internal_fields as $internal_field) {
unset($object_data[$internal_field]);
}
return $object_data;
}
public function getPrintableFields()
{
$object_data = get_object_vars($this);
foreach ($this->non_printable_fields as $non_printable_field) {
unset($object_data[$non_printable_field]);
}
return $object_data;
}
public function save()
{
$object_data = $this->getData();
if ($object_data["_id"]) {
$this->dataStore->update($object_data);
} else {
unset($object_data["_id"]);
$new_object_data = $this->dataStore->insert($object_data);
$this->_id = $new_object_data["_id"];
}
}
public function delete()
{
if ($this->_id) {
$this->dataStore->deleteById($this->_id);
}
}
public function loadData($data)
{
$object_data = $this->getData();
foreach ($object_data as $key => $value) {
if (array_key_exists($key, $data)) {
$this->$key = $data[$key];
}
}
}
public function read($criteria = null)
{
$object_data = null;
if ($this->_id) {
$object_data = $this->dataStore->findById($this->_id);
} elseif ($criteria) {
$object_data = $this->dataStore->findOneBy($criteria);
}
if (!is_null($object_data)) {
$this->loadData($object_data);
}
return !is_null($object_data);
}
public function readAll($criteria)
{
$objects = array();
$queryBuilder = $this->dataStore->createQueryBuilder();
$objects_data = $queryBuilder
->where($criteria)
->orderBy([$this->sort_field => "asc"])
->getQuery()
->fetch();
foreach ($objects_data as $object_data) {
$Object = $this->factory->new(get_class($this));
$Object->loadData($object_data);
$objects[] = $Object;
}
return $objects;
}
function print($format = "", $controls = array()) {
$object_data = $this->getData();
$printable_object_data = $this->getPrintableFields();
$display_keys = array();
foreach ($object_data as $key => $value) {
$display_keys[] = '$' . $key;
}
$values_to_display = array();
foreach ($printable_object_data as $key => $value) {
$value_to_display = $value;
if (substr($key, 0, 3) == "is_") {
if ($value) {
$value_to_display = "true";
} else {
$value_to_display = "false";
}
} elseif (strpos($key, "date") !== false && $key != "candidate_account") {
if ($value) {
$value_to_display = date("Y-m-d H:i:s", $value);
} else {
$value_to_display = "";
}
}
$values_to_display[$key] = $value_to_display;
$object_data[$key] = $value_to_display;
}
$updated_values_to_display = array();
foreach ($values_to_display as $key => $value_to_display) {
$updated_value_to_display = $value_to_display;
if (array_key_exists($key, $controls)) {
$updated_value_to_display = str_replace(
$display_keys,
$object_data,
$controls[$key]);
}
$updated_values_to_display[$key] = $updated_value_to_display;
}
$values_to_display = $updated_values_to_display;
if ($format == "") {
foreach ($values_to_display as $key => $value_to_display) {
print "<b>" . ucwords(str_replace("_", " ", $key)) . "</b>: " . $value_to_display . br();
}
}
if ($format == "table") {
print "<tr>\n";
foreach ($values_to_display as $key => $value_to_display) {
print "<td>";
print $value_to_display;
print "</td>\n";
}
print "</tr>\n";
}
if ($format == "table_header") {
print "<tr>\n";
foreach ($printable_object_data as $key => $value) {
print "<th>";
print ucwords(str_replace("_", " ", $key));
print "</th>\n";
}
print "</tr>\n";
}
}
public function formHTML($values = array())
{
$object_data = $this->getPrintableFields();
unset($object_data["_id"]);
if (array_key_exists("epoch", $object_data)) {
unset($object_data["epoch"]);
}
$form_html = '';
foreach ($object_data as $key => $value) {
$form_value = $value;
if (array_key_exists($key, $values)) {
$form_value = $values[$key];
}
if (substr($key, 0, 3) != "is_" && strpos($key, "date") === false) {
$form_html .= '
<div class="col-sm-2">
<div class="form-group">
<label>' . ucwords(str_replace("_", " ", $key)) . '</label>
<input type="text" name="' . $key . '" id="' . $key . '" class="form-control" value="' . $form_value . '">
</div>
</div>';
}
}
return $form_html;
}
}
class Group extends BaseObject
{
public $domain;
/**
* FIO Public Key of the account which owns the domain.
* Used for historical purposes once account becomes an msig.
*/
public $group_fio_public_key;
/**
* NEW
*/
public $group_account;
/**
* in SUFs
*/
public $member_application_fee;
/**
* NEW
* Used to keep track of the current election
*/
public $epoch;
public $date_created;
/**
* Throws exception
*/
public function create($creator_account, $creator_member_name, $fio_public_key, $group_account, $domain, $member_application_fee)
{
$Group = $this->factory->new("Group");
$criteria = ["domain", "=", $domain];
$found = $Group->read($criteria);
if ($found) {
throw new Exception("A group for domain " . $domain . " already exists.", 1);
}
/*
create key pair
send funds to an address to create the domain and account there
change permissions on the account to be the account who created it
create domain in that qccount
create fio address in that account
*/
// TODO: check to see if this key has an account yet
$Group->group_fio_public_key = $fio_public_key;
// TODO: check to see if this domain exists on chain yet
// TODO: support existing domains if owned by the key?
// TODO: create domain if needed
$membership_payment_transaction_id = 12345;
$Group->domain = $domain;
// TODO: should I calculate this automatically using the pub key?
$Group->group_account = $group_account;
$Group->member_application_fee = $member_application_fee;
$Group->date_created = time();
$Group->epoch = 1;
$Group->save();
// TODO: adjust the permissions of the group so that $creator_account is the owner
$bio = "I am Satoshi.";
$proposal_name = "creator";
$Group->apply($creator_account, $creator_member_name, $bio, $membership_payment_transaction_id, $proposal_name);
$Group->approve($creator_account);
$members = $Group->getMembers();
$members[0]->is_admin = true;
$members[0]->save();
return $Group;
}
public function getPendingMembers()
{
$PendingMember = $this->factory->new("PendingMember");
$criteria = [["domain", "=", $this->domain]];
$pendingmembers = $PendingMember->readAll($criteria);
return $pendingmembers;
}
public function getMembers()
{
$Member = $this->factory->new("Member");
$criteria = [["domain", "=", $this->domain], ["is_active", "=", true]];
$members = $Member->readAll($criteria);
return $members;
}
public function getInactiveMembers()
{
$Member = $this->factory->new("Member");
$criteria = [["domain", "=", $this->domain], ["is_active", "=", false]];
$members = $Member->readAll($criteria);
return $members;
}
public function getDisabledMembers()
{
$Member = $this->factory->new("Member");
$criteria = [["domain", "=", $this->domain], ["is_disabled", "=", true]];
$members = $Member->readAll($criteria);
return $members;
}
public function getAdmins()
{
$Member = $this->factory->new("Member");
$criteria = [["domain", "=", $this->domain], ["is_active", "=", true], ["is_admin", "=", true]];
$admins = $Member->readAll($criteria);
return $admins;
}
public function getAdminCandidates()
{
$AdminCandidate = $this->factory->new("AdminCandidate");
$criteria = [["domain", "=", $this->domain]];
$admincandidates = $AdminCandidate->readAll($criteria);
return $admincandidates;
}
public function getApplicatonFee($client)
{
$fee = 0;
try {
$params = array(
"end_point" => "register_fio_address",
"fio_address" => "",
);
$response = $client->post('/v1/chain/get_fee', [
GuzzleHttp\RequestOptions::JSON => $params,
]);
$result = json_decode($response->getBody());
$fee = $result->fee;
} catch (\Exception $e) {}
return ($this->member_application_fee + $fee);
}
public function apply($account, $member_name_requested, $bio, $membership_payment_transaction_id, $membership_proposal_name)
{
$PendingMember = $this->factory->new("PendingMember");
// TODO: validate $membership_payment_transaction_id
// TODO: double check on chain member_name_requested hasn't been claimed already.
$criteria = [["domain", "=", $this->domain], ["account", "=", $account]];
$found = $PendingMember->read($criteria);
if ($found) {
throw new Exception($account . " is already a pending member for " . $this->domain . ".", 1);
}
$criteria = [["domain", "=", $this->domain], ["member_name_requested", "=", $member_name_requested]];
$found = $PendingMember->read($criteria);
if ($found) {
throw new Exception($member_name_requested . "@" . $this->domain . " has already been requested by a pending member of " . $this->domain . ".", 1);
}
$Member = $this->factory->new("Member");
$criteria = [["domain", "=", $this->domain], ["account", "=", $account]];
$found = $Member->read($criteria);
if ($found) {
throw new Exception($account . " is already a member of " . $this->domain . ".", 1);
}
$criteria = [["domain", "=", $this->domain], ["member_name", "=", $member_name_requested]];
$found = $Member->read($criteria);
if ($found) {
throw new Exception($member_name_requested . "@" . $this->domain . " has already been claimed by an existing member of " . $this->domain . ".", 1);
}
$PendingMember->member_name_requested = $member_name_requested;
$PendingMember->domain = $this->domain;
$PendingMember->account = $account;
$PendingMember->bio = $bio;
$PendingMember->application_date = time();
$PendingMember->membership_payment_transaction_id = $membership_payment_transaction_id;
$PendingMember->membership_proposal_name = $membership_proposal_name;
$PendingMember->save();
return $PendingMember;
}
public function approve($account)
{
$PendingMember = $this->factory->new("PendingMember");
$criteria = [["domain", "=", $this->domain], ["account", "=", $account]];
$found = $PendingMember->read($criteria);
if (!$found) {
throw new Exception($account . " is not a pending member for " . $this->domain . ".", 1);
}
// TODO: check on chain to ensure membership_payment_transaction_id is valid
// TODO: double check member_name_requested hasn't been claimed already.
$Member = $this->factory->new("Member");
$Member->member_name = $PendingMember->member_name_requested;
$Member->domain = $this->domain;
$Member->account = $account;
$Member->bio = $PendingMember->bio;
$Member->date_added = time();
$Member->last_verified_date = time();
// TODO: set to true if there are no other group members?
$Member->is_admin = false;
$Member->is_active = true;
$Member->save();
$PendingMember->delete();
return $Member;
}
public function deactivate($account)
{
$Member = $this->factory->new("Member");
$criteria = [["domain", "=", $this->domain], ["account", "=", $account]];
$found = $Member->read($criteria);
if (!$found) {
throw new Exception($account . " is not a member of " . $this->domain . ".", 1);
}
if (!$Member->is_active) {
throw new Exception($account . " is not active.", 1);
}
if ($Member->is_admin) {
throw new Exception($account . " is an admin. Please hold a new election first.", 1);
}
$Member->is_active = false;
$Member->save();
return $Member;
}
public function activate($account)
{
$Member = $this->factory->new("Member");
$criteria = [["domain", "=", $this->domain], ["account", "=", $account]];
$found = $Member->read($criteria);
if (!$found) {
throw new Exception($account . " is not a member of " . $this->domain . ".", 1);
}
if ($Member->is_disabled) {
throw new Exception($account . " is disabled. Only the admins can enable and active the account.", 1);
}
if ($Member->is_active) {
throw new Exception($account . " is already active.", 1);
}
$Member->is_active = true;
$Member->save();
return $Member;
}
public function disable($account)
{
$Member = $this->factory->new("Member");
$criteria = [["domain", "=", $this->domain], ["account", "=", $account]];
$found = $Member->read($criteria);
if (!$found) {
throw new Exception($account . " is not a member of " . $this->domain . ".", 1);
}
if ($Member->is_disabled) {
throw new Exception($account . " is already disabled.", 1);
}
if ($Member->is_admin) {
throw new Exception($account . " is an admin. Please hold a new election first.", 1);
}
$Member->is_disabled = true;
$Member->is_active = false;
$Member->save();
return $Member;
}
public function enable($account)
{
$Member = $this->factory->new("Member");
$criteria = [["domain", "=", $this->domain], ["account", "=", $account]];
$found = $Member->read($criteria);
if (!$found) {
throw new Exception($account . " is not a member of " . $this->domain . ".", 1);
}
if (!$Member->is_disabled) {
throw new Exception($account . " is not disabled.", 1);
}
$Member->is_disabled = false;
$Member->save();
return $Member;
}
public function updateBio($account, $bio)
{
$Member = $this->factory->new("Member");
$criteria = [["domain", "=", $this->domain], ["account", "=", $account]];
$found = $Member->read($criteria);
if (!$found) {
throw new Exception($account . " is not a member of " . $this->domain . ".", 1);
}
$Member->bio = $bio;
$Member->save();
}
public function removeMember($account)
{
$Member = $this->factory->new("Member");
$criteria = [["domain", "=", $this->domain], ["account", "=", $account]];
$found = $Member->read($criteria);
if (!$found) {
throw new Exception($account . " is not a member of " . $this->domain . ".", 1);
}
if ($Member->is_active) {
throw new Exception($account . " is still active within " . $this->domain . ". Only deactivated members can be removed.", 1);
}
$Member->delete();
}
public function verifyMember($account)
{
$Member = $this->factory->new("Member");
$criteria = [["domain", "=", $this->domain], ["account", "=", $account]];
$found = $Member->read($criteria);
if (!$found) {
throw new Exception($account . " is not a member of " . $this->domain . ".", 1);
}
if (!$Member->is_active) {
throw new Exception($account . " is not active within " . $this->domain . ". Only activate members can be verfied.", 1);
}
$Member->last_verified_date = time();
$Member->save();
}
public function registerCandidate($account)
{
$Member = $this->factory->new("Member");
$criteria = [["domain", "=", $this->domain], ["account", "=", $account]];
$found = $Member->read($criteria);
if (!$found) {
throw new Exception($account . " is not a member of " . $this->domain . ".", 1);
}
$AdminCandidate = $this->factory->new("AdminCandidate");
$criteria = [["domain", "=", $this->domain], ["account", "=", $account]];
$found = $AdminCandidate->read($criteria);
if ($found) {
throw new Exception($account . " is already an admin candidate for " . $this->domain . ".", 1);
}
$AdminCandidate->domain = $this->domain;
$AdminCandidate->account = $account;
$AdminCandidate->save();
}
public function createElection($number_of_admins, $vote_threshold, $votes_per_member, $vote_date)
{
$Election = $this->factory->new("Election");
$criteria = [["domain", "=", $this->domain], ["is_complete", "=", false]];
$found = $Election->read($criteria);
if ($found) {
throw new Exception("An election with epoch " . $Election->epoch . " is still pending. Please complete that election before creating a new one.", 1);
}
$Election->domain = $this->domain;
$Election->epoch = $this->epoch;
$Election->vote_date = $vote_date;
$Election->number_of_admins = $number_of_admins;
$Election->vote_threshold = $vote_threshold;
$Election->votes_per_member = $votes_per_member;
$Election->save();
return $Election;
}
public function hasActiveElection()
{
$has_election = false;
try {
$Election = $this->getCurrentElection();
if (!$Election->is_complete) {
$has_election = true;
}
} catch (Exception $e) {}
return $has_election;
}
public function getLastCertifiedElection()
{
$Election = $this->factory->new("Election");
$queryBuilder = $Election->dataStore->createQueryBuilder();
$criteria = [["domain", "=", $this->domain], ["is_complete", "=", true], ["date_certified", "!=", null]];
$objects_data = $queryBuilder
->where($criteria)
->orderBy(["date_certified" => "desc"])
->getQuery()
->fetch();
if (!isset($objects_data[0])) {
throw new Exception("No previous certified election found for " . $this->domain . " can't be found.", 1);
}
$Election->loadData($objects_data[0]);
return $Election;
}
public function getCurrentElection()
{
$Election = $this->factory->new("Election");
$found = $Election->read([["domain", "=", $this->domain], ["epoch", "=", $this->epoch]]);
if (!$found) {
throw new Exception("An election with epoch " . $this->epoch . " for " . $this->domain . " can't be found.", 1);
}
return $Election;
}
public function vote($voter_account, $candidate_account, $rank, $vote_weight)
{
$Election = $this->getCurrentElection();
// note: this can throw exception
$Election->vote($voter_account, $candidate_account, $rank, $vote_weight);
}
public function removeVote($voter_account, $candidate_account)
{
$Election = $this->getCurrentElection();
// note: this can throw exception
$Election->removeVote($voter_account, $candidate_account);
}
public function recordVoteResults()
{
$Election = $this->getCurrentElection();
// note: this can throw exception
$Election->recordVoteResults();
}
public function certifyVoteResults()
{
$Election = $this->getCurrentElection();
// note: this can throw exception
$Election->certifyVoteResults();
$this->epoch++;
$this->save();
}
}
class PendingMember extends BaseObject
{
public $domain;
/**
* FIO address at domain
*/
public $member_name_requested;
public $account;
/**
* Limit to 255 chars
*/
public $bio;
public $application_date;
/**
* NEW
*/
public $membership_payment_transaction_id;
public $membership_proposal_name;
public $non_printable_fields = array('domain', 'membership_payment_transaction_id', 'membership_proposal_name');
public $sort_field = "application_date";
}
class Member extends BaseObject
{
public $domain;
public $member_name;
public $account;
/**
* Limit to 255 chars
*/
public $bio;
public $date_added;
/**
* Date the member was added to the group and then later updated each time membership is verified.
*/
public $last_verified_date;
/**
* Set to true for groups creator or if elected as an admin of the group)
*/
public $is_admin;
public $is_disabled;
/**
* NEW
*/
public $is_active;
public $last_login_date;
public $non_printable_fields = array('domain');
public $sort_field = "date_added";
}
class AdminCandidate extends BaseObject
{
public $domain;
/**
* NEW: updated this to just account
*/
public $account;
public $non_printable_fields = array('domain');
}
class Vote extends BaseObject
{
public $domain;
/**
* integer that increases with each election
*/
public $epoch;
public $voter_account;
public $candidate_account;
/**
* use ranked choice voting
* If a member votes for 5 candidates, they would rank each vote 1 through 5.
*/
public $rank;
/**
* NEW: vote weight (number of tokens the user holds?)
*/
public $vote_weight;
/**
* NEW:
*/
public $date_of_vote;
public $non_printable_fields = array('domain');
public $sort_field = "vote_weight";
}
class VoteResult extends BaseObject
{
public $domain;
/**
* integer that increases with each election
*/
public $epoch;
public $candidate_account;
/**
* The resulting rank for this candidate after the vote is complete
*/
public $rank;
/**
* Total number of votes received by this candidate
*/
public $votes;
public $non_printable_fields = array('domain');
public $sort_field = "rank";
}
class Election extends BaseObject
{
public $domain;
/**
* integer that increases with each election
*/
public $epoch;
public $vote_date;
/**