forked from ec-europa/nexteuropa_user_management
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nexteuropa_user_management.install
98 lines (87 loc) · 3.16 KB
/
nexteuropa_user_management.install
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
<?php
/**
* @file
* Nexteuropa_user_management.install.
*/
define('NEXTEUROPA_USER_MANAGEMENT_USER_MANAGER_ROLE', 'User management');
/**
* Implements hook_requirements().
*/
function nexteuropa_user_management_requirements($phase) {
if ($phase === 'install') {
// Ensure translations don't break during installation.
$t = get_t();
$existing_role = user_role_load_by_name(NEXTEUROPA_USER_MANAGEMENT_USER_MANAGER_ROLE);
if ($existing_role !== FALSE) {
return array(
'role' => array(
'title' => $t('Special role already exist!'),
'description' => $t('Special role already exist! Make sure you rename it ("%role_name") before install this module.', ['%role_name' => NEXTEUROPA_USER_MANAGEMENT_USER_MANAGER_ROLE]),
'severity' => REQUIREMENT_ERROR,
),
);
}
}
}
/**
* Implements hook_install().
*/
function nexteuropa_user_management_install() {
// Ensure translations don't break during installation.
$t = get_t();
$role = new stdClass();
$role->name = NEXTEUROPA_USER_MANAGEMENT_USER_MANAGER_ROLE;
if (user_role_save($role) === FALSE) {
drupal_set_message($t('Role creation failed!'), 'error');
return;
}
variable_set('nexteuropa_user_management_user_manager_rid', $role->rid);
// Do not use user_role_grant_permission() since it relies on
// hook_permission(), however hook_permission() only runs after the module is
// enabled, so in the current moment the hook_permission() didn't run, which
// means the defined permissions inside that hook is not existing yet.
db_merge('role_permission')
->key(array(
'rid' => $role->rid,
'permission' => 'access nexteuropa user management views',
'module' => 'nexteuropa_user_management',
))
->execute();
db_merge('role_permission')
->key(array(
'rid' => $role->rid,
'permission' => 'nexteuropa manage users non restricted operations',
'module' => 'nexteuropa_user_management',
))
->execute();
// Grant the minimal permissions to the role to be able to access the views
// and be usable.
user_role_grant_permissions($role->rid, array(
'access administration pages',
'view the administration theme',
));
// If admin_menu is installed, grant access to that one too, otherwise user
// won't be able to navigate to the views.
if (module_exists('admin_menu')) {
user_role_grant_permissions($role->rid, array(
'access administration menu',
));
}
// Add administrator role for user 1 so he can access the views. The views is
// restricted to administrator and user management role, for some reason the
// user 1 doesn't penetrate through on that restriction.
$user_1_wrapper = entity_metadata_wrapper('user', 1);
$user_1_roles = $user_1_wrapper->roles->value();
$admin_role = variable_get('user_admin_role', 0);
if (!in_array($admin_role, $user_1_roles, FALSE)) {
$user_1_roles[] = $admin_role;
$user_1_wrapper->roles->set($user_1_roles);
$user_1_wrapper->save();
}
}
/**
* Implements hook_uninstall().
*/
function nexteuropa_user_management_uninstall() {
user_role_delete((int) variable_get('nexteuropa_user_management_user_manager_rid'));
}