[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/lib/plugins/usermanager/ -> remote.php (source)

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