UserPermissionsForm.php in User Permissions 8
File
src/Form/UserPermissionsForm.php
View source
<?php
namespace Drupal\user_permissions\Form;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\Url;
use Drupal\user\Entity\Role;
use Drupal\user\Entity\User;
use Drupal\user\Form\UserPermissionsRoleSpecificForm;
use Drupal\user\PermissionHandlerInterface;
use Drupal\user\RoleStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
class UserPermissionsForm extends UserPermissionsRoleSpecificForm {
protected $account;
protected $user;
public function __construct(PermissionHandlerInterface $permission_handler, RoleStorageInterface $role_storage, ModuleHandlerInterface $module_handler, AccountInterface $account) {
parent::__construct($permission_handler, $role_storage, $module_handler);
$this->account = $account;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('user.permissions'), $container
->get('entity.manager')
->getStorage('user_role'), $container
->get('module_handler'), $container
->get('current_user'));
}
public function buildForm(array $form, FormStateInterface $form_state, $user = NULL) {
if (!$this->account
->hasPermission('administer permissions')) {
return new RedirectResponse(Url::fromRoute('user.page'));
}
if (!is_null($user)) {
$uid = $user;
$this->user = User::load($uid);
}
else {
$form['user'] = [
'#markup' => $this
->t('User could not be found.'),
];
return $form;
}
$role_name = '_user_role_' . $uid;
$role = Role::load($role_name);
if ($role) {
$this->userRole = Role::load($role_name);
$form = parent::buildForm($form, $form_state, $this->userRole);
}
else {
$this->userRole = Role::load(USER_PERMISSIONS_NO_ROLE);
$form = parent::buildForm($form, $form_state, $this->userRole);
foreach ($form['permissions']['#header'] as $key => &$data) {
if (is_array($data) && array_key_exists('data', $data)) {
$data['data'] = $role_name;
}
}
$form['permissions'][$this->userRole
->id()]['#default_value'] = [];
$form['role_names']['#value'][$this->userRole
->id()] = $role_name;
}
$blocked_permissions = [];
$user_roles = $this->user
->getRoles();
foreach (user_role_permissions($user_roles) as $rid => $permissions) {
if ($rid != $role
->get('id')) {
$blocked_permissions += array_filter($permissions);
}
}
$rid = $role
->get('id');
foreach ($blocked_permissions as $permission) {
if (isset($form['permissions'][$permission][$rid])) {
$form['permissions'][$permission][$rid]['#default_value'] = 1;
$form['permissions'][$permission][$rid]['#value'] = $permission;
$form['permissions'][$permission][$rid]['#disabled'] = TRUE;
$form['permissions'][$permission][$rid]['#attributes']['checked'] = TRUE;
}
}
$form['role_names'][$this->userRole
->id()]['#markup'] = 'Enable?';
$form['role_name'] = [
'#type' => 'hidden',
'#value' => $this->userRole
->label(),
];
$form['uid'] = [
'#type' => 'hidden',
'#value' => $uid,
];
return $form;
}
public function submitForm(array &$form, FormStateInterface $form_state) {
$perms = [];
$uid = (int) $form_state
->getValue('uid');
$role_name = $form_state
->getValue('role_name');
$input = $form_state
->getUserInput();
if (array_key_exists($role_name, $input)) {
$perms = $input[$role_name];
}
if ($role_name == USER_PERMISSIONS_NO_ROLE) {
if (!empty($perms)) {
$role_name = '_user_role_' . $uid;
$this->userRole = Role::create([
'id' => $role_name,
'label' => $role_name,
]);
$this->userRole
->save();
foreach ($form_state
->getValue('role_names') as $role_name => $name) {
user_role_change_permissions($this->userRole
->label(), (array) $form_state
->getValue($role_name));
}
}
}
else {
$perms_exist = array_filter($perms);
if (empty($perms_exist)) {
$this->userRole
->delete();
}
else {
foreach ($form_state
->getValue('role_names') as $role_name => $name) {
user_role_change_permissions($role_name, (array) $form_state
->getValue($role_name));
}
}
}
}
public function getFormId() {
return 'user_permissions_form';
}
}