Moves SCIM to service.

This commit is contained in:
Pierre Ozoux 2022-05-18 18:51:34 +02:00
parent 0c5164f917
commit 69120adc10
4 changed files with 39 additions and 93 deletions

View file

@ -4,40 +4,39 @@ declare(strict_types=1);
namespace OCA\SCIMServiceProvider\Controller; namespace OCA\SCIMServiceProvider\Controller;
use OCP\Accounts\IAccountManager; use OCP\AppFramework\ApiController;
use OCP\AppFramework\Http\Response; use OCP\AppFramework\Http\Response;
use OCP\IConfig;
use OCP\IGroupManager; use OCP\IGroupManager;
use OCP\IRequest; use OCP\IRequest;
use OCP\IUserManager; use OCP\IUserManager;
use OCP\IUserSession;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use OCA\SCIMServiceProvider\Responses\SCIMListResponse; use OCA\SCIMServiceProvider\Responses\SCIMListResponse;
use OCA\SCIMServiceProvider\Responses\SCIMJSONResponse; use OCA\SCIMServiceProvider\Responses\SCIMJSONResponse;
use OCA\SCIMServiceProvider\Responses\SCIMErrorResponse; use OCA\SCIMServiceProvider\Responses\SCIMErrorResponse;
class GroupController extends ASCIMGroup { use OCA\SCIMServiceProvider\Service\SCIMGroup;
class GroupController extends ApiController {
/** @var LoggerInterface */ /** @var LoggerInterface */
private $logger; private $logger;
private $SCIMGroup;
public function __construct(string $appName, public function __construct(string $appName,
IRequest $request, IRequest $request,
IUserManager $userManager, IUserManager $userManager,
IConfig $config,
IGroupManager $groupManager, IGroupManager $groupManager,
IUserSession $userSession, LoggerInterface $logger,
IAccountManager $accountManager, SCIMGroup $SCIMGroup) {
LoggerInterface $logger) {
parent::__construct($appName, parent::__construct($appName,
$request, $request,
$userManager, $userManager,
$config, $groupManager);
$groupManager,
$userSession,
$accountManager);
$this->logger = $logger; $this->logger = $logger;
$this->SCIMGroup = $SCIMGroup;
$this->groupManager = $groupManager;
$this->userManager = $userManager;
} }
/** /**
@ -48,7 +47,7 @@ class GroupController extends ASCIMGroup {
public function index(): SCIMListResponse { public function index(): SCIMListResponse {
$SCIMGroups = $this->groupManager->search('', null, 0); $SCIMGroups = $this->groupManager->search('', null, 0);
$SCIMGroups = array_map(function ($group) { $SCIMGroups = array_map(function ($group) {
return $this->getSCIMGroup($group->getGID()); return $this->SCIMGroup->get($group->getGID());
}, $SCIMGroups); }, $SCIMGroups);
return new SCIMListResponse($SCIMGroups); return new SCIMListResponse($SCIMGroups);
} }
@ -63,8 +62,7 @@ class GroupController extends ASCIMGroup {
* @throws Exception * @throws Exception
*/ */
public function show(string $id): SCIMJSONResponse { public function show(string $id): SCIMJSONResponse {
$group = $this->getSCIMGroup($id); $group = $this->SCIMGroup->get($id);
// getUserData returns empty array if not enough permissions
if (empty($group)) { if (empty($group)) {
return new SCIMErrorResponse(['message' => 'Group not found'], 404); return new SCIMErrorResponse(['message' => 'Group not found'], 404);
} }
@ -101,7 +99,7 @@ class GroupController extends ASCIMGroup {
$targetUser = $this->userManager->get($member['value']); $targetUser = $this->userManager->get($member['value']);
$group->addUser($targetUser); $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); $group->addUser($targetUser);
// todo implement member removal (: // todo implement member removal (:
} }
return new SCIMJSONResponse($this->getSCIMGroup($id)); return new SCIMJSONResponse($this->SCIMGroup->get($id));
} }
/** /**

View file

@ -4,46 +4,42 @@ declare(strict_types=1);
namespace OCA\SCIMServiceProvider\Controller; namespace OCA\SCIMServiceProvider\Controller;
use OCP\Accounts\IAccountManager; use OCP\AppFramework\ApiController;
use OCP\AppFramework\Http\Response; use OCP\AppFramework\Http\Response;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IRequest; use OCP\IRequest;
use OCP\IUserManager; use OCP\IUserManager;
use OCP\IUserSession;
use OCP\Security\ISecureRandom; use OCP\Security\ISecureRandom;
use Psr\Log\LoggerInterface; use Psr\Log\LoggerInterface;
use OCA\SCIMServiceProvider\Responses\SCIMListResponse; use OCA\SCIMServiceProvider\Responses\SCIMListResponse;
use OCA\SCIMServiceProvider\Responses\SCIMJSONResponse; use OCA\SCIMServiceProvider\Responses\SCIMJSONResponse;
use OCA\SCIMServiceProvider\Responses\SCIMErrorResponse; use OCA\SCIMServiceProvider\Responses\SCIMErrorResponse;
class UserController extends ASCIMUser { use OCA\SCIMServiceProvider\Service\SCIMUser;
class UserController extends ApiController {
/** @var LoggerInterface */ /** @var LoggerInterface */
private $logger; private $logger;
/** @var ISecureRandom */ /** @var ISecureRandom */
private $secureRandom; private $secureRandom;
private $SCIMUser;
public function __construct(string $appName, public function __construct(string $appName,
IRequest $request, IRequest $request,
IUserManager $userManager, IUserManager $userManager,
IConfig $config,
IGroupManager $groupManager,
IUserSession $userSession,
IAccountManager $accountManager,
LoggerInterface $logger, LoggerInterface $logger,
ISecureRandom $secureRandom) { ISecureRandom $secureRandom,
SCIMUser $SCIMUser) {
parent::__construct($appName, parent::__construct($appName,
$request, $request,
$userManager, $userManager);
$config,
$groupManager,
$userSession,
$accountManager);
$this->logger = $logger; $this->logger = $logger;
$this->secureRandom = $secureRandom; $this->secureRandom = $secureRandom;
$this->SCIMUser = $SCIMUser;
$this->userManager = $userManager;
} }
/** /**
@ -59,7 +55,7 @@ class UserController extends ASCIMUser {
$SCIMUsers = array(); $SCIMUsers = array();
foreach ($userIds as $userId) { foreach ($userIds as $userId) {
$userId = (string) $userId; $userId = (string) $userId;
$SCIMUser = $this->getSCIMUser($userId); $SCIMUser = $this->SCIMUser->get($userId);
// Do not insert empty entry // Do not insert empty entry
if (!empty($SCIMUser)) { if (!empty($SCIMUser)) {
$SCIMUsers[] = $SCIMUser; $SCIMUsers[] = $SCIMUser;
@ -79,7 +75,7 @@ class UserController extends ASCIMUser {
* @throws Exception * @throws Exception
*/ */
public function show(string $id): SCIMJSONResponse { public function show(string $id): SCIMJSONResponse {
$user = $this->getSCIMUser($id); $user = $this->SCIMUser->get($id);
// getUserData returns empty array if not enough permissions // getUserData returns empty array if not enough permissions
if (empty($user)) { if (empty($user)) {
return new SCIMErrorResponse(['message' => 'User not found'], 404); return new SCIMErrorResponse(['message' => 'User not found'], 404);
@ -116,7 +112,7 @@ class UserController extends ASCIMUser {
} }
} }
$newUser->setEnabled($active); $newUser->setEnabled($active);
return new SCIMJSONResponse($this->getSCIMUser($userName)); return new SCIMJSONResponse($this->SCIMUser->get($userName));
} catch (Exception $e) { } catch (Exception $e) {
$this->logger->warning('Failed createUser attempt with SCIMException exeption.', ['app' => 'SCIMServiceProvider']); $this->logger->warning('Failed createUser attempt with SCIMException exeption.', ['app' => 'SCIMServiceProvider']);
throw $e; throw $e;
@ -151,7 +147,7 @@ class UserController extends ASCIMUser {
if (isset($active)) { if (isset($active)) {
$targetUser->setEnabled($active); $targetUser->setEnabled($active);
} }
return new SCIMJSONResponse($this->getSCIMUser($id)); return new SCIMJSONResponse($this->SCIMUser->get($id));
} }
/** /**

View file

@ -2,43 +2,17 @@
declare(strict_types=1); declare(strict_types=1);
namespace OCA\SCIMServiceProvider\Controller; namespace OCA\SCIMServiceProvider\Service;
use OC\Group\Manager; use OC\Group\Manager;
use OCP\Accounts\IAccountManager;
use OCP\AppFramework\ApiController;
use OCP\IConfig;
use OCP\IGroupManager; use OCP\IGroupManager;
use OCP\IRequest;
use OCP\IUserManager;
use OCP\IUserSession;
abstract class ASCIMGroup extends ApiController { class SCIMGroup {
/** @var IUserManager */
protected $userManager;
/** @var IConfig */
protected $config;
/** @var IGroupManager|Manager */ // FIXME Requires a method that is not on the interface /** @var IGroupManager|Manager */ // FIXME Requires a method that is not on the interface
protected $groupManager; protected $groupManager;
/** @var IUserSession */
protected $userSession;
/** @var IAccountManager */
protected $accountManager;
public function __construct(string $appName, public function __construct(IGroupManager $groupManager) {
IRequest $request,
IUserManager $userManager,
IConfig $config,
IGroupManager $groupManager,
IUserSession $userSession,
IAccountManager $accountManager) {
parent::__construct($appName, $request);
$this->userManager = $userManager;
$this->config = $config;
$this->groupManager = $groupManager; $this->groupManager = $groupManager;
$this->userSession = $userSession;
$this->accountManager = $accountManager;
} }
/** /**
@ -49,7 +23,7 @@ abstract class ASCIMGroup extends ApiController {
* @return array * @return array
* @throws Exception * @throws Exception
*/ */
protected function getSCIMGroup(string $groupId): array { public function get(string $groupId): array {
$groupId = urldecode($groupId); $groupId = urldecode($groupId);
// Check the group exists // Check the group exists

View file

@ -2,43 +2,21 @@
declare(strict_types=1); 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\IConfig;
use OCP\IGroupManager;
use OCP\IRequest;
use OCP\IUserManager; use OCP\IUserManager;
use OCP\IUserSession;
abstract class ASCIMUser extends ApiController { class SCIMUser {
/** @var IUserManager */ /** @var IUserManager */
protected $userManager; protected $userManager;
/** @var IConfig */ /** @var IConfig */
protected $config; 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->userManager = $userManager;
$this->config = $config; $this->config = $config;
$this->groupManager = $groupManager;
$this->userSession = $userSession;
$this->accountManager = $accountManager;
} }
/** /**
@ -49,7 +27,7 @@ abstract class ASCIMUser extends ApiController {
* @return array * @return array
* @throws Exception * @throws Exception
*/ */
protected function getSCIMUser(string $userId): array { public function get(string $userId): array {
// Check if the target user exists // Check if the target user exists
$targetUserObject = $this->userManager->get($userId); $targetUserObject = $this->userManager->get($userId);
if ($targetUserObject === null) { if ($targetUserObject === null) {