本文整理汇总了PHP中Drupal\Core\Entity\EntityInterface::getRoles方法的典型用法代码示例。如果您正苦于以下问题:PHP EntityInterface::getRoles方法的具体用法?PHP EntityInterface::getRoles怎么用?PHP EntityInterface::getRoles使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Drupal\Core\Entity\EntityInterface
的用法示例。
在下文中一共展示了EntityInterface::getRoles方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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();
}
}
示例2: validateEntity
/**
* {@inheritdoc}
*/
protected function validateEntity(EntityInterface $entity)
{
/** @var \Drupal\user\UserInterface $entity */
$role_check_success = TRUE;
// See if we're filtering users based on roles.
if (!empty($this->options['restrict_roles']) && !empty($this->options['roles'])) {
$roles = $this->options['roles'];
if (!(bool) array_intersect($entity->getRoles(), $roles)) {
$role_check_success = FALSE;
}
}
return $role_check_success && parent::validateEntity($entity);
}
示例3: buildRow
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity)
{
$row['username']['data'] = array('#theme' => 'username', '#account' => $entity);
$row['status'] = $entity->isActive() ? $this->t('active') : $this->t('blocked');
$roles = array_map('\\Drupal\\Component\\Utility\\String::checkPlain', user_role_names(TRUE));
unset($roles[DRUPAL_AUTHENTICATED_RID]);
$users_roles = array();
foreach ($entity->getRoles() as $role) {
if (isset($roles[$role])) {
$users_roles[] = $roles[$role];
}
}
asort($users_roles);
$row['roles']['data'] = array('#theme' => 'item_list', '#items' => $users_roles);
$row['member_for'] = format_interval(REQUEST_TIME - $entity->getCreatedTime());
$row['access'] = $entity->access ? $this->t('@time ago', array('@time' => format_interval(REQUEST_TIME - $entity->getLastAccessedTime()))) : t('never');
return $row + parent::buildRow($entity);
}
示例4: buildRow
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity)
{
$row['username']['data'] = array('#theme' => 'username', '#account' => $entity);
$row['status'] = $entity->isActive() ? $this->t('active') : $this->t('blocked');
$roles = user_role_names(TRUE);
unset($roles[RoleInterface::AUTHENTICATED_ID]);
$users_roles = array();
foreach ($entity->getRoles() as $role) {
if (isset($roles[$role])) {
$users_roles[] = $roles[$role];
}
}
asort($users_roles);
$row['roles']['data'] = array('#theme' => 'item_list', '#items' => $users_roles);
$row['member_for'] = $this->dateFormatter->formatTimeDiffSince($entity->getCreatedTime());
$row['access'] = $entity->access ? $this->t('@time ago', array('@time' => $this->dateFormatter->formatTimeDiffSince($entity->getLastAccessedTime()))) : t('never');
return $row + parent::buildRow($entity);
}
示例5: buildRow
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity)
{
$row['username']['data'] = array('#theme' => 'username', '#account' => $entity);
$row['status'] = $entity->isActive() ? $this->t('active') : $this->t('blocked');
$roles = user_role_names(TRUE);
unset($roles[RoleInterface::AUTHENTICATED_ID]);
$users_roles = array();
foreach ($entity->getRoles() as $role) {
if (isset($roles[$role])) {
$users_roles[] = $roles[$role];
}
}
asort($users_roles);
$row['roles']['data'] = array('#theme' => 'item_list', '#items' => $users_roles);
$options = ['return_as_object' => TRUE];
$row['member_for']['data'] = $this->dateFormatter->formatTimeDiffSince($entity->getCreatedTime(), $options)->toRenderable();
$last_access = $this->dateFormatter->formatTimeDiffSince($entity->getLastAccessedTime(), $options);
if ($entity->getLastAccessedTime()) {
$row['access']['data']['#markup'] = $last_access->getString();
CacheableMetadata::createFromObject($last_access)->applyTo($row['access']['data']);
} else {
$row['access']['data']['#markup'] = t('never');
}
return $row + parent::buildRow($entity);
}