Skip to content

Instantly share code, notes, and snippets.

@f1dz
Last active November 9, 2017 08:06
Show Gist options
  • Save f1dz/82a18427be5a1e9081090bbe2ee7f10f to your computer and use it in GitHub Desktop.
Save f1dz/82a18427be5a1e9081090bbe2ee7f10f to your computer and use it in GitHub Desktop.
SSO Yii2
<?php
namespace app\components;
use yii\base\Component;
use app\models\Session as SessionModel;
/**
* Created by PhpStorm.
* User: ofid
* Date: 10/30/17
* Time: 13.55
*/
class AppSession extends Component
{
public function init(){
$session = SessionModel::findOne(['user_id' => \Yii::$app->user->id]);
if(@$session->session_id != \Yii::$app->session->id)
\Yii::$app->user->logout();
parent::init();
}
}
<?php
use yii\db\Migration;
/**
* Class m171030_064758_create_table_session
*/
class m171030_064758_create_table_session extends Migration
{
/**
* @inheritdoc
*/
public function safeUp()
{
$this->createTable('session',[
'id' => $this->primaryKey(),
'user_id' => $this->integer()->notNull(),
'session_id' => $this->string(128)->null(),
'user_ip' => $this->string(128)->null(),
'user_host' => $this->string(128)->null(),
'remote_ip' => $this->string(128)->null(),
'remote_host' => $this->string(128)->null(),
'user_agent' => $this->string(1024)->null(),
'created_at' => $this->dateTime(),
'updated_at' => $this->dateTime()
]);
}
/**
* @inheritdoc
*/
public function safeDown()
{
echo "m171030_064758_create_table_session cannot be reverted.\n";
$this->dropTable('session');
}
/*
// Use up()/down() to run migration code without a transaction.
public function up()
{
}
public function down()
{
echo "m171030_064758_create_table_session cannot be reverted.\n";
return false;
}
*/
}
<?php
namespace app\models;
use Yii;
/**
* This is the model class for table "session".
*
* @property int $id
* @property int $user_id
* @property string $session_id
* @property string $user_ip
* @property string $user_host
* @property string $remote_ip
* @property string $remote_host
* @property string $user_agent
* @property string $created_at
* @property string $updated_at
*/
class Session extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'session';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['user_id'], 'required'],
[['user_id'], 'integer'],
[['created_at', 'updated_at'], 'safe'],
[['session_id', 'user_ip', 'user_host', 'remote_ip', 'remote_host'], 'string', 'max' => 128],
[['user_agent'], 'string', 'max' => 1024],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'user_id' => 'User ID',
'session_id' => 'Session ID',
'user_ip' => 'User Ip',
'user_host' => 'User Host',
'remote_ip' => 'Remote Ip',
'remote_host' => 'Remote Host',
'user_agent' => 'User Agent',
'created_at' => 'Created At',
'updated_at' => 'Updated At',
];
}
public function afterSave($insert, $changedAttributes)
{
$log = new LogLogin();
$log->attributes = [
'user_id' => $this->user_id,
'user_ip' => $this->user_ip,
'user_host' => $this->user_host,
'remote_ip' => $this->remote_ip,
'remote_host' => $this->remote_host,
'user_agent' => $this->user_agent,
'created_at' => date('Y-m-d H:i:s'),
];
$log->save();
parent::afterSave($insert, $changedAttributes);
}
}
<?php
namespace app\controllers;
use app\models\Session;
use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\Response;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
class SiteController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['logout','index'],
'rules' => [
[
'actions' => ['logout','index'],
'allow' => true,
'roles' => ['@'],
],
],
]
];
}
/**
* @inheritdoc
*/
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
/**
* Displays homepage.
*
* @return string
*/
public function actionIndex()
{
return $this->redirect('/dashboard');
}
/**
* Login action.
*
* @return Response|string
*/
public function actionLogin()
{
$this->layout = '//entry';
if (!Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
$session = Session::findOne(['user_id' => Yii::$app->user->id]);
if(is_null($session)) {
$session = new Session();
$session->created_at = date('Y-m-d H:i:s');
}
$session->attributes = [
'user_id' => Yii::$app->user->id,
'session_id' => Yii::$app->session->id,
'user_ip' => Yii::$app->request->getRemoteIP(),
'user_host' => Yii::$app->request->getRemoteIP(),
'remote_ip' => Yii::$app->request->getRemoteIP(),
'remote_host' => Yii::$app->request->getRemoteIP(),
'user_agent' => Yii::$app->request->getUserAgent(),
'updated_at' => date('Y-m-d H:i:s')
];
$session->save();
return $this->goBack();
}
return $this->render('login', [
'model' => $model,
]);
}
/**
* Logout action.
*
* @return Response
*/
public function actionLogout()
{
Yii::$app->user->logout();
return $this->goHome();
}
}
<?php
$config = [
...
'bootstrap' => ['log','AppSession'],
...
'components' => [
...
'AppSession' => [
'class' => 'app\components\AppSession'
],
...
];
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment