當前位置: 首頁>>代碼示例>>PHP>>正文


PHP Role::load方法代碼示例

本文整理匯總了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']);
 }
開發者ID:badelas,項目名稱:afroweb,代碼行數:10,代碼來源:AjaxCommentsSettingsFormTest.php

示例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');
 }
開發者ID:ddrozdik,項目名稱:dmaps,代碼行數:32,代碼來源:CacheContextOptimizationTest.php

示例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();
 }
開發者ID:aWEBoLabs,項目名稱:taxi,代碼行數:28,代碼來源:CommentUserNameTest.php

示例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();
 }
開發者ID:justincletus,項目名稱:webdrupalpro,代碼行數:25,代碼來源:RoleTest.php

示例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;
     }
 }
開發者ID:AohRveTPV,項目名稱:security_review,代碼行數:38,代碼來源:Security.php

示例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);
 }
開發者ID:ddrozdik,項目名稱:dmaps,代碼行數:29,代碼來源:EntityCacheTagsTestBase.php

示例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();
 }
開發者ID:318io,項目名稱:318-io,代碼行數:10,代碼來源:TourCacheTagsTest.php

示例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)));
 }
開發者ID:ddrozdik,項目名稱:dmaps,代碼行數:32,代碼來源:MigrateUserRoleTest.php

示例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]));
 }
開發者ID:Greg-Boggs,項目名稱:electric-dev,代碼行數:19,代碼來源:MigrateUserRoleTest.php

示例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();
 }
開發者ID:aWEBoLabs,項目名稱:taxi,代碼行數:10,代碼來源:EntityViewBuilderTest.php

示例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();
 }
開發者ID:davidsoloman,項目名稱:drupalconsole.com,代碼行數:12,代碼來源:FeedCacheTagsTest.php

示例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"]');
 }
開發者ID:eigentor,項目名稱:tommiblog,代碼行數:12,代碼來源:ResourceTest.php

示例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();
 }
開發者ID:sojo,項目名稱:d8_friendsofsilence,代碼行數:12,代碼來源:UserCacheTagsTest.php

示例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'));
 }
開發者ID:aritnath1990,項目名稱:simplenewslatest,代碼行數:12,代碼來源:SimplenewsPersonalizationFormsTest.php

示例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();
 }
開發者ID:Wylbur,項目名稱:gj,代碼行數:12,代碼來源:ResourceTest.php


注:本文中的Drupal\user\Entity\Role::load方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。