Esempio n. 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);
 }