From 69120adc10684c2dc3b3b9be1cbf02ecefed7d92 Mon Sep 17 00:00:00 2001 From: Pierre Ozoux Date: Wed, 18 May 2022 18:51:34 +0200 Subject: [PATCH] Moves SCIM to service. --- lib/Controller/GroupController.php | 32 ++++++++--------- lib/Controller/UserController.php | 34 ++++++++----------- .../ASCIMGroup.php => Service/SCIMGroup.php} | 34 +++---------------- .../ASCIMUser.php => Service/SCIMUser.php} | 32 +++-------------- 4 files changed, 39 insertions(+), 93 deletions(-) rename lib/{Controller/ASCIMGroup.php => Service/SCIMGroup.php} (56%) rename lib/{Controller/ASCIMUser.php => Service/SCIMUser.php} (58%) diff --git a/lib/Controller/GroupController.php b/lib/Controller/GroupController.php index eefc719..4715cdc 100644 --- a/lib/Controller/GroupController.php +++ b/lib/Controller/GroupController.php @@ -4,40 +4,39 @@ declare(strict_types=1); namespace OCA\SCIMServiceProvider\Controller; -use OCP\Accounts\IAccountManager; +use OCP\AppFramework\ApiController; use OCP\AppFramework\Http\Response; -use OCP\IConfig; use OCP\IGroupManager; use OCP\IRequest; use OCP\IUserManager; -use OCP\IUserSession; use Psr\Log\LoggerInterface; use OCA\SCIMServiceProvider\Responses\SCIMListResponse; use OCA\SCIMServiceProvider\Responses\SCIMJSONResponse; use OCA\SCIMServiceProvider\Responses\SCIMErrorResponse; -class GroupController extends ASCIMGroup { +use OCA\SCIMServiceProvider\Service\SCIMGroup; + +class GroupController extends ApiController { /** @var LoggerInterface */ private $logger; + private $SCIMGroup; public function __construct(string $appName, IRequest $request, IUserManager $userManager, - IConfig $config, IGroupManager $groupManager, - IUserSession $userSession, - IAccountManager $accountManager, - LoggerInterface $logger) { + LoggerInterface $logger, + SCIMGroup $SCIMGroup) { parent::__construct($appName, $request, $userManager, - $config, - $groupManager, - $userSession, - $accountManager); + $groupManager); $this->logger = $logger; + $this->SCIMGroup = $SCIMGroup; + $this->groupManager = $groupManager; + $this->userManager = $userManager; } /** @@ -48,7 +47,7 @@ class GroupController extends ASCIMGroup { public function index(): SCIMListResponse { $SCIMGroups = $this->groupManager->search('', null, 0); $SCIMGroups = array_map(function ($group) { - return $this->getSCIMGroup($group->getGID()); + return $this->SCIMGroup->get($group->getGID()); }, $SCIMGroups); return new SCIMListResponse($SCIMGroups); } @@ -63,8 +62,7 @@ class GroupController extends ASCIMGroup { * @throws Exception */ public function show(string $id): SCIMJSONResponse { - $group = $this->getSCIMGroup($id); - // getUserData returns empty array if not enough permissions + $group = $this->SCIMGroup->get($id); if (empty($group)) { return new SCIMErrorResponse(['message' => 'Group not found'], 404); } @@ -101,7 +99,7 @@ class GroupController extends ASCIMGroup { $targetUser = $this->userManager->get($member['value']); $group->addUser($targetUser); } - return new SCIMJSONResponse($this->getSCIMGroup($id)); + return new SCIMJSONResponse($this->SCIMGroup->get($id)); } @@ -127,7 +125,7 @@ class GroupController extends ASCIMGroup { $group->addUser($targetUser); // todo implement member removal (: } - return new SCIMJSONResponse($this->getSCIMGroup($id)); + return new SCIMJSONResponse($this->SCIMGroup->get($id)); } /** diff --git a/lib/Controller/UserController.php b/lib/Controller/UserController.php index af5b7f4..a757a00 100644 --- a/lib/Controller/UserController.php +++ b/lib/Controller/UserController.php @@ -4,46 +4,42 @@ declare(strict_types=1); namespace OCA\SCIMServiceProvider\Controller; -use OCP\Accounts\IAccountManager; +use OCP\AppFramework\ApiController; use OCP\AppFramework\Http\Response; -use OCP\IConfig; -use OCP\IGroupManager; use OCP\IRequest; use OCP\IUserManager; -use OCP\IUserSession; use OCP\Security\ISecureRandom; use Psr\Log\LoggerInterface; use OCA\SCIMServiceProvider\Responses\SCIMListResponse; use OCA\SCIMServiceProvider\Responses\SCIMJSONResponse; use OCA\SCIMServiceProvider\Responses\SCIMErrorResponse; -class UserController extends ASCIMUser { +use OCA\SCIMServiceProvider\Service\SCIMUser; + + +class UserController extends ApiController { /** @var LoggerInterface */ private $logger; /** @var ISecureRandom */ private $secureRandom; + private $SCIMUser; public function __construct(string $appName, IRequest $request, IUserManager $userManager, - IConfig $config, - IGroupManager $groupManager, - IUserSession $userSession, - IAccountManager $accountManager, LoggerInterface $logger, - ISecureRandom $secureRandom) { + ISecureRandom $secureRandom, + SCIMUser $SCIMUser) { parent::__construct($appName, $request, - $userManager, - $config, - $groupManager, - $userSession, - $accountManager); + $userManager); $this->logger = $logger; $this->secureRandom = $secureRandom; + $this->SCIMUser = $SCIMUser; + $this->userManager = $userManager; } /** @@ -59,7 +55,7 @@ class UserController extends ASCIMUser { $SCIMUsers = array(); foreach ($userIds as $userId) { $userId = (string) $userId; - $SCIMUser = $this->getSCIMUser($userId); + $SCIMUser = $this->SCIMUser->get($userId); // Do not insert empty entry if (!empty($SCIMUser)) { $SCIMUsers[] = $SCIMUser; @@ -79,7 +75,7 @@ class UserController extends ASCIMUser { * @throws Exception */ public function show(string $id): SCIMJSONResponse { - $user = $this->getSCIMUser($id); + $user = $this->SCIMUser->get($id); // getUserData returns empty array if not enough permissions if (empty($user)) { return new SCIMErrorResponse(['message' => 'User not found'], 404); @@ -116,7 +112,7 @@ class UserController extends ASCIMUser { } } $newUser->setEnabled($active); - return new SCIMJSONResponse($this->getSCIMUser($userName)); + return new SCIMJSONResponse($this->SCIMUser->get($userName)); } catch (Exception $e) { $this->logger->warning('Failed createUser attempt with SCIMException exeption.', ['app' => 'SCIMServiceProvider']); throw $e; @@ -151,7 +147,7 @@ class UserController extends ASCIMUser { if (isset($active)) { $targetUser->setEnabled($active); } - return new SCIMJSONResponse($this->getSCIMUser($id)); + return new SCIMJSONResponse($this->SCIMUser->get($id)); } /** diff --git a/lib/Controller/ASCIMGroup.php b/lib/Service/SCIMGroup.php similarity index 56% rename from lib/Controller/ASCIMGroup.php rename to lib/Service/SCIMGroup.php index 747d056..6c7520a 100644 --- a/lib/Controller/ASCIMGroup.php +++ b/lib/Service/SCIMGroup.php @@ -2,43 +2,17 @@ declare(strict_types=1); -namespace OCA\SCIMServiceProvider\Controller; +namespace OCA\SCIMServiceProvider\Service; use OC\Group\Manager; -use OCP\Accounts\IAccountManager; -use OCP\AppFramework\ApiController; -use OCP\IConfig; use OCP\IGroupManager; -use OCP\IRequest; -use OCP\IUserManager; -use OCP\IUserSession; -abstract class ASCIMGroup extends ApiController { - /** @var IUserManager */ - protected $userManager; - /** @var IConfig */ - protected $config; +class SCIMGroup { /** @var IGroupManager|Manager */ // FIXME Requires a method that is not on the interface protected $groupManager; - /** @var IUserSession */ - protected $userSession; - /** @var IAccountManager */ - protected $accountManager; - public function __construct(string $appName, - IRequest $request, - IUserManager $userManager, - IConfig $config, - IGroupManager $groupManager, - IUserSession $userSession, - IAccountManager $accountManager) { - parent::__construct($appName, $request); - - $this->userManager = $userManager; - $this->config = $config; + public function __construct(IGroupManager $groupManager) { $this->groupManager = $groupManager; - $this->userSession = $userSession; - $this->accountManager = $accountManager; } /** @@ -49,7 +23,7 @@ abstract class ASCIMGroup extends ApiController { * @return array * @throws Exception */ - protected function getSCIMGroup(string $groupId): array { + public function get(string $groupId): array { $groupId = urldecode($groupId); // Check the group exists diff --git a/lib/Controller/ASCIMUser.php b/lib/Service/SCIMUser.php similarity index 58% rename from lib/Controller/ASCIMUser.php rename to lib/Service/SCIMUser.php index 1e8b1b3..1f4220d 100644 --- a/lib/Controller/ASCIMUser.php +++ b/lib/Service/SCIMUser.php @@ -2,43 +2,21 @@ declare(strict_types=1); -namespace OCA\SCIMServiceProvider\Controller; +namespace OCA\SCIMServiceProvider\Service; -use OC\Group\Manager; -use OCP\Accounts\IAccountManager; -use OCP\AppFramework\ApiController; use OCP\IConfig; -use OCP\IGroupManager; -use OCP\IRequest; use OCP\IUserManager; -use OCP\IUserSession; -abstract class ASCIMUser extends ApiController { +class SCIMUser { /** @var IUserManager */ protected $userManager; /** @var IConfig */ protected $config; - /** @var IGroupManager|Manager */ // FIXME Requires a method that is not on the interface - protected $groupManager; - /** @var IUserSession */ - protected $userSession; - /** @var IAccountManager */ - protected $accountManager; - - public function __construct(string $appName, - IRequest $request, - IUserManager $userManager, - IConfig $config, - IGroupManager $groupManager, - IUserSession $userSession, - IAccountManager $accountManager) { - parent::__construct($appName, $request); + public function __construct(IUserManager $userManager, + IConfig $config) { $this->userManager = $userManager; $this->config = $config; - $this->groupManager = $groupManager; - $this->userSession = $userSession; - $this->accountManager = $accountManager; } /** @@ -49,7 +27,7 @@ abstract class ASCIMUser extends ApiController { * @return array * @throws Exception */ - protected function getSCIMUser(string $userId): array { + public function get(string $userId): array { // Check if the target user exists $targetUserObject = $this->userManager->get($userId); if ($targetUserObject === null) {