コード例 #1
0
 public function action_register()
 {
     $user_hash = Session::get('ninjauth.user');
     $authentication = Session::get('ninjauth.authentication');
     // Working with what?
     $strategy = Strategy::forge($authentication['provider']);
     $full_name = Input::post('full_name') ?: Arr::get($user_hash, 'name');
     $username = Input::post('username') ?: Arr::get($user_hash, 'nickname');
     $email = Input::post('email') ?: Arr::get($user_hash, 'email');
     $password = Input::post('password');
     if ($username and $full_name and $email and $password) {
         $user_id = $strategy->adapter->create_user(array('username' => $username, 'email' => $email, 'full_name' => $full_name, 'password' => $password));
         if ($user_id) {
             Model_Authentication::forge(array('user_id' => $user_id, 'provider' => $authentication['provider'], 'uid' => $authentication['uid'], 'access_token' => $authentication['access_token'], 'secret' => $authentication['secret'], 'refresh_token' => $authentication['refresh_token'], 'expires' => $authentication['expires'], 'created_at' => time()))->save();
             Session::set_flash('ninjauth.user_id', $user_id);
             Response::redirect(static::$registered_redirect);
         }
     }
     return View::forge('register', array('user' => (object) compact('username', 'full_name', 'email', 'password')));
 }
コード例 #2
0
ファイル: auth.php プロジェクト: nobuhiko/mylogbook
 public function action_register()
 {
     $user_hash = Session::get('ninjauth.user');
     $authentication = Session::get('ninjauth.authentication');
     $strategy = \NinjAuth\Strategy::forge($authentication['provider']);
     $email = Input::post('email') ?: Arr::get($user_hash, 'email');
     $val = Validation::forge();
     $val->add('email', 'email')->add_rule('required')->add_rule('valid_email');
     if (Input::method() != 'POST' || $val->run() === false) {
         return View::forge('register', array('user' => (object) compact('email'), 'error' => $val->error('email')));
     }
     // todo トランザクション
     $user_id = $strategy->adapter->create_user(array('username' => Arr::get($user_hash, 'nickname'), 'email' => $email));
     if ($user_id) {
         \NinjAuth\Model_Authentication::forge(array('user_id' => $user_id, 'provider' => $authentication['provider'], 'uid' => $authentication['uid'], 'access_token' => $authentication['access_token'], 'secret' => $authentication['secret'], 'refresh_token' => $authentication['refresh_token'], 'expires' => $authentication['expires'], 'created_at' => time()))->save();
         Model_Profile::forge(array('full_name' => Arr::get($user_hash, 'name'), 'image' => Arr::get($user_hash, 'image'), 'location' => Arr::get($user_hash, 'location'), 'description' => Arr::get($user_hash, 'description'), 'website' => Arr::get($user_hash, 'urls.Website'), 'twitter' => Arr::get($user_hash, 'urls.Twitter'), 'user_id' => $user_id))->save();
         Session::set_flash('ninjauth.user_id', $user_id);
         Response::redirect(static::$registered_redirect);
     }
 }
コード例 #3
0
 public function login_or_register()
 {
     $token = $this->callback();
     switch ($this->name) {
         case 'oauth':
             $user_hash = $this->provider->get_user_info($this->consumer, $token);
             break;
         case 'oauth2':
             $user_hash = $this->provider->get_user_info($token);
             break;
         case 'openid':
             $user_hash = $this->get_user_info($token);
             break;
         default:
             throw new Exception("Unsupported Strategy: {$this->name}");
     }
     // If there is no uid we don't know who this is
     if (empty($user_hash['uid'])) {
         throw new Exception('No uid in response from the provider, meaning we have no idea who you are.');
     }
     // UID and logged in? Just attach this authentication to a user
     if ($this->adapter->is_logged_in()) {
         $user_id = $this->adapter->get_user_id();
         $num_linked = count(Model_Authentication::find_one_by_user_id($user_id));
         // Allowed multiple providers, or not authed yet?
         if ($num_linked === 0 or Config::get('ninjauth.link_multiple_providers') === true) {
             // Attach this account to the logged in user
             Model_Authentication::forge(array('user_id' => $user_id, 'provider' => $this->provider->name, 'uid' => $user_hash['uid'], 'access_token' => isset($token->access_token) ? $token->access_token : null, 'secret' => isset($token->secret) ? $token->secret : null, 'expires' => isset($token->expires) ? $token->expires : null, 'refresh_token' => isset($token->refresh_token) ? $token->refresh_token : null, 'created_at' => time()))->save();
             // Attachment went ok so we'll redirect
             return 'linked';
         } else {
             $auth = Model_Authentication::find_one_by_user_id($user_id);
             throw new AuthException(sprintf('This user is already linked to "%s".', $auth->provider));
         }
     } elseif ($authentication = Model_Authentication::find(array('where' => array('uid' => $user_hash['uid'], 'provider' => $this->provider->name)))) {
         // Force a login with this username
         $authentication = current($authentication);
         if ($this->adapter->force_login((int) $authentication->user_id)) {
             // credentials ok, go right in
             return 'logged_in';
         }
         throw new AuthException('This user could not be logged in.');
     } else {
         // Did the provider return enough information to log the user in?
         if ($this->adapter->can_auto_login($user_hash)) {
             // Make a user with what we have (password is made for them)
             $user_id = $this->adapter->create_user($user_hash);
             // Attach this authentication to the new user
             $saved = Model_Authentication::forge(array('user_id' => $user_id, 'provider' => $this->provider->name, 'uid' => $user_hash['uid'], 'access_token' => isset($token->access_token) ? $token->access_token : null, 'secret' => isset($token->secret) ? $token->secret : null, 'expires' => isset($token->expires) ? $token->expires : null, 'refresh_token' => isset($token->refresh_token) ? $token->refresh_token : null, 'created_at' => time()))->save();
             // Force a login with this users id
             if ($saved and $this->adapter->force_login((int) $user_id)) {
                 // credentials ok, go right in
                 return 'registered';
             }
             exit('We tried automatically creating a user but that just really did not work. Not sure why...');
         } else {
             Session::set('ninjauth', array('user' => $user_hash, 'authentication' => array('provider' => $this->provider->name, 'uid' => $user_hash['uid'], 'access_token' => isset($token->access_token) ? $token->access_token : null, 'secret' => isset($token->secret) ? $token->secret : null, 'expires' => isset($token->expires) ? $token->expires : null, 'refresh_token' => isset($token->refresh_token) ? $token->refresh_token : null)));
             return 'register';
         }
     }
 }