Пример #1
0
 public function getUserByAuthIdentity($provider, $identity)
 {
     $auths = new Application_Model_User_Authentications();
     if ($auth = $auths->getAuthentication($provider, $identity)) {
         if ($user = $this->getUserBy('user_id', $auth->user_id)) {
             return $user;
         }
     }
     return false;
 }
Пример #2
0
 public function createFromExternalAction()
 {
     //@todo check if the user isn't already logged in with a local account - redirect to account home if so
     $auth = Zend_Auth::getInstance();
     $authSess = new Zend_Session_Namespace('Auth');
     $identity = $auth->getIdentity();
     $external = $authSess->external;
     $users = new Application_Model_Users();
     //if(!isset($authData['identity']) || !isset($authData['properties']) || !isset($authData['properties']['email']) || !isset($authData['provider']))
     //	throw Exception('Invalid auth data');
     $form = new Application_Form_RegisterFromExternal();
     $form->setAction($this->_helper->url('create-from-external'));
     if ($this->getRequest()->isPost()) {
         if (!$form->isValid($this->getRequest()->getPost())) {
             // Did not pass validation...
             $this->view->form = $form;
             return $this->render();
         }
         $values = $form->getValues();
         /*array_merge(
          		$form->getValues(),
          		array(
          			'email'=>$external['properties']['email']
          		)
          	);*/
         //if(!isset($values['email']) || !isset($values['username']))
         //  throw new Exception('Email or username not supplied');
         if (!isset($external['provider']) || !isset($external['identity'])) {
             throw new Exception('Email or username not supplied');
         }
         //check for local with matching email
         if (!($user = $users->getUserBy('email', $values['email']))) {
             //no local user with matching email, create new local user
             try {
                 //create new user
                 $user = $users->createUser($values);
                 //email user their password
                 if (isset($user->email)) {
                     $email = $user->email;
                     $email_to = $user->display_name ?: $user->username;
                     $password = $values['password'];
                     $subject = $email_to . ', you logged in to Practical Plants';
                     $html = '<h1>Hello ' . $email_to . ', thanks for logging in to Practical Plants</h1>' . '<p>You logged in using ' . $external['provider'] . '</p>' . '<p>Next time you log in, either use ' . $external['provider'] . ', or you can login using the account details you just chose: </p>' . '<blockquote><p>Email: ' . $email . '</p>' . '<p>Password: '******'</p></blockquote>' . '<p>If you have any questions, drop by the <a href="http://practicalplants.org/community">Community Forums</a> or email us: hello@practicalplants.org</p>';
                     $text = "Hello {$email_to}, thanks for logging in to Practical Plants\n" . "You logged in using {$external['provider']}.\n" . "Next time you log in, either use {$external['provider']}, or you can login using the account details you just chose: \n" . "\tEmail: {$email}\n" . "\tPassword: {$password}\n" . "If you have any questions, drop by the Community Forums (http://practicalplants.org/community) or email us: hello@practicalplants.org";
                     $this->sendMail($email_to, $email, $html, $text, $subject);
                 }
                 $users->setUserActive($user);
                 //set email confirmed and active, we trust the external authentication service
                 $auths = new Application_Model_User_Authentications();
                 //associate the users external authentication with the local user
                 $auths->addAuthentication($user, $external['provider'], $external['identity']);
                 //add local user to session
                 $authSess->user = $user->toArray();
                 //update auth identity to email address (used for local auth)
                 $auth->getStorage()->write($user->email);
                 //tell integrated services we've logged in
                 $integrations = new Application_Model_Integrations();
                 $integrations->onAuthenticate();
                 //show success message
                 $this->view->external_provider = $authSess->external['provider'];
                 return $this->render('external-user-created');
             } catch (Exception $e) {
                 //print_r($values);
                 if (!($error = $e->getMessage())) {
                     $error = 'Sorry, an error occurred while creating your account. Please try again.';
                 }
                 $this->view->message = $error;
                 return $this->render('error');
             }
         } else {
             $this->view->message = 'Cannot create user. User with the email address ' . $values['email'] . ' already exists.';
             return $this->render('error');
         }
     } else {
         if (isset($authSess->external)) {
             $props = $authSess->external['properties'];
             if (isset($props['name'])) {
                 $props['display_name'] = $props['name'];
             }
             if (isset($props['screen_name'])) {
                 $props['username'] = $props['screen_name'];
             }
             $fields = array_keys($form->getValues());
             $populate = array();
             foreach ($props as $k => $v) {
                 if (in_array($k, $fields)) {
                     $populate[$k] = $v;
                 }
             }
             $form->populate($populate);
             $this->view->form = $form;
         } else {
             throw Exception('No external authentication data.');
         }
     }
 }