-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from digit-devs/master
Initial commit of the module
- Loading branch information
Showing
7 changed files
with
864 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,37 @@ | ||
NExteEuropa User Managemente | ||
NextEuropa User Management | ||
|
||
# Setting up | ||
You will need to add into the settings.php one of the following to 'variable': | ||
```php | ||
$conf['nexteuropa_user_management_banned_roles'] = array(); | ||
$conf['nexteuropa_user_management_banned_role_ids'] = array(); | ||
``` | ||
|
||
Inside the `nexteuropa_user_management_banned_roles` include the role names | ||
(string type) which you want to exclude as a grantable role by User management | ||
users. | ||
Inside the `nexteuropa_user_management_banned_role_ids` include the role ids | ||
(integer type except the two exception see below) which you want to exclude as a | ||
grantable role by User management users. | ||
|
||
To exclude Administrator role use `<!!ADMIN_RID!!>` token in the | ||
`nexteuropa_user_management_banned_role_ids` array. | ||
To exclude User management role use `<!!USER_MANAGER_RID!!>` token in the | ||
`nexteuropa_user_management_banned_role_ids` array. | ||
|
||
So to exclude as grantable role for User management user, put this into the | ||
settings.php: | ||
```php | ||
$conf['nexteuropa_user_management_banned_role_ids'] = array( | ||
'<!!ADMIN_RID!!>', | ||
'<!!USER_MANAGER_RID!!>', | ||
); | ||
``` | ||
|
||
## Default values for setting variables | ||
If `nexteuropa_user_management_banned_roles` remains undefined, it will be | ||
considered as an empty array. | ||
If the `nexteuropa_user_management_banned_role_ids` remains undefined, it will | ||
be considered as admin and user management was set. | ||
The two condition above are independent from eachother, by defining role names, | ||
won't remove role ids' default values. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,208 @@ | ||
<?php | ||
/** | ||
* @file | ||
* Code for the NextEuropa Manage Roles feature, implemented actions. | ||
*/ | ||
|
||
/** | ||
* Implements hook_action_info(). | ||
*/ | ||
function nexteuropa_user_management_action_info() { | ||
return array( | ||
'nexteuropa_user_management_block_selected_user_action' => array( | ||
'label' => t('Block the selected user'), | ||
'type' => 'user', | ||
'configurable' => FALSE, | ||
'triggers' => array(), | ||
), | ||
'nexteuropa_user_management_unblock_selected_user_action' => array( | ||
'label' => t('Unblock the selected user'), | ||
'type' => 'user', | ||
'configurable' => FALSE, | ||
'triggers' => array(), | ||
), | ||
'nexteuropa_user_management_change_user_roles_action' => array( | ||
'label' => t('Change user roles'), | ||
'type' => 'user', | ||
'configurable' => TRUE, | ||
'triggers' => array(), | ||
), | ||
); | ||
} | ||
|
||
/** | ||
* Block a specific user. | ||
* | ||
* @param object $entity | ||
* (optional) An entity object; if it is provided and it has a uid property, | ||
* the user with that ID is blocked. | ||
* @param array $context | ||
* (optional) An associative array; currently not used here. | ||
* | ||
* @ingroup actions | ||
* | ||
* @throws \Exception | ||
*/ | ||
function nexteuropa_user_management_block_selected_user_action(&$entity, $context = array()) { | ||
// If there's no user or tries to modify himself then exit. | ||
if (!isset($entity->uid) || $entity->uid === $GLOBALS['user']->uid) { | ||
return; | ||
} | ||
|
||
$account = user_load($entity->uid); | ||
$account = user_save($account, array('status' => 0)); | ||
watchdog('action', 'User %name blocked.', array('%name' => $account->name)); | ||
} | ||
|
||
/** | ||
* Unblock a specific user. | ||
* | ||
* @param object $entity | ||
* (optional) An entity object; if it is provided and it has a uid property, | ||
* the user with that ID is unblocked. | ||
* @param array $context | ||
* (optional) An associative array; currently not used here. | ||
* | ||
* @ingroup actions | ||
* | ||
* @throws \Exception | ||
*/ | ||
function nexteuropa_user_management_unblock_selected_user_action(&$entity, $context = array()) { | ||
// If there's no user or tries to modify himself then exit. | ||
if (!isset($entity->uid) || $entity->uid === $GLOBALS['user']->uid) { | ||
return; | ||
} | ||
|
||
$account = user_load($entity->uid); | ||
$account = user_save($account, array('status' => 1)); | ||
watchdog('action', 'Unblock user %name.', array('%name' => $account->name)); | ||
} | ||
|
||
/** | ||
* Action settings form. | ||
* | ||
* @param array $context | ||
* Provides a context for the action form. | ||
* | ||
* @return array | ||
* Renderable array. | ||
*/ | ||
function nexteuropa_user_management_change_user_roles_action_form($context) { | ||
if (!user_access('nexteuropa manage users non restricted operations') && !user_access('administer permissions')) { | ||
drupal_set_message(t('Your user does not have access to modify user roles.'), 'error'); | ||
return array(); | ||
} | ||
|
||
$roles = user_roles(TRUE); | ||
// Can't edit authenticated role. | ||
unset($roles[DRUPAL_AUTHENTICATED_RID]); | ||
|
||
// If the current user has access to grant any role (i.e. user 1 and | ||
// administrator role), it's useless to restrict here the list, however if it | ||
// has no this right, restrict it. | ||
if (!user_access('administer permissions')) { | ||
_nexteuropa_user_management_restrict_roles($roles); | ||
} | ||
|
||
if (empty($roles)) { | ||
return array( | ||
'message' => array( | ||
'#markup' => t("There's no role which you can assign."), | ||
), | ||
); | ||
} | ||
|
||
$form = array(); | ||
$form['add_roles'] = array( | ||
'#type' => 'checkboxes', | ||
'#title' => t('Add roles'), | ||
'#description' => t('Choose one or more roles you would like to assign to the selected users.'), | ||
'#options' => $roles, | ||
); | ||
$form['remove_roles'] = array( | ||
'#type' => 'checkboxes', | ||
'#title' => t('Remove roles'), | ||
'#description' => t('Choose one or more roles you would like to remove from the selected users.'), | ||
'#options' => $roles, | ||
); | ||
|
||
return $form; | ||
} | ||
|
||
/** | ||
* Provides a validation for the action form. | ||
* | ||
* @param array $form | ||
* Action form, renderable array. | ||
* @param array $form_state | ||
* Action form state. | ||
*/ | ||
function nexteuropa_user_management_change_user_roles_action_validate($form, $form_state) { | ||
if (!$form_state['values']['add_roles'] && !$form_state['values']['remove_roles']) { | ||
form_set_error('add_roles', t('You have not chosen any role to add or remove. Please select something to do.')); | ||
} | ||
} | ||
|
||
/** | ||
* Action form submit handler, it will returns roles. | ||
* | ||
* @param array $form | ||
* Action form's renderable array. | ||
* @param array $form_state | ||
* Action form's state. | ||
* | ||
* @return array | ||
* Returns the action form results. Roles to remove and add to the selected | ||
* users. | ||
*/ | ||
function nexteuropa_user_management_change_user_roles_action_submit($form, $form_state) { | ||
return array( | ||
'add_roles' => array_filter($form_state['values']['add_roles']), | ||
'remove_roles' => array_filter($form_state['values']['remove_roles']), | ||
); | ||
} | ||
|
||
/** | ||
* Add/remove roles for the 'Change user roles' action. | ||
* | ||
* @param object $entity | ||
* An user entity, roles will be modified on this entity. | ||
* @param array $context | ||
* Determinates which roles will be added, by the 'add_roles' array key, and | ||
* which will be removed by the 'remove_roles' key. Changes will be made on | ||
* the $entity object. | ||
*/ | ||
function nexteuropa_user_management_change_user_roles_action($entity, $context = array()) { | ||
// Silently exit if: | ||
// - there's no user or | ||
// - tries to modify himself or | ||
// - nothing to be set or | ||
// - user does not have permission to change roles. | ||
if ( | ||
!isset($entity->uid) || | ||
$entity->uid === $GLOBALS['user']->uid || | ||
empty($context['add_roles']) && empty($context['remove_roles']) || | ||
!user_access('nexteuropa manage users non restricted operations') && !user_access('administer permissions') | ||
) { | ||
return; | ||
} | ||
|
||
$wrapper = entity_metadata_wrapper('user', $entity); | ||
|
||
$original_roles = $roles = $wrapper->roles->value(); | ||
if (is_array($context['add_roles'])) { | ||
$roles = array_merge($roles, $context['add_roles']); | ||
} | ||
if (is_array($context['remove_roles'])) { | ||
$roles = array_diff($roles, $context['remove_roles']); | ||
} | ||
$wrapper->roles->set($roles); | ||
$wrapper->save(); | ||
|
||
watchdog('action', 'Roles was modified on %name user, added: "%added_roles", removed: "%removed_roles" and original: "%original_roles".', array( | ||
'%name' => $wrapper->label(), | ||
'%added_roles' => implode(', ', $context['add_roles']), | ||
'%removed_roles' => implode(', ', $context['remove_roles']), | ||
'%original_roles' => implode(', ', $original_roles), | ||
)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* nexteuropa_user_management.features.inc | ||
*/ | ||
|
||
/** | ||
* Implements hook_views_api(). | ||
*/ | ||
function nexteuropa_user_management_views_api($module = NULL, $api = NULL) { | ||
return array("api" => "3.0"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
name = NextEuropa User Management | ||
description = Provides a role with view to be able to manage the users. | ||
core = 7.x | ||
package = NextEuropa | ||
hidden = TRUE | ||
dependencies[] = ctools | ||
dependencies[] = entity | ||
dependencies[] = features | ||
dependencies[] = views | ||
dependencies[] = views_bulk_operations | ||
features[ctools][] = views:views_default:3.0 | ||
features[features_api][] = api:2 | ||
features[views_view][] = nexteuropa_user_management | ||
multisite_version = 2.5 | ||
php = 5.6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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')); | ||
} |
Oops, something went wrong.