From 5ed6060606793250700985b55d1b4823c675762a Mon Sep 17 00:00:00 2001 From: Pellaeon Lin Date: Fri, 29 Aug 2014 17:00:04 +0000 Subject: [PATCH] Connect controllers to DB, try to use dependency injection --- controller/registrationcontroller.php | 48 ++++++++++++++------------- 1 file changed, 25 insertions(+), 23 deletions(-) diff --git a/controller/registrationcontroller.php b/controller/registrationcontroller.php index 4ee3cab..d3b21a3 100644 --- a/controller/registrationcontroller.php +++ b/controller/registrationcontroller.php @@ -21,10 +21,12 @@ class RegistrationController extends Controller { private $mail; private $l10n; + private $db; - public function __construct($appName, IRequest $request, Mail $mail, $l10n){ + public function __construct($appName, IRequest $request, Mail $mail, $l10n, $db){ $this->mail = $mail; $this->l10n = $l10n; + $this->db = $db; parent::__construct($appName, $request); } @@ -46,33 +48,33 @@ class RegistrationController extends Controller { * @NoCSRFRequired * @PublicPage */ - public function sendEmail() { - // TODO: Check if user with this email already exists + public function validateEmail() { + $email = $this->request->getParam('email'); + if ( !filter_var($email, FILTER_VALIDATE_EMAIL) ) { + $this->displayRegisterPage($this->l10n->t('Email address you entered is not valid'), true); + return; + } - if ( !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ) { - $this->displayRegisterPage($l->t('Email address you entered is not valid'), true); + if ( $this->db->find($email) ) { + $this->displayRegisterPage($this->l10n->t('There is already a pending registration with this email'), true); return; } // FEATURE: allow only from specific email domain - $token = self::savePendingRegistration($_POST['email']); - if ( $token === false ) { - $this->displayRegisterPage($l->t('There is already a pending registration with this email'), true); - } elseif ( strlen($token) === 64 ) { - $link = OC_Helper::linkToRoute('core_registration_register_form', - array('token' => $token)); - $link = OC_Helper::makeURLAbsolute($link); - $from = OCP\Util::getDefaultEmailAddress('register'); - $tmpl = new OC_Template('core/registration', 'email'); - $tmpl->assign('link', $link, false); - $msg = $tmpl->fetchPage(); - try { - OC_Mail::send($_POST['email'], 'ownCloud User', $l->t('Verify your ownCloud registration request'), $msg, $from, 'ownCloud'); - } catch (Exception $e) { - OC_Template::printErrorPage( 'A problem occurs during sending the e-mail please contact your administrator.'); - return; - } - $this->displayRegisterPage('', true); + $token = $this->db->savePendingRegistration($email); + $link = OC_Helper::linkToRoute('core_registration_register_form', + array('token' => $token)); + $link = OC_Helper::makeURLAbsolute($link); + $from = OCP\Util::getDefaultEmailAddress('register'); + $tmpl = new OC_Template('core/registration', 'email'); + $tmpl->assign('link', $link, false); + $msg = $tmpl->fetchPage(); + try { + OC_Mail::send($_POST['email'], 'ownCloud User', $l->t('Verify your ownCloud registration request'), $msg, $from, 'ownCloud'); + } catch (Exception $e) { + OC_Template::printErrorPage( 'A problem occurs during sending the e-mail please contact your administrator.'); + return; } + $this->displayRegisterPage('', true); }