From 3b9ff4a92e275ef301db767d81fa17053904b731 Mon Sep 17 00:00:00 2001 From: Christoph Wurst Date: Wed, 10 May 2023 09:32:29 +0200 Subject: [PATCH] fix(devmanual): Modernize user manager and user session docs Signed-off-by: Christoph Wurst --- developer_manual/basics/events.rst | 6 +- developer_manual/digging_deeper/users.rst | 103 ++++++++-------------- 2 files changed, 41 insertions(+), 68 deletions(-) diff --git a/developer_manual/basics/events.rst b/developer_manual/basics/events.rst index 382ae92e8..1b5c0dcde 100644 --- a/developer_manual/basics/events.rst +++ b/developer_manual/basics/events.rst @@ -742,7 +742,7 @@ The hook logic should be in a separate class that is being registered in the `Ap */ $container->registerService('UserHooks', function($c) { return new UserHooks( - $c->query('ServerContainer')->getUserManager() + $c->get(\OCP\IUserManager::class) ); }); } @@ -804,7 +804,7 @@ The following hooks are available: Session ``````` -Injectable from the ServerContainer by calling the method **getUserSession()**. +Injectable from the ServerContainer with the ``\OCP\IUserSession`` service. Hooks available in scope **\\OC\\User**: @@ -822,7 +822,7 @@ Hooks available in scope **\\OC\\User**: UserManager ``````````` -Injectable from the ServerContainer by calling the method **getUserManager()**. +Injectable from the ServerContainer with the ``\OCP\IUserManager`` service. Hooks available in scope **\\OC\\User**: diff --git a/developer_manual/digging_deeper/users.rst b/developer_manual/digging_deeper/users.rst index c49821715..4c3602c15 100644 --- a/developer_manual/digging_deeper/users.rst +++ b/developer_manual/digging_deeper/users.rst @@ -4,64 +4,54 @@ User management .. sectionauthor:: Bernhard Posselt -Users can be managed using the UserManager which is injected from the ServerContainer: +Users can be managed using the IUserManager service that can be :ref:`injected`. .. code-block:: php + :caption: lib/Service/UserService.php + :emphasize-lines: 8, 10-12 userManager = $userManager; + } - public function __construct(array $urlParams=array()){ - parent::__construct('myapp', $urlParams); - - $container = $this->getContainer(); - - /** - * Controllers - */ - $container->registerService('UserService', function($c) { - return new UserService( - $c->query('UserManager') - ); - }); - - $container->registerService('UserManager', function($c) { - return $c->query('ServerContainer')->getUserManager(); - }); + public function createUser($userId, $password) { + return $this->userManager->create($userId, $password); } } - Creating users -------------- Creating a user is done by passing a username and password to the create method: .. code-block:: php + :caption: lib/Service/UserService.php + :emphasize-lines: 13 userManager = $userManager; } public function create($userId, $password) { return $this->userManager->create($userId, $password); } - } Modifying users @@ -75,15 +65,16 @@ Users can be modified by getting a user by the userId or by a search pattern. Th * Get their home directory .. code-block:: php + :emphasize-lines: 13 userManager = $userManager; } @@ -108,68 +99,50 @@ Users can be modified by getting a user by the userId or by a search pattern. Th User session information ------------------------ -To login, logout or getting the currently logged in user, the UserSession has to be injected from the ServerContainer: +To login, logout or getting the currently logged in user, the IUserSession service that can be :ref:`injected`. .. code-block:: php + :caption: lib/Service/UserService.php getContainer(); - - /** - * Controllers - */ - $container->registerService('UserService', function($c) { - return new UserService( - $c->query('UserSession') - ); - }); - - $container->registerService('UserSession', function($c) { - return $c->query('ServerContainer')->getUserSession(); - }); - - // currently logged in user, userId can be gotten by calling the - // getUID() method on it - $container->registerService('User', function($c) { - return $c->query('UserSession')->getUser(); - }); + public function __construct(IUserSession $userSession){ + $this->userSession = $userSession; } } -Then users can be logged in by using: +Then users can be logged in and out by using: .. code-block:: php + :caption: lib/Service/UserService.php + :emphasize-lines: 15,19 userSession = $userSession; } - public function login($userId, $password) { + public function login(string $userId, string $password): void { return $this->userSession->login($userId, $password); } - public function logout() { + public function logout(): void { $this->userSession->logout(); } - }