You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

111 lines
3.6 KiB

  1. <?php
  2. use dokuwiki\Extension\AuthPlugin;
  3. use dokuwiki\Extension\RemotePlugin;
  4. use dokuwiki\Remote\AccessDeniedException;
  5. use dokuwiki\Remote\RemoteException;
  6. /**
  7. * DokuWiki Plugin usermanager (Action Component)
  8. *
  9. * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
  10. * @author Chris Smith <chris@jalakai.co.uk>
  11. */
  12. class remote_plugin_usermanager extends RemotePlugin
  13. {
  14. /**
  15. * Create a new user
  16. *
  17. * If no password is provided, a password is auto generated. If the user can't be created
  18. * by the auth backend a return value of `false` is returned. You need to check this return
  19. * value rather than relying on the error code only.
  20. *
  21. * Superuser permission are required to create users.
  22. *
  23. * @param string $user The user's login name
  24. * @param string $name The user's full name
  25. * @param string $mail The user's email address
  26. * @param string[] $groups The groups the user should be in
  27. * @param string $password The user's password, empty for autogeneration
  28. * @param bool $notify Whether to send a notification email to the user
  29. * @return bool Wether the user was successfully created
  30. * @throws AccessDeniedException
  31. * @throws RemoteException
  32. * @todo handle error messages from auth backend
  33. */
  34. public function createUser($user, $name, $mail, $groups, $password = '', $notify = false)
  35. {
  36. if (!auth_isadmin()) {
  37. throw new AccessDeniedException('Only admins are allowed to create users', 114);
  38. }
  39. /** @var AuthPlugin $auth */
  40. global $auth;
  41. if (!$auth->canDo('addUser')) {
  42. throw new AccessDeniedException(
  43. sprintf('Authentication backend %s can\'t do addUser', $auth->getPluginName()),
  44. 404
  45. );
  46. }
  47. $user = trim($auth->cleanUser($user));
  48. $name = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $name));
  49. $mail = trim(preg_replace('/[\x00-\x1f:<>&%,;]+/', '', $mail));
  50. if ($user === '') throw new RemoteException('empty or invalid user', 401);
  51. if ($name === '') throw new RemoteException('empty or invalid user name', 402);
  52. if (!mail_isvalid($mail)) throw new RemoteException('empty or invalid mail address', 403);
  53. if ((string)$password === '') {
  54. try {
  55. $password = auth_pwgen($user);
  56. } catch (\Exception $e) {
  57. throw new RemoteException('Could not generate password', 405);
  58. }
  59. }
  60. if (!is_array($groups) || $groups === []) {
  61. $groups = null;
  62. }
  63. $ok = (bool)$auth->triggerUserMod('create', [$user, $password, $name, $mail, $groups]);
  64. if ($ok && $notify) {
  65. auth_sendPassword($user, $password);
  66. }
  67. return $ok;
  68. }
  69. /**
  70. * Remove a user
  71. *
  72. * You need to be a superuser to delete users.
  73. *
  74. * @param string[] $user The login name of the user to delete
  75. * @return bool wether the user was successfully deleted
  76. * @throws AccessDeniedException
  77. * @todo handle error messages from auth backend
  78. */
  79. public function deleteUser($user)
  80. {
  81. if (!auth_isadmin()) {
  82. throw new AccessDeniedException('Only admins are allowed to delete users', 114);
  83. }
  84. global $auth;
  85. if (!$auth->canDo('delUser')) {
  86. throw new AccessDeniedException(
  87. sprintf('Authentication backend %s can\'t do delUser', $auth->getPluginName()),
  88. 404
  89. );
  90. }
  91. /** @var AuthPlugin $auth */
  92. global $auth;
  93. return (bool)$auth->triggerUserMod('delete', [[$user]]);
  94. }
  95. }