function upload_users_set_role($hook, $type, $return, $params) { $header = $params['header']; $user = $params['user']; $value = $params['value']; if ($header == 'user_upload_role') { $role = roles_get_role_by_name($value); roles_set_role($role, $user); return true; } return $return; }
/** * Assigns user role when user is created * * @param string $event "create" * @param string $type "user" * @param ElggUser $user User entity * @return void */ function roles_create_user($event, $type, $user) { $rolename = get_input('role', false); if (elgg_is_admin_logged_in() && $rolename) { // admin is adding a user, give them the role they asked for $role = roles_get_role_by_name($rolename); if ($role) { roles_set_role($role, $user); } } }
/** * * Saves user role upon changing role on the user settings page * * @param string $hook_name Equals "usersettings:save" * @param string $entity_type Equals "user" * @param mixed $return_value * @param mixed $params An associative array of parameters provided by the hook trigger */ function roles_user_settings_save($hook_name, $entity_type, $return_value, $params) { $role_name = get_input('role'); $user_id = get_input('guid'); $role_name = roles_filter_role_name($role_name); $role = roles_get_role_by_name($role_name); $user = get_entity($user_id); $res = roles_set_role($role, $user); if ($res === false) { register_error(elgg_echo('user:role:fail')); return false; } else { if ($res === true) { system_message(elgg_echo('user:role:success')); return true; } } return null; }
$guid = get_input('guid'); $role_name = get_input('role'); $user = get_entity($guid); $role = roles_get_role_by_name($role_name); if (!elgg_is_admin_logged_in()) { register_error(elgg_echo(PLUGIN_ID . ':set:error:admin_gatekeeper')); forward(REFERER); } if (!elgg_instanceof($user, 'user')) { register_error(elgg_echo(PLUGIN_ID . ':set:error:no_user')); forward(REFERER); } if (!elgg_instanceof($role, 'object', 'role')) { register_error(elgg_echo(PLUGIN_ID . ':set:error:no_role')); forward(REFERER); } $current_role = roles_get_role($user); if ($role->guid == $current_role->guid) { register_error(elgg_echo(PLUGIN_ID . ':set:error:equivalent_role')); forward(REFERER); } if (roles_set_role($role, $user)) { system_message(elgg_echo(PLUGIN_ID . ':set:success')); if (elgg_is_xhr()) { $output = array('user' => array('guid' => $user->guid, 'name' => $user->name), 'role' => array('guid' => $role->guid, 'name' => $role->name, 'title' => $role->title)); print json_encode($output); } } else { register_error(elgg_echo(PLUGIN_ID . ':set:error:unknown')); } forward(REFERER);
/** * Set user role upon import * * @global array $UPLOAD_USERS_ROLES_CACHE Cache roles * @param string $hook Equals 'header:custom_method' * @param string $type Equals 'upload_users' * @param boolean $return Return flag received by the hook * @param array $params An array of additional hook parameters * @return boolean If true, the upload script will not attempt to store the value as metadata */ function upload_users_set_role($hook, $type, $return, $params) { if (!elgg_is_active_plugin('roles')) { return $return; } $metadata_name = $params['metadata_name']; $user = $params['user']; $value = $params['value']; if ($metadata_name != 'user_upload_role' || !$value || !elgg_instanceof($user, 'user')) { return $return; } global $UPLOAD_USERS_ROLES_CACHE; if (!isset($UPLOAD_USERS_ROLES_CACHE[$value])) { $role = roles_get_role_by_name($value); if ($role) { $UPLOAD_USERS_ROLES_CACHE[$value] = $role; } } else { $role = $UPLOAD_USERS_ROLES_CACHE[$value]; } $return['status'] = roles_set_role($role, $user); elgg_log("Upload users: assigning role {$value} to {$user->guid}; Status: {$return['status']}"); return $return; }
function roles_pm_profile_type_change($event, $type, $metadata) { if ($metadata->name != 'custom_profile_type') { return true; } switch ($event) { case 'delete': remove_entity_relationships($metadata->entity_guid, 'has_role'); break; case 'create': case 'update': $profile_type_guid = $metadata->value; $roles = elgg_get_entities_from_relationship(array('relationship_guid' => $profile_type_guid, 'relationship' => 'roles_profile_type', 'inverse_relationship' => true)); $role = $roles[0]; $user = get_entity($metadata->entity_guid); if (!roles_has_role($user, $role->name)) { roles_set_role($role, $user); } break; default: return true; break; } return true; }