Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(devmanual): Add user managers doc #10355

Merged
merged 1 commit into from
May 10, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions developer_manual/digging_deeper/users.rst
Original file line number Diff line number Diff line change
Expand Up @@ -173,3 +173,82 @@ Then users can be logged in by using:
}

}

User objects
------------

User objects can be acquired from the ``IUserManager::get`` method.

.. code-block:: php
:caption: lib/Service/UserService.php
:emphasize-lines: 17

<?php

namespace OCA\MyApp\Service;

use OCP\IUser;
use OCP\IUserManager;

class UserService {
private IUserManager $userManager;

public function __construct(IUserManager $userManager) {
$this->userManager = $userManager;
}

public function foo(string $userId): void {
/** @var IUser|null $user */
$user = $this->userManager->get($userId);
if ($user !== null) {
// User exists
} else {
// The user does not exist
}
}
}

User managers
^^^^^^^^^^^^^

.. versionadded:: 27

Nextcloud users can be defined as managers of other users. This is an informational property and has no influence on authorization. A user manager is not to confuse with admins or sub admins.

.. code-block:: php
:caption: lib/Service/UserService.php
:emphasize-lines: 22, 29-31

<?php

namespace OCA\MyApp\Service;

use OCP\IUser;
use OCP\IUserManager;

class UserService {
private IUserManager $userManager;

public function __construct(IUserManager $userManager) {
$this->userManager = $userManager;
}

public function updateUserManagers(string $userId): void {
/** @var IUser|null $user */
$user = $this->userManager->get('user123');
if ($user === null) {
throw \InvalidArgumentException("User $userId does not exist");
}

$managerUids = $user->getManagerUids();
// Turn UIDs into user objects
$managers = array_map(function(string $uid) {
return $this->userManager->get($uid);
}, $managerUids));
// Remove and managers that no longer exist as user
$existingManagers = array_filter($managers);
$user->setManagerUids(array_map(function(IUser $admin) {
return $user->getUID();
}, $existingManagers));
}
}