当前位置: 首页>>代码示例>>PHP>>正文


PHP UserInterface::addRole方法代码示例

本文整理汇总了PHP中Drupal\user\UserInterface::addRole方法的典型用法代码示例。如果您正苦于以下问题:PHP UserInterface::addRole方法的具体用法?PHP UserInterface::addRole怎么用?PHP UserInterface::addRole使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Drupal\user\UserInterface的用法示例。


在下文中一共展示了UserInterface::addRole方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: 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());
 }
开发者ID:aWEBoLabs,项目名称:taxi,代码行数:29,代码来源:UserRoleConditionTest.php

示例2: setUp

 protected function setUp()
 {
     parent::setUp();
     $this->enableViewsTestModule();
     ViewTestData::createTestViews(get_class($this), array('views_test_data'));
     $this->webUser = $this->drupalCreateUser();
     $normal_role = $this->drupalCreateRole(array());
     $this->normalUser = $this->drupalCreateUser(array('views_test_data test permission'));
     $this->normalUser->addRole($normal_role);
     // @todo when all the plugin information is cached make a reset function and
     // call it here.
 }
开发者ID:ddrozdik,项目名称:dmaps,代码行数:12,代码来源:AccessTest.php

示例3: setUp

 /**
  * {@inheritdoc}
  */
 protected function setUp()
 {
     parent::setUp();
     $this->drupalPlaceBlock('system_breadcrumb_block');
     $this->enableViewsTestModule();
     $this->webUser = $this->drupalCreateUser();
     $roles = $this->webUser->getRoles();
     $this->webRole = $roles[0];
     $this->normalRole = $this->drupalCreateRole(array());
     $this->normalUser = $this->drupalCreateUser(array('views_test_data test permission'));
     $this->normalUser->addRole($this->normalRole);
     $this->normalUser->save();
     // @todo when all the plugin information is cached make a reset function and
     // call it here.
 }
开发者ID:318io,项目名称:318-io,代码行数:18,代码来源:AccessTestBase.php

示例4: setUp

 /**
  * Sets up the test.
  */
 protected function setUp()
 {
     parent::setUp();
     $this->installSchema('system', ['sequences', 'key_value_expire']);
     $this->installEntitySchema('user');
     \Drupal::service('router.builder')->rebuild();
     /** @var \Drupal\user\RoleInterface $role */
     $role = Role::create(array('id' => 'admin', 'label' => 'admin'));
     $role->grantPermission('link to any page');
     $role->save();
     $this->testUser = User::create(array('name' => 'foobar', 'mail' => 'foobar@example.com'));
     $this->testUser->addRole($role->id());
     $this->testUser->save();
     \Drupal::service('current_user')->setAccount($this->testUser);
 }
开发者ID:sojo,项目名称:d8_friendsofsilence,代码行数:18,代码来源:PathElementFormTest.php

示例5: setUp

 protected function setUp($import_test_views = TRUE)
 {
     parent::setUp($import_test_views);
     ViewTestData::createTestViews(get_class($this), ['comment_test_views']);
     $this->installEntitySchema('user');
     $this->installEntitySchema('comment');
     $this->installConfig(['user']);
     $entity_manager = $this->container->get('entity.manager');
     $this->commentStorage = $entity_manager->getStorage('comment');
     $this->userStorage = $entity_manager->getStorage('user');
     // Insert a row for the anonymous user.
     $this->userStorage->create(['uid' => 0, 'name' => '', 'status' => 0])->save();
     $admin_role = Role::create(['id' => 'admin']);
     $admin_role->grantPermission('administer comments');
     $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 = $this->userStorage->create(['name' => $this->randomMachineName()]);
     $this->adminUser->addRole('admin');
     $this->adminUser->save();
 }
开发者ID:neetumorwani,项目名称:blogging,代码行数:23,代码来源:CommentViewKernelTestBase.php

示例6: doExecute

 /**
  * Assign role to a user.
  *
  * @param \Drupal\user\UserInterface $account
  *   User object.
  * @param \Drupal\user\RoleInterface[] $roles
  *   Array of UserRoles to assign.
  */
 protected function doExecute(UserInterface $account, array $roles)
 {
     foreach ($roles as $role) {
         // Skip adding the role to the user if they already have it.
         if (!$account->hasRole($role->id())) {
             // If you try to add anonymous or authenticated role to user, Drupal
             // will throw an \InvalidArgumentException. Anonymous or authenticated
             // role ID must not be assigned manually.
             $account->addRole($role->id());
             // Set flag that indicates if the entity should be auto-saved later.
             $this->saveLater = TRUE;
         }
     }
 }
开发者ID:Progressable,项目名称:openway8,代码行数:22,代码来源:UserRoleAdd.php

示例7: setUp

 /**
  * Sets up the test.
  */
 protected function setUp()
 {
     parent::setUp();
     $this->installEntitySchema('user');
     $this->installSchema('system', ['sequences']);
     $this->installConfig(['filter', 'filter_test']);
     // Filter tips link to the full-page.
     \Drupal::service('router.builder')->rebuild();
     /* @var \Drupal\Core\Render\ElementInfoManager $manager */
     $manager = \Drupal::service('plugin.manager.element_info');
     $manager->clearCachedDefinitions();
     $manager->getDefinitions();
     /* @var \Drupal\filter\FilterFormatInterface $filter_test_format */
     $filter_test_format = FilterFormat::load('filter_test');
     /* @var \Drupal\user\RoleInterface $role */
     $role = Role::create(['id' => 'admin', 'label' => 'admin']);
     $role->grantPermission($filter_test_format->getPermissionName());
     $role->save();
     $this->testUser = User::create(['name' => 'foobar', 'mail' => 'foobar@example.com']);
     $this->testUser->addRole($role->id());
     $this->testUser->save();
     \Drupal::service('current_user')->setAccount($this->testUser);
 }
开发者ID:aWEBoLabs,项目名称:taxi,代码行数:26,代码来源:TextFormatElementFormTest.php

示例8: setUpFixtures

 /**
  * {@inheritdoc}
  */
 protected function setUpFixtures()
 {
     $this->installEntitySchema('user');
     $this->installEntitySchema('entity_test');
     $this->installConfig(['entity_test']);
     EntityViewMode::create(['id' => 'entity_test.foobar', 'targetEntityType' => 'entity_test', 'status' => TRUE, 'enabled' => TRUE, 'label' => 'My view mode'])->save();
     $display = EntityViewDisplay::create(['targetEntityType' => 'entity_test', 'bundle' => 'entity_test', 'mode' => 'foobar', 'label' => 'My view mode', 'status' => TRUE]);
     $display->save();
     $field_storage = FieldStorageConfig::create(['field_name' => 'test_field', 'entity_type' => 'entity_test', 'type' => 'string']);
     $field_storage->save();
     $field_config = FieldConfig::create(['field_name' => 'test_field', 'entity_type' => 'entity_test', 'bundle' => 'entity_test']);
     $field_config->save();
     // Create some test entities.
     for ($i = 1; $i <= 3; $i++) {
         EntityTest::create(['name' => "Article title {$i}", 'test_field' => "Test {$i}"])->save();
     }
     $role = Role::create(['id' => 'test_role']);
     $role->grantPermission('bypass node access');
     $role->save();
     $this->user = User::create(['name' => 'test user']);
     $this->user->addRole($role->id());
     $this->user->save();
     parent::setUpFixtures();
 }
开发者ID:darrylri,项目名称:protovbmwmo,代码行数:27,代码来源:RenderedEntityTest.php

示例9: addRole

 /**
  * {@inheritdoc}
  */
 public function addRole($rid)
 {
     $this->subject->addRole($rid);
 }
开发者ID:mosswoodcreative,项目名称:d8-api-test,代码行数:7,代码来源:TokenAuthUser.php

示例10: testStandard

 /**
  * Tests Standard installation profile.
  */
 function testStandard()
 {
     $this->drupalGet('');
     $this->assertLink(t('Contact'));
     $this->clickLink(t('Contact'));
     $this->assertResponse(200);
     // Test anonymous user can access 'Main navigation' block.
     $this->adminUser = $this->drupalCreateUser(array('administer blocks', 'post comments', 'skip comment approval', 'create article content', 'create page content'));
     $this->drupalLogin($this->adminUser);
     // Configure the block.
     $this->drupalGet('admin/structure/block/add/system_menu_block:main/bartik');
     $this->drupalPostForm(NULL, array('region' => 'sidebar_first', 'id' => 'main_navigation'), t('Save block'));
     // Verify admin user can see the block.
     $this->drupalGet('');
     $this->assertText('Main navigation');
     // Verify we have role = aria on system_powered_by and help_block
     // blocks.
     $this->drupalGet('admin/structure/block');
     $elements = $this->xpath('//div[@role=:role and @id=:id]', array(':role' => 'complementary', ':id' => 'block-bartik-help'));
     $this->assertEqual(count($elements), 1, 'Found complementary role on help block.');
     $this->drupalGet('');
     $elements = $this->xpath('//div[@role=:role and @id=:id]', array(':role' => 'complementary', ':id' => 'block-bartik-powered'));
     $this->assertEqual(count($elements), 1, 'Found complementary role on powered by block.');
     // Verify anonymous user can see the block.
     $this->drupalLogout();
     $this->assertText('Main navigation');
     // Ensure comments don't show in the front page RSS feed.
     // Create an article.
     $this->drupalCreateNode(array('type' => 'article', 'title' => 'Foobar', 'promote' => 1, 'status' => 1, 'body' => array(array('value' => 'Then she picked out two somebodies,<br />Sally and me', 'format' => 'basic_html'))));
     // Add a comment.
     $this->drupalLogin($this->adminUser);
     $this->drupalGet('node/1');
     $this->assertRaw('Then she picked out two somebodies,<br />Sally and me', 'Found a line break.');
     $this->drupalPostForm(NULL, array('subject[0][value]' => 'Barfoo', 'comment_body[0][value]' => 'Then she picked out two somebodies, Sally and me'), t('Save'));
     // Fetch the feed.
     $this->drupalGet('rss.xml');
     $this->assertText('Foobar');
     $this->assertNoText('Then she picked out two somebodies, Sally and me');
     // Ensure block body exists.
     $this->drupalGet('block/add');
     $this->assertFieldByName('body[0][value]');
     // Now we have all configuration imported, test all of them for schema
     // conformance. Ensures all imported default configuration is valid when
     // standard profile modules are enabled.
     $names = $this->container->get('config.storage')->listAll();
     /** @var \Drupal\Core\Config\TypedConfigManagerInterface $typed_config */
     $typed_config = $this->container->get('config.typed');
     foreach ($names as $name) {
         $config = $this->config($name);
         $this->assertConfigSchema($typed_config, $name, $config->get());
     }
     // Ensure that configuration from the Standard profile is not reused when
     // enabling a module again since it contains configuration that can not be
     // installed. For example, editor.editor.basic_html is editor configuration
     // that depends on the ckeditor module. The ckeditor module can not be
     // installed before the editor module since it depends on the editor module.
     // The installer does not have this limitation since it ensures that all of
     // the install profiles dependencies are installed before creating the
     // editor configuration.
     foreach (FilterFormat::loadMultiple() as $filter) {
         // Ensure that editor can be uninstalled by removing use in filter
         // formats. It is necessary to prime the filter collection before removing
         // the filter.
         $filter->filters();
         $filter->removeFilter('editor_file_reference');
         $filter->save();
     }
     \Drupal::service('module_installer')->uninstall(array('editor', 'ckeditor'));
     $this->rebuildContainer();
     \Drupal::service('module_installer')->install(array('editor'));
     /** @var \Drupal\contact\ContactFormInterface $contact_form */
     $contact_form = ContactForm::load('feedback');
     $recipients = $contact_form->getRecipients();
     $this->assertEqual(['simpletest@example.com'], $recipients);
     $role = Role::create(['id' => 'admin_theme', 'label' => 'Admin theme']);
     $role->grantPermission('view the administration theme');
     $role->save();
     $this->adminUser->addRole($role->id());
     $this->adminUser->save();
     $this->drupalGet('node/add');
     $this->assertResponse(200);
     // Ensure that there are no pending updates after installation.
     $this->drupalLogin($this->rootUser);
     $this->drupalGet('update.php/selection');
     $this->assertText('No pending updates.');
     // Ensure that there are no pending entity updates after installation.
     $this->assertFalse($this->container->get('entity.definition_update_manager')->needsUpdates(), 'After installation, entity schema is up to date.');
     // Make sure the optional image styles are not installed.
     $this->drupalGet('admin/config/media/image-styles');
     $this->assertNoText('Max 325x325');
     $this->assertNoText('Max 650x650');
     $this->assertNoText('Max 1300x1300');
     $this->assertNoText('Max 2600x2600');
     // Make sure the optional image styles are installed after enabling
     // the responsive_image module.
     \Drupal::service('module_installer')->install(array('responsive_image'));
     $this->rebuildContainer();
//.........这里部分代码省略.........
开发者ID:ddrozdik,项目名称:dmaps,代码行数:101,代码来源:StandardTest.php

示例11: testStandard

 /**
  * Tests Standard installation profile.
  */
 function testStandard()
 {
     $this->drupalGet('');
     $this->assertLink(t('Contact'));
     $this->clickLink(t('Contact'));
     $this->assertResponse(200);
     // Test anonymous user can access 'Main navigation' block.
     $this->adminUser = $this->drupalCreateUser(array('administer blocks', 'post comments', 'skip comment approval', 'create article content', 'create page content'));
     $this->drupalLogin($this->adminUser);
     // Configure the block.
     $this->drupalGet('admin/structure/block/add/system_menu_block:main/bartik');
     $this->drupalPostForm(NULL, array('region' => 'sidebar_first', 'id' => 'main_navigation'), t('Save block'));
     // Verify admin user can see the block.
     $this->drupalGet('');
     $this->assertText('Main navigation');
     // Verify we have role = aria on system_powered_by and help_block
     // blocks.
     $this->drupalGet('admin/structure/block');
     $elements = $this->xpath('//div[@role=:role and @id=:id]', array(':role' => 'complementary', ':id' => 'block-bartik-help'));
     $this->assertEqual(count($elements), 1, 'Found complementary role on help block.');
     $this->drupalGet('');
     $elements = $this->xpath('//div[@role=:role and @id=:id]', array(':role' => 'complementary', ':id' => 'block-bartik-powered'));
     $this->assertEqual(count($elements), 1, 'Found complementary role on powered by block.');
     // Verify anonymous user can see the block.
     $this->drupalLogout();
     $this->assertText('Main navigation');
     // Ensure comments don't show in the front page RSS feed.
     // Create an article.
     $this->drupalCreateNode(array('type' => 'article', 'title' => 'Foobar', 'promote' => 1, 'status' => 1, 'body' => array(array('value' => 'Then she picked out two somebodies,<br />Sally and me', 'format' => 'basic_html'))));
     // Add a comment.
     $this->drupalLogin($this->adminUser);
     $this->drupalGet('node/1');
     $this->assertRaw('Then she picked out two somebodies,<br />Sally and me', 'Found a line break.');
     $this->drupalPostForm(NULL, array('subject[0][value]' => 'Barfoo', 'comment_body[0][value]' => 'Then she picked out two somebodies, Sally and me'), t('Save'));
     // Fetch the feed.
     $this->drupalGet('rss.xml');
     $this->assertText('Foobar');
     $this->assertNoText('Then she picked out two somebodies, Sally and me');
     // Ensure block body exists.
     $this->drupalGet('block/add');
     $this->assertFieldByName('body[0][value]');
     // Now we have all configuration imported, test all of them for schema
     // conformance. Ensures all imported default configuration is valid when
     // standard profile modules are enabled.
     $names = $this->container->get('config.storage')->listAll();
     /** @var \Drupal\Core\Config\TypedConfigManagerInterface $typed_config */
     $typed_config = $this->container->get('config.typed');
     foreach ($names as $name) {
         $config = $this->config($name);
         $this->assertConfigSchema($typed_config, $name, $config->get());
     }
     // Ensure that configuration from the Standard profile is not reused when
     // enabling a module again since it contains configuration that can not be
     // installed. For example, editor.editor.basic_html is editor configuration
     // that depends on the ckeditor module. The ckeditor module can not be
     // installed before the editor module since it depends on the editor module.
     // The installer does not have this limitation since it ensures that all of
     // the install profiles dependencies are installed before creating the
     // editor configuration.
     \Drupal::service('module_installer')->uninstall(array('editor', 'ckeditor'));
     $this->rebuildContainer();
     \Drupal::service('module_installer')->install(array('editor'));
     /** @var \Drupal\contact\ContactFormInterface $contact_form */
     $contact_form = ContactForm::load('feedback');
     $recipients = $contact_form->getRecipients();
     $this->assertEqual(['simpletest@example.com'], $recipients);
     $role = Role::create(['id' => 'admin_theme', 'label' => 'Admin theme']);
     $role->grantPermission('view the administration theme');
     $role->save();
     $this->adminUser->addRole($role->id());
     $this->adminUser->save();
     $this->drupalGet('node/add');
     $this->assertResponse(200);
 }
开发者ID:nstielau,项目名称:drops-8,代码行数:77,代码来源:StandardTest.php


注:本文中的Drupal\user\UserInterface::addRole方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。