X-Git-Url: http://aleph1.co.uk/gitweb/?a=blobdiff_plain;ds=sidebyside;f=web%2Fcore%2Fmodules%2Fuser%2Fsrc%2FPlugin%2FAction%2FChangeUserRoleBase.php;fp=web%2Fcore%2Fmodules%2Fuser%2Fsrc%2FPlugin%2FAction%2FChangeUserRoleBase.php;h=de4c4040fecb41e9ba1167e0b4026971b3e78ee3;hb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;hp=0000000000000000000000000000000000000000;hpb=57c063afa3f66b07c4bbddc2d6129a96d90f0aad;p=yaffs-website diff --git a/web/core/modules/user/src/Plugin/Action/ChangeUserRoleBase.php b/web/core/modules/user/src/Plugin/Action/ChangeUserRoleBase.php new file mode 100644 index 000000000..de4c4040f --- /dev/null +++ b/web/core/modules/user/src/Plugin/Action/ChangeUserRoleBase.php @@ -0,0 +1,102 @@ +entityType = $entity_type; + } + + /** + * {@inheritdoc} + */ + public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) { + return new static( + $configuration, + $plugin_id, + $plugin_definition, + $container->get('entity.manager')->getDefinition('user_role') + ); + } + + /** + * {@inheritdoc} + */ + public function defaultConfiguration() { + return [ + 'rid' => '', + ]; + } + + /** + * {@inheritdoc} + */ + public function buildConfigurationForm(array $form, FormStateInterface $form_state) { + $roles = user_role_names(TRUE); + unset($roles[RoleInterface::AUTHENTICATED_ID]); + $form['rid'] = [ + '#type' => 'radios', + '#title' => t('Role'), + '#options' => $roles, + '#default_value' => $this->configuration['rid'], + '#required' => TRUE, + ]; + return $form; + } + + /** + * {@inheritdoc} + */ + public function submitConfigurationForm(array &$form, FormStateInterface $form_state) { + $this->configuration['rid'] = $form_state->getValue('rid'); + } + + /** + * {@inheritdoc} + */ + public function calculateDependencies() { + if (!empty($this->configuration['rid'])) { + $prefix = $this->entityType->getConfigPrefix() . '.'; + $this->addDependency('config', $prefix . $this->configuration['rid']); + } + return $this->dependencies; + } + + /** + * {@inheritdoc} + */ + public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) { + /** @var \Drupal\user\UserInterface $object */ + $access = $object->access('update', $account, TRUE) + ->andIf($object->roles->access('edit', $account, TRUE)); + + return $return_as_object ? $access : $access->isAllowed(); + } + +}