Esempio n. 1
0
 /**
  * Allow the user to login and register using a 3rd party provider.
  */
 function action_provider_return()
 {
     $provider_name = $this->request->param('provider');
     $provider = Provider::factory($provider_name);
     if (!is_object($provider)) {
         Message::add('error', 'Provider is not enabled; please select another provider or log in normally.');
         $this->redirect('user/login');
         return;
     }
     // verify the request
     if ($provider->verify()) {
         // check for previously connected user
         $uid = $provider->user_id();
         $user_identity = ORM::factory('User_Identity')->where('provider', '=', $provider_name)->and_where('identity', '=', $uid)->find();
         if ($user_identity->loaded()) {
             $user = $user_identity->user;
             if ($user->loaded() && $user->id == $user_identity->user_id && is_numeric($user->id)) {
                 // found, log user in
                 Auth::instance()->force_login($user);
                 // redirect to the user account
                 $this->redirect('user/profile');
                 return;
             }
         }
         // create new account
         if (!Auth::instance()->logged_in()) {
             // Instantiate a new user
             $user = ORM::factory('User');
             // fill in values
             // generate long random password (maximum that passes validation is 42 characters)
             $password = $user->generate_password(42);
             $values = array('username' => $user->generate_username(str_replace(' ', '.', $provider->name())), 'password' => $password, 'password_confirm' => $password);
             if (Valid::email($provider->email(), TRUE)) {
                 $values['email'] = $provider->email();
             }
             try {
                 // If the post data validates using the rules setup in the user model
                 $user->create_user($values, array('username', 'password', 'email'));
                 // Add the login role to the user (add a row to the db)
                 $login_role = new Model_Role(array('name' => 'login'));
                 $user->add('roles', $login_role);
                 // create user identity after we have the user id
                 $user_identity = ORM::factory('User_Identity');
                 $user_identity->user_id = $user->id;
                 $user_identity->provider = $provider_name;
                 $user_identity->identity = $provider->user_id();
                 $user_identity->save();
                 // sign the user in
                 Auth::instance()->login($values['username'], $password);
                 // redirect to the user account
                 $this->redirect('user/profile');
             } catch (ORM_Validation_Exception $e) {
                 if ($provider_name == 'twitter') {
                     Message::add('error', 'The Twitter API does not support retrieving your email address; you will have to enter it manually.');
                 } else {
                     Message::add('error', 'We have successfully retrieved some of the data from your other account, but we were unable to get all the required fields. Please complete form below to register an account.');
                 }
                 // in case the data for some reason fails, the user will still see something sensible:
                 // the normal registration form.
                 $view = View::factory('user/register');
                 $errors = $e->errors('register');
                 // Move external errors to main array, for post helper compatibility
                 $errors = array_merge($errors, isset($errors['_external']) ? $errors['_external'] : array());
                 $view->set('errors', $errors);
                 // Pass on the old form values
                 $values['password'] = $values['password_confirm'] = '';
                 $view->set('defaults', $values);
                 if (Kohana::$config->load('useradmin')->captcha) {
                     // FIXME: Is this the best place to include and use recaptcha?
                     include Kohana::find_file('vendor', 'recaptcha/recaptchalib');
                     $recaptcha_config = Kohana::$config->load('recaptcha');
                     $recaptcha_error = null;
                     $view->set('captcha_enabled', true);
                     $view->set('recaptcha_html', recaptcha_get_html($recaptcha_config['publickey'], $recaptcha_error));
                 }
                 $this->template->content = $view;
             }
         } else {
             Message::add('error', 'You are logged in, but the email received from the provider does not match the email associated with your account.');
             $this->redirect('user/profile');
         }
     } else {
         Message::add('error', 'Retrieving information from the provider failed. Please register below.');
         $this->redirect('user/register');
     }
 }
Esempio n. 2
0
 /**
  * Allow the user to login and register using a 3rd party provider.
  */
 function action_provider_return()
 {
     $provider_name = $this->request->param('provider');
     $provider = Provider::factory($provider_name);
     if (!is_object($provider)) {
         Message::add('error', __('provider.not.enabled.select.different.or.login'));
         $this->request->redirect('user/login');
         return;
     }
     // verify the request
     if ($provider->verify()) {
         // check for previously connected user
         $uid = $provider->user_id();
         $user_identity = ORM::factory('user_identity')->where('provider', '=', $provider_name)->and_where('identity', '=', $uid)->find();
         if ($user_identity->loaded()) {
             $user = $user_identity->user;
             if ($user->loaded() && $user->id == $user_identity->user_id && is_numeric($user->id)) {
                 // found, log user in
                 Auth::instance()->force_login($user);
                 // redirect to the user account
                 $this->request->redirect(Session::instance()->get_once('returnUrl', 'user/profile'));
                 return;
             }
         }
         // If register is disabled, don't create new account
         if (!Kohana::$config->load('useradmin.register_enabled')) {
             $this->request->redirect('user/login');
         }
         // create new account
         if (!Auth::instance()->logged_in()) {
             /** @var $user Useradmin_Model_User */
             $user = ORM::factory('user');
             // fill in values
             // generate long random password (maximum that passes validation is 42 characters)
             $password = $user->generate_password(42);
             $values = array('username' => $user->generate_username(str_replace(' ', '.', $provider->name())), 'password' => $password, 'password_confirm' => $password);
             if (Valid::email($provider->email(), TRUE)) {
                 $values['email'] = $provider->email();
             }
             try {
                 // If the post data validates using the rules setup in the user model
                 $user->create_user($values, $this->user_model_fields);
                 // Add the login role to the user (add a row to the db)
                 $login_role = new Model_Role(array('name' => 'login'));
                 $user->add('roles', $login_role);
                 // create user identity after we have the user id
                 $user_identity = ORM::factory('user_identity');
                 $user_identity->user_id = $user->id;
                 $user_identity->provider = $provider_name;
                 $user_identity->identity = $provider->user_id();
                 $user_identity->save();
                 // sign the user in
                 Auth::instance()->login($values['username'], $password);
                 // redirect to the user account
                 $this->request->redirect(Session::instance()->get_once('returnUrl', 'user/profile'));
             } catch (ORM_Validation_Exception $e) {
                 /*
                  * Redirect back to the front page in case they
                  * try to create another account with a separate provider
                  */
                 Message::add('error', 'A matching account already exists with another provider. Please select another login or registration method.');
                 $this->request->redirect('user/login');
                 if ($provider_name == 'twitter') {
                     Message::add('error', __('twitter.no.email.retrive.support'));
                 } else {
                     Message::add('error', 'please.complete.data.from.other.account');
                 }
                 // in case the data for some reason fails, the user will still see something sensible:
                 // the normal registration form.
                 $view = View::factory('user/register');
                 $errors = $e->errors('register');
                 // Move external errors to main array, for post helper compatibility
                 $errors = array_merge($errors, isset($errors['_external']) ? $errors['_external'] : array());
                 $view->set('errors', $errors);
                 // Pass on the old form values
                 $values['password'] = $values['password_confirm'] = '';
                 $view->set('defaults', $values);
                 if (Kohana::$config->load('useradmin')->captcha) {
                     // FIXME: Is this the best place to include and use recaptcha?
                     include Kohana::find_file('vendor', 'recaptcha/recaptchalib');
                     $recaptcha_config = Kohana::$config->load('recaptcha');
                     $recaptcha_error = null;
                     $view->set('captcha_enabled', true);
                     $view->set('recaptcha_html', recaptcha_get_html($recaptcha_config['publickey'], $recaptcha_error));
                 }
                 $this->template->content = $view;
             }
         } else {
             Message::add('error', __('logged.in.but.account.emails.do.not.match'));
             $this->request->redirect('user/profile');
         }
     } else {
         Message::add('error', __('retrieving.info.from.provider.failed.register.below'));
         $this->request->redirect('user/register');
     }
 }