本文整理汇总了PHP中Drupal\user\UserInterface类的典型用法代码示例。如果您正苦于以下问题:PHP UserInterface类的具体用法?PHP UserInterface怎么用?PHP UserInterface使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了UserInterface类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: createForUser
/**
* Creates an order for the specified user, and redirects to the edit page.
*
* @param \Drupal\user\UserInterface $user
* The user to create the order for.
*/
public function createForUser(UserInterface $user)
{
$order = Order::create(['uid' => $user->id(), 'order_status' => uc_order_state_default('post_checkout')]);
$order->save();
uc_order_comment_save($order->id(), \Drupal::currentUser()->id(), $this->t('Order created by the administration.'), 'admin');
return $this->redirect('entity.uc_order.edit_form', ['uc_order' => $order->id()]);
}
示例2: addRoleDelegationElement
/**
* @param array $form
* @param \Drupal\user\UserInterface $user
* @return array
*/
public static function addRoleDelegationElement(array $form, UserInterface $user)
{
$current_user = \Drupal::currentUser();
$roles_current = $user->getRoles(TRUE);
$roles_delegate = array();
$roles = user_roles(TRUE);
unset($roles[AccountInterface::AUTHENTICATED_ROLE]);
unset($roles['administrator']);
foreach ($roles as $rid => $role) {
if ($current_user->hasPermission('assign all roles') || $current_user->hasPermission("assign {$role->get('id')} role")) {
$roles_delegate[$rid] = isset($form['account']['roles']['#options'][$rid]) ? $form['account']['roles']['#options'][$rid] : $role->get('id');
}
}
if (empty($roles_delegate)) {
// No role can be assigned.
return $form;
}
if (!isset($form['account'])) {
$form['account'] = array('#type' => 'value', '#value' => $user);
}
$default_options = array();
foreach ($roles_current as $role) {
if (in_array($role, $roles_delegate)) {
$default_options[$role] = $role;
}
}
// Generate the form items.
$form['account']['roles_change'] = array('#type' => 'checkboxes', '#title' => isset($form['account']['roles']['#title']) ? $form['account']['roles']['#title'] : t('Roles'), '#options' => $roles_delegate, '#default_value' => array_keys(array_intersect_key(array_flip($roles_current), $roles_delegate)), '#description' => isset($form['account']['roles']['#description']) ? $form['account']['roles']['#description'] : t('Change roles assigned to user.'));
return $form;
}
示例3: save
/**
* @inheritdoc
*/
public function save(UserInterface $account, $provider, $authname, $data = NULL)
{
if (!is_scalar($data)) {
$data = serialize($data);
}
$this->connection->merge('authmap')->keys(array('uid' => $account->id(), 'provider' => $provider))->fields(array('authname' => $authname, 'data' => $data))->execute();
}
示例4: userProfileForm
public function userProfileForm(RouteMatchInterface $route_match, UserInterface $user, ProfileTypeInterface $profile_type) {
$profile = $this->entityManager()->getStorage('profile')->create([
'uid' => $user->id(),
'type' => $profile_type->id(),
]);
return $this->entityFormBuilder()->getForm($profile, 'add', ['uid' => $user->id(), 'created' => REQUEST_TIME]);
}
示例5: setAuthor
/**
* {@inheritdoc}
*/
public function setAuthor(UserInterface $account)
{
if ($this->translation->hasField('content_translation_uid')) {
$this->translation->set('content_translation_uid', $account->id());
} else {
$this->translation->setOwner($account);
}
return $this;
}
示例6: doExecute
/**
* Unblock a user.
*
* @param \Drupal\user\UserInterface $user
* The user to unblock.
*/
protected function doExecute(UserInterface $user)
{
// Do nothing if user is anonymous or isn't blocked.
if ($user->isAuthenticated() && $user->isBlocked()) {
$user->activate();
// Set flag that indicates if the entity should be auto-saved later.
$this->saveLater = TRUE;
}
}
示例7: flickr_photos_access
public function flickr_photos_access(\Drupal\user\UserInterface $user, Drupal\Core\Session\AccountInterface $account)
{
$view_access = FALSE;
if (!empty($user) && $user->id()) {
if (isset($user->flickr['nsid'])) {
$view_access = \Drupal::currentUser()->hasPermission('administer flickr') || $user->status && (\Drupal::currentUser()->hasPermission('view all flickr photos') || \Drupal::currentUser()->hasPermission('view own flickr photos') && \Drupal::currentUser()->uid == $user->id());
}
}
return $view_access;
}
示例8: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
new Settings(array('hash_salt' => 'test'));
// The mocked super user account, with the same roles as Account 2.
$this->account1 = $this->getMockBuilder('Drupal\\user\\Entity\\User')->disableOriginalConstructor()->setMethods(array('getRoles', 'id'))->getMock();
$this->account1->expects($this->any())->method('id')->willReturn(1);
$this->account1->expects($this->never())->method('getRoles');
// Account 2: 'administrator' and 'authenticated' roles.
$roles_1 = array('administrator', 'authenticated');
$this->account2 = $this->getMockBuilder('Drupal\\user\\Entity\\User')->disableOriginalConstructor()->setMethods(array('getRoles', 'id'))->getMock();
$this->account2->expects($this->any())->method('getRoles')->will($this->returnValue($roles_1));
$this->account2->expects($this->any())->method('id')->willReturn(2);
// Account 3: 'authenticated' and 'administrator' roles (different order).
$roles_3 = array('authenticated', 'administrator');
$this->account3 = $this->getMockBuilder('Drupal\\user\\Entity\\User')->disableOriginalConstructor()->setMethods(array('getRoles', 'id'))->getMock();
$this->account3->expects($this->any())->method('getRoles')->will($this->returnValue($roles_3));
$this->account3->expects($this->any())->method('id')->willReturn(3);
// Updated account 2: now also 'editor' role.
$roles_2_updated = array('editor', 'administrator', 'authenticated');
$this->account2Updated = $this->getMockBuilder('Drupal\\user\\Entity\\User')->disableOriginalConstructor()->setMethods(array('getRoles', 'id'))->getMock();
$this->account2Updated->expects($this->any())->method('getRoles')->will($this->returnValue($roles_2_updated));
$this->account2Updated->expects($this->any())->method('id')->willReturn(2);
// Mocked private key + cache services.
$random = Crypt::randomBytesBase64(55);
$this->privateKey = $this->getMockBuilder('Drupal\\Core\\PrivateKey')->disableOriginalConstructor()->setMethods(array('get'))->getMock();
$this->privateKey->expects($this->any())->method('get')->will($this->returnValue($random));
$this->cache = $this->getMockBuilder('Drupal\\Core\\Cache\\CacheBackendInterface')->disableOriginalConstructor()->getMock();
$this->staticCache = $this->getMockBuilder('Drupal\\Core\\Cache\\CacheBackendInterface')->disableOriginalConstructor()->getMock();
$this->permissionsHash = new PermissionsHashGenerator($this->privateKey, $this->cache, $this->staticCache);
}
示例9: testSubscriberFormFieldSync
/**
* Tests that fields are synchronized using the Subscriber form.
*/
public function testSubscriberFormFieldSync()
{
// Create a subscriber for the user.
$subscriber = Subscriber::create(array('uid' => $this->user->id(), 'mail' => 'anything@example.com'));
$subscriber->save();
// Edit subscriber field and assert user field is changed accordingly.
$this->drupalLogin($this->user);
$this->drupalGet('admin/people/simplenews/edit/' . $subscriber->id());
$this->assertField('field_shared[0][value]');
$this->assertRaw($this->user->field_shared->value);
$new_value = $this->randomMachineName();
$this->drupalPostForm(NULL, array('field_shared[0][value]' => $new_value), t('Save'));
$this->drupalGet('admin/people/simplenews/edit/' . $subscriber->id());
$this->assertRaw($new_value);
$this->user = User::load($this->user->id());
$this->assertEqual($this->user->field_shared->value, $new_value);
// Unset the sync setting and assert field is not synced.
$this->drupalPostForm('admin/config/people/simplenews/settings/subscriber', array('simplenews_sync_fields' => FALSE), t('Save configuration'));
$unsynced_value = $this->randomMachineName();
$this->drupalPostForm('admin/people/simplenews/edit/' . $subscriber->id(), array('field_shared[0][value]' => $unsynced_value), t('Save'));
$this->drupalGet('admin/people/simplenews/edit/' . $subscriber->id());
$this->assertRaw($unsynced_value);
$this->user = User::load($this->user->id());
$this->assertEqual($this->user->field_shared->value, $new_value);
$this->assertNotEqual($this->user->field_shared->value, $unsynced_value);
}
示例10: testProfileTabs
/**
* Tests tabs in profile UI.
*/
public function testProfileTabs()
{
$types_data = ['profile_type_0' => ['label' => $this->randomMachineName()], 'profile_type_1' => ['label' => $this->randomMachineName()]];
/** @var ProfileType[] $types */
$types = [];
foreach ($types_data as $id => $values) {
$types[$id] = ProfileType::create(['id' => $id] + $values);
$types[$id]->save();
}
$this->container->get('router.builder')->rebuild();
$this->user1 = User::create(['name' => $this->randomMachineName(), 'mail' => $this->randomMachineName() . '@example.com']);
$this->user1->save();
$this->user2 = User::create(['name' => $this->randomMachineName(), 'mail' => $this->randomMachineName() . '@example.com']);
$this->user2->save();
// Create new profiles.
$profile1 = Profile::create($expected = ['type' => $types['profile_type_0']->id(), 'uid' => $this->user1->id()]);
$profile1->save();
$profile2 = Profile::create($expected = ['type' => $types['profile_type_1']->id(), 'uid' => $this->user2->id()]);
$profile2->save();
$this->drupalLogin($this->adminUser);
$this->drupalGet('admin/config');
$this->clickLink('User profiles');
$this->assertResponse(200);
$this->assertUrl('admin/config/people/profiles');
$this->assertLink($profile1->label());
$this->assertLinkByHref($profile2->toUrl('canonical')->toString());
$tasks = [['entity.profile.collection', []], ['entity.profile_type.collection', []]];
$this->assertLocalTasks($tasks, 0);
}
示例11: testCommentNodeCommentStatistics
/**
* Tests the node comment statistics.
*/
function testCommentNodeCommentStatistics()
{
$node_storage = $this->container->get('entity.manager')->getStorage('node');
// Set comments to have subject and preview disabled.
$this->drupalLogin($this->adminUser);
$this->setCommentPreview(DRUPAL_DISABLED);
$this->setCommentForm(TRUE);
$this->setCommentSubject(FALSE);
$this->setCommentSettings('default_mode', CommentManagerInterface::COMMENT_MODE_THREADED, 'Comment paging changed.');
$this->drupalLogout();
// Checks the initial values of node comment statistics with no comment.
$node = $node_storage->load($this->node->id());
$this->assertEqual($node->get('comment')->last_comment_timestamp, $this->node->getCreatedTime(), 'The initial value of node last_comment_timestamp is the node created date.');
$this->assertEqual($node->get('comment')->last_comment_name, NULL, 'The initial value of node last_comment_name is NULL.');
$this->assertEqual($node->get('comment')->last_comment_uid, $this->webUser->id(), 'The initial value of node last_comment_uid is the node uid.');
$this->assertEqual($node->get('comment')->comment_count, 0, 'The initial value of node comment_count is zero.');
// Post comment #1 as web_user2.
$this->drupalLogin($this->webUser2);
$comment_text = $this->randomMachineName();
$this->postComment($this->node, $comment_text);
// Checks the new values of node comment statistics with comment #1.
// The node cache needs to be reset before reload.
$node_storage->resetCache(array($this->node->id()));
$node = $node_storage->load($this->node->id());
$this->assertEqual($node->get('comment')->last_comment_name, NULL, 'The value of node last_comment_name is NULL.');
$this->assertEqual($node->get('comment')->last_comment_uid, $this->webUser2->id(), 'The value of node last_comment_uid is the comment #1 uid.');
$this->assertEqual($node->get('comment')->comment_count, 1, 'The value of node comment_count is 1.');
// Prepare for anonymous comment submission (comment approval enabled).
$this->drupalLogin($this->adminUser);
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, array('access comments' => TRUE, 'post comments' => TRUE, 'skip comment approval' => FALSE));
// Ensure that the poster can leave some contact info.
$this->setCommentAnonymous('1');
$this->drupalLogout();
// Post comment #2 as anonymous (comment approval enabled).
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
$anonymous_comment = $this->postComment($this->node, $this->randomMachineName(), '', TRUE);
// Checks the new values of node comment statistics with comment #2 and
// ensure they haven't changed since the comment has not been moderated.
// The node needs to be reloaded with the cache reset.
$node_storage->resetCache(array($this->node->id()));
$node = $node_storage->load($this->node->id());
$this->assertEqual($node->get('comment')->last_comment_name, NULL, 'The value of node last_comment_name is still NULL.');
$this->assertEqual($node->get('comment')->last_comment_uid, $this->webUser2->id(), 'The value of node last_comment_uid is still the comment #1 uid.');
$this->assertEqual($node->get('comment')->comment_count, 1, 'The value of node comment_count is still 1.');
// Prepare for anonymous comment submission (no approval required).
$this->drupalLogin($this->adminUser);
user_role_change_permissions(RoleInterface::ANONYMOUS_ID, array('access comments' => TRUE, 'post comments' => TRUE, 'skip comment approval' => TRUE));
$this->drupalLogout();
// Post comment #3 as anonymous.
$this->drupalGet('comment/reply/node/' . $this->node->id() . '/comment');
$anonymous_comment = $this->postComment($this->node, $this->randomMachineName(), '', array('name' => $this->randomMachineName()));
$comment_loaded = Comment::load($anonymous_comment->id());
// Checks the new values of node comment statistics with comment #3.
// The node needs to be reloaded with the cache reset.
$node_storage->resetCache(array($this->node->id()));
$node = $node_storage->load($this->node->id());
$this->assertEqual($node->get('comment')->last_comment_name, $comment_loaded->getAuthorName(), 'The value of node last_comment_name is the name of the anonymous user.');
$this->assertEqual($node->get('comment')->last_comment_uid, 0, 'The value of node last_comment_uid is zero.');
$this->assertEqual($node->get('comment')->comment_count, 2, 'The value of node comment_count is 2.');
}
示例12: testWhosOnlineBlock
/**
* Test the Who's Online block.
*/
function testWhosOnlineBlock()
{
$block = $this->drupalPlaceBlock('views_block:who_s_online-who_s_online_block');
// Generate users.
$user1 = $this->drupalCreateUser(array('access user profiles'));
$user2 = $this->drupalCreateUser(array());
$user3 = $this->drupalCreateUser(array());
// Update access of two users to be within the active timespan.
$this->updateAccess($user1->id());
$this->updateAccess($user2->id(), REQUEST_TIME + 1);
// Insert an inactive user who should not be seen in the block, and ensure
// that the admin user used in setUp() does not appear.
$inactive_time = REQUEST_TIME - 15 * 60 - 1;
$this->updateAccess($user3->id(), $inactive_time);
$this->updateAccess($this->adminUser->id(), $inactive_time);
// Test block output.
\Drupal::currentUser()->setAccount($user1);
$content = entity_view($block, 'block');
$this->drupalSetContent(render($content));
$this->assertRaw(t('2 users'), 'Correct number of online users (2 users).');
$this->assertText($user1->getUsername(), 'Active user 1 found in online list.');
$this->assertText($user2->getUsername(), 'Active user 2 found in online list.');
$this->assertNoText($user3->getUsername(), 'Inactive user not found in online list.');
$this->assertTrue(strpos($this->drupalGetContent(), $user1->getUsername()) > strpos($this->drupalGetContent(), $user2->getUsername()), 'Online users are ordered correctly.');
}
示例13: testUsername
/**
* Test the username formatter.
*/
public function testUsername()
{
$view_id = $this->randomMachineName();
$view = View::create(['id' => $view_id, 'base_table' => 'comment_field_data', 'display' => ['default' => ['display_plugin' => 'default', 'id' => 'default', 'display_options' => ['fields' => ['name' => ['table' => 'comment_field_data', 'field' => 'name', 'id' => 'name', 'plugin_id' => 'field', 'type' => 'comment_username'], 'subject' => ['table' => 'comment_field_data', 'field' => 'subject', 'id' => 'subject', 'plugin_id' => 'field', 'type' => 'string', 'settings' => ['link_to_entity' => TRUE]]]]]]]);
$view->save();
/* @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */
$account_switcher = \Drupal::service('account_switcher');
/* @var \Drupal\Core\Render\RendererInterface $renderer */
$renderer = \Drupal::service('renderer');
$account_switcher->switchTo($this->adminUser);
$executable = Views::getView($view_id);
$build = $executable->preview();
$this->setRawContent($renderer->renderRoot($build));
$this->verbose($this->getRawContent());
$this->assertLink('My comment title');
$this->assertLink('Anonymous comment title');
$this->assertLink($this->adminUser->label());
$this->assertLink('barry (not verified)');
$account_switcher->switchTo(new AnonymousUserSession());
$executable = Views::getView($view_id);
$executable->storage->invalidateCaches();
$build = $executable->preview();
$this->setRawContent($renderer->renderRoot($build));
// No access to user-profiles, so shouldn't be able to see links.
$this->assertNoLink($this->adminUser->label());
// Note: External users aren't pointing to drupal user profiles.
$this->assertLink('barry (not verified)');
$this->verbose($this->getRawContent());
$this->assertLink('My comment title');
$this->assertLink('Anonymous comment title');
}
示例14: 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());
}
示例15: testLabelCallback
/**
* Test label callback.
*/
function testLabelCallback()
{
$this->assertEqual($this->account->label(), $this->account->getUsername(), 'The username should be used as label');
// Setup a random anonymous name to be sure the name is used.
$name = $this->randomMachineName();
$this->config('user.settings')->set('anonymous', $name)->save();
$this->assertEqual($this->anonymous->label(), $name, 'The variable anonymous should be used for name of uid 0');
}