Example #1
0
 /**
  * Set the mail address of a user
  *
  * @NoAdminRequired
  * @NoSubadminRequired
  *
  * @param string $id
  * @param string $mailAddress
  * @return DataResponse
  */
 public function setMailAddress($id, $mailAddress)
 {
     $userId = $this->userSession->getUser()->getUID();
     if ($userId !== $id && !$this->isAdmin && !$this->subAdminFactory->isUserAccessible($userId, $id)) {
         return new DataResponse(array('status' => 'error', 'data' => array('message' => (string) $this->l10n->t('Forbidden'))), Http::STATUS_FORBIDDEN);
     }
     if ($mailAddress !== '' && !$this->mailer->validateMailAddress($mailAddress)) {
         return new DataResponse(array('status' => 'error', 'data' => array('message' => (string) $this->l10n->t('Invalid mail address'))), Http::STATUS_UNPROCESSABLE_ENTITY);
     }
     $user = $this->userManager->get($id);
     if (!$user) {
         return new DataResponse(array('status' => 'error', 'data' => array('message' => (string) $this->l10n->t('Invalid user'))), Http::STATUS_UNPROCESSABLE_ENTITY);
     }
     // this is the only permission a backend provides and is also used
     // for the permission of setting a email address
     if (!$user->canChangeDisplayName()) {
         return new DataResponse(array('status' => 'error', 'data' => array('message' => (string) $this->l10n->t('Unable to change mail address'))), Http::STATUS_FORBIDDEN);
     }
     // delete user value if email address is empty
     if ($mailAddress === '') {
         $this->config->deleteUserValue($id, 'settings', 'email');
     } else {
         $this->config->setUserValue($id, 'settings', 'email', $mailAddress);
     }
     return new DataResponse(array('status' => 'success', 'data' => array('username' => $id, 'mailAddress' => $mailAddress, 'message' => (string) $this->l10n->t('Email saved'))), Http::STATUS_OK);
 }
 /**
  * Count all unique users visible for the current admin/subadmin.
  *
  * @NoAdminRequired
  *
  * @return DataResponse
  */
 public function stats()
 {
     $userCount = 0;
     if ($this->isAdmin) {
         $countByBackend = $this->userManager->countUsers();
         if (!empty($countByBackend)) {
             foreach ($countByBackend as $count) {
                 $userCount += $count;
             }
         }
     } else {
         $groupNames = $this->subAdminFactory->getSubAdminsOfGroups($this->userSession->getUser()->getUID());
         $uniqueUsers = [];
         foreach ($groupNames as $groupName) {
             $group = $this->groupManager->get($groupName);
             if (!is_null($group)) {
                 foreach ($group->getUsers() as $uid => $displayName) {
                     $uniqueUsers[$uid] = true;
                 }
             }
         }
         $userCount = count($uniqueUsers);
     }
     return new DataResponse(['totalUsers' => $userCount]);
 }