-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathProcessUserGroups.module
executable file
·599 lines (498 loc) · 17.1 KB
/
ProcessUserGroups.module
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
<?php
class ProcessUserGroups extends Process {
/**
* Return information about this module (required)
*
*/
public static function getModuleInfo() {
return array(
'title' => __('User Groups', __FILE__),
'summary' => __('Manage user groups for ProcessWire for streamlined access management', __FILE__),
'version' => 11,
'author' => 'Antti Peisa, Niklas Lakanen, Teppo Koivula',
'href' => 'http://modules.processwire.com/',
'permission' => 'user-admin',
);
}
/**
* The name that will be used for the page this module creates
*
* It should be unique and the same (or similar) to the module name, but all lowercase.
* It should not be the same as any other ProcessWire admin page name.
*
*/
const pageName = 'usergroups';
/**
* This is an optional initialization function called before any execute functions.
*
*/
public function init() {
parent::init(); // required
$this->userGroups = $this->modules->get("UserGroups");
}
/**
* This function is executed when a page with your Process assigned is accessed.
*
* Output table of all currently existing groups along with member count and
* actions, such as "remove".
*
* @return string rendered table
*/
public function ___execute() {
$out = "";
$group_str = $this->_('Group');
$description_str = $this->_('Description');
$users_count_str = $this->_("Users");
$remove_str = $this->_('Remove');
// Grab filter form
$form = $this->getFilterForm();
if ($form instanceof InputfieldFieldset || $form instanceof InputfieldForm) {
$out .= $form->render();
} else if ($form) {
$out .= $form;
}
$table = $this->modules->get("MarkupAdminDataTable");
$table->setEncodeEntities(false);
$table->headerRow(array(
$group_str,
$description_str,
$users_count_str,
$remove_str
));
// Build filter selector (if filter form exists)
$selector = array('limit=25');
if ( $form ) {
$selector[] = $this->getFilterSelector();
}
$selector = implode(',',$selector);
$groups = $this->page->children($selector);
foreach ($groups as $group) {
if ($group->name == "everyone") {
$table->row(array(
$group->title,
'',
"∞",
" "
));
} elseif ($group->name == "logged") {
$table->row(array(
$group->title,
'',
wire('users')->count(),
" "
));
} else {
$table->row(array(
// not using "title => url" because it doesn't work if the group's title is a number
'<a href="./edit/?group_id='.$group->id.'">'.$group->title.'</a>',
$group->group_description,
wire('users')->find("user_groups={$group->id}, limit=2")->getTotal(),
$remove_str => "./remove/?group_id=" . $group->id
));
}
}
$table->action(array($this->_('Add New Group') => 'add/'));
$out .= $table->render().$groups->renderPager();
return $out;
}
public function ___executeAdd() {
Wire::setFuel('processHeadline', $this->_('Add New Group'));
$this->breadcrumbs->add(new Breadcrumb('../', $this->_('User Groups')));
$group_title = trim($this->input->post->group_title);
if (!is_null($group_title) && $group_title != "") {
$newgroup = wire('pages')->add('user-group', wire('page'), array(
'title' => $group_title,
'group_description' => $this->input->post->group_description
));
$this->message($this->_("New group added:") . " " . $group_title);
$this->session->redirect("../");
}
$form = $this->modules->get("InputfieldForm");
$form->action = "./";
$field = $this->modules->get("InputfieldText");
$field->attr('id+name', 'group_title');
$field->label = $this->_("Group title");
$field->required = true;
$field->attr('required', 'required');
$form->add($field);
$field = $this->modules->get("InputfieldText");
$field->attr('id+name', 'group_description');
$field->label = $this->_("Group description (optional)");
$form->add($field);
$submit = $this->modules->get("InputfieldSubmit");
$submit->value = $this->_("Create New Group");
$form->add($submit);
return $form->render();
}
/**
* Edit or save single group
*
* @throws WireException if GET param group_id is invalid
* @throws WireException if group matching group_id not found
* @return string rendered form
*/
public function ___executeEdit() {
$group_id = (int) $this->input->get->group_id;
if ($group_id < 1) throw new WireException("Invalid group id");
$group = $this->pages->get("template=user-group, id=$group_id");
if (!$group->id) throw new WireExeption("Group not found: $group_id");
if ($this->input->post->submit) {
// Update group details
$group_title = trim($this->input->post->group_title);
if (!is_null($group_title) && $group_title != "") $group->title = $group_title;
$group->group_description = $this->input->post->group_description;
$group->save();
// Add new users into group
foreach ($this->input->post->direct_users as $user_id) {
if (empty($user_id)) continue;
$user_id = trim($user_id, " ,");
if (strpos($user_id, ',')) {
// For some reason pageListSelectMultiple returns array, but with only one item comma separating values (123,1234)
foreach (explode(",", $user_id) as $user_id) {
$u = wire('users')->get($user_id);
if (!$u->id) continue;
$u->user_groups->add($group);
$u->save();
}
} else {
$u = wire('users')->get($user_id);
if (!$u->id) continue;
$u->user_groups->add($group);
$u->save();
}
}
// Remove users from group
if ($this->input->post->remove) {
foreach ($this->input->post->remove as $user_id) {
$u = wire('users')->get($user_id);
$u->user_groups->remove($group);
$u->save();
}
}
// Reload page
$this->session->redirect("./?group_id=$group_id");
}
$this->input->whitelist("group_id", $group_id);
$usersInGroup = $this->users->find("user_groups=$group");
$out = '';
Wire::setFuel('processHeadline', $group->title);
$this->breadcrumbs->add(new Breadcrumb('../', $this->_('User Groups')));
if ($usersInGroup->count() > 0) {
$table = $this->modules->get("MarkupAdminDataTable");
$table->setEncodeEntities(false);
$table->headerRow(array(
$this->_("Name"),
$this->_("Email"),
$this->_("Remove")
));
foreach ($usersInGroup as $u) {
$str = '';
if (empty($str)) $str = "<input type='checkbox' name='remove[]' value='{$u->id}' />";
$table->row(array(
$u->name,
$u->email,
$str
));
}
$html = $table->render();
} else {
$html = $this->_("No users in group");
}
$form = $this->modules->get("InputfieldForm");
$form->action = "./?group_id=$group_id";
$field = $this->modules->get("InputfieldText");
$field->attr('id+name', 'group_title');
$field->label = $this->_("Group title");
$field->required = true;
$field->attr('required', 'required');
$field->value = $group->title;
$form->add($field);
$field = $this->modules->get("InputfieldText");
$field->attr('id+name', 'group_description');
$field->label = $this->_("Group description (optional)");
$field->value = $group->group_description;
$form->add($field);
$markup = $this->modules->get("InputfieldMarkup");
$markup->label = $this->_("Users in this group");
$markup->value = $html;
//$markup->value .= $usersInGroup->renderPager();
$form->add($markup);
//echo "<pre>";
//var_dump($group->getInputfields()->get("roles"));
//exit();
/*
$field = $group->getInputfields()->get("roles");
$field->columnWidth = 50;
$form->add($field);
*/
$field = $this->modules->get("InputfieldPageAutocomplete");
// $field->columnWidth = 50;
$field->label = $this->_("Add users to group");
$field->name = "direct_users";
$field->template_id = $this->config->userTemplateID;
$field->labelFieldName = "name";
$field->searchFields = "name email";
$form->add($field);
$submit = $this->modules->get("InputfieldSubmit");
$form->add($submit);
$out .= $form->render();
return $out;
}
/**
* Removes specified group
*
* If GET param confirm is set and evaluates to true, group specified by GET
* param group_id is removed, otherwise confirmation form is displayed.
*
* @return mixed confirm form markup or void
*/
public function ___executeRemove() {
Wire::setFuel('processHeadline', $this->_('Remove Group'));
$this->breadcrumbs->add(new Breadcrumb('../', $this->_('User Groups')));
$group_id = (int) $this->input->get->group_id;
if (!$group_id) throw new WireException("Invalid group id");
$group = $this->pages->get("template=user-group, id=$group_id");
if (!$group->id) throw new WireException("Invalid group");
if ($this->input->get->confirm) {
// remove users from group (or group from users, whatever)
foreach ($this->users->find("user_groups=$group") as $user) {
$user->user_groups->remove($group);
$user->save();
}
// remove group (grab title for message use first, though)
$group_title = $group->title;
$group->delete();
// set message and redirect to groups page
$this->message($this->_("Group Removed:") . " " . $group_title);
$this->session->redirect("../");
} else {
// output confirmation form to avoid accidentally removing groups
$form = $this->modules->get("InputfieldForm");
$form->method = "GET";
$form->action = ".";
$field = $this->modules->get("InputfieldHidden");
$field->attr('name', 'group_id');
$field->attr('value', $group_id);
$form->add($field);
$field = $this->modules->get("InputfieldCheckbox");
$field->attr('name', 'confirm');
$field->attr('value', 1);
$field->label = sprintf($this->_("Permanently remove group %s"), $group->title);
$field->description = $this->_("Once removed, groups cannot be recovered. Members of removed group are not affected (other than being removed from said group).");
$form->add($field);
$submit = $this->modules->get("InputfieldSubmit");
$submit->value = $this->_("Remove Group");
$form->add($submit);
return $form->render();
}
}
/**
* Build filter forms for execute method
*
* This being a method of it's own helps us both keep this separate from
* main execute method, but also makes it possible for external code to
* alter return value (add/remove fields etc.) based on their needs.
*
* @return mixed InputfieldFieldset by default, but can be altered by hooks
*/
public function ___getFilterForm() {
$fields = $this->modules->get("InputfieldFieldset");
$form = $this->modules->get("InputfieldForm");
$form->attr('method', 'get');
$form->attr('id', 'userSearch');
$form->label = $this->_("Filter Groups");
if (!$this->input->get->search) {
$form->collapsed = Inputfield::collapsedYes;
}
$field = $this->modules->get("InputfieldText");
$field->attr('name', 'search');
$field->label = $this->_("Search");
if ($this->input->get->search) {
$this->input->whitelist('search', $this->input->get->search);
$field->attr('value', $this->sanitizer->text($this->input->get->search));
}
$form->add($field);
$field = $this->modules->get("InputfieldSubmit");
$field->attr('value', $this->_("Search"));
$form->add($field);
$fields->add($form);
return $fields;
}
/**
* Build selector for execute method
*
* @return string selector string
*/
public function ___getFilterSelector() {
$selector = "check_access=0";
if ($this->input->whitelist->search) {
$search = $this->sanitizer->selectorValue($this->input->whitelist->search);
$selector .= ", title%=$search";
}
return $selector;
}
/**
* This creates a new page with this Process module assigned.
*/
public function ___install() {
// create the page our module will be assigned to
$page = new Page();
$page->template = 'admin';
$page->name = self::pageName;
$page->parent = $this->pages->get($this->config->adminRootPageID)->child('name=access');
$page->process = $this;
$info = self::getModuleInfo();
$page->title = $info['title'];
// save the page
$page->save();
// tell the user we created this page
$this->message("Created Page: {$page->path}");
// new field
$f = $this->fields->get('group_description');
if ( ! $f->id ) {
$f = new Field();
$f->type = $this->modules->get("FieldtypeText");
$f->name = "group_description";
$f->label = "Group description";
$f->inputfield = 'InputfieldText';
$f->save();
}
// new fieldgroup
$ug = new Fieldgroup();
$ug->name = "user-group";
$ug->add($this->fields->get('title'));
$ug->add($f);
$ug->save();
// new template using the fieldgroup
$t = new Template();
$t->name = "user-group";
$t->fieldgroup = $ug; // add the fieldgroup
$t->flags = Template::flagSystem;
$t->noGlobal = 1;
$t->save();
// Add new field user template (user_groups that user belongs to)
$f = $this->fields->get('user_groups');
if (! $f->id) {
$f = new Field();
$f->type = $this->modules->get("FieldtypePage");
$f->name = "user_groups";
$f->label = "User Groups";
$f->inputfield = 'InputfieldAsmSelect';
$f->findPagesSelector = "template=user-group, name!=everyone|logged, sort=title, include=all";
$f->labelFieldName = "title";
$f->save();
}
$ufg = $this->fieldgroups->get("user");
$ufg->add($f);
$ufg->save();
$f = $this->fields->get('manage_access');
if ( ! $f->id) {
$f = new Field();
$f->type = $this->modules->get("FieldtypeCheckbox");
$f->name = "manage_access";
$f->label = "Manage access";
$f->description = "Choose if you want to select groups that can view or edit this page. Otherwise access is inherited from parent page";
$f->inputfield = "InputfieldCheckbox";
$f->flags = Field::flagGlobal;
$f->collapsed = Inputfield::collapsedHidden;
$f->save();
}
$f = $this->fields->get('view_groups');
if ( ! $f->id) {
$f = new Field();
$f->type = $this->modules->get("FieldtypePage");
$f->name = "view_groups";
$f->label = "View Groups";
$f->description = "Groups that can view this page";
$f->inputfield = "InputfieldAsmSelect";
$f->parent_id = $page->id;
$f->template_id = $t->id;
$f->labelFieldName = "title";
$f->flags = Field::flagGlobal;
$f->collapsed = Inputfield::collapsedHidden;
$f->save();
}
$f = $this->fields->get('edit_groups');
if ( ! $f->id) {
$f = new Field();
$f->type = $this->modules->get("FieldtypePage");
$f->name = "edit_groups";
$f->label = "Edit Groups";
$f->description = "Groups that can edit this page";
$f->inputfield = "InputfieldAsmSelect";
$f->findPagesSelector = "template=user-group, name!=everyone|logged, include=all";
$f->labelFieldName = "title";
$f->flags = Field::flagGlobal;
$f->collapsed = Inputfield::collapsedHidden;
$f->save();
}
$f = $this->fields->get('inherit_access');
if ( ! $f->id) {
$f = new Field();
$f->type = $this->modules->get("FieldtypePage");
$f->name = "inherit_access";
$f->label = "Inherit Access";
$f->description = "Page that this page inherits access from";
$f->inputfield = "InputfieldPageListSelect";
$f->labelFieldName = "path";
$f->derefAsPage = FieldtypePage::derefAsPageOrFalse;
$f->flags = Field::flagGlobal;
$f->collapsed = Inputfield::collapsedHidden;
# required for inheritance to work when a page is unpublished
$f->allowUnpub = 1;
$f->save();
}
$newpage = $this->pages->add('user-group', $page, 'everyone', array('title' => 'Everyone'));
$newpage->save();
$everyoneGroupId = $newpage->id;
$newpage = $this->pages->add('user-group', $page, 'logged', array('title' => 'Logged in users'));
$newpage->save();
// Lets activate the manage access for home page
$home = $this->pages->get(1);
$home->manage_access = 1;
// And let's give group "Everyone" view access to get things going
$home->view_groups->add($everyoneGroupId);
$home->save();
}
/**
* Called only when your module is uninstalled
*
* This should return the site to the same state it was in before the module was installed.
*
*/
public function ___uninstall() {
// find the page we installed, locating it by the process field (which has the module ID)
// it would probably be sufficient just to locate by name, but this is just to be extra sure.
$moduleID = $this->modules->getModuleID($this);
$page = $this->pages->get("template=admin, process=$moduleID, name=" . self::pageName);
if($page->id) {
// if we found the page, let the user know and delete it
$this->message("Deleting Page: {$page->path}");
wire('pages')->delete($page, true);
}
$t = $this->templates->get('user-group');
if ($t->id) {
$t->flags = Template::flagSystemOverride;
$t->flags = 0;
$this->templates->delete($t);
$fg = $this->fieldgroups->get('user-group');
if ($fg->id) $this->fieldgroups->delete($fg);
}
$rFields = $this->fields->find("name=user_groups|inherit_access|manage_access|view_groups|edit_groups|group_description");
foreach ($rFields as $f) {
$f->flags = 0;
$f->save();
}
foreach ($this->fieldgroups as $fg) {
$fg->remove($this->fields->get('manage_access'));
$fg->remove($this->fields->get('view_groups'));
$fg->remove($this->fields->get('edit_groups'));
$fg->remove($this->fields->get('user_groups'));
$fg->remove($this->fields->get('inherit_access'));
$fg->save();
}
foreach ($rFields as $f) {
$this->fields->delete($f);
}
}
}