scimserviceprovider/lib/Service/SCIMUser.php

85 lines
2 KiB
PHP
Raw Normal View History

2022-04-29 13:58:37 +00:00
<?php
declare(strict_types=1);
2022-05-18 16:51:34 +00:00
namespace OCA\SCIMServiceProvider\Service;
2022-04-29 13:58:37 +00:00
use OCP\IConfig;
use OCP\IUserManager;
2022-05-18 16:51:34 +00:00
class SCIMUser {
2022-04-29 13:58:37 +00:00
/** @var IUserManager */
protected $userManager;
/** @var IConfig */
protected $config;
2022-05-18 16:51:34 +00:00
public function __construct(IUserManager $userManager,
IConfig $config) {
2022-04-29 13:58:37 +00:00
$this->userManager = $userManager;
$this->config = $config;
}
/**
2022-05-02 10:21:25 +00:00
* creates an object with all user data
2022-04-29 13:58:37 +00:00
*
* @param string $userId
* @param bool $includeScopes
* @return array
2022-05-02 10:21:25 +00:00
* @throws Exception
2022-04-29 13:58:37 +00:00
*/
2022-05-18 16:51:34 +00:00
public function get(string $userId): array {
2022-04-29 13:58:37 +00:00
// Check if the target user exists
$targetUserObject = $this->userManager->get($userId);
if ($targetUserObject === null) {
2022-05-02 10:21:25 +00:00
return [];
2022-04-29 13:58:37 +00:00
}
2022-04-29 16:03:57 +00:00
$enabled = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'enabled', 'true') === 'true';
$externalId = $this->config->getUserValue($targetUserObject->getUID(), 'SCIMServiceProvider', 'ExternalId', '');
$email = $targetUserObject->getSystemEMailAddress();
2022-04-29 16:03:57 +00:00
$SCIMUser = [
'schemas' => ["urn:ietf:params:scim:schemas:core:2.0:User"],
'id' => $userId,
2022-05-02 10:21:25 +00:00
'name' => [
'formatted' => $targetUserObject->getDisplayName()
],
'meta' => [
2022-04-29 13:58:37 +00:00
'resourceType' => 'User',
'location' => '/Users/' . $userId,
2022-05-19 10:23:46 +00:00
'created' => '1970-01-01T00:00:00.000Z',
'lastModified' => '1970-01-01T00:00:00.000Z'
],
'userName' => $userId,
'displayName' => $targetUserObject->getDisplayName(),
'active' => $enabled
];
if ($externalId !== '') {
$SCIMUser['externalId'] = $externalId;
}
if ($email !== null) {
2022-05-19 15:01:37 +00:00
$SCIMUser['emails'] = [ // todo if no emails
2022-04-29 16:02:39 +00:00
[
'primary' => true,
'value' => $email
2022-04-29 16:02:39 +00:00
]
];
}
return $SCIMUser;
2022-04-29 13:58:37 +00:00
}
/**
* Sets externalId on user
*
* @param string $userId
* @param string $externalId
* @throws Exception
*/
public function setExternalId(string $userId, string $externalId) {
$this->config->setUserValue($userId, 'SCIMServiceProvider', 'ExternalId', $externalId);
}
2022-04-29 13:58:37 +00:00
}