diff --git a/common/config/main.php b/common/config/main.php index d484172..a288bcf 100644 --- a/common/config/main.php +++ b/common/config/main.php @@ -40,6 +40,125 @@ return [ 'authManager' => [ 'class' => 'yii\rbac\DbManager', ], + + //подключаем конфигурации API соц сетей для авторизации + + 'eauth' => [ + 'class' => 'nodge\eauth\EAuth', + 'popup' => true, // Use the popup window instead of redirecting. + 'cache' => false, // Cache component name or false to disable cache. Defaults to 'cache' on production environments. + 'cacheExpire' => 0, // Cache lifetime. Defaults to 0 - means unlimited. + 'httpClient' => [ + // uncomment this to use streams in safe_mode + //'useStreamsFallback' => true, + ], + 'services' => [ // You can change the providers and their classes. + 'google' => [ + // register your app here: https://code.google.com/apis/console/ + 'class' => 'nodge\eauth\services\GoogleOAuth2Service', + 'clientId' => 'artbox-1138', + 'clientSecret' => '', + 'title' => 'Google', + ], + 'twitter' => [ + // register your app here: https://dev.twitter.com/apps/new + 'class' => 'nodge\eauth\services\TwitterOAuth1Service', + 'key' => '8vReLxI63vTs98MBMqhvrszwy', + 'secret' => 'jOqNbHIkQw4cVKKJkgrMtaEeCcfbeT1GTik4pF6O9D7AmqcwOG', + ], + 'yandex' => [ + // register your app here: https://oauth.yandex.ru/client/my + 'class' => 'nodge\eauth\services\YandexOAuth2Service', + 'clientId' => 'ea13195ac0424ff8a190838bec41bb71', + 'clientSecret' => '911f2c9afcbf4f5f9319b3134c096c86', + 'title' => 'Yandex', + ], + 'facebook' => [ + // register your app here: https://developers.facebook.com/apps/ + 'class' => 'nodge\eauth\services\FacebookOAuth2Service', + 'clientId' => '1642047622727997', + 'clientSecret' => 'f5b7ba4f062a568678b764fc74cc416e', + ], + 'yahoo' => [ + 'class' => 'nodge\eauth\services\YahooOpenIDService', + //'realm' => '*.example.org', // your domain, can be with wildcard to authenticate on subdomains. + ], + 'linkedin' => [ + // register your app here: https://www.linkedin.com/secure/developer + 'class' => 'nodge\eauth\services\LinkedinOAuth1Service', + 'key' => '77s41eixn3dyvo', + 'secret' => '1xLZQ7RRK6RNjo4U', + 'title' => 'LinkedIn (OAuth1)', + ], + 'linkedin_oauth2' => [ + // register your app here: https://www.linkedin.com/secure/developer + 'class' => 'nodge\eauth\services\LinkedinOAuth2Service', + 'clientId' => '77s41eixn3dyvo', + 'clientSecret' => '1xLZQ7RRK6RNjo4U', + 'title' => 'LinkedIn (OAuth2)', + ], + 'github' => [ + // register your app here: https://github.com/settings/applications + 'class' => 'nodge\eauth\services\GitHubOAuth2Service', + 'clientId' => 'd00283b5cfb225cd1600', + 'clientSecret' => 'f482361fad7184819d452f421c8b09db60830b42', + ], + 'live' => [ + // register your app here: https://account.live.com/developers/applications/index + 'class' => 'nodge\eauth\services\LiveOAuth2Service', + 'clientId' => '00000000481796AE', + 'clientSecret' => 'rt9GiJrlKz3sE6CvdOeuwWyYbl1tQT03', + ], + 'steam' => [ + 'class' => 'nodge\eauth\services\SteamOpenIDService', + //'realm' => '*.example.org', // your domain, can be with wildcard to authenticate on subdomains. + ], + 'instagram' => [ + // register your app here: https://instagram.com/developer/register/ + 'class' => 'nodge\eauth\services\InstagramOAuth2Service', + 'clientId' => '...', + 'clientSecret' => '...', + ], + 'vkontakte' => [ + // register your app here: https://vk.com/editapp?act=create&site=1 + 'class' => 'nodge\eauth\services\VKontakteOAuth2Service', + 'clientId' => '5155388', + 'clientSecret' => 'jxgmdGVQw7huGKRpnX3a', + ], + 'mailru' => [ + // register your app here: http://api.mail.ru/sites/my/add + 'class' => 'nodge\eauth\services\MailruOAuth2Service', + 'clientId' => '739322', + 'clientSecret' => 'd6ce7be6ff791375adff58fe0e4460b2', + ], + 'odnoklassniki' => [ + // register your app here: http://dev.odnoklassniki.ru/wiki/pages/viewpage.action?pageId=13992188 + // ... or here: http://www.odnoklassniki.ru/dk?st.cmd=appsInfoMyDevList&st._aid=Apps_Info_MyDev + 'class' => 'nodge\eauth\services\OdnoklassnikiOAuth2Service', + 'clientId' => '...', + 'clientSecret' => '...', + 'clientPublic' => '...', + 'title' => 'Odnoklas.', + ], + ], + ], + + // (optionally) you can configure logging + 'log' => [ + 'targets' => [ + [ + 'class' => 'yii\log\FileTarget', + 'logFile' => '@app/runtime/logs/eauth.log', + 'categories' => ['nodge\eauth\*'], + 'logVars' => [], + ], + ], + ], + + /*========End======= + *end api sicial + * */ + ], 'language' => 'ru-RU' ]; diff --git a/common/models/LoginForm.php b/common/models/LoginForm.php index afc1c23..eee2366 100644 --- a/common/models/LoginForm.php +++ b/common/models/LoginForm.php @@ -1,78 +1,64 @@ hasErrors()) { - $user = $this->getUser(); - if (!$user || !$user->validatePassword($this->password)) { - $this->addError($attribute, 'Incorrect username or password.'); - } + $user = $this->getUser(); + if (!$user || !$user->validatePassword($this->password)) { + $this->addError('password', 'Incorrect username or password.'); } } - /** * Logs in a user using the provided username and password. - * * @return boolean whether the user is logged in successfully */ public function login() { if ($this->validate()) { - return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0); + return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0); } else { return false; } } - /** * Finds user by [[username]] * * @return User|null */ - protected function getUser() + private function getUser() { - if ($this->_user === null) { + if ($this->_user === false) { $this->_user = User::findByUsername($this->username); } - return $this->_user; } -} +} \ No newline at end of file diff --git a/common/models/User.php b/common/models/User.php index 21ec7ac..720697a 100644 --- a/common/models/User.php +++ b/common/models/User.php @@ -24,8 +24,13 @@ use developeruz\db_rbac\interfaces\UserRbacInterface; */ class User extends ActiveRecord implements IdentityInterface, UserRbacInterface { + /** + * @var array EAuth attributes + */ + const STATUS_DELETED = 0; const STATUS_ACTIVE = 10; + public $profile; /** * @inheritdoc @@ -59,10 +64,36 @@ class User extends ActiveRecord implements IdentityInterface, UserRbacInterface /** * @inheritdoc */ - public static function findIdentity($id) - { - return static::findOne(['id' => $id, 'status' => self::STATUS_ACTIVE]); - } + public static function findIdentity($id) { + if (Yii::$app->getSession()->has('user-'.$id)) { + return new self(Yii::$app->getSession()->get('user-'.$id)); + } + else { + return isset(self::$users[$id]) ? new self(self::$users[$id]) : null; + } + } + /** + * @param \nodge\eauth\ServiceBase $service + * @return User + * @throws ErrorException + */ + public static function findByEAuth($service) { + if (!$service->getIsAuthenticated()) { + throw new ErrorException('EAuth user should be authenticated before creating identity.'); + } + $id = $service->getServiceName().'-'.$service->getId(); + $attributes = array( + 'id' => $id, + 'username' => $service->getAttribute('name'), + 'authKey' => md5($id), + 'profile' => $service->getAttributes(), + ); + $attributes['profile']['service'] = $service->getServiceName(); + Yii::$app->getSession()->set('user-'.$id, $attributes); + return new self($attributes); + } + + public $authKey; /** * @inheritdoc diff --git a/composer.json b/composer.json index 55b907d..a2cf6a9 100644 --- a/composer.json +++ b/composer.json @@ -21,7 +21,8 @@ "dmstr/yii2-adminlte-asset": "2.*", "yiisoft/yii2-jui": "^2.0", "kartik-v/yii2-widget-select2": "@dev", - "developeruz/yii2-db-rbac": "*" + "developeruz/yii2-db-rbac": "*", + "nodge/yii2-eauth": "*" }, "require-dev": { "yiisoft/yii2-codeception": "*", diff --git a/composer.lock b/composer.lock index 603df36..2f55525 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "52ade047603d8461342b32b7f76785af", - "content-hash": "35117bfe670ed414624b171df746a308", + "hash": "6b310fccece1d1eddc11db43624c06ab", "packages": [ { "name": "almasaeed2010/adminlte", @@ -461,16 +460,16 @@ }, { "name": "dmstr/yii2-adminlte-asset", - "version": "2.2.3", + "version": "2.2.4", "source": { "type": "git", "url": "https://github.com/dmstr/yii2-adminlte-asset.git", - "reference": "45cffbfe90d79f3c405b4cc98f7f318ae372bb10" + "reference": "c842a15ceef4e903f70ac927ec3246e6d53e1148" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/dmstr/yii2-adminlte-asset/zipball/45cffbfe90d79f3c405b4cc98f7f318ae372bb10", - "reference": "45cffbfe90d79f3c405b4cc98f7f318ae372bb10", + "url": "https://api.github.com/repos/dmstr/yii2-adminlte-asset/zipball/c842a15ceef4e903f70ac927ec3246e6d53e1148", + "reference": "c842a15ceef4e903f70ac927ec3246e6d53e1148", "shasum": "" }, "require": { @@ -511,7 +510,7 @@ "extension", "yii2" ], - "time": "2015-10-07 21:31:26" + "time": "2015-11-06 10:35:36" }, { "name": "ezyang/htmlpurifier", @@ -559,16 +558,16 @@ }, { "name": "kartik-v/yii2-krajee-base", - "version": "v1.7.7", + "version": "v1.7.8", "source": { "type": "git", "url": "https://github.com/kartik-v/yii2-krajee-base.git", - "reference": "c0adff9d9762f4fd3bf0e7cd0000fcab0bf00f19" + "reference": "f90b9ff515ced4e40ff5ec8156a5c9f7f51e2a7f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/kartik-v/yii2-krajee-base/zipball/c0adff9d9762f4fd3bf0e7cd0000fcab0bf00f19", - "reference": "c0adff9d9762f4fd3bf0e7cd0000fcab0bf00f19", + "url": "https://api.github.com/repos/kartik-v/yii2-krajee-base/zipball/f90b9ff515ced4e40ff5ec8156a5c9f7f51e2a7f", + "reference": "f90b9ff515ced4e40ff5ec8156a5c9f7f51e2a7f", "shasum": "" }, "require": { @@ -601,7 +600,7 @@ "widget", "yii2" ], - "time": "2015-06-16 05:19:57" + "time": "2015-11-22 06:35:23" }, { "name": "kartik-v/yii2-widget-select2", @@ -652,17 +651,178 @@ "time": "2015-09-22 03:10:57" }, { + "name": "lusitanian/oauth", + "version": "v0.3.6", + "source": { + "type": "git", + "url": "https://github.com/Lusitanian/PHPoAuthLib.git", + "reference": "4ce8c488971410233eb3b1e6d9ac4e81debb41d5" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Lusitanian/PHPoAuthLib/zipball/4ce8c488971410233eb3b1e6d9ac4e81debb41d5", + "reference": "4ce8c488971410233eb3b1e6d9ac4e81debb41d5", + "shasum": "" + }, + "require": { + "php": ">=5.3.0" + }, + "require-dev": { + "phpunit/phpunit": "3.7.*", + "predis/predis": "0.8.*@dev", + "symfony/http-foundation": "~2.1" + }, + "suggest": { + "ext-openssl": "Allows for usage of secure connections with the stream-based HTTP client.", + "predis/predis": "Allows using the Redis storage backend.", + "symfony/http-foundation": "Allows using the Symfony Session storage backend." + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "0.1-dev" + } + }, + "autoload": { + "psr-0": { + "OAuth": "src", + "OAuth\\Unit": "tests" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "David Desberg", + "email": "david@daviddesberg.com" + }, + { + "name": "Pieter Hordijk", + "email": "info@pieterhordijk.com" + } + ], + "description": "PHP 5.3+ oAuth 1/2 Library", + "keywords": [ + "Authentication", + "authorization", + "oauth", + "security" + ], + "time": "2015-09-09 06:43:02" + }, + { + "name": "nodge/lightopenid", + "version": "1.1.2", + "source": { + "type": "git", + "url": "https://github.com/Nodge/LightOpenID.git", + "reference": "a5492cc0c932c557b7e9b54a6e5bbd85cc5fa041" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Nodge/LightOpenID/zipball/a5492cc0c932c557b7e9b54a6e5bbd85cc5fa041", + "reference": "a5492cc0c932c557b7e9b54a6e5bbd85cc5fa041", + "shasum": "" + }, + "require": { + "php": ">=5.2" + }, + "type": "library", + "autoload": { + "classmap": [ + "openid.php", + "provider/provider.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT License" + ], + "authors": [ + { + "name": "Mewp", + "homepage": "http://code.google.com/p/lightopenid/" + }, + { + "name": "Ignat Ignatov", + "homepage": "https://github.com/iignatov/LightOpenID" + } + ], + "description": "Lightweight PHP5 library for easy OpenID authentication.", + "homepage": "https://github.com/Nodge/LightOpenID", + "keywords": [ + "Authentication", + "OpenId" + ], + "time": "2013-08-31 16:48:56" + }, + { + "name": "nodge/yii2-eauth", + "version": "2.3.0", + "source": { + "type": "git", + "url": "https://github.com/Nodge/yii2-eauth.git", + "reference": "25963e78c6083734736187bff992dd9da60fd125" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Nodge/yii2-eauth/zipball/25963e78c6083734736187bff992dd9da60fd125", + "reference": "25963e78c6083734736187bff992dd9da60fd125", + "shasum": "" + }, + "require": { + "lib-curl": "*", + "lusitanian/oauth": "~0.3.0", + "nodge/lightopenid": "~1.1.0", + "php": ">=5.4.0", + "yiisoft/yii2": "*" + }, + "type": "yii2-extension", + "extra": { + "bootstrap": "nodge\\eauth\\Bootstrap" + }, + "autoload": { + "psr-4": { + "nodge\\eauth\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "New BSD License" + ], + "authors": [ + { + "name": "Maxim Zemskov", + "email": "nodge@yandex.ru", + "homepage": "http://nodge.ru/" + } + ], + "description": "Yii2 EAuth Extension. EAuth allows to authenticate users with accounts on other websites (Google, Facebook, Twitter, etc).", + "homepage": "https://github.com/Nodge/yii2-eauth", + "keywords": [ + "Authentication", + "OpenId", + "eauth", + "extension", + "oauth", + "yii2" + ], + "time": "2015-10-17 16:51:17" + }, + { "name": "rmrevin/yii2-fontawesome", - "version": "2.12.0", + "version": "2.12.2", "source": { "type": "git", "url": "https://github.com/rmrevin/yii2-fontawesome.git", - "reference": "51d33ec9d1d7edab2166f1d42668c1bc8d28c068" + "reference": "62308687477734c166753de0ad9e2333dc7dce32" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/rmrevin/yii2-fontawesome/zipball/51d33ec9d1d7edab2166f1d42668c1bc8d28c068", - "reference": "51d33ec9d1d7edab2166f1d42668c1bc8d28c068", + "url": "https://api.github.com/repos/rmrevin/yii2-fontawesome/zipball/62308687477734c166753de0ad9e2333dc7dce32", + "reference": "62308687477734c166753de0ad9e2333dc7dce32", "shasum": "" }, "require": { @@ -688,9 +848,9 @@ ], "authors": [ { - "name": "Roman Revin", - "email": "xgismox@gmail.com", - "homepage": "http://rmrevin.ru/" + "name": "Revin Roman", + "email": "roman@rmrevin.com", + "homepage": "https://rmrevin.com/" } ], "description": "Asset Bundle for Yii2 with Font Awesome", @@ -701,7 +861,7 @@ "font", "yii" ], - "time": "2015-08-15 16:50:16" + "time": "2015-11-20 09:17:45" }, { "name": "swiftmailer/swiftmailer", diff --git a/frontend/controllers/SiteController.php b/frontend/controllers/SiteController.php index f66584a..bd77425 100644 --- a/frontend/controllers/SiteController.php +++ b/frontend/controllers/SiteController.php @@ -16,7 +16,8 @@ use yii\filters\VerbFilter; use yii\filters\AccessControl; use frontend\models\OptionsToValues; use yii\validators\EmailValidator; - +use common\models\User; +use yii\helpers\VarDumper; use common\models\Page; /** @@ -52,6 +53,11 @@ class SiteController extends Controller 'logout' => ['post'], ], ], + 'eauth' => [ + // required to disable csrf validation on OpenID requests + 'class' => \nodge\eauth\openid\ControllerBehavior::className(), + 'only' => ['login'], + ], ]; } @@ -94,6 +100,42 @@ class SiteController extends Controller */ public function actionLogin() { + + $serviceName = Yii::$app->getRequest()->getQueryParam('service'); + if (isset($serviceName)) { + /** @var $eauth \nodge\eauth\ServiceBase */ + $eauth = Yii::$app->get('eauth')->getIdentity($serviceName); + $eauth->setRedirectUrl(Yii::$app->getUser()->getReturnUrl()); + $eauth->setCancelUrl(Yii::$app->getUrlManager()->createAbsoluteUrl('site/login')); + + try { + if ($eauth->authenticate()) { +// var_dump($eauth->getIsAuthenticated(), $eauth->getAttributes()); exit; + + $identity = User::findByEAuth($eauth); + Yii::$app->getUser()->login($identity); + + // special redirect with closing popup window + $eauth->redirect(); + } + else { + // close popup window and redirect to cancelUrl + $eauth->cancel(); + } + } + catch (\nodge\eauth\ErrorException $e) { + // save error to show it later + Yii::$app->getSession()->setFlash('error', 'EAuthException: '.$e->getMessage()); + + // close popup window and redirect to cancelUrl +// $eauth->cancel(); + $eauth->redirect($eauth->getCancelUrl()); + } + } + + + + if (!\Yii::$app->user->isGuest) { return $this->goHome(); } @@ -126,7 +168,13 @@ class SiteController extends Controller * @return mixed */ public function actionContact() - { + { + + //Yii::$app->user->logout(); + $identity = Yii::$app->getUser()->getIdentity(); + var_dump($identity[profile]); + die(); + $model = new ContactForm(); if ($model->load(Yii::$app->request->post()) && $model->validate()) { if ($model->sendEmail(Yii::$app->params['adminEmail'])) { @@ -217,6 +265,7 @@ class SiteController extends Controller $option_list = $option_model->find()->where(1)->all(); $option_values = array(); $post = \Yii::$app->request->post(); + if(!empty(\Yii::$app->request->post())) { $options_to_values = array(); $hasErrors = false; diff --git a/frontend/views/site/login.php b/frontend/views/site/login.php index fe67ee0..746a527 100644 --- a/frontend/views/site/login.php +++ b/frontend/views/site/login.php @@ -1,39 +1,33 @@ title = 'Login'; $this->params['breadcrumbs'][] = $this->title; ?> -
Please fill out the following fields to login:
- -Do you already have an account on one of these sites? Click the logo to log in with it here:
+ 'site/login')); ?> +Please fill out the following fields to login:
- -