Exemple #1
0
 /**
  * Осуществляет авторизацию в github. В случае, если пользователь авторизован в первый раз - добавляет новую запись
  * в таблицу Users. Модель пользователя помещается в сессию "profile". Далее проиходит редирект на /auth/callback
  */
 public function action_github()
 {
     if ($error = $this->request->query('error_code')) {
         $this->generate_auth_error();
     }
     $gh = Oauth::instance('github');
     if ($gh->login()) {
         $profile = $gh->get_user();
         if ($profile) {
             $token = $gh->get_token();
             Cookie::set("auth_token", $token);
             $user = Model_User::findByAttribute('github_id', $profile->id);
             if ($user->is_empty()) {
                 $user = new Model_User();
                 if ($profile->name) {
                     $user->name = $profile->name;
                 } else {
                     $user->name = $profile->login;
                 }
                 $user->github_id = $profile->id;
                 $user->github_uri = $profile->login;
                 $user->photo = $profile->avatar_url;
                 if ($result = $user->save()) {
                     $inserted_id = $result[0];
                     $new_session = new Model_Sessions();
                     $new_session->save($inserted_id, $token);
                 }
             } else {
                 $new_session = new Model_Sessions();
                 if (!$new_session->get_user_id($token)) {
                     $new_session->save($user->id, $token);
                 }
             }
         }
     } else {
     }
     $this->auth_callback('/');
 }