123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- <?php
- declare(strict_types=1);
- namespace OCA\Provisioning_API\Controller;
- use OC\Group\Manager;
- use OC\User\Backend;
- use OC\User\NoUserException;
- use OC_Helper;
- use OCA\Provisioning_API\ResponseDefinitions;
- use OCP\Accounts\IAccountManager;
- use OCP\Accounts\PropertyDoesNotExistException;
- use OCP\AppFramework\Http;
- use OCP\AppFramework\OCS\OCSException;
- use OCP\AppFramework\OCS\OCSNotFoundException;
- use OCP\AppFramework\OCSController;
- use OCP\Files\NotFoundException;
- use OCP\IConfig;
- use OCP\IGroupManager;
- use OCP\IRequest;
- use OCP\IUserManager;
- use OCP\IUserSession;
- use OCP\L10N\IFactory;
- use OCP\User\Backend\ISetDisplayNameBackend;
- use OCP\User\Backend\ISetPasswordBackend;
- abstract class AUserData extends OCSController {
- public const SCOPE_SUFFIX = 'Scope';
- public const USER_FIELD_DISPLAYNAME = 'display';
- public const USER_FIELD_LANGUAGE = 'language';
- public const USER_FIELD_LOCALE = 'locale';
- public const USER_FIELD_PASSWORD = 'password';
- public const USER_FIELD_QUOTA = 'quota';
- public const USER_FIELD_MANAGER = 'manager';
- public const USER_FIELD_NOTIFICATION_EMAIL = 'notify_email';
-
- protected $userManager;
-
- protected $config;
-
- protected $groupManager;
-
- protected $userSession;
-
- protected $accountManager;
-
- protected $l10nFactory;
- public function __construct(string $appName,
- IRequest $request,
- IUserManager $userManager,
- IConfig $config,
- IGroupManager $groupManager,
- IUserSession $userSession,
- IAccountManager $accountManager,
- IFactory $l10nFactory) {
- parent::__construct($appName, $request);
- $this->userManager = $userManager;
- $this->config = $config;
- $this->groupManager = $groupManager;
- $this->userSession = $userSession;
- $this->accountManager = $accountManager;
- $this->l10nFactory = $l10nFactory;
- }
-
- protected function getUserData(string $userId, bool $includeScopes = false): ?array {
- $currentLoggedInUser = $this->userSession->getUser();
- assert($currentLoggedInUser !== null, 'No user logged in');
- $data = [];
-
- $targetUserObject = $this->userManager->get($userId);
- if ($targetUserObject === null) {
- throw new OCSNotFoundException('User does not exist');
- }
- $isAdmin = $this->groupManager->isAdmin($currentLoggedInUser->getUID());
- if ($isAdmin
- || $this->groupManager->getSubAdmin()->isUserAccessible($currentLoggedInUser, $targetUserObject)) {
- $data['enabled'] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'enabled', 'true') === 'true';
- } else {
-
- if ($currentLoggedInUser->getUID() !== $targetUserObject->getUID()) {
- return null;
- }
- }
-
- $userAccount = $this->accountManager->getAccount($targetUserObject);
- $groups = $this->groupManager->getUserGroups($targetUserObject);
- $gids = [];
- foreach ($groups as $group) {
- $gids[] = $group->getGID();
- }
- if ($isAdmin) {
- try {
-
-
-
- $data['storageLocation'] = $targetUserObject->getHome();
- } catch (NoUserException $e) {
- throw new OCSNotFoundException($e->getMessage(), $e);
- }
- }
-
- $data['id'] = $targetUserObject->getUID();
- $data['lastLogin'] = $targetUserObject->getLastLogin() * 1000;
- $data['backend'] = $targetUserObject->getBackendClassName();
- $data['subadmin'] = $this->getUserSubAdminGroupsData($targetUserObject->getUID());
- $data[self::USER_FIELD_QUOTA] = $this->fillStorageInfo($targetUserObject->getUID());
- $managerUids = $targetUserObject->getManagerUids();
- $data[self::USER_FIELD_MANAGER] = empty($managerUids) ? '' : $managerUids[0];
- try {
- if ($includeScopes) {
- $data[IAccountManager::PROPERTY_AVATAR . self::SCOPE_SUFFIX] = $userAccount->getProperty(IAccountManager::PROPERTY_AVATAR)->getScope();
- }
- $data[IAccountManager::PROPERTY_EMAIL] = $targetUserObject->getSystemEMailAddress();
- if ($includeScopes) {
- $data[IAccountManager::PROPERTY_EMAIL . self::SCOPE_SUFFIX] = $userAccount->getProperty(IAccountManager::PROPERTY_EMAIL)->getScope();
- }
- $additionalEmails = $additionalEmailScopes = [];
- $emailCollection = $userAccount->getPropertyCollection(IAccountManager::COLLECTION_EMAIL);
- foreach ($emailCollection->getProperties() as $property) {
- $additionalEmails[] = $property->getValue();
- if ($includeScopes) {
- $additionalEmailScopes[] = $property->getScope();
- }
- }
- $data[IAccountManager::COLLECTION_EMAIL] = $additionalEmails;
- if ($includeScopes) {
- $data[IAccountManager::COLLECTION_EMAIL . self::SCOPE_SUFFIX] = $additionalEmailScopes;
- }
- $data[IAccountManager::PROPERTY_DISPLAYNAME] = $targetUserObject->getDisplayName();
- $data[IAccountManager::PROPERTY_DISPLAYNAME_LEGACY] = $data[IAccountManager::PROPERTY_DISPLAYNAME];
- if ($includeScopes) {
- $data[IAccountManager::PROPERTY_DISPLAYNAME . self::SCOPE_SUFFIX] = $userAccount->getProperty(IAccountManager::PROPERTY_DISPLAYNAME)->getScope();
- }
- foreach ([
- IAccountManager::PROPERTY_PHONE,
- IAccountManager::PROPERTY_ADDRESS,
- IAccountManager::PROPERTY_WEBSITE,
- IAccountManager::PROPERTY_TWITTER,
- IAccountManager::PROPERTY_FEDIVERSE,
- IAccountManager::PROPERTY_ORGANISATION,
- IAccountManager::PROPERTY_ROLE,
- IAccountManager::PROPERTY_HEADLINE,
- IAccountManager::PROPERTY_BIOGRAPHY,
- IAccountManager::PROPERTY_PROFILE_ENABLED,
- ] as $propertyName) {
- $property = $userAccount->getProperty($propertyName);
- $data[$propertyName] = $property->getValue();
- if ($includeScopes) {
- $data[$propertyName . self::SCOPE_SUFFIX] = $property->getScope();
- }
- }
- } catch (PropertyDoesNotExistException $e) {
-
- throw new OCSException($e->getMessage(), Http::STATUS_INTERNAL_SERVER_ERROR, $e);
- }
- $data['groups'] = $gids;
- $data[self::USER_FIELD_LANGUAGE] = $this->l10nFactory->getUserLanguage($targetUserObject);
- $data[self::USER_FIELD_LOCALE] = $this->config->getUserValue($targetUserObject->getUID(), 'core', 'locale');
- $data[self::USER_FIELD_NOTIFICATION_EMAIL] = $targetUserObject->getPrimaryEMailAddress();
- $backend = $targetUserObject->getBackend();
- $data['backendCapabilities'] = [
- 'setDisplayName' => $backend instanceof ISetDisplayNameBackend || $backend->implementsActions(Backend::SET_DISPLAYNAME),
- 'setPassword' => $backend instanceof ISetPasswordBackend || $backend->implementsActions(Backend::SET_PASSWORD),
- ];
- return $data;
- }
-
- protected function getUserSubAdminGroupsData(string $userId): array {
- $user = $this->userManager->get($userId);
-
- if ($user === null) {
- throw new OCSNotFoundException('User does not exist');
- }
-
- $subAdminGroups = $this->groupManager->getSubAdmin()->getSubAdminsGroups($user);
- $groups = [];
- foreach ($subAdminGroups as $key => $group) {
- $groups[] = $group->getGID();
- }
- return $groups;
- }
-
- protected function fillStorageInfo(string $userId): array {
- try {
- \OC_Util::tearDownFS();
- \OC_Util::setupFS($userId);
- $storage = OC_Helper::getStorageInfo('/', null, true, false);
- $data = [
- 'free' => $storage['free'],
- 'used' => $storage['used'],
- 'total' => $storage['total'],
- 'relative' => $storage['relative'],
- self::USER_FIELD_QUOTA => $storage['quota'],
- ];
- } catch (NotFoundException $ex) {
-
- $user = $this->userManager->get($userId);
- if ($user === null) {
- throw new OCSException('User does not exist', 101);
- }
- $quota = $user->getQuota();
- if ($quota !== 'none') {
- $quota = OC_Helper::computerFileSize($quota);
- }
- $data = [
- self::USER_FIELD_QUOTA => $quota !== false ? $quota : 'none',
- 'used' => 0
- ];
- } catch (\Exception $e) {
- \OC::$server->get(\Psr\Log\LoggerInterface::class)->error(
- "Could not load storage info for {user}",
- [
- 'app' => 'provisioning_api',
- 'user' => $userId,
- 'exception' => $e,
- ]
- );
-
- \OC_Util::tearDownFS();
- return [];
- }
- return $data;
- }
- }
|