本文整理汇总了PHP中Drupal\user\Entity\Role::load方法的典型用法代码示例。如果您正苦于以下问题:PHP Role::load方法的具体用法?PHP Role::load怎么用?PHP Role::load使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\user\Entity\Role
的用法示例。
在下文中一共展示了Role::load方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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']);
}
示例2: 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');
}
示例3: 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();
}
示例4: 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();
}
示例5: 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;
}
}
示例6: 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);
}
示例7: 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();
}
示例8: 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)));
}
示例9: assertRole
/**
* Helper function to perform assertions on a user role.
*
* @param string $id
* The role ID.
* @param string[] $permissions
* An array of user permissions.
* @param int $lookupId
* The original numeric ID of the role in the source database.
* @param \Drupal\migrate\Plugin\MigrateIdMapInterface $id_map
* The map table plugin.
*/
protected function assertRole($id, array $permissions, $lookupId, MigrateIdMapInterface $id_map) {
/** @var \Drupal\user\RoleInterface $role */
$role = Role::load($id);
$this->assertInstanceOf(RoleInterface::class, $role);
$this->assertSame($permissions, $role->getPermissions());
$this->assertSame([[$id]], $id_map->lookupDestinationIds(['rid' => $lookupId]));
}
示例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: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
// Give anonymous users permission to access feeds, so that we can verify
// the cache tags of cached versions of feeds.
$user_role = Role::load(DRUPAL_ANONYMOUS_RID);
$user_role->grantPermission('access news feeds');
$user_role->save();
}
示例12: testSerializationClassIsOptional
/**
* Tests that serialization_class is optional.
*/
public function testSerializationClassIsOptional()
{
$this->enableService('serialization_test', 'POST', 'json');
Role::load(RoleInterface::ANONYMOUS_ID)->grantPermission('restful post serialization_test')->save();
$serialized = $this->container->get('serializer')->serialize(['foo', 'bar'], 'json');
$this->httpRequest('serialization_test', 'POST', $serialized, 'application/json');
$this->assertResponse(200);
$this->assertResponseBody('["foo","bar"]');
}
示例13: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
// Give anonymous users permission to view user profiles, so that we can
// verify the cache tags of cached versions of user profile pages.
$user_role = Role::load(RoleInterface::ANONYMOUS_ID);
$user_role->grantPermission('access user profiles');
$user_role->save();
}
示例14: setUp
/**
* {@inheritdoc}
*/
public function setUp()
{
parent::setUp();
$this->addField('string', 'field_shared', 'user');
$this->addField('string', 'field_shared', 'simplenews_subscriber');
Role::load('anonymous')->grantPermission('subscribe to newsletters')->grantPermission('access user profiles')->save();
Role::load('authenticated')->grantPermission('subscribe to newsletters')->save();
$this->admin = $this->drupalCreateUser(array('administer users'));
}
示例15: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
$this->config = $this->config('rest.settings');
// Create an entity programmatically.
$this->entity = $this->entityCreate('entity_test');
$this->entity->save();
Role::load(AccountInterface::ANONYMOUS_ROLE)->grantPermission('view test entity')->save();
}