Skip to content
Snippets Groups Projects
Commit 4fdb2dcf authored by Steven Ayers's avatar Steven Ayers
Browse files

Issue #3361975 by bluegeek9: Rename AuthorizationController

parent 0e99015e
No related branches found
No related tags found
1 merge request!7Issue #3361975: Rename AuthorizationController
......@@ -25,11 +25,11 @@ function authorization_help($route_name, RouteMatchInterface $route_match) {
* Implements hook_user_login().
*/
function authorization_user_login($account) {
/** @var \Drupal\authorization\AuthorizationController $controller */
$controller = \Drupal::service('authorization.manager');
$controller->setUser($account);
$controller->setAllProfiles();
$processed_authorizations = $controller->getProcessedAuthorizations();
/** @var \Drupal\authorization\AuthorizationServiceInterface $service */
$service = \Drupal::service('authorization.manager');
$service->setUser($account);
$service->setAllProfiles();
$processed_authorizations = $service->getProcessedAuthorizations();
if (\Drupal::config('authorization.settings')->get('authorization_message')) {
foreach ($processed_authorizations as $authorization) {
......
......@@ -21,8 +21,8 @@ services:
tags:
- { name: event_subscriber }
authorization.manager:
class: Drupal\authorization\AuthorizationController
class: Drupal\authorization\Service\AuthorizationService
arguments: ['@entity_type.manager', '@logger.channel.authorization']
logger.channel.authorization:
parent: logger.channel_base
arguments: ['authorization']
parent: logger.channel_base
arguments: ['authorization']
<?php
declare(strict_types = 1);
namespace Drupal\authorization;
use Drupal\user\UserInterface;
/**
* Authorization Service interface.
*/
interface AuthorizationServiceInterface {
/**
* Set the user.
*
* We pass in the user by hand so we can act on the provisional Drupal
* user object we have available during login and other operations.
*
* @param \Drupal\user\UserInterface $user
* The user to act upon.
*/
public function setUser(UserInterface $user): void;
/**
* Get the user.
*
* @return \Drupal\user\UserInterface
* The user to act upon.
*/
public function getUser(): UserInterface;
/**
* Process a specific profile.
*
* Saves the user account.
*
* @param string|int $profile_id
* Authorization profile to act upon.
*/
public function setIndividualProfile($profile_id): void;
/**
* Fetch and process all available profiles.
*
* Saves the user account.
*/
public function setAllProfiles(): void;
/**
* Query a specific profile.
*
* This does *not* save the user account. We need this to simulate granting
* to know that in some modes we want to abort any further actions
* (e.g. no valid proposals in exclusive mode and deny access set).
*
* @param string $profile_id
* Authorization profile to act upon.
*/
public function queryIndividualProfile(string $profile_id): void;
/**
* Fetch and query all available profiles.
*
* This does *not* save the user account.
*
* @see queryIndividualProfile()
*/
public function queryAllProfiles(): void;
/**
* Returns list of all authorizations, which were processed.
*
* @return AuthorizationResponse[]
* Authorizations by human-readable label.
*/
public function getProcessedAuthorizations(): array;
/**
* Clear processed authorizations.
*
* If the service is called multiple times (e.g. for testing with query(),
* instead of set()) this allows one to clear the list of processed
* authorizations.
*/
public function clearAuthorizations(): void;
}
......@@ -2,17 +2,18 @@
declare(strict_types=1);
namespace Drupal\authorization;
namespace Drupal\authorization\Service;
use Drupal\authorization\AuthorizationServiceInterface;
use Drupal\authorization\Entity\AuthorizationProfile;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\user\UserInterface;
use Psr\Log\LoggerInterface;
/**
* Authorization controller.
* Authorization service.
*/
class AuthorizationController {
class AuthorizationService implements AuthorizationServiceInterface {
/**
* Entity type manager.
......@@ -46,7 +47,7 @@ class AuthorizationController {
protected $processedAuthorizations = [];
/**
* Constructs a new AuthorizationController object.
* Constructs a new AuthorizationService object.
*/
public function __construct(
EntityTypeManagerInterface $entity_type_manager,
......@@ -57,25 +58,21 @@ class AuthorizationController {
}
/**
* Set the user.
*
* We pass in the user by hand so we can act on the provisional Drupal
* user object we have available during login and other operations.
*
* @param \Drupal\user\UserInterface $user
* The user to act upon.
* {@inheritdoc}
*/
public function setUser(UserInterface $user) {
public function setUser(UserInterface $user): void {
$this->user = $user;
}
/**
* Process a specific profile.
*
* Saves the user account.
*
* @param string|int $profile_id
* Authorization profile to act upon.
* {@inheritdoc}
*/
public function getUser(): UserInterface {
return $this->user;
}
/**
* {@inheritdoc}
*/
public function setIndividualProfile($profile_id): void {
/** @var \Drupal\authorization\Entity\AuthorizationProfile $profile */
......@@ -89,9 +86,7 @@ class AuthorizationController {
}
/**
* Fetch and process all available profiles.
*
* Saves the user account.
* {@inheritdoc}
*/
public function setAllProfiles(): void {
$queryResults = $this->entityTypeManager
......@@ -105,14 +100,7 @@ class AuthorizationController {
}
/**
* Query a specific profile.
*
* This does *not* save the user account. We need this to simulate granting
* to know that in some modes we want to abort any further actions
* (e.g. no valid proposals in exclusive mode and deny access set).
*
* @param string $profile_id
* Authorization profile to act upon.
* {@inheritdoc}
*/
public function queryIndividualProfile(string $profile_id): void {
/** @var \Drupal\authorization\Entity\AuthorizationProfile $profile */
......@@ -127,11 +115,7 @@ class AuthorizationController {
}
/**
* Fetch and query all available profiles.
*
* This does *not* save the user account.
*
* @see queryIndividualProfile()
* {@inheritdoc}
*/
public function queryAllProfiles(): void {
$queryResults = $this->entityTypeManager->getStorage('authorization_profile')
......@@ -143,6 +127,20 @@ class AuthorizationController {
}
}
/**
* {@inheritdoc}
*/
public function getProcessedAuthorizations(): array {
return $this->processedAuthorizations;
}
/**
* {@inheritdoc}
*/
public function clearAuthorizations(): void {
$this->processedAuthorizations = [];
}
/**
* Process Authorizations.
*
......@@ -157,25 +155,4 @@ class AuthorizationController {
}
}
/**
* Returns list of all authorizations, which were processed.
*
* @return AuthorizationResponse[]
* Authorizations by human-readable label.
*/
public function getProcessedAuthorizations(): array {
return $this->processedAuthorizations;
}
/**
* Clear processed authorizations.
*
* If the service is called multiple times (e.g. for testing with query(),
* instead of set()) this allows one to clear the list of processed
* authorizations.
*/
public function clearAuthorizations(): void {
$this->processedAuthorizations = [];
}
}
......@@ -12,7 +12,7 @@ use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
*
* @group authorization
*/
class ControllerTest extends EntityKernelTestBase {
class ServiceTest extends EntityKernelTestBase {
/**
* {@inheritdoc}
......@@ -45,23 +45,23 @@ class ControllerTest extends EntityKernelTestBase {
/**
* Test processing authorizations.
*/
public function testController(): void {
public function testService(): void {
$user = $this->createUser();
$user->save();
$user->proposals = ['student'];
/** @var \Drupal\authorization\AuthorizationController $controller */
$controller = $this->container->get('authorization.manager');
$controller->setUser($user);
$controller->queryAllProfiles();
$authorizations = $controller->getProcessedAuthorizations();
/** @var \Drupal\authorization\AuthorizationServiceInterface $service */
$service = $this->container->get('authorization.manager');
$service->setUser($user);
$service->queryAllProfiles();
$authorizations = $service->getProcessedAuthorizations();
$authorization = reset($authorizations);
self::assertArrayHasKey('student', $authorization->getAuthorizationsApplied());
$controller->clearAuthorizations();
$service->clearAuthorizations();
$user->proposals = ['exception'];
$controller->queryAllProfiles();
$authorizations = $controller->getProcessedAuthorizations();
$service->queryAllProfiles();
$authorizations = $service->getProcessedAuthorizations();
$authorization = reset($authorizations);
self::assertEmpty($authorization->getAuthorizationsApplied());
self::assertEquals(TRUE, $authorization->getSkipped());
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment