/** * Validate user email. * * @param string $value */ public function validate($value, $state = NULL) { if (!$value) { return TRUE; } switch ($state) { case self::EXCLUDE_SELF: return TRUE; break; } $user = new Db_ORM('users'); $user->email = $value; return $user->find() && $this->element->addError(t('Email is already taken!')) ? FALSE : TRUE; }
/** * Default dispatcher * * @param string $action */ public function index_action($action = '', $subaction = NULL) { if (!empty($_POST['token'])) { // Get the profile of authorized user $UserProfile = $this->api->getAuthInfo($_POST['token']); // Check for errors if (!empty($UserProfile->error_type)) { // Debug info for developer error(t($UserProfile->error_type . ": " . $UserProfile->error_message)); } elseif (empty($UserProfile)) { error(t('Temporary error with Loginza authentification.')); } else { $this->session->loginza = $UserProfile; } } if ($loginza = $this->session->loginza) { /** * There we have 3 ways of workflow * * 1. User is logged in. Add new identity to database if it's not exist. * 2. User is registred. Authorize. * 3. User is not registred. Show register form connected and fullfilled with Loginza data (login, e-mail and so on). */ $user = new Db_ORM('users_loginza'); $user->identity = $loginza->identity; // If user is logged in if ($this->user->id) { // If integration is found if ($user->find()) { // If integration belongs to the current user if ($user->uid == $this->user->id) { $user->loginza->data = json_encode($loginza); $user->update(); flash_info(t('Your integration with profile <b>%s</b> was updated successfully.', 'Loginza', $loginza->identity), t('Updated succeed.')); } else { flash_error(t('Profile <b>%s</b> is integrated with sombody else account. You cannot use it before someone would left it out.', 'Loginza', $loginza->identity), t('Update failure.')); } } else { // Create new database record $user->uid = $this->user->id; $user->provider = $loginza->provider; $UserProfile = new Loginza_UserProfile($loginza); isset($loginza->photo) && ($user->photo = $loginza->photo); $user->full_name = $UserProfile->genFullName(); $user->data = json_encode($loginza); $user->save(); } $this->session->loginza = NULL; // Redirect to user profile redirect(Url::gear('user') . 'edit/#tab-social'); } else { // Record found → try to log in if ($user->find()) { $search = new User_Object(); $search->id = $user->uid; if ($search->find()) { $this->user->forceLogin($user->uid, 'id'); } else { flash_error(t('Cannot find user with id <b>%s</b>.', 'Loginza', $user->uid)); } $this->session->loginza = NULL; // This tiny little redirect caused error by Loginza "Invalid / empty session data! Retry auth.: // Left it where it is for memories. // Important! Do not uncomment! //back(); } else { if (!access('user register')) { return info('You don\'t have an access to registration'); } success('First step of registration is done. Please, fill some fields to complete your registration.'); $form = new Form('User.register'); $UserProfile = new Loginza_UserProfile($loginza); $tpl = new Template('Loginza.register'); $tpl->loginza = $loginza; $tpl->profile = $UserProfile; append('content', $tpl->render()); $data['login'] = $UserProfile->genFullName(); isset($loginza->email) && ($data['email'] = $loginza->email); $form->setValues($data); if ($data = $form->result()) { $this->user->object($data); $this->user->hashPassword(); if ($uid = $this->user->save()) { // Create new database record $user->uid = $uid; $user->provider = $loginza->provider; $UserProfile = new Loginza_UserProfile($loginza); isset($loginza->photo) && ($user->photo = $loginza->photo); $user->full_name = $UserProfile->genFullName(); $user->data = json_encode($loginza); $user->save(); } $this->session->loginza = NULL; flash_success('User was successfully registered! Please, check your email for further instructions.', 'Registration succeed.'); redirect(); } append('content', $form->render()); } } } }