User management

Users can be managed using the UserManager which is injected from the ServerContainer:

  1. <?php
  2. namespace OCA\MyApp\AppInfo;
  3. use \OCP\AppFramework\App;
  4. use \OCA\MyApp\Service\UserService;
  5. class Application extends App {
  6. public function __construct(array $urlParams=array()){
  7. parent::__construct('myapp', $urlParams);
  8. $container = $this->getContainer();
  9. /**
  10. * Controllers
  11. */
  12. $container->registerService('UserService', function($c) {
  13. return new UserService(
  14. $c->query('UserManager')
  15. );
  16. });
  17. $container->registerService('UserManager', function($c) {
  18. return $c->query('ServerContainer')->getUserManager();
  19. });
  20. }
  21. }

Creating users

Creating a user is done by passing a username and password to the create method:

  1. <?php
  2. namespace OCA\MyApp\Service;
  3. class UserService {
  4. private $userManager;
  5. public function __construct($userManager){
  6. $this->userManager = $userManager;
  7. }
  8. public function create($userId, $password) {
  9. return $this->userManager->create($userId, $password);
  10. }
  11. }

Modifying users

Users can be modified by getting a user by the userId or by a search pattern. The returned user objects can then be used to:

  • Delete them
  • Set a new password
  • Disable/Enable them
  • Get their home directory
  1. <?php
  2. namespace OCA\MyApp\Service;
  3. class UserService {
  4. private $userManager;
  5. public function __construct($userManager){
  6. $this->userManager = $userManager;
  7. }
  8. public function delete($userId) {
  9. return $this->userManager->get($userId)->delete();
  10. }
  11. // recoveryPassword is used for the encryption app to recover the keys
  12. public function setPassword($userId, $password, $recoveryPassword) {
  13. return $this->userManager->get($userId)->setPassword($password, $recoveryPassword);
  14. }
  15. public function disable($userId) {
  16. return $this->userManager->get($userId)->setEnabled(false);
  17. }
  18. public function getHome($userId) {
  19. return $this->userManager->get($userId)->getHome();
  20. }
  21. }

User session information

To login, logout or getting the currently logged in user, the UserSession has to be injected from the ServerContainer:

  1. <?php
  2. namespace OCA\MyApp\AppInfo;
  3. use \OCP\AppFramework\App;
  4. use \OCA\MyApp\Service\UserService;
  5. class Application extends App {
  6. public function __construct(array $urlParams=array()){
  7. parent::__construct('myapp', $urlParams);
  8. $container = $this->getContainer();
  9. /**
  10. * Controllers
  11. */
  12. $container->registerService('UserService', function($c) {
  13. return new UserService(
  14. $c->query('UserSession')
  15. );
  16. });
  17. $container->registerService('UserSession', function($c) {
  18. return $c->query('ServerContainer')->getUserSession();
  19. });
  20. // currently logged in user, userId can be gotten by calling the
  21. // getUID() method on it
  22. $container->registerService('User', function($c) {
  23. return $c->query('UserSession')->getUser();
  24. });
  25. }
  26. }

Then users can be logged in by using:

  1. <?php
  2. namespace OCA\MyApp\Service;
  3. class UserService {
  4. private $userSession;
  5. public function __construct($userSession){
  6. $this->userSession = $userSession;
  7. }
  8. public function login($userId, $password) {
  9. return $this->userSession->login($userId, $password);
  10. }
  11. public function logout() {
  12. $this->userSession->logout();
  13. }
  14. }