本文整理汇总了PHP中Drupal\user\Entity\Role类的典型用法代码示例。如果您正苦于以下问题:PHP Role类的具体用法?PHP Role怎么用?PHP Role使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Role类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testUserSelectionByRole
/**
* Tests user selection by roles.
*/
function testUserSelectionByRole()
{
$field_definition = FieldInstanceConfig::loadByName('user', 'user', 'user_reference');
$field_definition->settings['handler_settings']['filter']['role'] = array($this->role1->id() => $this->role1->id(), $this->role2->id() => 0);
$field_definition->settings['handler_settings']['filter']['type'] = 'role';
$field_definition->save();
$user1 = $this->createUser(array('name' => 'aabb'));
$user1->addRole($this->role1->id());
$user1->save();
$user2 = $this->createUser(array('name' => 'aabbb'));
$user2->addRole($this->role1->id());
$user2->save();
$user3 = $this->createUser(array('name' => 'aabbbb'));
$user3->addRole($this->role2->id());
$user3->save();
/** @var \Drupal\entity_reference\EntityReferenceAutocomplete $autocomplete */
$autocomplete = \Drupal::service('entity_reference.autocomplete');
$matches = $autocomplete->getMatches($field_definition, 'user', 'user', 'NULL', '', 'aabb');
$this->assertEqual(count($matches), 2);
$users = array();
foreach ($matches as $match) {
$users[] = $match['label'];
}
$this->assertTrue(in_array($user1->label(), $users));
$this->assertTrue(in_array($user2->label(), $users));
$this->assertFalse(in_array($user3->label(), $users));
$matches = $autocomplete->getMatches($field_definition, 'user', 'user', 'NULL', '', 'aabbbb');
$this->assertEqual(count($matches), 0, '');
}
示例2: getUserRoles
/**
* 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;
}
示例3: testRolePurchaseCheckout
public function testRolePurchaseCheckout()
{
// Add role assignment to the test product.
$rid = $this->drupalCreateRole(array('access content'));
$this->drupalLogin($this->adminUser);
$this->drupalPostForm('node/' . $this->product->id() . '/edit/features', array('feature' => 'role'), t('Add'));
$edit = array('role' => $rid, 'end_override' => TRUE, 'expire_relative_duration' => 1, 'expire_relative_granularity' => 'day');
$this->drupalPostForm(NULL, $edit, t('Save feature'));
// Check out with the test product.
$method = $this->createPaymentMethod('other');
$this->addToCart($this->product);
$order = $this->checkout();
uc_payment_enter($order->id(), $method['id'], $order->getTotal());
// Test that the role was granted.
// @todo Re-enable when Rules is available.
// $this->assertTrue($order->getOwner()->hasRole($rid), 'Existing user was granted role.');
// Test that the email is correct.
$role = Role::load($rid);
// @todo Re-enable when Rules is available.
// $this->assertMailString('subject', $role->label(), 4, 'Role assignment email mentions role in subject line.');
// Test that the role product / user relation is deleted with the user.
user_delete($order->getOwnerId());
// Run cron to ensure deleted users are handled correctly.
$this->cronRun();
}
示例4: rolePermissions
/**
* Returns the permission strings that a group of roles have.
*
* @param array $roleIDs
* The array of roleIDs to check.
* @param bool $groupByRoleId
* Choose whether to group permissions by role ID.
* @return array
* An array of the permissions untrusted roles have. If $groupByRoleId is
* true, the array key is the role ID, the value is the array of permissions
* the role has.
*/
public static function rolePermissions(array $roleIDs, $groupByRoleId = FALSE)
{
// Get the permissions the given roles have, grouped by roles.
$permissions_grouped = user_role_permissions($roleIDs);
// Fill up the administrative roles' permissions too.
foreach ($roleIDs as $roleID) {
$role = Role::load($roleID);
/** @var Role $role */
if ($role->isAdmin()) {
$permissions_grouped[$roleID] = static::permissions();
}
}
if ($groupByRoleId) {
// If the result should be grouped, we have nothing else to do.
return $permissions_grouped;
} else {
// Merge the grouped permissions into $untrusted_permissions.
$untrusted_permissions = array();
foreach ($permissions_grouped as $rid => $permissions) {
$untrusted_permissions = array_merge($untrusted_permissions, $permissions);
}
// Remove duplicate elements and fix indexes.
$untrusted_permissions = array_values(array_unique($untrusted_permissions));
return $untrusted_permissions;
}
}
示例5: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
$admin_roles = $this->adminUser->getRoles();
$admin_role = Role::load(reset($admin_roles));
$this->grantPermissions($admin_role, ['administer site configuration']);
}
示例6: testUserRole
/**
* Tests user role migration.
*/
public function testUserRole()
{
/** @var \Drupal\migrate\entity\Migration $migration */
$id_map = Migration::load('d6_user_role')->getIdMap();
$rid = 'anonymous';
$anonymous = Role::load($rid);
$this->assertIdentical($rid, $anonymous->id());
$this->assertIdentical(array('migrate test anonymous permission', 'use text format filtered_html'), $anonymous->getPermissions());
$this->assertIdentical(array($rid), $id_map->lookupDestinationId(array(1)));
$rid = 'authenticated';
$authenticated = Role::load($rid);
$this->assertIdentical($rid, $authenticated->id());
$this->assertIdentical(array('migrate test authenticated permission', 'use text format filtered_html'), $authenticated->getPermissions());
$this->assertIdentical(array($rid), $id_map->lookupDestinationId(array(2)));
$rid = 'migrate_test_role_1';
$migrate_test_role_1 = Role::load($rid);
$this->assertIdentical($rid, $migrate_test_role_1->id());
$this->assertIdentical(array('migrate test role 1 test permission', 'use text format full_html', 'use text format php_code'), $migrate_test_role_1->getPermissions());
$this->assertIdentical(array($rid), $id_map->lookupDestinationId(array(3)));
$rid = 'migrate_test_role_2';
$migrate_test_role_2 = Role::load($rid);
$this->assertIdentical(array('migrate test role 2 test permission', 'use PHP for settings', 'administer contact forms', 'skip comment approval', 'edit own blog content', 'edit any blog content', 'delete own blog content', 'delete any blog content', 'create forum content', 'delete any forum content', 'delete own forum content', 'edit any forum content', 'edit own forum content', 'administer nodes', 'access content overview', 'use text format php_code'), $migrate_test_role_2->getPermissions());
$this->assertIdentical($rid, $migrate_test_role_2->id());
$this->assertIdentical(array($rid), $id_map->lookupDestinationId(array(4)));
$rid = 'migrate_test_role_3_that_is_long';
$migrate_test_role_3 = Role::load($rid);
$this->assertIdentical($rid, $migrate_test_role_3->id());
$this->assertIdentical(array($rid), $id_map->lookupDestinationId(array(5)));
}
示例7: setUp
/**
* {@inheritdoc}
*/
protected function setUp($import_test_views = TRUE)
{
parent::setUp($import_test_views);
$this->installEntitySchema('user');
$this->installEntitySchema('comment');
// Create the anonymous role.
$this->installConfig(['user']);
// Create an anonymous user.
$storage = \Drupal::entityManager()->getStorage('user');
// Insert a row for the anonymous user.
$storage->create(array('uid' => 0, 'name' => '', 'status' => 0))->save();
$admin_role = Role::create(['id' => 'admin', 'permissions' => ['administer comments', 'access user profiles']]);
$admin_role->save();
/* @var \Drupal\user\RoleInterface $anonymous_role */
$anonymous_role = Role::load(Role::ANONYMOUS_ID);
$anonymous_role->grantPermission('access comments');
$anonymous_role->save();
$this->adminUser = User::create(['name' => $this->randomMachineName(), 'roles' => [$admin_role->id()]]);
$this->adminUser->save();
// Create some comments.
$comment = Comment::create(['subject' => 'My comment title', 'uid' => $this->adminUser->id(), 'name' => $this->adminUser->label(), 'entity_type' => 'entity_test', 'comment_type' => 'entity_test', 'status' => 1]);
$comment->save();
$comment_anonymous = Comment::create(['subject' => 'Anonymous comment title', 'uid' => 0, 'name' => 'barry', 'mail' => 'test@example.com', 'homepage' => 'https://example.com', 'entity_type' => 'entity_test', 'comment_type' => 'entity_test', 'created' => 123456, 'status' => 1]);
$comment_anonymous->save();
}
示例8: testRevisionView
public function testRevisionView()
{
$entity = EnhancedEntity::create(['name' => 'rev 1', 'type' => 'default']);
$entity->save();
$revision = clone $entity;
$revision->name->value = 'rev 2';
$revision->setNewRevision(TRUE);
$revision->isDefaultRevision(FALSE);
$revision->save();
/** @var \Symfony\Component\HttpKernel\HttpKernelInterface $http_kernel */
$http_kernel = \Drupal::service('http_kernel');
$request = Request::create($revision->url('revision'));
$response = $http_kernel->handle($request);
$this->assertEquals(403, $response->getStatusCode());
$role = Role::create(['id' => 'test_role']);
$role->grantPermission('view all entity_test_enhanced revisions');
$role->grantPermission('administer entity_test_enhanced');
$role->save();
$user = User::create(['name' => 'Test user']);
$user->addRole($role->id());
\Drupal::service('account_switcher')->switchTo($user);
$request = Request::create($revision->url('revision'));
$response = $http_kernel->handle($request);
$this->assertEquals(200, $response->getStatusCode());
$this->assertNotContains('rev 1', $response->getContent());
$this->assertContains('rev 2', $response->getContent());
}
示例9: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
// Give anonymous users permission to view test entities, so that we can
// verify the cache tags of cached versions of test entity pages.
$user_role = Role::load(RoleInterface::ANONYMOUS_ID);
$user_role->grantPermission('view test entity');
$user_role->save();
// Create an entity.
$this->entity = $this->createEntity();
// If this is an entity with field UI enabled, then add a configurable
// field. We will use this configurable field in later tests to ensure that
// field configuration invalidate render cache entries.
if ($this->entity->getEntityType()->get('field_ui_base_route')) {
// Add field, so we can modify the field storage and field entities to
// verify that changes to those indeed clear cache tags.
entity_create('field_storage_config', array('field_name' => 'configurable_field', 'entity_type' => $this->entity->getEntityTypeId(), 'type' => 'test_field', 'settings' => array()))->save();
entity_create('field_config', array('entity_type' => $this->entity->getEntityTypeId(), 'bundle' => $this->entity->bundle(), 'field_name' => 'configurable_field', 'label' => 'Configurable field', 'settings' => array()))->save();
// Reload the entity now that a new field has been added to it.
$storage = $this->container->get('entity.manager')->getStorage($this->entity->getEntityTypeId());
$storage->resetCache();
$this->entity = $storage->load($this->entity->id());
}
// Create a referencing and a non-referencing entity.
list($this->referencingEntity, $this->nonReferencingEntity, ) = $this->createReferenceTestEntities($this->entity);
}
示例10: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
$this->installConfig(array('user', 'entity_test'));
// Give anonymous users permission to view test entities.
Role::load(RoleInterface::ANONYMOUS_ID)->grantPermission('view test entity')->save();
}
示例11: role_create
public function role_create($role_machine_name, $role_human_readable_name = '')
{
// In D6 and D7, when we create a new role, the role
// machine name is specified, and the numeric rid is
// auto-assigned (next available id); in D8, when we
// create a new role, we need to specify both the rid,
// which is now the role machine name, and also a human-readable
// role name. If the client did not provide a human-readable
// name, then we'll use the role machine name in its place.
if (empty($role_human_readable_name)) {
$role_human_readable_name = ucfirst($role_machine_name);
}
$role = new Role(array('id' => $role_machine_name, 'label' => $role_human_readable_name), 'user_role');
$role->save();
return $role;
}
示例12: testUserPermissionCacheContextOptimization
/**
* Ensures that 'user.permissions' cache context is able to define cache tags.
*/
public function testUserPermissionCacheContextOptimization()
{
$user1 = $this->createUser();
$this->assertEqual($user1->id(), 1);
$authenticated_user = $this->createUser(['administer permissions']);
$role = $authenticated_user->getRoles()[1];
$test_element = ['#cache' => ['keys' => ['test'], 'contexts' => ['user', 'user.permissions']]];
\Drupal::service('account_switcher')->switchTo($authenticated_user);
$element = $test_element;
$element['#markup'] = 'content for authenticated users';
$output = \Drupal::service('renderer')->renderRoot($element);
$this->assertEqual($output, 'content for authenticated users');
// Verify that the render caching is working so that other tests can be
// trusted.
$element = $test_element;
$element['#markup'] = 'this should not be visible';
$output = \Drupal::service('renderer')->renderRoot($element);
$this->assertEqual($output, 'content for authenticated users');
// Even though the cache contexts have been optimized to only include 'user'
// cache context, the element should have been changed because
// 'user.permissions' cache context defined a cache tags for permission
// changes, which should have bubbled up for the element when it was
// optimized away.
Role::load($role)->revokePermission('administer permissions')->save();
$element = $test_element;
$element['#markup'] = 'this should be visible';
$output = \Drupal::service('renderer')->renderRoot($element);
$this->assertEqual($output, 'this should be visible');
}
示例13: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
// Give anonymous users permission to view nodes, so that we can verify the
// cache tags of cached versions of node pages.
Role::load(RoleInterface::ANONYMOUS_ID)->grantPermission('access tour')->save();
}
示例14: setUp
/**
* Performs setup tasks before each individual test method is run.
*/
public function setUp()
{
parent::setUp('content_access');
// The parent method already installs most needed node and comment schemas,
// but here we also need the comment statistics.
$this->installSchema('comment', array('comment_entity_statistics'));
// Create a node type for testing.
$type = NodeType::create(array('type' => 'page', 'name' => 'page'));
$type->save();
// Create anonymous user role.
$role = Role::create(array('id' => 'anonymous', 'label' => 'anonymous'));
$role->save();
// Insert the anonymous user into the database, as the user table is inner
// joined by \Drupal\comment\CommentStorage.
User::create(array('uid' => 0, 'name' => ''))->save();
// Create a node with attached comment.
$this->nodes[0] = Node::create(array('status' => NODE_PUBLISHED, 'type' => 'page', 'title' => 'test title'));
$this->nodes[0]->save();
$comment_type = CommentType::create(array('id' => 'comment', 'target_entity_type_id' => 'node'));
$comment_type->save();
$this->installConfig(array('comment'));
$this->addDefaultCommentField('node', 'page');
$comment = Comment::create(array('entity_type' => 'node', 'entity_id' => $this->nodes[0]->id(), 'field_name' => 'comment', 'body' => 'test body', 'comment_type' => $comment_type->id()));
$comment->save();
$this->comments[] = $comment;
$this->nodes[1] = Node::create(array('status' => NODE_PUBLISHED, 'type' => 'page', 'title' => 'some title'));
$this->nodes[1]->save();
$this->nodes[2] = Node::create(array('status' => NODE_NOT_PUBLISHED, 'type' => 'page', 'title' => 'other title'));
$this->nodes[2]->save();
// Also index users, to verify that they are unaffected by the processor.
$this->index->set('datasources', array('entity:comment', 'entity:node', 'entity:user'));
$this->index->save();
$this->index = entity_load('search_api_index', $this->index->id(), TRUE);
}
示例15: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
$this->installSchema('system', 'sequences');
$this->installEntitySchema('user');
$this->manager = $this->container->get('plugin.manager.condition');
// Set up the authenticated and anonymous roles.
Role::create(array('id' => RoleInterface::ANONYMOUS_ID, 'label' => 'Anonymous user'))->save();
Role::create(array('id' => RoleInterface::AUTHENTICATED_ID, 'label' => 'Authenticated user'))->save();
// Create new role.
$rid = strtolower($this->randomMachineName(8));
$label = $this->randomString(8);
$role = Role::create(array('id' => $rid, 'label' => $label));
$role->save();
$this->role = $role;
// Setup an anonymous user for our tests.
$this->anonymous = User::create(array('name' => '', 'uid' => 0));
$this->anonymous->save();
// Loading the anonymous user adds the correct role.
$this->anonymous = User::load($this->anonymous->id());
// Setup an authenticated user for our tests.
$this->authenticated = User::create(array('name' => $this->randomMachineName()));
$this->authenticated->save();
// Add the custom role.
$this->authenticated->addRole($this->role->id());
}