/** * Build a list of all roles that is asssigned to a profile. */ public static function buildRoleList($rids) { $list = array(); foreach ($rids as $rid => $value) { $role = user_role_load($rid); $list[] = $role->name; } return implode(', ', $list); }
/** * Refactor the roles property with rid-name format. */ public function getRoles($roles) { $return = array(); foreach ($roles as $role) { $info = user_role_load($role); $return[$info->rid] = $info->name; } return $return; }
/** * Explain your records in the {node_access} table. * * In order to help developers and administrators understand the forces * that control access to any given node, the DNA module provides the * Devel Node Access block, which lists all the grant records in the * {node_access} table for that node. * * However, every Node Access module is free in how it defines and uses the * 'realm' and 'gid' fields in its records in the {node_access} table, and * it's often difficult to interpret them. This hook passes each record * that DNA wants to display, and the owning module is expected to return * an explanation of that record. * * The explanation should not be localized (not be passed through t()), so * that administrators seeking help can present English explanations. * * @param $row * The record from the {node_access} table, as object. The member fields are: * nid, gid, realm, grant_view, grant_update, grant_delete. * * @return * A string with a (short!) explanation of the given {node_access} row, * to be displayed in DNA's 'Devel Node Access' block. It will be displayed * as HTML; any variable parts must already be sanitized. * * @see hook_node_access_records() * @see devel_node_access_node_access_explain() * * @ingroup node_access */ function hook_node_access_explain($row) { if ($row->realm == 'mymodule_myrealm') { if ($row->grant_view) { $role = user_role_load($row->gid); return 'Role ' . String::placeholder($role->name) . ' may view this node.'; } else { return 'No access.'; } } }
/** * Explain your records in the {node_access} table. * * In order to help developers and administrators understand the forces * that control access to any given node, the DNA module provides the * Devel Node Access block, which lists all the grant records in the * {node_access} table for that node. * * However, every Node Access module is free in how it defines and uses the * 'realm' and 'gid' fields in its records in the {node_access} table, and * it's often difficult to interpret them. This hook passes each record * that DNA wants to display, and the owning module is expected to return * an explanation of that record. * * The explanation should not be localized (not be passed through t()), so * that administrators seeking help can present English explanations. * * @param $row * The record from the {node_access} table, as object. The member fields are: * nid, gid, realm, grant_view, grant_update, grant_delete. * * @return * A string with a (short!) explanation of the given {node_access} row, * to be displayed in DNA's 'Devel Node Access' block. It will be displayed * as HTML; any variable parts must already be sanitized. * * @see hook_node_access_records() * @see devel_node_access_node_access_explain() * * @ingroup node_access */ function hook_node_access_explain($row) { if ($row->realm == 'mymodule_myrealm') { if ($row->grant_view) { $role = user_role_load($row->gid); return t('Role %role may view this node.', array('%role' => $role->name)); } else { return 'No access.'; } } }
/** * Explain your records in the {node_access} table. * * In order to help developers and administrators understand the forces * that control access to any given node, the DNA module provides the * Devel Node Access block, which lists all the grant records in the * {node_access} table for that node. * * However, every Node Access module is free in how it defines and uses the * 'realm' and 'gid' fields in its records in the {node_access} table, and * it's often difficult to interpret them. This hook passes each record * that DNA wants to display, and the owning module is expected to return * an explanation of that record. * * The explanation should not be localized (not be passed through t()), so * that administrators seeking help can present English explanations. * * @param $row * The record from the {node_access} table, as object. The member fields are: * nid, gid, realm, grant_view, grant_update, grant_delete. * * @return * A string with a (short!) explanation of the given {node_access} row, * to be displayed in DNA's 'Devel Node Access' block. It will be displayed * as HTML; any variable parts must already be sanitized. * * @see hook_node_access_records() * @see devel_node_access_node_access_explain() * * @ingroup node_access */ function hook_node_access_explain($row) { if ($row->realm == 'mymodule_myrealm') { if ($row->grant_view) { $role = user_role_load($row->gid); return 'Role ' . \Drupal\Component\Utility\SafeMarkup::placeholder($role->name) . ' may view this node.'; } else { return 'No access.'; } } }
/** * Load existing role from database * * @param NodeInterface $node * @param Context $context * * @return stdClass */ protected function loadExistingRole(NodeInterface $node, Context $context) { switch ($node->getName()) { case 'anonymous': return user_role_load(DRUPAL_ANONYMOUS_RID); case 'authenticated': return user_role_load(DRUPAL_AUTHENTICATED_RID); default: return user_role_load_by_name($this->getRoleName($node, $context)); } }
/** * Explain your records in the {node_access} table. * * In order to help developers and administrators understand the forces * that control access to any given node, the DNA module provides the * Devel Node Access block, which lists all the grant records in the * {node_access} table for that node. * * However, every Node Access module is free in how it defines and uses the * 'realm' and 'gid' fields in its records in the {node_access} table, and * it's often difficult to interpret them. This hook passes each record * that DNA wants to display, and the owning module is expected to return * an explanation of that record. * * The explanation should not be localized (not be passed through t()), so * that administrators seeking help can present English explanations. * * @param $row * The record from the {node_access} table, as object. The member fields are: * nid, gid, realm, grant_view, grant_update, grant_delete. * * @return * A string with a (short!) explanation of the given {node_access} row, * to be displayed in DNA's 'Devel Node Access' block. It will be displayed * as HTML; any variable parts must already be sanitized. * * @see hook_node_access_records() * @see devel_node_access_node_access_explain() * * @ingroup node_access */ function hook_node_access_explain($row) { if ($row->realm == 'mymodule_myrealm') { if ($row->grant_view) { $role = user_role_load($row->gid); return \Drupal\Component\Utility\SafeMarkup::format('Role %role may view this node.', array('%role' => $role->name)); } else { return 'No access.'; } } }
/** * {@inheritdoc} */ public function userAddRole(\stdClass $user, $role_name) { // Allow both machine and human role names. $roles = user_role_names(); $id = array_search($role_name, $roles); if (FALSE !== $id) { $role_name = $id; } if (!($role = user_role_load($role_name))) { throw new \RuntimeException(sprintf('No role "%s" exists.', $role_name)); } $account = \user_load($user->uid); $account->addRole($role->id()); $account->save(); }
/** * Creates a role with specified permissions. * * @param $permissions * Array of permission names to assign to role. * @param $name * (optional) String for the name of the role. Defaults to a random string. * @return * Role name of newly created role, or FALSE if role creation failed. */ protected function backdropCreateRole(array $permissions, $name = NULL) { // Generate random name if it was not passed. if (!$name) { $name = $this->randomName(); } // Check the all the permissions strings are valid. if (!$this->checkPermissions($permissions)) { return FALSE; } // Create new role. $role = new stdClass(); $role->name = $name; $role->label = $name; user_role_save($role); user_role_grant_permissions($role->name, $permissions); $role = user_role_load($role->name); $this->assertTrue(isset($role->name), t('Created role of name: @name', array('@name' => $name)), t('Role')); if ($role && !empty($role->name)) { $this->assertTrue(count($role->permissions) == count($permissions), t('Created permissions: @perms', array('@perms' => implode(', ', $permissions))), t('Role')); return $role->name; } else { return FALSE; } }
/** * {@inheritDoc} */ public function userCreate(\stdClass $user) { // Default status to TRUE if not explicitly creating a blocked user. if (!isset($user->status)) { $user->status = 1; } // Convert roles to proper structure. if (isset($user->roles)) { foreach ($user->roles as $key => $rid) { $role = user_role_load($rid); unset($user->roles[$key]); $user->roles[$rid] = $role->name; } } // Clone user object, otherwise user_save() changes the password to the // hashed password. $account = clone $user; user_save($account, (array) $user); // Store UID. $user->uid = $account->uid; }
/** * Helper method to serialize options. */ function _reformat_roles($form, &$form_state) { $roles = array(); foreach ($form_state['values']['roles'] as $rid) { if ($rid !== 0 && ($role = user_role_load($rid))) { $roles[] = $role->name; } } unset($form_state['values']['roles']); $form_state['values']['options']['roles'] = $roles; }
/** * Converts an array of role ids or role names to an array of role_id => * role_name key/paid values. * * @param array $roles * An array of role ids or role names. * * @return array * An associative array with role id as key and role name as value. * * @throws \Exception * if provided role id or role name does not exist. */ private static function formatRoles($roles) { if (is_string($roles) || is_numeric($roles)) { $roles = array($roles); } $output_roles = array(); foreach ($roles as $rid) { if (is_numeric($rid) && ($role = user_role_load($rid))) { $output_roles[$role->rid] = $role->name; } elseif (is_string($rid) && ($role = user_role_load_by_name($rid))) { $output_roles[$role->rid] = $role->name; } else { throw new \Exception("Role {$rid} does not exist."); } } return $output_roles; }