/** * Set the mail address of a user * * @NoAdminRequired * @NoSubadminRequired * * @param string $id * @param string $mailAddress * @return DataResponse * * TODO: Tidy up and write unit tests - code is mainly static method calls */ public function setMailAddress($id, $mailAddress) { // FIXME: Remove this static function call at some point… if ($this->userSession->getUser()->getUID() !== $id && !$this->isAdmin && !\OC_SubAdmin::isUserAccessible($this->userSession->getUser()->getUID(), $id)) { return new DataResponse(array('status' => 'error', 'data' => array('message' => (string) $this->l10n->t('Forbidden'))), Http::STATUS_FORBIDDEN); } if ($mailAddress !== '' && !$this->mail->validateAddress($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); } $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); }
/** * @dataProvider validateMailProvider * @param $address * @param $expected */ public function testValidateEmail($address, $expected) { $actual = \OC_Mail::validateAddress($address); $this->assertEquals($expected, $actual); }
/** * Returns the default email address * @param string $user_part the user part of the address * @return string the default email address * * Assembles a default email address (using the server hostname * and the given user part, and returns it * Example: when given lostpassword-noreply as $user_part param, * and is currently accessed via http(s)://example.com/, * it would return '*****@*****.**' * * If the configuration value 'mail_from_address' is set in * config.php, this value will override the $user_part that * is passed to this function */ public static function getDefaultEmailAddress($user_part) { $user_part = \OC_Config::getValue('mail_from_address', $user_part); $host_name = self::getServerHostName(); $host_name = \OC_Config::getValue('mail_domain', $host_name); $defaultEmailAddress = $user_part . '@' . $host_name; if (\OC_Mail::validateAddress($defaultEmailAddress)) { return $defaultEmailAddress; } // in case we cannot build a valid email address from the hostname let's fallback to 'localhost.localdomain' return $user_part . '@localhost.localdomain'; }
<?php OC_JSON::checkLoggedIn(); OCP\JSON::callCheck(); $l = \OC::$server->getL10N('settings'); // Get data if (isset($_POST['email']) && OC_Mail::validateAddress($_POST['email'])) { $email = trim($_POST['email']); OC_Preferences::setValue(OC_User::getUser(), 'settings', 'email', $email); OC_JSON::success(array("data" => array("message" => $l->t("Email saved")))); } else { OC_JSON::error(array("data" => array("message" => $l->t("Invalid email")))); }