View source
<?php
function user_patterns($data) {
$files = array(
'modules/user/user.admin.inc',
'modules/user/user.pages.inc',
);
$actions['role'] = array(
PATTERNS_INFO => t('Create/Modify/Delete Roles'),
PATTERNS_CREATE => array(
'user_admin_roles',
),
PATTERNS_MODIFY => array(
'user_admin_role',
),
PATTERNS_DELETE => array(
'user_admin_role_delete_confirm',
),
PATTERNS_FILES => $files,
PATTERNS_EXPORT => array(
PATTERNS_EXPORT_ALL => 'user_patterns_export_all_roles',
),
);
$actions['permission'] = array(
PATTERNS_INFO => t('Create/Modify/Delete Permissions'),
PATTERNS_MODIFY => array(
'set_user_permissions',
),
PATTERNS_FILES => $files,
PATTERNS_EXPORT => array(
PATTERNS_EXPORT_ALL => 'user_patterns_export_all_permissions',
),
);
$actions['user'] = array(
PATTERNS_INFO => t('Create/Modify/Delete Users'),
PATTERNS_CREATE => array(
'user_register_form',
),
PATTERNS_MODIFY => array(
'set_user_values',
),
PATTERNS_DELETE => array(
'user_cancel_confirm_form',
),
PATTERNS_FILES => $files,
PATTERNS_EXPORT => array(
PATTERNS_EXPORT_ALL => 'user_patterns_export_all_users',
),
);
$actions['role_users'] = array(
PATTERNS_INFO => t('Add/Remove roles to users'),
PATTERNS_CREATE => array(
'set_role_users',
),
PATTERNS_DELETE => array(
'unset_role_users',
),
PATTERNS_FILES => $files,
PATTERNS_EXPORT => array(
PATTERNS_EXPORT_ALL => 'user_patterns_export_all_role_users',
),
);
return $actions;
}
function user_patterns_export_all_roles($args = NULL, &$result = NULL) {
$roles = user_roles(TRUE);
$result = array();
switch ($args['type']) {
case PATTERNS_CREATE:
foreach ($roles as $rid => $role) {
$data = array(
'tag' => 'role',
);
$data['name'] = $role;
$action = array(
PATTERNS_CREATE => $data,
);
array_push($result, $action);
}
break;
case PATTERNS_MODIFY:
foreach ($roles as $rid => $role) {
$data = array(
'tag' => 'role',
);
$data['name'] = $role;
$data['rid'] = $rid;
$action = array(
PATTERNS_MODIFY => $data,
);
array_push($result, $action);
}
break;
}
return $result;
}
function user_patterns_export_all_permissions($args = NULL, &$result = NULL) {
$roles = user_roles();
$permissions = user_role_permissions($roles);
$result = array();
foreach ($permissions as $rid => $perms) {
$permissions = array_keys($perms);
$action = array(
PATTERNS_MODIFY => array(
'tag' => 'permission',
'role' => $roles[$rid],
'value' => $permissions,
),
);
array_push($result, $action);
}
return $result;
}
function user_patterns_export_all_users($args = NULL, &$result = NULL) {
$users = _user_patterns_get_all_users();
$result = array();
switch ($args['type']) {
case PATTERNS_CREATE:
foreach ($users as $user) {
$data = array(
'tag' => 'user',
);
$data['name'] = $user->name;
$data['mail'] = $user->mail;
$data['password'] = user_password();
$data['timezone'] = $user->timezone;
$data['status'] = $user->status;
$action = array(
PATTERNS_CREATE => $data,
);
array_push($result, $action);
}
break;
case PATTERNS_MODIFY:
foreach ($users as $uid => $user) {
$data = array(
'tag' => 'user',
);
$data['uid'] = $uid;
$data['name'] = $user->name;
$data['mail'] = $user->mail;
$data['timezone'] = $user->timezone;
$data['status'] = $user->status;
$action = array(
PATTERNS_MODIFY => $data,
);
array_push($result, $action);
}
break;
}
return $result;
}
function user_patterns_export_all_role_users($args = NULL, &$result = NULL) {
$roles = user_roles(TRUE);
$result = array();
foreach ($roles as $rid => $role) {
$data = array(
'tag' => 'role_users',
);
$data['role'] = $role;
$data['users'] = _user_patterns_users_with_role($role);
$action = array(
PATTERNS_CREATE => $data,
);
array_push($result, $action);
}
return $result;
}
function user_patterns_prepare($action, $tag, &$data) {
$status = PATTERNS_SUCCESS;
$msg = t('Execution successful');
if ($tag == 'user') {
if (!isset($data['name'])) {
if (!isset($data['uid'])) {
$status = PATTERNS_ERR;
$msg = t('Cannot create/modify user. Missing name and uid.');
}
$data['name'] = db_select('users', 'u')
->fields('u', array(
'name',
))
->condition('u.uid', $data['uid'])
->execute()
->fetchField();
}
else {
if (!isset($data['uid'])) {
$data['uid'] = db_select('users', 'u')
->fields('u', array(
'uid',
))
->condition('u.name', $data['name'])
->execute()
->fetchField();
}
}
if (isset($data['password'])) {
$data['pass'] = array(
'pass1' => $data['password'],
'pass2' => $data['password'],
);
}
else {
$data['pass'] = '';
}
}
elseif ($tag == 'role') {
}
return patterns_results($status, $msg);
}
function user_patterns_validate($action, $tag, &$data) {
$result = array();
$status = PATTERNS_SUCCESS;
$msg = '';
switch ($tag) {
case 'role':
switch ($action) {
case PATTERNS_CREATE:
$mandatory_attributes = array(
'name',
);
if (!_patterns_has_all_mandatory_attributes($data, $mandatory_attributes, $msg)) {
return patterns_results(PATTERNS_ERR, $msg);
}
if (_patterns_has_uninterpretable_attributes($data, $mandatory_attributes, $msg)) {
$status = PATTERNS_WARN;
}
break;
case PATTERNS_MODIFY:
$mandatory_attributes = array(
'name',
'rid',
);
if (!_patterns_has_all_mandatory_attributes($data, $mandatory_attributes, $msg)) {
return patterns_results(PATTERNS_ERR, $msg);
}
if (_patterns_has_uninterpretable_attributes($data, $mandatory_attributes, $msg)) {
$status = PATTERNS_WARN;
}
break;
case PATTERNS_DELETE:
$mandatory_attributes = array(
'rid',
);
if (!_patterns_has_all_mandatory_attributes($data, $mandatory_attributes, $msg)) {
return patterns_results(PATTERNS_ERR, $msg);
}
if (_patterns_has_uninterpretable_attributes($data, $mandatory_attributes, $msg)) {
$status = PATTERNS_WARN;
}
break;
}
break;
case 'permission':
if ($action == PATTERNS_CREATE || $action == PATTERNS_DELETE) {
$msg = t('Action %action is uncompatible for tag %tag.', array(
'%action' => $action,
'%tag' => $tag,
));
return patterns_results(PATTERNS_ERR, $msg);
}
else {
if ($action == PATTERNS_MODIFY) {
$mandatory_attributes = array(
'role',
'value',
);
if (!_patterns_has_all_mandatory_attributes($data, $mandatory_attributes, $msg)) {
return patterns_results(PATTERNS_ERR, $msg);
}
if (_patterns_has_uninterpretable_attributes($data, $mandatory_attributes, $msg)) {
$status = PATTERNS_WARN;
}
}
}
break;
case 'user':
switch ($action) {
case PATTERNS_CREATE:
$mandatory_attributes = array(
'name',
'mail',
'password',
'timezone',
'status',
);
if (!_patterns_has_all_mandatory_attributes($data, $mandatory_attributes, $msg)) {
return patterns_results(PATTERNS_ERR, $msg);
}
$interpretable_attributes = array(
'name',
'mail',
'password',
'timezone',
'status',
'uid',
'pass',
);
if (_patterns_has_uninterpretable_attributes($data, $interpretable_attributes, $msg)) {
$status = PATTERNS_WARN;
}
break;
case PATTERNS_MODIFY:
$mandatory_attributes = array(
'uid',
);
if (!_patterns_has_all_mandatory_attributes($data, $mandatory_attributes, $msg)) {
return patterns_results(PATTERNS_ERR, $msg);
}
$interpretable_attributes = array(
'uid',
'name',
'mail',
'timezone',
'status',
'pass',
);
if (_patterns_has_uninterpretable_attributes($data, $interpretable_attributes, $msg)) {
$status = PATTERNS_WARN;
}
break;
case PATTERNS_DELETE:
$mandatory_attributes = array(
'uid',
);
if (!_patterns_has_all_mandatory_attributes($data, $mandatory_attributes, $msg)) {
return patterns_results(PATTERNS_ERR, $msg);
}
if (_patterns_has_uninterpretable_attributes($data, $mandatory_attributes, $msg)) {
$status = PATTERNS_WARN;
}
break;
}
break;
case 'role_users':
switch ($action) {
case PATTERNS_CREATE:
case PATTERNS_DELETE:
$mandatory_attributes = array(
'role',
'users',
);
if (!_patterns_has_all_mandatory_attributes($data, $mandatory_attributes, $msg)) {
return patterns_results(PATTERNS_ERR, $msg);
}
if (_patterns_has_uninterpretable_attributes($data, $mandatory_attributes, $msg)) {
$status = PATTERNS_WARN;
}
break;
default:
$msg = t('Action %action is uncompatible for tag %tag.', array(
'%action' => $action,
'%tag' => $tag,
));
return patterns_results(PATTERNS_ERR, $msg);
}
break;
}
switch ($tag) {
case 'role':
$current_roles = user_roles();
switch ($action) {
case PATTERNS_CREATE:
$role_name = $data['name'];
if (in_array($role_name, $current_roles)) {
$result[] = array(
PATTERNS_WARNING_ALREADY_DEFINED_ELEMENT => t('The role name %role_name already exists in the system.', array(
'%role_name' => $role_name,
)),
);
}
break;
case PATTERNS_MODIFY:
$role_name = $data['name'];
$role_rid = $data['rid'];
if (!array_key_exists($role_rid, $current_roles)) {
$result[] = array(
PATTERNS_WARNING_ELEMENT_UNDEFINED => t('The role id %rid does not exist in the system.', array(
'%rid' => $role_rid,
)),
);
}
if (in_array($role_name, $current_roles)) {
$result[] = array(
PATTERNS_WARNING_ALREADY_DEFINED_ELEMENT => t('The role name %role_name already exists in the system.', array(
'%role_name' => $role_name,
)),
);
}
break;
case PATTERNS_DELETE:
$role_rid = $data['rid'];
if (!array_key_exists($role_rid, $current_roles) && !in_array($role_rid, $current_roles)) {
$result[] = array(
PATTERNS_WARNING_ELEMENT_UNDEFINED => t('The value %rid does not exist in the system as role name neither as a numeric rid.', array(
'%rid' => $role_rid,
)),
);
}
break;
}
break;
case 'permission':
if ($action == PATTERNS_MODIFY) {
$permissions = $data['value'];
$role = $data['role'];
$available_permissions = user_permission_get_modules();
$msg = "";
foreach ($permissions as $permission) {
if (!array_key_exists($permission, $available_permissions)) {
$msg .= t('Permission %permission is not available in the system.<br>', array(
'%permission' => $permission,
));
}
}
if ($msg != "") {
$result[] = array(
PATTERNS_WARNING_ELEMENT_UNDEFINED => $msg,
);
}
if (!user_role_load_by_name($role)) {
$result[] = array(
PATTERNS_WARNING_UNMET_DEPENDENCY => t('The role %role does not exist in the system.', array(
'%role' => $role,
)),
);
}
}
break;
case 'user':
$users = _user_patterns_get_all_users();
switch ($action) {
case PATTERNS_CREATE:
if (user_load_by_name($data['name'])) {
$result[] = array(
PATTERNS_WARNING_ALREADY_DEFINED_ELEMENT => t('The username %username already exists in the system.', array(
'%username' => $data['name'],
)),
);
}
if (user_load_by_mail($data['mail'])) {
$result[] = array(
PATTERNS_WARNING_ALREADY_DEFINED_ELEMENT => t('The username %mail already exists in the system.', array(
'%mail' => $data['mail'],
)),
);
}
break;
case PATTERNS_MODIFY:
if (!user_load($data['uid'])) {
$result[] = array(
PATTERNS_WARNING_ELEMENT_UNDEFINED => t('The User ID %uid does not exist in the system.', array(
'%uid' => $data['uid'],
)),
);
}
break;
case PATTERNS_DELETE:
if (!user_load($data['uid'])) {
$result[] = array(
PATTERNS_WARNING_ELEMENT_UNDEFINED => t('The User ID %uid does not exist in the system.', array(
'%uid' => $data['uid'],
)),
);
}
break;
}
break;
case 'role_users':
switch ($action) {
case PATTERNS_CREATE:
case PATTERNS_DELETE:
if (!user_role_load_by_name($data['role'])) {
$result[] = array(
PATTERNS_WARNING_UNMET_DEPENDENCY => t('The role %role does not exist in the system.', array(
'%role' => $data['role'],
)),
);
}
foreach ($data['users'] as $username) {
if (!user_load_by_name($username)) {
$result[] = array(
PATTERNS_WARNING_UNMET_DEPENDENCY => t('The username %username does not exist in the system.', array(
'%username' => $username,
)),
);
}
}
break;
}
break;
}
return patterns_results($status, $msg, $result);
}
function user_patterns_build($action, $form_id, &$data = NULL, &$a) {
$status = PATTERNS_SUCCESS;
$msg = '';
$result = NULL;
if ($form_id == 'user_register_form') {
if (isset($data['uid'])) {
unset($data['uid']);
}
}
elseif ($form_id == 'user_profile_form') {
if (empty($data['mail'])) {
$data['mail'] = db_query("SELECT mail FROM {users} WHERE uid = :uid", array(
'uid' => $data['uid'],
))
->fetchField();
}
if (empty($data['name'])) {
$data['name'] = db_query("SELECT name FROM {users} WHERE uid = :uid", array(
'uid' => $data['uid'],
))
->fetchField();
}
static $old_q;
$old_q = $_GET['q'];
$_GET['q'] = 'user/' . $data['uid'] . '/edit';
}
elseif ($id == 'user_cancel_confirm_form') {
$data['confirm'] = 1;
switch ($data['method']) {
case '1':
$data['user_cancel_method'] = 'user_cancel_block';
break;
case '2':
$data['user_cancel_method'] = 'user_cancel_block_unpublish';
break;
case '3':
$data['user_cancel_method'] = 'user_cancel_reassign';
break;
case '4':
$data['user_cancel_method'] = 'user_cancel_delete';
break;
}
}
elseif ($form_id == 'user_admin_roles' || $form_id == 'user_admin_role_delete_confirm' || $form_id == 'user_admin_role') {
if ($action === PATTERNS_DELETE) {
$data['op'] = t('Delete');
}
elseif ($data['rid']) {
$data['op'] = t('Save role');
}
else {
$data['op'] = t('Add role');
}
}
$result = $data;
return patterns_results($status, $msg, $result);
}
function user_patterns_params($action, $form_id, &$data = NULL, &$a) {
if ($form_id == 'user_cancel_confirm_form' && $form_id == 'user_profile_form') {
$result = array(
user_load($data['uid']),
);
}
elseif ($form_id == 'user_admin_role_delete_confirm' || $form_id == 'user_admin_role') {
$result = array(
(object) $data,
);
}
return patterns_results(PATTERNS_SUCCESS, t('Execution successful'), $result);
}
function user_patterns_cleanup($action, $tag, &$data) {
return patterns_results();
}
function set_user_permissions($form_id, &$form_state) {
$role = user_role_load_by_name($form_state['values']['role']);
$permissions = $form_state['values']['value'];
$modules = user_permission_get_modules();
foreach ($permissions as $key => $name) {
if (!isset($modules[$name])) {
unset($permissions[$key]);
}
}
patterns_set_user_permissions($role->rid, $permissions);
$msg = t('Permissions for role %role successfully updated.', array(
'%role' => $role->name,
));
return patterns_results(PATTERNS_SUCCESS, $msg);
}
function patterns_set_user_permissions($rid, $permissions) {
user_role_grant_permissions($rid, $permissions);
}
function set_user_values($form_id, &$form_state) {
$uid = $form_state['values']['uid'];
$attributes = array_diff_key($form_state['values'], array(
'uid' => $uid,
));
patterns_set_user_values($uid, $attributes);
$msg = t('Information for user %uid successfully updated.', array(
'%uid' => $uid,
));
return patterns_results(PATTERNS_SUCCESS, $msg);
}
function patterns_set_user_values($uid, $attributes) {
$num_updated = db_update('users')
->fields($attributes)
->condition('uid', $uid, '=')
->execute();
}
function set_role_users($form_id, &$form_state) {
$role = user_role_load_by_name($form_state['values']['role']);
$uids = array();
foreach ($form_state['values']['users'] as $username) {
$u = user_load_by_name($username);
$uids[] = $u->uid;
}
user_multiple_role_edit($uids, 'add_role', $role->rid);
$msg = count($uids) > 0 ? t('Role %role was assigned to the following users: %users.', array(
'%role' => $form_state['values']['role'],
'%users' => implode(', ', $uids),
)) : t('Role %role was applied to no user.', array(
'%role' => $form_state['values']['role'],
));
return patterns_results(PATTERNS_SUCCESS, $msg);
}
function unset_role_users($form_id, &$form_state) {
$role = user_role_load_by_name($form_state['values']['role']);
$uids = array();
foreach ($form_state['values']['users'] as $username) {
$u = user_load_by_name($username);
$uids[] = $u->uid;
}
user_multiple_role_edit($uids, 'remove_role', $role->rid);
$msg = count($uids) > 0 ? t('Role %role was removed to the following users: %users.', array(
'%role' => $form_state['values']['role'],
'%users' => implode(', ', $uids),
)) : t('Role %role was removed to no user.', array(
'%role' => $form_state['values']['role'],
));
return patterns_results(PATTERNS_SUCCESS, $msg);
}
function _user_patterns_get_all_users() {
$query = db_select('users', 'u');
$query
->fields('u', array(
'uid',
));
$query
->condition('uid', '0', '<>');
$uids = $query
->execute()
->fetchCol();
return user_load_multiple($uids);
}
function _user_patterns_users_with_role($role) {
$users_with_role = array();
$role_info = user_role_load_by_name($role);
$users = entity_load('user');
foreach ($users as $user) {
if (array_key_exists($role_info->rid, $user->roles)) {
array_push($users_with_role, $user->name);
}
}
return $users_with_role;
}