scimserviceprovider/lib/Controller/UserController.php

179 lines
4.6 KiB
PHP
Raw Normal View History

2022-04-29 13:58:37 +00:00
<?php
declare(strict_types=1);
namespace OCA\SCIMServiceProvider\Controller;
2022-05-18 16:51:34 +00:00
use OCP\AppFramework\ApiController;
2022-04-29 13:58:37 +00:00
use OCP\AppFramework\Http\Response;
use OCP\IRequest;
use OCP\IUserManager;
use OCP\Security\ISecureRandom;
use Psr\Log\LoggerInterface;
use OCA\SCIMServiceProvider\Responses\SCIMListResponse;
2022-05-02 10:21:25 +00:00
use OCA\SCIMServiceProvider\Responses\SCIMJSONResponse;
use OCA\SCIMServiceProvider\Responses\SCIMErrorResponse;
2022-05-18 16:51:34 +00:00
use OCA\SCIMServiceProvider\Service\SCIMUser;
class UserController extends ApiController {
2022-04-29 13:58:37 +00:00
/** @var LoggerInterface */
private $logger;
/** @var ISecureRandom */
private $secureRandom;
2022-05-18 16:51:34 +00:00
private $SCIMUser;
2022-05-18 16:15:40 +00:00
2022-04-29 13:58:37 +00:00
public function __construct(string $appName,
IRequest $request,
IUserManager $userManager,
LoggerInterface $logger,
2022-05-18 16:51:34 +00:00
ISecureRandom $secureRandom,
SCIMUser $SCIMUser) {
2022-04-29 13:58:37 +00:00
parent::__construct($appName,
$request,
2022-05-18 16:51:34 +00:00
$userManager);
2022-04-29 13:58:37 +00:00
$this->logger = $logger;
$this->secureRandom = $secureRandom;
2022-05-18 16:51:34 +00:00
$this->SCIMUser = $SCIMUser;
$this->userManager = $userManager;
2022-04-29 13:58:37 +00:00
}
/**
* @NoCSRFRequired
*
* returns a list of users and their data
*/
public function index(): SCIMListResponse {
$users = [];
$users = $this->userManager->search('', null, 0);
$userIds = array_keys($users);
$SCIMUsers = array();
foreach ($userIds as $userId) {
$userId = (string) $userId;
2022-05-18 16:51:34 +00:00
$SCIMUser = $this->SCIMUser->get($userId);
2022-04-29 13:58:37 +00:00
// Do not insert empty entry
if (!empty($SCIMUser)) {
2022-04-29 16:03:24 +00:00
$SCIMUsers[] = $SCIMUser;
2022-04-29 13:58:37 +00:00
}
}
return new SCIMListResponse($SCIMUsers);
}
/**
* @NoCSRFRequired
*
* gets user info
*
* @param string $id
2022-05-02 10:21:25 +00:00
* @return SCIMJSONResponse
* @throws Exception
2022-04-29 13:58:37 +00:00
*/
2022-05-02 10:21:25 +00:00
public function show(string $id): SCIMJSONResponse {
2022-05-18 16:51:34 +00:00
$user = $this->SCIMUser->get($id);
2022-04-29 13:58:37 +00:00
// getUserData returns empty array if not enough permissions
if (empty($user)) {
2022-05-02 10:21:25 +00:00
return new SCIMErrorResponse(['message' => 'User not found'], 404);
2022-04-29 13:58:37 +00:00
}
2022-05-02 10:21:25 +00:00
return new SCIMJSONResponse($user);
2022-04-29 13:58:37 +00:00
}
/**
* @NoCSRFRequired
*
* @param bool $active
* @param string $displayName
* @param array $emails
* @param string $externalId
2022-04-29 13:58:37 +00:00
* @param string $userName
2022-05-02 10:21:25 +00:00
* @return SCIMJSONResponse
* @throws Exception
2022-04-29 13:58:37 +00:00
*/
public function create(bool $active = true,
string $displayName = '',
2022-04-29 16:03:57 +00:00
array $emails = [],
string $externalId = '',
2022-05-02 10:21:25 +00:00
string $userName = ''): SCIMJSONResponse {
2022-04-29 13:58:37 +00:00
if ($this->userManager->userExists($userName)) {
$this->logger->error('Failed createUser attempt: User already exists.', ['app' => 'SCIMServiceProvider']);
2022-05-02 10:21:25 +00:00
return new SCIMErrorResponse(['message' => 'User already exists'], 409);
2022-04-29 13:58:37 +00:00
}
try {
$newUser = $this->userManager->createUser($userName, $this->secureRandom->generate(64));
$this->logger->info('Successful createUser call with userid: ' . $userName, ['app' => 'SCIMServiceProvider']);
2022-04-29 13:58:37 +00:00
foreach ($emails as $email) {
$this->logger->error('Log email: ' . $email['value'], ['app' => 'SCIMServiceProvider']);
if ($email['primary'] === true) {
$newUser->setEMailAddress($email['value']);
}
}
2022-04-29 16:03:57 +00:00
$newUser->setEnabled($active);
$this->SCIMUser->setExternalId($userName, $externalId);
2022-05-18 16:51:34 +00:00
return new SCIMJSONResponse($this->SCIMUser->get($userName));
2022-05-02 10:21:25 +00:00
} catch (Exception $e) {
2022-04-29 13:58:37 +00:00
$this->logger->warning('Failed createUser attempt with SCIMException exeption.', ['app' => 'SCIMServiceProvider']);
throw $e;
}
}
/**
* @NoCSRFRequired
*
* @param string $id
*
2022-04-29 13:58:37 +00:00
* @param bool $active
* @param string $displayName
* @param array $emails
2022-04-29 13:58:37 +00:00
* @return DataResponse
2022-05-02 10:21:25 +00:00
* @throws Exception
2022-04-29 13:58:37 +00:00
*/
public function update(string $id,
2022-04-29 13:58:37 +00:00
bool $active,
string $displayName = '',
2022-05-02 10:21:25 +00:00
array $emails = []): SCIMJSONResponse {
2022-04-29 13:58:37 +00:00
$targetUser = $this->userManager->get($id);
if ($targetUser === null) {
2022-05-02 10:21:25 +00:00
return new SCIMErrorResponse(['message' => 'User not found'], 404);
2022-04-29 13:58:37 +00:00
}
foreach ($emails as $email) {
if ($email['primary'] === true) {
$targetUser->setEMailAddress($email['value']);
}
}
2022-04-29 16:03:57 +00:00
if (isset($active)) {
$targetUser->setEnabled($active);
}
2022-05-18 16:51:34 +00:00
return new SCIMJSONResponse($this->SCIMUser->get($id));
2022-04-29 13:58:37 +00:00
}
/**
* @NoCSRFRequired
*
* @param string $id
* @return DataResponse
*/
public function destroy(string $id): Response {
$targetUser = $this->userManager->get($id);
if ($targetUser === null) {
2022-05-02 10:21:25 +00:00
return new SCIMErrorResponse(['message' => 'User not found'], 404);
2022-04-29 13:58:37 +00:00
}
// Go ahead with the delete
if ($targetUser->delete()) {
$response = new Response();
$response->setStatus(204);
return $response;
} else {
2022-05-02 10:21:25 +00:00
return new SCIMErrorResponse(['message' => 'Couldn\'t delete user'], 503);
2022-04-29 13:58:37 +00:00
}
}
}