/** * Returns role entities allowed to masquerade as. * * @return \Drupal\user\RoleInterface[] * An associative array with the role id as the key and the role object as * value. */ protected function getUserRoles() { $roles = Role::loadMultiple(); // Do not allow masquerade as anonymous user, use private browsing. unset($roles[RoleInterface::ANONYMOUS_ID]); return $roles; }
/** * Drupal8Module constructor. */ public function __construct(ModuleContainer $container, $config = null) { $this->config = array_merge(['drupal_root' => Configuration::projectDir() . 'web', 'site_path' => 'sites/test', 'create_users' => true, 'destroy_users' => true, 'test_user_pass' => 'test'], (array) $config); // Bootstrap a bare minimum Kernel so we can interact with Drupal. $autoloader = (require $this->config['drupal_root'] . '/autoload.php'); $kernel = new TestDrupalKernel('prod', $autoloader, $this->config['drupal_root']); $kernel->bootTestEnvironment($this->config['site_path']); // Allow for setting some basic info output. $this->output = new ConsoleOutput(); // Get our role definitions as we use them a lot. $this->roles = Role::loadMultiple(); parent::__construct($container); }
/** * {@inheritdoc} */ public function form(array $form, FormStateInterface $form_state) { $form = parent::form($form, $form_state); $type = $this->entity; if ($this->operation == 'add') { $form['#title'] = $this->t('Add profile type'); } else { $form['#title'] = $this->t('Edit %label profile type', ['%label' => $type->label()]); } $form['label'] = ['#title' => t('Label'), '#type' => 'textfield', '#default_value' => $type->label(), '#description' => t('The human-readable name of this profile type.'), '#required' => TRUE, '#size' => 30]; $form['id'] = ['#type' => 'machine_name', '#default_value' => $type->id(), '#maxlength' => EntityTypeInterface::BUNDLE_MAX_LENGTH, '#machine_name' => ['exists' => '\\Drupal\\profile\\Entity\\ProfileType::load', 'source' => ['label']]]; $form['registration'] = ['#type' => 'checkbox', '#title' => t('Include in user registration form'), '#default_value' => $type->getRegistration()]; $form['multiple'] = ['#type' => 'checkbox', '#title' => t('Allow multiple profiles'), '#default_value' => $type->getMultiple()]; $form['roles'] = ['#type' => 'checkboxes', '#title' => t('Allowed roles'), '#description' => $this->t('Limit the users that can have this profile by role.</br><em>None will indicate that all users can have this profile type.</em>'), '#options' => [], '#default_value' => $type->getRoles()]; foreach (Role::loadMultiple() as $role) { /** @var \Drupal\user\Entity\Role $role */ // We aren't interested in anon role. if ($role->id() !== Role::ANONYMOUS_ID) { $form['roles']['#options'][$role->id()] = $role->label(); } } return $this->protectBundleIdElement($form); }
/** * The number of users who have admin-level user roles. * * @return int */ private function getAdminCount() { $roles_name = array(); $get_roles = Role::loadMultiple(); unset($get_roles[DRUPAL_ANONYMOUS_RID]); $permission = array('administer permissions', 'administer users'); foreach ($permission as $key => $value) { $filtered_roles = array_filter($get_roles, function ($role) use($value) { return $role->hasPermission($value); }); foreach ($filtered_roles as $role_name => $data) { $roles_name[] = $role_name; } } if (!empty($roles_name)) { $roles_name_unique = array_unique($roles_name); $query = db_select('user__roles', 'ur'); $query->fields('ur', array('entity_id')); $query->condition('ur.bundle', 'user', '='); $query->condition('ur.deleted', '0', '='); $query->condition('ur.roles_target_id', $roles_name_unique, 'IN'); $count = $query->countQuery()->execute()->fetchField(); } return isset($count) && is_numeric($count) ? $count : NULL; }
/** * Tests user role migration. */ public function testUserRole() { $id_map = $this->getMigration('d6_user_role')->getIdMap(); $this->assertRoles($id_map); // Test there are no duplicated roles. $roles = [ 'anonymous1', 'authenticated1', 'administrator1', 'migrate_test_role_11', 'migrate_test_role_21', 'migrate_test_role_3_that_is_longer_than_thirty_two_characters1' ]; $this->assertEmpty(Role::loadMultiple($roles)); // Remove the map row for the migrate_test_role_1 role and rerun the // migration. This will re-import the migrate_test_role_1 role migration // again. $this->sourceDatabase->insert('role') ->fields([ 'rid' => 6, 'name' => 'migrate test role 4', ]) ->execute(); $this->sourceDatabase->insert('permission') ->fields([ 'pid' => 7, 'rid' => 6, 'perm' => 'access content', 'tid' => 0, ]) ->execute(); $id_map->delete(['rid' => 3]); $this->executeMigration('d6_user_role'); // Test there are no duplicated roles. $roles[] = 'migrate_test_role_41'; $this->assertEmpty(Role::loadMultiple($roles)); // Test that the existing roles have not changed. $this->assertRoles($id_map); // Test the migration of the new role, migrate_test_role_4. $permissions = ['access content']; $this->assertRole('migrate_test_role_4', $permissions, 6, $id_map); }
/** * Tests user role migration. */ public function testUserRole() { $this->assertEntity('anonymous', 'anonymous user', 1); $this->assertEntity('authenticated', 'authenticated user', 2); $this->assertEntity('administrator', 'administrator', 3); // Test there are no duplicated roles. $roles = [ 'anonymous1', 'authenticated1', 'administrator1', ]; $this->assertEmpty(Role::loadMultiple($roles)); // Remove the map row for the administrator role and rerun the migration. // This will re-import the administrator role again. $id_map = $this->getMigration('d7_user_role')->getIdMap(); $id_map->delete(['rid' => 3]); $this->sourceDatabase->insert('role') ->fields([ 'rid' => 4, 'name' => 'test role', 'weight' => 10, ]) ->execute(); $this->sourceDatabase->insert('role_permission') ->fields([ 'rid' => 4, 'permission' => 'access content', 'module' => 'node', ]) ->execute(); $this->executeMigration('d7_user_role'); // Test there are no duplicated roles. $roles = [ 'anonymous1', 'authenticated1', 'administrator1', ]; $this->assertEmpty(Role::loadMultiple($roles)); // Test that the existing roles have not changed. $this->assertEntity('administrator', 'administrator', 3); $this->assertEntity('anonymous', 'anonymous user', 1); $this->assertEntity('authenticated', 'authenticated user', 2); // Test the migration of the new role, test role. $this->assertEntity('test_role', 'test role', 4); }