/** * Validate wether the given columns are long enough * * @param array $data The data to validate * @param Garp_Model_Db $model * @param bool $onlyIfAvailable Wether to skip validation on fields that are not in the array * @return void * @throws Garp_Model_Validator_Exception */ public function validate(array $data, Garp_Model_Db $model, $onlyIfAvailable = true) { $theFields = $this->_fields; $applicableFields = array_keys(array_get_subset($data, array_keys($theFields))); $tooShortFields = array_filter($applicableFields, function ($field) use($theFields, $data) { return !is_null($data[$field]) && strlen($data[$field]) < $theFields[$field]; }); if (count($tooShortFields)) { $first = current($tooShortFields); throw new Garp_Model_Validator_Exception(Garp_Util_String::interpolate(__(self::ERROR_MESSAGE), array('value' => $first, 'min' => $theFields[$first]))); } }
public function testShouldSendEmail() { if (!$this->_testsEnabled) { return; } $pwless = new Garp_Auth_Adapter_Passwordless(); $pwless->requestToken(array('email' => self::TEST_EMAIL)); $userModel = new Model_User(); $theUser = $userModel->fetchRow(); $authModel = new Model_AuthPasswordless(); $authRecord = $authModel->fetchRow(); $tokenUrl = new Garp_Util_FullUrl(array(array('method' => 'passwordless'), 'auth_submit')) . '?uid=' . $theUser->id . '&token=' . $authRecord->token; $storedMessage = file_get_contents(GARP_APPLICATION_PATH . '/../tests/tmp/' . self::TEST_EMAIL . '.tmp'); $expectedMessage = Garp_Util_String::interpolate($this->_getMockEmailMessage(), array('LOGIN_URL' => $tokenUrl)); // Pass thru actual Mime part, otherwise the two wil never be the same $mp = new Zend_Mime_Part($expectedMessage); $mp->encoding = Zend_Mime::ENCODING_QUOTEDPRINTABLE; $mp->type = Zend_Mime::TYPE_TEXT; $mp->disposition = Zend_Mime::DISPOSITION_INLINE; $mp->charset = 'iso-8859-1'; // Just check for the token url. Message is encoded so checking for entire message to be // correct is overly complex (and not the responsibility of this unit test). $this->assertTrue(strpos($storedMessage, $mp->getContent("\r\n")) !== false); }
/** * Process the login request. @see G_AuthController::loginAction as to * why this is separate. * * @return void */ public function processAction() { // allow callers to set a targetUrl via the request if ($targetUrl = $this->_getSubmittedTargetUrl()) { Garp_Auth::getInstance()->getStore()->targetUrl = $targetUrl; } // never cache the process request $this->_helper->cache->setNoCacheHeaders($this->getResponse()); // This action does not render a view, it only redirects elsewhere. $this->_helper->viewRenderer->setNoRender(true); $method = $this->getRequest()->getParam('method') ?: 'db'; $adapter = Garp_Auth_Factory::getAdapter($method); $authVars = Garp_Auth::getInstance()->getConfigValues(); $postData = $this->getRequest()->getPost(); // Before login hook. $this->_beforeLogin($authVars, $adapter, $postData); /** * Params can come from GET or POST. * The implementing adapter should decide which to use, * using the current request to fetch params. */ if (!($userData = $adapter->authenticate($this->getRequest(), $this->getResponse()))) { $this->_respondToFaultyProcess($adapter); return; } $this->_helper->viewRenderer->setNoRender(true); // Check if adapter issued a redirect (as is the case with oAuth for instance) if ($this->getResponse()->isRedirect()) { return; } if ($userData instanceof Garp_Db_Table_Row) { $userData = $userData->toArray(); } // Save user data in a store. Garp_Auth::getInstance()->store($userData, $method); // Store User role in a cookie, so that we can use it with Javascript. if (!Garp_Auth::getInstance()->getStore() instanceof Garp_Store_Cookie) { $this->_storeRoleInCookie(); } // Determine targetUrl. // This is the URL the user was trying to access before logging in, or a default URL. $router = Zend_Controller_Front::getInstance()->getRouter(); if (!empty($authVars['login']['successRoute'])) { $targetUrl = $router->assemble(array(), $authVars['login']['successRoute']); } elseif (!empty($authVars['login']['successUrl'])) { $targetUrl = $authVars['login']['successUrl']; } else { $targetUrl = '/'; } $store = Garp_Auth::getInstance()->getStore(); if ($store->targetUrl) { $targetUrl = $store->targetUrl; unset($store->targetUrl); } // After login hook. $this->_afterLogin($userData, $targetUrl); // Set a Flash message welcoming the user. $flashMessenger = $this->_helper->getHelper('FlashMessenger'); $fullName = new Garp_Util_FullName($userData); $successMsg = __($authVars['login']['successMessage']); if (strpos($successMsg, '%s') !== false) { $successMsg = sprintf($successMsg, $fullName); } elseif (strpos('%USERNAME%', $successMsg) !== false) { $successMsg = Garp_Util_String::interpolate($successMsg, array('USERNAME' => $fullName)); } $flashMessenger->addMessage($successMsg); $this->_redirect($targetUrl); }
protected function _interpolateEmailBody($body, $userId, $token) { return Garp_Util_String::interpolate($body, array('LOGIN_URL' => $this->_getLoginUrl($userId, $token))); }
/** * Send validation email to the user * @param Garp_Db_Table_Row $user The user * @param String $code The validation code * @param String $updateOrInsert Wether this was caused by an insert or an update * @return Boolean */ public function sendEmailValidationEmail(Garp_Db_Table_Row $user, $code, $updateOrInsert = 'insert') { $authVars = Garp_Auth::getInstance()->getConfigValues('validateemail'); // Render the email message $activationUrl = '/g/auth/validateemail/c/' . $code . '/e/' . md5($user->email) . '/'; if (!empty($authVars['email_partial'])) { $bootstrap = Zend_Controller_Front::getInstance()->getParam('bootstrap'); $view = $bootstrap->getResource('View'); $emailMessage = $view->partial($authVars['email_partial'], 'default', array('user' => $user, 'activationUrl' => $activationUrl, 'updateOrInsert' => $updateOrInsert)); $messageParam = 'htmlMessage'; } else { $snippetId = 'validate email '; $snippetId .= $updateOrInsert == 'insert' ? 'new user' : 'existing user'; $snippetId .= ' email'; $emailMessage = __($snippetId); $emailMessage = Garp_Util_String::interpolate($emailMessage, array('USERNAME' => (string) new Garp_Util_FullName($user), 'ACTIVATION_URL' => (string) new Garp_Util_FullUrl($activationUrl))); $messageParam = 'message'; } $mailer = new Garp_Mailer(); return $mailer->send(array('to' => $user->email, 'subject' => __($authVars['email_subject']), $messageParam => $emailMessage)); }
protected function _getForgotPasswordSnippet($user, $activationUrl) { $authVars = $this->getConfigValues('forgotpassword'); $snippet_column = !empty($authVars['email_snippet_column']) ? $authVars['email_snippet_column'] : 'text'; $snippet_identifier = !empty($authVars['email_snippet_identifier']) ? $authVars['email_snippet_identifier'] : 'forgot password email'; $snippetModel = $this->_getSnippetModel(); $emailSnippet = $snippetModel->fetchByIdentifier($snippet_identifier); $emailMessage = $emailSnippet->{$snippet_column}; return Garp_Util_String::interpolate($emailMessage, array('USERNAME' => (string) new Garp_Util_FullName($user), 'ACTIVATION_URL' => (string) new Garp_Util_FullUrl($activationUrl))); }