Skip to content
Merged
Show file tree
Hide file tree
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
6 changes: 3 additions & 3 deletions developer_manual/basics/events.rst
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,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)
);
});
}
Expand Down Expand Up @@ -783,7 +783,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**:

Expand All @@ -801,7 +801,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**:

Expand Down
103 changes: 38 additions & 65 deletions developer_manual/digging_deeper/users.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,64 +4,54 @@ User management

.. sectionauthor:: Bernhard Posselt <dev@bernhard-posselt.com>

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<dependency-injection>`.

.. code-block:: php
:caption: lib/Service/UserService.php
:emphasize-lines: 8, 10-12

<?php
namespace OCA\MyApp\AppInfo;

use \OCP\AppFramework\App;

use \OCA\MyApp\Service\UserService;


class Application extends App {
namespace OCA\MyApp\Service;

public function __construct(array $urlParams=array()){
parent::__construct('myapp', $urlParams);
use OCP\IUserManager;

$container = $this->getContainer();
class UserService {
private IUserManager $userManager;

/**
* Controllers
*/
$container->registerService('UserService', function($c) {
return new UserService(
$c->query('UserManager')
);
});
public function __construct(IUserManager $userManager){
$this->userManager = $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

<?php

namespace OCA\MyApp\Service;

class UserService {
private IUserManager $userManager;

private $userManager;

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

public function create($userId, $password) {
return $this->userManager->create($userId, $password);
}

}

Modifying users
Expand All @@ -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

<?php
namespace OCA\MyApp\Service;

class UserService {

private $userManager;
private IUserManager $userManager;

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

Expand All @@ -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<dependency-injection>`.

.. code-block:: php
:caption: lib/Service/UserService.php

<?php
namespace OCA\MyApp\AppInfo;

use \OCP\AppFramework\App;

use \OCA\MyApp\Service\UserService;


class Application extends App {

public function __construct(array $urlParams=array()){
parent::__construct('myapp', $urlParams);

$container = $this->getContainer();
namespace OCA\MyApp\Service;

/**
* Controllers
*/
$container->registerService('UserService', function($c) {
return new UserService(
$c->query('UserSession')
);
});
use OCP\IUserSession;

$container->registerService('UserSession', function($c) {
return $c->query('ServerContainer')->getUserSession();
});
class UserService {
private IUserSession $userSession;

// 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

<?php

namespace OCA\MyApp\Service;

class UserService {
use OCP\IUserSession;

private $userSession;
class UserService {
private IUserSession $userSession;

public function __construct($userSession){
public function __construct(IUserSession $userSession){
$this->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();
}

}