Mercurial > hg > isophonics-drupal-site
comparison core/modules/user/src/Plugin/Action/ChangeUserRoleBase.php @ 0:4c8ae668cc8c
Initial import (non-working)
author | Chris Cannam |
---|---|
date | Wed, 29 Nov 2017 16:09:58 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:4c8ae668cc8c |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\user\Plugin\Action; | |
4 | |
5 use Drupal\Core\Action\ConfigurableActionBase; | |
6 use Drupal\Core\Entity\DependencyTrait; | |
7 use Drupal\Core\Entity\EntityTypeInterface; | |
8 use Drupal\Core\Form\FormStateInterface; | |
9 use Drupal\Core\Plugin\ContainerFactoryPluginInterface; | |
10 use Drupal\Core\Session\AccountInterface; | |
11 use Drupal\user\RoleInterface; | |
12 use Symfony\Component\DependencyInjection\ContainerInterface; | |
13 | |
14 /** | |
15 * Provides a base class for operations to change a user's role. | |
16 */ | |
17 abstract class ChangeUserRoleBase extends ConfigurableActionBase implements ContainerFactoryPluginInterface { | |
18 | |
19 use DependencyTrait; | |
20 | |
21 /** | |
22 * The user role entity type. | |
23 * | |
24 * @var \Drupal\Core\Entity\EntityTypeInterface | |
25 */ | |
26 protected $entityType; | |
27 | |
28 /** | |
29 * {@inheritdoc} | |
30 */ | |
31 public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeInterface $entity_type) { | |
32 parent::__construct($configuration, $plugin_id, $plugin_definition); | |
33 $this->entityType = $entity_type; | |
34 } | |
35 | |
36 /** | |
37 * {@inheritdoc} | |
38 */ | |
39 public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { | |
40 return new static( | |
41 $configuration, | |
42 $plugin_id, | |
43 $plugin_definition, | |
44 $container->get('entity.manager')->getDefinition('user_role') | |
45 ); | |
46 } | |
47 | |
48 /** | |
49 * {@inheritdoc} | |
50 */ | |
51 public function defaultConfiguration() { | |
52 return [ | |
53 'rid' => '', | |
54 ]; | |
55 } | |
56 | |
57 /** | |
58 * {@inheritdoc} | |
59 */ | |
60 public function buildConfigurationForm(array $form, FormStateInterface $form_state) { | |
61 $roles = user_role_names(TRUE); | |
62 unset($roles[RoleInterface::AUTHENTICATED_ID]); | |
63 $form['rid'] = [ | |
64 '#type' => 'radios', | |
65 '#title' => t('Role'), | |
66 '#options' => $roles, | |
67 '#default_value' => $this->configuration['rid'], | |
68 '#required' => TRUE, | |
69 ]; | |
70 return $form; | |
71 } | |
72 | |
73 /** | |
74 * {@inheritdoc} | |
75 */ | |
76 public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { | |
77 $this->configuration['rid'] = $form_state->getValue('rid'); | |
78 } | |
79 | |
80 /** | |
81 * {@inheritdoc} | |
82 */ | |
83 public function calculateDependencies() { | |
84 if (!empty($this->configuration['rid'])) { | |
85 $prefix = $this->entityType->getConfigPrefix() . '.'; | |
86 $this->addDependency('config', $prefix . $this->configuration['rid']); | |
87 } | |
88 return $this->dependencies; | |
89 } | |
90 | |
91 /** | |
92 * {@inheritdoc} | |
93 */ | |
94 public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) { | |
95 /** @var \Drupal\user\UserInterface $object */ | |
96 $access = $object->access('update', $account, TRUE) | |
97 ->andIf($object->roles->access('edit', $account, TRUE)); | |
98 | |
99 return $return_as_object ? $access : $access->isAllowed(); | |
100 } | |
101 | |
102 } |