-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from neSpecc/deploy
Deploy
- Loading branch information
Showing
15 changed files
with
286 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
<?php defined('SYSPATH') OR die('No direct access allowed.'); | ||
|
||
/** | ||
* Class Controller_Auth | ||
*/ | ||
class Controller_Auth extends Controller_Base_preDispatch | ||
{ | ||
|
||
/** | ||
* Осуществляет авторизацию в ВК. В случае, если пользователь авторизован в первый раз - добавляет новую запись | ||
* в таблицу Users. Модель пользователя помещается в сессию "profile". Далее проиходит редирект на /auth/callback | ||
*/ | ||
public function action_vk() | ||
{ | ||
$vk = Oauth::instance('vkontakte'); | ||
if ($vk->login()) | ||
{ | ||
$profile = $vk->get_user(); | ||
if ($profile) | ||
{ | ||
Session::instance()->set('profile', $profile); | ||
|
||
$user = DB::select('*')->from('Users')->where('uid', '=', ":uid")->param(":uid", $profile->uid)->execute(); | ||
#$user = Model_Users::factory('Users')->where('uid', '=', ":uid")->param(":uid", $profile->uid)->find(); | ||
if (!isset($user[0])) | ||
{ | ||
DB::insert('Users', array('first_name', 'last_name', 'uid'))->values(array($profile->first_name, $profile->last_name, $profile->uid))->execute(); | ||
/*$user = Model_Users::factory('Users'); | ||
$user->uid = $profile->uid; | ||
$user->first_name = $profile->first_name; | ||
$user->last_name = $profile->last_name; | ||
$user->save(); | ||
*/ | ||
} | ||
$this->auth_callback('/'); | ||
} | ||
} | ||
else | ||
{ | ||
# Add auth error view | ||
$this->auth_callback('/'); | ||
} | ||
|
||
} | ||
|
||
/** | ||
* Деавторизует пользователя путем очищения сессии "profile". Возвращает на главную страницу. | ||
*/ | ||
public function action_logout() | ||
{ | ||
Session::instance()->delete('profile'); | ||
Controller::redirect('/'); | ||
} | ||
|
||
/** | ||
* Место для пост-авторизации. В конце осуществляет редирект страницу $page. | ||
*/ | ||
private function auth_callback($page='/') | ||
{ | ||
Controller::redirect($page); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php defined('SYSPATH') or die('No direct script access.'); | ||
|
||
class Dao_Auth extends Dao_Base | ||
{ | ||
private $profile; | ||
|
||
/** | ||
* Dao_Auth constructor. | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->profile = Session::instance()->get('profile'); | ||
} | ||
|
||
public function is_guest() | ||
{ | ||
return isset($this->profile); | ||
} | ||
|
||
public function get_profile() | ||
{ | ||
return $this->profile; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php defined('SYSPATH') OR die('No Direct Script Access'); | ||
|
||
Class Model_Users extends ORM | ||
{ | ||
protected $_table_name = 'Users'; | ||
protected $_table_columns = array( | ||
'id' => NULL, | ||
'first_name' => NULL, | ||
'last_name' => NULL, | ||
'uid' => NULL | ||
); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
defined( 'SYSPATH' ) or die( 'No direct script access.' ); | ||
|
||
return array( | ||
'vkontakte' => array( | ||
'APP_ID' => '4874439', | ||
'APP_SECRET' => 'w6FvjB8uQDFCNN8n4vCO', | ||
'SETTINGS' => NULL, | ||
'REDIRECT_URI' => 'http://'.Arr::get($_SERVER, 'SERVER_NAME').'/auth/vk', | ||
'GET_CODE_URI' => 'https://oauth.vk.com/authorize/', | ||
'GET_TOKEN_URI' => 'https://oauth.vk.com/access_token' | ||
), | ||
'odnoklassniki' => array( | ||
'APP_ID' => NULL, | ||
'APP_SECRET' => NULL, | ||
'APP_PUBLIC' => NULL, | ||
'SETTINGS' => NULL, | ||
'REDIRECT_URI' => NULL, | ||
'GET_CODE_URI' => NULL, | ||
'GET_TOKEN_URI' => NULL | ||
), | ||
'facebook' => array( | ||
'APP_ID' => NULL, | ||
'APP_SECRET' => NULL, | ||
'SETTINGS' => NULL, | ||
'REDIRECT_URI' => NULL, | ||
'GET_CODE_URI' => NULL, | ||
'GET_TOKEN_URI' => NULL | ||
), | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,19 @@ | ||
<div class="header_text"> | ||
<?php if ($auth->is_guest()): ?> | ||
Добрый день, <?= $auth->get_profile()->first_name; ?> | ||
<?php endif; ?> | ||
</div> | ||
|
||
<div class="m_logo_wrap"> | ||
<div class="m_logo"></div> | ||
<a href="/article">Статьи</a> | ||
<a href="/join">Подать заявку</a> | ||
<a href="/task">Задания</a> | ||
|
||
<?php if (!$auth->is_guest()): ?> | ||
<a href='/auth/vk'>Вход VK</a> | ||
<?php else: ?> | ||
<a href='/auth/logout'>Выход</a> | ||
<?php endif; ?> | ||
|
||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
CREATE TABLE IF NOT EXISTS `Users` ( | ||
`id` int(11) NOT NULL, | ||
`first_name` tinytext NOT NULL, | ||
`last_name` tinytext NOT NULL, | ||
`uid` int(11) NOT NULL | ||
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8; | ||
|
||
ALTER TABLE `Users` | ||
ADD PRIMARY KEY (`id`); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php defined('SYSPATH') or die('No direct script access.'); | ||
|
||
abstract class Kohana_Oauth { | ||
|
||
protected static $instance; | ||
|
||
public static function instance($service) | ||
{ | ||
$config = Kohana::$config->load('oauth'); | ||
$class = 'Oauth_' . ucfirst($service); | ||
return Kohana_Oauth::$instance = new $class($config -> get($service)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
<?php defined('SYSPATH') or die('No direct script access.'); | ||
|
||
class Kohana_Oauth_Vkontakte extends Oauth { | ||
|
||
protected static $config; | ||
|
||
public function __construct($config) | ||
{ | ||
//var_dump($config); | ||
//die(); | ||
self::$config = $config; | ||
} | ||
|
||
public function login_query() | ||
{ | ||
$params = array( | ||
'client_id' => self::$config['APP_ID'], | ||
'scope' => self::$config['SETTINGS'], | ||
'redirect_uri' => self::$config['REDIRECT_URI'], | ||
'response_type' => 'code' | ||
); | ||
return self::$config['GET_CODE_URI'].'?'.http_build_query($params); | ||
} | ||
|
||
// @param code sended at backref | ||
private function get_access_token() | ||
{ | ||
$params = Arr::get($_SERVER, 'QUERY_STRING'); | ||
|
||
// http://www.php.net/manual/ru/function.parse-str.php | ||
// parse_str($input_string, $output_array) | ||
parse_str($params, $params); | ||
|
||
// if we on 1st step -> go to authorization page | ||
if ( empty($params['code']) ) | ||
{ | ||
Controller::redirect($this->login_query()); | ||
} | ||
|
||
if (!$params) | ||
{ | ||
throw new Kohana_Exception('NO QUERY PARAMS'); | ||
} | ||
|
||
if (isset($error)) | ||
throw new Kohana_Exception('Error: '.$error.' Description: '.$error_description); | ||
$params = array( | ||
'client_id' => self::$config['APP_ID'], | ||
'code' => $params['code'], | ||
'client_secret' => self::$config['APP_SECRET'], | ||
'redirect_uri' => self::$config['REDIRECT_URI'] | ||
); | ||
$resp = Request::factory(self::$config['GET_TOKEN_URI']) | ||
->method('GET') | ||
->query($params) | ||
->execute(); | ||
|
||
$resp = json_decode($resp); | ||
if (empty($resp->access_token)) | ||
{ | ||
throw new Kohana_Exception('Error: '.$resp->error.' Description: '.$resp->error_description); | ||
} | ||
Session::instance()->set('vk_token', $resp->access_token); | ||
Session::instance()->set('vk_user_id', $resp->user_id); | ||
return true; | ||
} | ||
|
||
public function get_user() | ||
{ | ||
$vk_token = Session::instance()->get('vk_token'); | ||
$vk_user_id = Session::instance()->get('vk_user_id'); | ||
if(!$vk_token || !$vk_user_id) | ||
{ | ||
throw new Kohana_Exception('Невозможно получить токен и id'); | ||
} | ||
$params = array( | ||
'uid' => $vk_user_id, | ||
'access_token' => $vk_token | ||
); | ||
$resp = Request::factory('https://api.vk.com/method/users.get') | ||
->method('GET') | ||
->query($params) | ||
->execute(); | ||
$resp = json_decode($resp); | ||
if (isset($resp->error)) | ||
{ | ||
throw new Kohana_Exception('Error: '.$resp->error.' Description: '.$resp->error_description); | ||
} | ||
$profile = array_shift($resp->response); | ||
return $profile; | ||
} | ||
|
||
public function login() | ||
{ | ||
return $this->get_access_token(); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?php defined('SYSPATH') or die('No direct script access.'); | ||
|
||
abstract class Oauth extends Kohana_Oauth { } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?php defined('SYSPATH') or die('No direct script access.'); | ||
|
||
class Oauth_Vkontakte extends Kohana_Oauth_Vkontakte { } |