本文整理汇总了PHP中Drupal\user\Entity\User类的典型用法代码示例。如果您正苦于以下问题:PHP User类的具体用法?PHP User怎么用?PHP User使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了User类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: content
public function content()
{
$user = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
$userId = $user->get('uid')->value;
$data = array(array('job_title' => '', 'job_path' => '', 'company_name' => '', 'application_date' => ''));
$appliedJobs = db_select('user_job_application', 'uja')->condition('uja.user_id', $userId, '=')->fields('uja', array('job_id', 'date'))->orderBy('uja.date', 'DESC')->execute()->fetchAll();
if ($appliedJobs) {
$x = 0;
foreach ($appliedJobs as $appliedJob) {
$jobNode = \Drupal\node\Entity\Node::load($appliedJob->job_id);
$jobTitle = $jobNode->getTitle();
$jobPathAlias = \Drupal::service('path.alias_manager')->getAliasByPath('/node/' . $appliedJob->job_id);
$companyNodeEntity = $jobNode->get('field_company');
$companyNode = \Drupal\node\Entity\Node::load($companyNodeEntity->entity->id());
$companyName = $companyNode->getTitle();
$data[$x]['job_title'] = $jobTitle;
$data[$x]['job_path'] = $jobPathAlias;
$data[$x]['company_name'] = $companyName;
$data[$x]['application_date'] = $appliedJob->date;
$x++;
}
}
$markUp = $this->createMarkUp($data);
return array('#type' => 'markup', '#markup' => $markUp);
}
示例2: 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' => DRUPAL_ANONYMOUS_RID, 'label' => 'Anonymous user'))->save();
Role::create(array('id' => DRUPAL_AUTHENTICATED_RID, '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('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());
}
示例3: createUserProfile
/**
* Helper function, which creates node of type person
*
* @param \Drupal\user\Entity\User $user
* @param bool $isIndividualSponsor
*/
public function createUserProfile(User $user, $isIndividualSponsor = FALSE)
{
$values = ['type' => 'person', 'title' => $user->getUsername(), 'uid' => 1, 'field_referenced_user' => ['target_id' => $user->id()]];
if ($isIndividualSponsor) {
$values['field_person_type'] = ['target_id' => 8];
}
$node = entity_create('node', $values);
$node->save();
}
示例4: setUpFixtures
/**
* {@inheritdoc}
*/
protected function setUpFixtures()
{
$this->installEntitySchema('user');
$this->installConfig(array('user'));
parent::setUpFixtures();
// Create a record for uid 1.
$this->rootUser = User::create(['name' => $this->randomMachineName()]);
$this->rootUser->save();
Views::viewsData()->clear();
}
示例5: testBasicAuth
/**
* Tests that CSRF check is not triggered for Basic Auth requests.
*/
public function testBasicAuth()
{
$curl_options = $this->getCurlOptions();
$curl_options[CURLOPT_HTTPAUTH] = CURLAUTH_BASIC;
$curl_options[CURLOPT_USERPWD] = $this->account->getUsername() . ':' . $this->account->pass_raw;
$this->curlExec($curl_options);
$this->assertResponse(201);
// Ensure that the entity was created.
$loaded_entity = $this->loadEntityFromLocationHeader($this->drupalGetHeader('location'));
$this->assertTrue($loaded_entity, 'An entity was created in the database');
}
示例6: tokenList
/**
* Provide a list of tokens.
*/
public function tokenList(User $user)
{
$entity_type = 'access_token';
$storage = $this->entityManager()->getStorage($entity_type);
$ids = $storage->getQuery()->condition('auth_user_id', $user->id())->execute();
if (empty($ids)) {
return ['#markup' => $this->t('There are no tokens for this user.')];
}
$view_controller = $this->entityManager()->getViewBuilder($entity_type);
$tokens = $storage->loadMultiple($ids);
return $view_controller->viewMultiple($tokens);
}
示例7: syncSmfPassword
/**
* Sets new hash password in smf
*
* @param \Drupal\User\Entity\User $drupalUser
* @param \stdClass $smfMember
*/
protected function syncSmfPassword(\Drupal\User\Entity\User $drupalUser, \stdClass $smfMember)
{
/**
* @var \Drupal\Core\Database\Connection $this ->smfConnection
*/
$hashedPasswd = sha1($drupalUser->getPassword());
$id_member = $this->smfConnection->select('members', 'm')->fields('m', ['id_member'])->condition('m.member_name', $drupalUser->getUsername())->condition('m.passwd', $hashedPasswd)->execute()->fetchField();
if (!$id_member) {
$updateResult = $this->smfConnection->update('members')->fields(['passwd' => $hashedPasswd, 'password_salt' => ''])->condition('member_name', $drupalUser->getUsername())->execute();
if ($updateResult) {
$smfMember->passwd = $hashedPasswd;
}
}
}
示例8: createTestEntity
/**
* Creates a test entity.
*
* @param string $entity_type
* An entity type.
*
* @return \Drupal\Core\Entity\EntityInterface
* The created test entity.
*/
protected function createTestEntity($entity_type)
{
$this->entity_name = $this->randomMachineName();
$this->entity_user = $this->createUser();
$this->entity_field_text = $this->randomMachineName();
// Pass in the value of the name field when creating. With the user
// field we test setting a field after creation.
$entity = entity_create($entity_type);
$entity->user_id->target_id = $this->entity_user->id();
$entity->name->value = $this->entity_name;
// Set a value for the test field.
$entity->field_test_text->value = $this->entity_field_text;
return $entity;
}
示例9: 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();
}
示例10: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
$config = $this->config('system.theme');
$config->set('default', 'bartik');
$config->set('admin', 'seven');
$config->save();
foreach (static::$modules as $module) {
$function = $module . '_schema';
module_load_install($module);
if (function_exists($function)) {
$schema = $function();
$this->installSchema($module, array_keys($schema));
}
}
$this->installEntitySchema('aggregator_feed');
$this->installEntitySchema('aggregator_item');
$this->installEntitySchema('block_content');
$this->installEntitySchema('comment');
$this->installEntitySchema('file');
$this->installEntitySchema('node');
$this->installEntitySchema('menu_link_content');
$this->installEntitySchema('taxonomy_term');
$this->installEntitySchema('user');
$this->installConfig(['block_content', 'comment', 'file', 'node', 'simpletest']);
// Install one of D8's test themes.
\Drupal::service('theme_handler')->install(array('test_theme'));
// Create a new user which needs to have UID 1, because that is expected by
// the assertions from
// \Drupal\migrate_drupal\Tests\d6\MigrateNodeRevisionTest.
User::create(['uid' => 1, 'name' => $this->randomMachineName(), 'status' => 1])->enforceIsNew(TRUE)->save();
$this->installMigrations('Drupal 6');
}
示例11: checkAccess
/**
* {@inheritdoc}
*
* When the $operation is 'add' then the $entity is of type 'profile_type',
* otherwise $entity is of type 'profile'.
*/
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account)
{
$account = $this->prepareUser($account);
$user_page = \Drupal::request()->attributes->get('user');
// Some times, operation edit is called update.
// Use edit in any case.
if ($operation == 'update') {
$operation = 'edit';
}
// Check that if profile type has require roles, the user the profile is
// being added to has any of the required roles.
if ($entity->getEntityTypeId() == 'profile') {
$profile_roles = ProfileType::load($entity->bundle())->getRoles();
$user_roles = $entity->getOwner()->getRoles(TRUE);
if (!empty(array_filter($profile_roles)) && !array_intersect($user_roles, $profile_roles)) {
return AccessResult::forbidden();
}
} elseif ($entity->getEntityTypeId() == 'profile_type') {
$profile_roles = $entity->getRoles();
$user_roles = User::load($user_page->id())->getRoles(TRUE);
if (!empty(array_filter($profile_roles)) && !array_intersect($user_roles, $profile_roles)) {
return AccessResult::forbidden();
}
}
if ($account->hasPermission('bypass profile access')) {
return AccessResult::allowed()->cachePerPermissions();
} elseif ($operation == 'add' && ($user_page->id() == $account->id() && $account->hasPermission($operation . ' own ' . $entity->id() . ' profile') || $account->hasPermission($operation . ' any ' . $entity->id() . ' profile')) || $operation != 'add' && ($entity->getOwnerId() == $account->id() && $account->hasPermission($operation . ' own ' . $entity->getType() . ' profile') || $account->hasPermission($operation . ' any ' . $entity->getType() . ' profile'))) {
return AccessResult::allowed()->cachePerPermissions();
} else {
return AccessResult::forbidden()->cachePerPermissions();
}
}
示例12: testShortcutSetUsersMigration
/**
* Test the shortcut set migration.
*/
public function testShortcutSetUsersMigration() {
// Check if migrated user has correct migrated shortcut set assigned.
$account = User::load(2);
$shortcut_set = shortcut_current_displayed_set($account);
/** @var \Drupal\shortcut\ShortcutSetInterface $shortcut_set */
$this->assertIdentical('shortcut_set_2', $shortcut_set->id());
}
示例13: execute
/**
* {@inheritdoc}
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new DrupalStyle($input, $output);
$uid = $input->getArgument('uid');
$account = User::load($uid);
if (!$account) {
// Error loading User entity.
$io->error(sprintf($this->trans('commands.user.login.clear.attempts.errors.invalid-user'), $uid));
return 1;
}
// Define event name and identifier.
$event = 'user.failed_login_user';
// Identifier is created by uid and IP address,
// Then we defined a generic identifier.
$identifier = "{$account->id()}-";
// Retrieve current database connection.
$database = $this->getDrupalService('database');
$schema = $database->schema();
$flood = $schema->findTables('flood');
if (!$flood) {
$io->error($this->trans('commands.user.login.clear.attempts.errors.no-flood'));
return 1;
}
// Clear login attempts.
$database->delete('flood')->condition('event', $event)->condition('identifier', $database->escapeLike($identifier) . '%', 'LIKE')->execute();
// Command executed successful.
$io->success(sprintf($this->trans('commands.user.login.clear.attempts.messages.successful'), $uid));
}
示例14: testRoleDeleteUserRoleReferenceDelete
/**
* Tests removal of role references on role entity delete.
*
* @see user_user_role_delete()
*/
public function testRoleDeleteUserRoleReferenceDelete()
{
// Create two test roles.
$role_storage = $this->container->get('entity.manager')->getStorage('user_role');
$role_storage->create(array('id' => 'test_role_one'))->save();
$role_storage->create(array('id' => 'test_role_two'))->save();
// Create user and assign both test roles.
$values = array('uid' => 1, 'name' => $this->randomString(), 'roles' => array('test_role_one', 'test_role_two'));
$user = User::create($values);
$user->save();
// Check that user has both roles.
$this->assertTrue($user->hasRole('test_role_one'));
$this->assertTrue($user->hasRole('test_role_two'));
// Delete test role one.
$test_role_one = $role_storage->load('test_role_one');
$test_role_one->delete();
// Load user again from the database.
$user = User::load($user->id());
// Check that user does not have role one anymore, still has role two.
$this->assertFalse($user->hasRole('test_role_one'));
$this->assertTrue($user->hasRole('test_role_two'));
// Create new role with same name.
$role_storage->create(array('id' => 'test_role_one'))->save();
// Load user again from the database.
$user = User::load($user->id());
// Check that user does not have role one.
$this->assertFalse($user->hasRole('test_role_one'));
$this->assertTrue($user->hasRole('test_role_two'));
}
示例15: setUp
/**
* {@inheritdoc}
*/
protected function setUp()
{
parent::setUp();
$this->installEntitySchema('node');
$this->installConfig(['node']);
$this->installSchema('node', ['node_access']);
$this->installSchema('system', ['sequences']);
// Create a new user which needs to have UID 1, because that is expected by
// the assertions from
// \Drupal\migrate_drupal\Tests\d6\MigrateNodeRevisionTest.
User::create(['uid' => 1, 'name' => $this->randomMachineName(), 'status' => 1])->enforceIsNew(TRUE)->save();
$node_type = entity_create('node_type', array('type' => 'test_planet'));
$node_type->save();
node_add_body_field($node_type);
$node_type = entity_create('node_type', array('type' => 'story'));
$node_type->save();
node_add_body_field($node_type);
$id_mappings = array('d6_node_type' => array(array(array('test_story'), array('story'))), 'd6_filter_format' => array(array(array(1), array('filtered_html')), array(array(2), array('full_html'))), 'd6_user' => array(array(array(1), array(1)), array(array(2), array(2))), 'd6_field_instance_widget_settings' => array(array(array('page', 'field_test'), array('node', 'page', 'default', 'test'))), 'd6_field_formatter_settings' => array(array(array('page', 'default', 'node', 'field_test'), array('node', 'page', 'default', 'field_test'))));
$this->prepareMigrations($id_mappings);
$migration = entity_load('migration', 'd6_node_settings');
$migration->setMigrationResult(MigrationInterface::RESULT_COMPLETED);
// Create a test node.
$node = entity_create('node', array('type' => 'story', 'nid' => 1, 'vid' => 1, 'revision_log' => '', 'title' => $this->randomString()));
$node->enforceIsNew();
$node->save();
$node = entity_create('node', array('type' => 'test_planet', 'nid' => 3, 'vid' => 4, 'revision_log' => '', 'title' => $this->randomString()));
$node->enforceIsNew();
$node->save();
}