Skip to content
Snippets Groups Projects
Commit 3cfade5a authored by Salim Qazizada's avatar Salim Qazizada
Browse files

initial commit

parents
Branches
Tags 1.0.0-beta1
No related merge requests found
.idea
\ No newline at end of file
Remove Invalid Permissions
name: RIP
description: "Removes Invalid Permissions"
type: module
package: RIP
core_version_requirement: ^8 || ^9 || ^10
version: '1.0.x'
<?php
/**
* @file
* Contains rip.module.
*/
services:
rip.commands:
class: \Drupal\rip\Commands\RipCommands
arguments: [ '@entity_type.manager', '@user.permissions' ]
tags:
- { name: drush.command }
<?php
namespace Drupal\rip\Commands;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Component\Plugin\Exception\PluginNotFoundException;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\user\Entity\Role;
use Drupal\user\PermissionHandler;
use Drupal\user\RoleStorageInterface;
use Drush\Commands\DrushCommands;
/**
* A Drush command file.
*
* @package Drupal\rip\Commands
*/
class RipCommands extends DrushCommands
{
/**
* The entity type manager.
*
* @var EntityTypeManagerInterface
*/
protected EntityTypeManagerInterface $entityTypeManager;
/**
* The permission handler.
*
* @var PermissionHandler
*/
protected PermissionHandler $permissionHandler;
/**
* The entity storage.
*
* @var EntityStorageInterface
*/
protected EntityStorageInterface $roleStorage;
/**
* RipCommands constructor.
*
* @throws InvalidPluginDefinitionException
* @throws PluginNotFoundException
*/
public function __construct(
EntityTypeManagerInterface $entity_type_manager,
PermissionHandler $permission_handler)
{
parent::__construct();
$this->entityTypeManager = $entity_type_manager;
$this->permissionHandler = $permission_handler;
$this->roleStorage = $entity_type_manager->getStorage('user_role');
}
/**
* @command remove-invalid-permissions
* @aliases rip
*
* @return void
*
* @throws EntityStorageException
*/
public function index(): void
{
$permissions = array_keys($this->permissionHandler->getPermissions());
/** @var Role[] $roles */
$roles = $this->roleStorage->loadMultiple();
/** @var RoleStorageInterface $role */
foreach ($roles as $role) {
$role_permissions = $role->getPermissions();
$diff_permissions_in_role = array_diff($role_permissions, $permissions);
if ($diff_permissions_in_role) {
foreach ($diff_permissions_in_role as $permission) {
$confirm = $this->confirm('Revoke ' . $permission . ' for ' . $role->id() . '?');
if ($confirm) {
$this->io()->note('Revoked');
$role->revokePermission($permission);
}
}
$role->save();
}
}
$this->logger()->success('Invalid Permissions removed.');
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment