-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuw_groups.module
86 lines (70 loc) · 2.11 KB
/
uw_groups.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
<?php
/**
* @file
* Contains uw_groups.module..
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\user\Entity\Role;
use Drupal\uw_groups\NetIDGroups;
/**
* Implements hook_help().
*/
function uw_groups_help($route_name, RouteMatchInterface $route_match)
{
switch ($route_name) {
// Main module help for the uw_groups module.
case 'help.page.uw_groups':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('UW Groups') . '</p>';
return $output;
default:
}
}
function uw_groups_user_login($account)
{
$NetIDGroups = new NetIDGroups;
// Load the current user
$currentUser = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
// Get this users groups
$groups = $NetIDGroups->getCurrentUserGroups();
// Get the user's Roles
$delete_roles = $user_roles = Drupal::currentUser()->getRoles();
// Get all active groups
$active_groups = $NetIDGroups->getActiveGroups();
// Get all Roles
$roleObjects = Role::loadMultiple();
// Sync user groups with Drupal Role Mappings
if (is_array($groups))
foreach ($groups as $group) {
// If it's not an active group, skip it
if (!in_array(trim($group), $active_groups)) {
continue;
}
// If the user has the role, don't delete it, and skip it.
if (in_array(trim($group), $user_roles)) {
if (($key = array_search(trim($group), $delete_roles)) !== false) {
unset($delete_roles[$key]);
}
continue;
}
// If the role exists, give it to the user
foreach ($roleObjects as $roleObject) {
// Assign the role to the user if the role exists
if ($roleObject->id() == trim($group)) {
$currentUser->addRole($roleObject->id());
}
}
}
if (is_array($delete_roles))
foreach ($delete_roles as $role) {
//Pre($role);
// If this role is an active group, then delete it
if (in_array($role, $active_groups)) {
echo 'remove:' . $role;
$currentUser->removeRole($role);
}
}
// Save role changes to the user
$currentUser->save();
}