/** * Осуществляет авторизацию в 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('/'); }
private function setGlobals() { // methods $this->methods = new Model_Methods(); View::set_global('methods', $this->methods); // stats $this->stats = new Model_Stats(); View::set_global('stats', $this->stats); // modules $this->redis = $this->_redis(); View::set_global('redis', $this->redis); $this->memcache = $memcache = Cache::instance('memcache'); View::set_global('memcache', $memcache); $this->session = Session::instance(); $auth = new Model_Sessions(); if ($auth->is_authorized()) { $user_id = $auth->get_user_id(); $this->user = Model_User::findByAttribute('id', $user_id); } else { $this->user = new Model_User(); } View::set_global('user', $this->user); View::set_global('auth', $auth); }