forked from Tangerine-Community/robbert-legacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Group.php
executable file
·583 lines (434 loc) · 15.6 KB
/
Group.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
<?php
include_once('./httpful.phar'); use \Httpful as h; // awesome http library
/**
* Handles all Tangerine-group level interactions.
*
* Interfaces with Users.
*
*
*/
class Group
{
/**
* @var string The name of this group, without any prefixes or url information.
*/
private $name;
/**
* @var array List of all user names who are admins as gleaned from the security doc.
*
* This variable is not a verified list, nor is it an array of User objects.
*/
private $admins;
/**
* @var array List of all user names who are readers as gleaned from the security doc.
*
* This variable is not a verified list, nor is it an array of User objects.
*/
private $readers;
/**
* @var boolean True if group exists, false if not, null if unknown.
*/
private $isExtant;
/**
* @var Config configuration object
*/
private $config;
/**
* Constructor, _soft-overloaded_
* if name set, will attempt to fetch.
* if successful, will simply read.
*/
public function __construct( $options = array() )
{
$this->config = new ConfigHelper();
$this->isExtant = null;
if ( isset( $options['name'] ) )
{
$this->set_name( $options['name'] );
try
{
$this->read();
} catch ( Exception $e )
{
$this->isExtant = false;
}
}
} // END of __construct
/**
* Mildly sanitizes name's characters and updates the member variables for url and doc.
* @param string $name
* @return \Group for chaining
*/
public function set_name ( $name )
{
$this->name = Helpers::safety_dance($name);
return $this;
} // END of set_name
/**
* Simple accessor.
* @return string The name of the group.
*/
public function get_name()
{
return $this->name;
}
/**
* Attempt to read the group.
* @return array Parsed json response from group's database.
*/
public function read()
{
$con = $this->config;
$response = h\Request::get( $con->group_db_url( $this->name, "main" ) )
->authenticateWith( $con->constants->ADMIN_U, $con->constants->ADMIN_P )
->send();
if ($response->code == 200)
{
$this->isExtant = true;
} else
{
throw new RuntimeException( $this->name . " group does not exist." );
}
return json_decode( $response, true );
}
/**
* Creates a group by adding an array element to the _user's doc and
* by creating another database with replicated tangerine docs.
* @throws RuntimeException for couchdb errors
* @return
*/
public function create()
{
$con = $this->config;
// Create a blank database
$put_response = h\Request::put( $con->group_db_url( $this->name, "main" ) )
->authenticateWith( $con->constants->ADMIN_U, $con->constants->ADMIN_P )
->send();
$response = json_decode( $put_response , true );
// If any error comes back from
if ( isset( $response['error'] ) )
{ // Specific errors
if ( $response['error'] == 'file_exists')
{
throw new RuntimeException( "Group already exists." );
} else
{
throw new RuntimeException( "CouchDB said: ". $response['error'] );
}
}
// Data for continuous backup replication
#$rep_doc = json_encode(array(
# "_id" => $con->constants->SERVER_NICKNAME . "_" . $this->name . "_" . $con->constants->BACKUP_SUFFIX,
# "source" => $con->group_db_url($this->name, "main", true),
# "target" => $con->group_db_url($this->name, "backup", true),
# "continuous" => true,
# "create_target" => true
#));
// Create a new backup replication
#$replication_response = h\Request::post( $con->db_url("_replicator", "backup") )
# ->authenticateWith( $con->constants->ADMIN_U, $con->constants->ADMIN_P )
# ->sendsJson()
# ->body( $rep_doc )
# ->send();
return $response;
} // END of create
/**
* Replicates design docs from tangerine trunk to the current group db.
* Will create them if not there.
*/
public function upgrade()
{
$con = $this->config;
$replicate_data = json_encode( array(
"source" => $con->constants->TRUNK,
"target" => $con->group_db_name( $this->name ),
"doc_ids" => explode( "\n", $con->constants->APP_DOCS )
));
$replicate_response = h\Request::post( $con->db_url("_replicate", "main") )
->authenticateWith( $con->constants->ADMIN_U, $con->constants->ADMIN_P )
->sendsJson()
->body( $replicate_data )
->send();
$this->add_uploader();
return json_decode( $replicate_response, true );
} // END of update_design_docs
/**
* Adds a special user prefixed with uploader that will remain a reader in
* this group for uploading purposes of mobile Tangerines.
*/
public function add_uploader()
{
$con = $this->config;
// calc new username and password
$uploader_name = "uploader-" . $this->get_name();
$uploader_pass = Helpers::calc_password(16);
$uploader_user = new User( array(
"name" => $uploader_name,
"pass" => $uploader_pass // in case we need to save later
));
// If read with no error, authenticated flag will be set.
$uploader_user->read();
// If authenticate, we're done, there's already a user.
if ( $uploader_user->is_authenticated() )
{
return true;
}
// If not, then make a user, add them as a reader
$test = $uploader_user->save();
$this->add_reader( $uploader_user, false );
$settings_doc_response = h\Request::get( $con->group_doc_url("settings", $this->name, "main") )
->authenticateWith( $con->constants->ADMIN_U, $con->constants->ADMIN_P )
->sendsJson()
->send();
$settings = json_decode( $settings_doc_response, true );
// @TODO break this out into a new method. Issue #4
if ( ! isset( $settings['upPass'] ) || $settings['upPass'] == "pass" )
{
$settings['upPass'] = $uploader_pass;
$settings['groupName'] = $this->name;
$settings['groupDDoc'] = $con->constants->D_DOC;
$settings['groupHost'] = $con->get_host('main');
$settings_doc_response = h\Request::put( $con->group_doc_url("settings", $this->name, "main") )
->authenticateWith( $con->constants->ADMIN_U, $con->constants->ADMIN_P )
->sendsJson()
->body( json_encode( $settings ) )
->send();
} else {
throw new RuntimeException( "Group already has an uploader" );
}
} // END of add_uploader
/**
* Completely deletes a database, on $user's word.
*/
public function destroy( User $user = null)
{
$con = $this->config;
// assert non-null user
if ( $user == null ) throw new InvalidArgumentException('Method requires an user.');
// assert admin user
$security_doc = $this->get_security_doc();
if ( ! in_array( $user->get_name(), $security_doc['admins']['names'] ) && count($security_doc['admins']['names']) == 1)
throw new InvalidArgumentException('User must be group admin.');
/*
* Your papers check out sir, deleting database.
*/
$data = array(
"create_target" => true,
"source" => $con->group_db_name( $this->name, "main" ),
"target" => $con->deleted_db_name( $this->name )
);
// stash a copy
$post_response = h\Request::post( $con->db_url("_replicate", "main") )
->authenticateWith( $con->constants->ADMIN_U, $con->constants->ADMIN_P )
->sendsJson()
->body( json_encode( $data ) )
->send();
$delete_response_raw = h\Request::delete( $con->group_db_url( $this->name, "main" ) )
->authenticateWith( $con->constants->ADMIN_U, $con->constants->ADMIN_P )
->send();
$delete_response = json_decode( $delete_response_raw, true );
// If the database was deleted, remove user's references to it
if ( ! isset( $delete_response['error'] ) )
{
$this->isExtant = false;
$user_categories = array(
$security_doc['admins']['names'],
$security_doc['members']['names']
);
foreach ( $user_categories as $category )
foreach ( $category as $user )
{
$notify_user = new User( array( "name" => $user, "admin" => true ));
$notify_user->remove_group( $this );
}
// Remove the uploader user
$uploader_user = new User( array(
"name" => "uploader-" . $this->get_name(),
"admin" => true
));
$uploader_user->destroy();
/*
* remove the backup entry in _replicator
*/
$backup_name = $con->constants->SERVER_NICKNAME . "_" . $this->name . "_" . $con->constants->BACKUP_SUFFIX;
$replicator_response = json_decode(
h\Request::get(
$con->doc_url($backup_name, "_replicator", "backup", true)
)->send()
, true);
$rev = "?rev=".$replicator_response['_rev'];
$delete_response_raw = h\Request::delete(
$con->doc_url($backup_name, "_replicator", "backup", true) . $rev
)
->authenticateWith( $con->constants->ADMIN_U, $con->constants->ADMIN_P )
->send();
}
return $delete_response;
} // END of update_design_docs
/**
* Adds an admin user to a group's database's security doc.
* Secretly also adds them to the reader/members list.
* @param $user User to be added to the group as an admin.
* @return the response
*/
public function add_admin( User $user = null, $reflexive = true )
{
if ( $user == null ) throw new UnexpectedValueException("User must be provided.");
$user_name = $user->get_name();
// Get original security doc
$old_doc = $this->get_security_doc();
// If they're already there, throw an exception
if ( in_array( $user_name, $old_doc['admins']['names'] ) ) throw new Exception( $user_name . " is already an admin in " . $this->name . "." );
// add them
array_push( $old_doc['admins']['names'], $user_name );
$new_doc = $old_doc;
// Send request
$put_response = json_decode( $this->set_security_doc( $new_doc ), true);
// if there was no error, update the user as well
if ( ! isset( $put_response['error'] ) && $reflexive ) $user->add_group( $this );
return $put_response;
} // END of add_admin
/**
* Removes an admin user from a group's database's security doc.
*/
public function remove_admin( User $user = null, $reflexive = true )
{
// Assert specified user.
if ( $user == null ) throw new InvalidArgumentException( "No user specified." );
$user_name = $user->get_name();
$old_doc = $this->get_security_doc();
// Assertions: two or more admins, one of which is specified.
if ( ! in_array( $user_name, $old_doc['admins']['names'] ) )
throw new InvalidArgumentException("User is not an administrator.");
if ( count($old_doc['admins']['names']) <= 1)
throw new UnderflowException("Group must have at least one admin.");
// Update security doc
$new_doc = $old_doc;
if ( in_array( $user_name, $new_doc['admins']['names'] ) )
$new_doc['admins']['names'] = Helpers::array_without( $user_name, $new_doc['admins']['names'] );
// Send request
$put_response = json_decode( $this->set_security_doc( $new_doc ), true);
// if there was no error, update the user as well
if ( ! isset( $put_response['error'] ) )
{
// only remove the group from the user's group list, if they're neither an admin nor reader.
if ( ! ( in_array( $user_name, $new_doc['admins']['names'] ) || in_array( $user_name, $new_doc['members']['names'] ) ) && $reflexive )
$user->remove_group( $this );
}
// return
return $put_response;
} // END of remove_admin_from_group
/**
* Adds a reader to the group's security doc.
* @return associative array of the Httpful response
*/
public function add_reader( User $user = null, $reflexive = true )
{
if ($user == null) throw new UnexpectedValueException( "No user required." );
$user_name = $user->get_name();
// Get original security doc
$old_doc = $this->get_security_doc();
// If they're already there, throw an exception
if ( in_array( $user_name, $old_doc['members']['names'] ) ) throw new Exception( $user_name . " is already a reader in " . $this->name . "." );
// add them
array_push( $old_doc['members']['names'], $user_name );
$new_doc = $old_doc;
// Send request
$put_response = json_decode( $this->set_security_doc( $new_doc ), true);
// if there was no error, update the user as well
if ( ! isset( $put_response['error'] ) && $reflexive ) $user->add_group( $this );
return $put_response;
} // END of add_reader
/**
* Removes reader from the group's security doc.
* Note: will not remove last reader. That would make it a reader party.
* One reader always remains, the uploader
* @return associative array of the Httpful response
*/
public function remove_reader( User $user = null, $reflexive = true )
{
// Assert specified user.
if ( $user == null ) throw new InvalidArgumentException( "No user specified." );
$user_name = $user->get_name();
$old_doc = $this->get_security_doc();
// Assertions: two or more readers, one of which is in the list.
if ( ! in_array( $user_name, $old_doc['members']['names'] ) )
throw new InvalidArgumentException($user_name . " is not a reader.");
// old strategy: If the last reader is being removed, add the backup user as a reader.
// if ( count($old_doc['members']['names']) <= 1) array_push( $old_doc['members']['names'], $backup_user->get_name() );
// Update security doc
$new_doc = $old_doc;
if ( in_array( $user_name, $new_doc['members']['names'] ) )
$new_doc['members']['names'] = Helpers::array_without( $user_name, $new_doc['members']['names'] );
// Send request
$put_response = json_decode( $this->set_security_doc( $new_doc ), true);
// if there was no error, update the user as well
if ( ! isset( $put_response['error'] ) )
{
// only remove the group from the user's group list, if they're neither an admin nor reader.
if ( ! ( in_array( $user_name, $new_doc['admins']['names'] ) && in_array( $user_name, $new_doc['members']['names'] ) ) && $reflexive )
$user->remove_group( $this );
}
return $put_response;
} // END of remove_reader
/**
* Returns the security doc from the current group.
* Note: If no doc set, will return blank structure of standard doc.
* @return Associative array of security doc body
*/
public function get_security_doc()
{
if ( ! isset( $this->name ) ) throw new LogicException( "Group name has not been set yet." );
$con = $this->config;
$response_raw = h\Request::get( $con->group_doc_url("_security", $this->name, "main") )
->authenticateWith( $con->constants->ADMIN_U, $con->constants->ADMIN_P )
->send();
$response = json_decode( $response_raw, true );
// If no doc set or if database doesn't exist, return blank structure of security doc
if ( empty($response) || ( isset( $response['error'] ) && $response['error'] == 'not_found' ) )
{
return array(
"admins" => array( "names" => array(), "roles" => array() ),
"members" => array( "names" => array(), "roles" => array() )
);
}
return $response;
} // END of get_security_doc
/**
* This saves the supplied security doc to the group's database.
* @param new_doc Must be a complete CouchDB doc
* @return httpful/Response object.
*/
public function set_security_doc( $new_doc = null )
{
if ( $new_doc == null ) throw InvalidArgumentException("Cowardly refusing to save a blank security doc.");
$con = $this->config;
return h\Request::put( $con->group_doc_url("_security", $this->name, "main") )
->authenticateWith( $con->constants->ADMIN_U, $con->constants->ADMIN_P )
->sendsJson()
->body( json_encode( $new_doc ) )
->send();
} // END of set_security_doc
public function is_admin( User $user )
{
$security_doc = $this->get_security_doc();
return in_array( $user->get_name(), $security_doc['admins']['names'] );
}
public function is_reader( User $user )
{
$security_doc = $this->get_security_doc();
return in_array( $user->get_name(), $security_doc['members']['names'] );
}
/**
* Returns extant flag.
* If flag not set, attempt to Group::read().
* @return boolean True if object is aware of group's existance, false otherwise.
* @see Group::read()
*/
public function isExtant()
{
if ( ! isset($this->isExant) ) { $this->read(); }
return $this->isExtant;
} // END of isExtant
}
?>