diff --git a/db/migrations/stored/R__functions.sql b/db/migrations/stored/R__functions.sql index 812979ff1067f8475da94ca27d9d252f3192d61b..095c16ccabb539bd94db43b76790185dcbefa4db 100644 --- a/db/migrations/stored/R__functions.sql +++ b/db/migrations/stored/R__functions.sql @@ -2232,6 +2232,26 @@ BEGIN RETURN vAccountID; END // +DROP FUNCTION IF EXISTS SignInByOpenID// +CREATE FUNCTION SignInByOpenID ( + pGlobalKey VARCHAR(255) CHARSET utf8 +) RETURNS int(11) # account id +NO SQL + BEGIN + DECLARE vAccountID INT DEFAULT -1; + + #check account existence + SELECT accounts.ID INTO vAccountID FROM accounts + WHERE accounts.globalKey1C = pGlobalKey + LIMIT 1; + IF vAccountID <= 0 THEN + RETURN -1; + END IF; + + INSERT INTO logs_signin (AccountID) VALUES (vAccountID); + RETURN vAccountID; + END // + -- returns: -- -1 : invalid token -- >0 : accountID diff --git a/db/migrations/structure/V13_8__add_external_1C_key.sql b/db/migrations/structure/V13_8__add_external_1C_key.sql new file mode 100644 index 0000000000000000000000000000000000000000..7f585ec490bfe8c66ce3274c1d996d435277e2f3 --- /dev/null +++ b/db/migrations/structure/V13_8__add_external_1C_key.sql @@ -0,0 +1,6 @@ +-- +-- Добавление идентификатора физ.лица из 1С в таблицу `accounts` +-- + +ALTER TABLE `accounts` + ADD COLUMN `globalKey1C` varchar(30) CHARACTER SET utf8 AFTER `ID`; \ No newline at end of file diff --git a/~dev_rating/application/classes/Controller/Handler/Sign.php b/~dev_rating/application/classes/Controller/Handler/Sign.php index 9a6e9dd39db528d2caf6c75f396ca4b114d92729..29df4f7bb128a1534f165f6bc2e3fe75a2c37d65 100644 --- a/~dev_rating/application/classes/Controller/Handler/Sign.php +++ b/~dev_rating/application/classes/Controller/Handler/Sign.php @@ -83,22 +83,23 @@ class Controller_Handler_Sign extends Controller_Handler public function action_openidfinish() { - if ($_GET["openid_sreg_student"] !== '1') { - $this->fail('К сожалению, личный кабинет сотрудника пока что находится в разработке. Следите за новостями!'); - } - if ($_GET['openid_mode'] == 'id_res') { $openid = new OpenID; $openid->SetIdentity($_GET['openid_identity']); $openid_validation_result = $openid->ValidateWithServer(); - if ($openid_validation_result == true) { + if ($openid_validation_result == true) + { $this->openid_signIn(); - } else if ($openid->IsError() == true) { + } + else if ($openid->IsError() == true) + { $error = $openid->GetError(); echo 'Ошибка!'; echo "[" . $error['code'] . "]: " . $error['description']; - } else { + } + else + { echo 'Ошибка!'; echo "При авторизации что-то пошло не так. Попробуете снова?"; } @@ -110,14 +111,14 @@ class Controller_Handler_Sign extends Controller_Handler private function openid_signIn() { - $flags = [ - 'isStudent' => $_GET["openid_sreg_student"], - 'isStaff' => $_GET["openid_sreg_staff"] - ]; - - echo 'Добро пожаловать!'; -// $this->redirect_url = Route::url('evaluation:student'); -// -// Account::signIn($_GET["openid_sig"], $_GET["openid_sreg_r61globalkey"], $flags); + $ok = User::instance()->signInByOpenID($_GET["openid_sreg_r61globalkey"]); + + if (!$ok) $this->fail(); + $this->redirect('/'); + + if ($this->user->isSignedIn()) + { + $this->redirect('/'); + } } } diff --git a/~dev_rating/application/classes/Model/Account.php b/~dev_rating/application/classes/Model/Account.php index 47fdd98732c46e1120b607cd01c616b1cd8151a7..7739324d5cd5a284e8e96e04f50a6a683ad067a3 100644 --- a/~dev_rating/application/classes/Model/Account.php +++ b/~dev_rating/application/classes/Model/Account.php @@ -110,6 +110,13 @@ class Model_Account extends Model ->execute()->get('ID'); } + public static function checkAuthOpenID($globalKey) { + $sql = 'SELECT `SignInByOpenID`(:key) AS `ID`'; + return DB::query(Database::SELECT, $sql) + ->param(':key', $globalKey) + ->execute()->get('ID'); + } + public static function checkAuthToken($token) { $sql = 'SELECT `SignInByToken`(:token) AS `ID`'; return DB::query(Database::SELECT, $sql) diff --git a/~dev_rating/application/classes/User.php b/~dev_rating/application/classes/User.php index 9df38f753c623e19fce0ad66ad25c9cd3d330e0a..92df3a21bce3ea451cd100d5fc7266de983979cc 100644 --- a/~dev_rating/application/classes/User.php +++ b/~dev_rating/application/classes/User.php @@ -159,6 +159,18 @@ class User implements ArrayAccess return $this->initSession($id, $this->hash($password)); } + /** + * Проверяет существования пользователя с заданным globalKey и авторизует его + * + * @param string $globalKey + * @return bool true, если авторизация прошла успешно, + * и false, если данные являются некорректными. + */ + public function signInByOpenID($globalKey) { + $id = (int) Model_Account::checkAuthOpenID($globalKey); + return $this->initSession($id, $this->hash($globalKey)); + } + public function signInByToken($token) { $id = (int) Model_Account::checkAuthToken($token); return $this->initSession($id, $this->hash($token)); diff --git a/~dev_rating/application/views/sign/inOpenID.twig b/~dev_rating/application/views/sign/inOpenID.twig index 36a99bcd7c495316c6135a485d3badfbdb2a1a7e..fb07feada15b4397901ed1cdfd93ae4f66b98e3e 100644 --- a/~dev_rating/application/views/sign/inOpenID.twig +++ b/~dev_rating/application/views/sign/inOpenID.twig @@ -3,9 +3,9 @@ {% block title %}Авторизация{% endblock %} {% block forms %} <div id='inputs'> - {{ sign.input('loginopenid', 'text', '', 'Логин OpenID', true) }} + {{ sign.input('loginopenid', 'text', '', 'Логин@sfedu.ru', true) }} </div> - {{ sign.input('signopenidin_b', 'button', 'Войти c OpenID') }} + {{ sign.input('signopenidin_b', 'button', 'Войти через OpenID.sfedu.ru') }} <div class='footer'> {{ HTML.anchor('sign/in', 'Вход через локальную учетную запись')|raw }} | {{ HTML.anchor('sign/up', 'Активировать аккаунт')|raw }} | {{ HTML.anchor('remind', 'Забыли пароль?')|raw }} </div>