Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git a/app.db b/app.db
- new file mode 100644
- index 0000000..694f00e
- Binary files /dev/null and b/app.db differ
- diff --git a/config/db.php b/config/db.php
- index bc75e61..7fe328d 100644
- --- a/config/db.php
- +++ b/config/db.php
- @@ -2,9 +2,10 @@
- return [
- 'class' => 'yii\db\Connection',
- - 'dsn' => 'mysql:host=localhost;dbname=yii2basic',
- - 'username' => 'root',
- - 'password' => '',
- + 'dsn' => 'sqlite:' . dirname(__DIR__) . DIRECTORY_SEPARATOR . 'app.db',
- + // 'dsn' => 'mysql:host=localhost;dbname=yii2basic',
- + // 'username' => 'root',
- + // 'password' => '',
- 'charset' => 'utf8',
- // Schema cache options (for production environment)
- diff --git a/config/web.php b/config/web.php
- index 88435f6..329b621 100644
- --- a/config/web.php
- +++ b/config/web.php
- @@ -20,7 +20,7 @@ $config = [
- 'class' => 'yii\caching\FileCache',
- ],
- 'user' => [
- - 'identityClass' => 'app\models\User',
- + 'identityClass' => 'app\models\MultiUser',
- 'enableAutoLogin' => true,
- ],
- 'errorHandler' => [
- @@ -61,14 +61,14 @@ if (YII_ENV_DEV) {
- $config['modules']['debug'] = [
- 'class' => 'yii\debug\Module',
- // uncomment the following to add your IP if you are not connecting from localhost.
- - //'allowedIPs' => ['127.0.0.1', '::1'],
- + 'allowedIPs' => ['127.0.0.1', '[::1]'],
- ];
- $config['bootstrap'][] = 'gii';
- $config['modules']['gii'] = [
- 'class' => 'yii\gii\Module',
- // uncomment the following to add your IP if you are not connecting from localhost.
- - //'allowedIPs' => ['127.0.0.1', '::1'],
- + 'allowedIPs' => ['127.0.0.1', '[::1]'],
- ];
- }
- diff --git a/migrations/m190214_180338_init.php b/migrations/m190214_180338_init.php
- new file mode 100644
- index 0000000..12fe258
- --- /dev/null
- +++ b/migrations/m190214_180338_init.php
- @@ -0,0 +1,61 @@
- +<?php
- +
- +use yii\db\Migration;
- +
- +/**
- + * Class m190214_180338_init
- + */
- +class m190214_180338_init extends Migration
- +{
- + /**
- + * {@inheritdoc}
- + */
- + public function safeUp()
- + {
- + $this->createTable('{{%admin}}', [
- + 'id' => $this->primaryKey(),
- + 'username' => $this->string(64)->notNull(),
- + 'password_hash' => $this->string(255)->notNull(),
- + ]);
- +
- + $this->createTable('{{%employee}}', [
- + 'id' => $this->primaryKey(),
- + 'username' => $this->string(64)->notNull(),
- + 'password_hash' => $this->string(255)->notNull(),
- + ]);
- +
- + $this->insert('{{%admin}}', [
- + 'username' => 'admin',
- + 'password_hash' => Yii::$app->security->generatePasswordHash('i-am-king'),
- + ]);
- +
- + $this->insert('{{%employee}}', [
- + 'username' => 'employee',
- + 'password_hash' => Yii::$app->security->generatePasswordHash('changeme'),
- + ]);
- + }
- +
- + /**
- + * {@inheritdoc}
- + */
- + public function safeDown()
- + {
- + $this->dropTable('{{%employee}}');
- + $this->dropTable('{{%admin}}');
- + }
- +
- + /*
- + // Use up()/down() to run migration code without a transaction.
- + public function up()
- + {
- +
- + }
- +
- + public function down()
- + {
- + echo "m190214_180338_init cannot be reverted.\n";
- +
- + return false;
- + }
- + */
- +}
- diff --git a/models/Admin.php b/models/Admin.php
- new file mode 100644
- index 0000000..7d6a62d
- --- /dev/null
- +++ b/models/Admin.php
- @@ -0,0 +1,57 @@
- +<?php
- +
- +namespace app\models;
- +
- +use Yii;
- +
- +/**
- + * This is the model class for table "admin".
- + *
- + * @property int $id
- + * @property string $username
- + * @property string $password_hash
- + */
- +class Admin extends \yii\db\ActiveRecord
- +{
- + /**
- + * {@inheritdoc}
- + */
- + public static function tableName()
- + {
- + return 'admin';
- + }
- +
- + /**
- + * {@inheritdoc}
- + */
- + public function rules()
- + {
- + return [
- + [['username', 'password_hash'], 'required'],
- + [['username'], 'string', 'max' => 64],
- + [['password_hash'], 'string', 'max' => 255],
- + ];
- + }
- +
- + /**
- + * {@inheritdoc}
- + */
- + public function attributeLabels()
- + {
- + return [
- + 'id' => 'ID',
- + 'username' => 'Username',
- + 'password_hash' => 'Password Hash',
- + ];
- + }
- +
- + public function findByUsername($name)
- + {
- + return static::find()->where(['username' => $name])->one();
- + }
- +
- + public function validatePassword($plaintextPassword)
- + {
- + return Yii::$app->security->validatePassword($plaintextPassword, $this->password_hash);
- + }
- +}
- diff --git a/models/Employee.php b/models/Employee.php
- new file mode 100644
- index 0000000..b6e6e23
- --- /dev/null
- +++ b/models/Employee.php
- @@ -0,0 +1,57 @@
- +<?php
- +
- +namespace app\models;
- +
- +use Yii;
- +
- +/**
- + * This is the model class for table "employee".
- + *
- + * @property int $id
- + * @property string $username
- + * @property string $password_hash
- + */
- +class Employee extends \yii\db\ActiveRecord
- +{
- + /**
- + * {@inheritdoc}
- + */
- + public static function tableName()
- + {
- + return 'employee';
- + }
- +
- + /**
- + * {@inheritdoc}
- + */
- + public function rules()
- + {
- + return [
- + [['username', 'password_hash'], 'required'],
- + [['username'], 'string', 'max' => 64],
- + [['password_hash'], 'string', 'max' => 255],
- + ];
- + }
- +
- + /**
- + * {@inheritdoc}
- + */
- + public function attributeLabels()
- + {
- + return [
- + 'id' => 'ID',
- + 'username' => 'Username',
- + 'password_hash' => 'Password Hash',
- + ];
- + }
- +
- + public function findByUsername($name)
- + {
- + return static::find()->where(['username' => $name])->one();
- + }
- +
- + public function validatePassword($plaintextPassword)
- + {
- + return Yii::$app->security->validatePassword($plaintextPassword, $this->password_hash);
- + }
- +}
- diff --git a/models/LoginForm.php b/models/LoginForm.php
- index cc6af26..c3ed202 100644
- --- a/models/LoginForm.php
- +++ b/models/LoginForm.php
- @@ -17,7 +17,7 @@ class LoginForm extends Model
- public $password;
- public $rememberMe = true;
- - private $_user = false;
- + private $_user;
- /**
- @@ -47,7 +47,7 @@ class LoginForm extends Model
- if (!$this->hasErrors()) {
- $user = $this->getUser();
- - if (!$user || !$user->validatePassword($this->password)) {
- + if (!isset($user) || !$user->validatePassword($this->password)) {
- $this->addError($attribute, 'Incorrect username or password.');
- }
- }
- @@ -60,7 +60,9 @@ class LoginForm extends Model
- public function login()
- {
- if ($this->validate()) {
- - return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0);
- + $identity = new MultiUser(['user' => $this->getUser()]);
- +
- + return Yii::$app->user->login($identity, $this->rememberMe ? 3600*24*30 : 0);
- }
- return false;
- }
- @@ -68,12 +70,16 @@ class LoginForm extends Model
- /**
- * Finds user by [[username]]
- *
- - * @return User|null
- + * @return MultiUser|null
- */
- public function getUser()
- {
- - if ($this->_user === false) {
- - $this->_user = User::findByUsername($this->username);
- + if (!isset($this->_user)) {
- + $this->_user = Admin::findByUsername($this->username);
- + }
- +
- + if (!isset($this->_user)) {
- + $this->_user = Employee::findByUsername($this->username);
- }
- return $this->_user;
- diff --git a/models/MultiUser.php b/models/MultiUser.php
- new file mode 100644
- index 0000000..f25a402
- --- /dev/null
- +++ b/models/MultiUser.php
- @@ -0,0 +1,83 @@
- +<?php
- +
- +namespace app\models;
- +
- +use Yii;
- +
- +class MultiUser extends \yii\base\BaseObject implements \yii\web\IdentityInterface
- +{
- + /**
- + * @var \yii\db\BaseActiveRecord
- + */
- + private $_user;
- +
- + /**
- + * {@inheritdoc}
- + */
- + public static function findIdentity($id)
- + {
- + // $id will be something like "admin-1" or "employee-1"
- + list($type, $pk) = explode('-', $id);
- +
- + switch ($type) {
- + case 'admin':
- + $model = Admin::findOne($pk);
- + break;
- + case 'employee':
- + $model = Employee::findOne($pk);
- + break;
- + }
- +
- + if (isset($model)) {
- + return new static(['user' => $model]);
- + }
- + }
- +
- + /**
- + * {@inheritdoc}
- + */
- + public static function findIdentityByAccessToken($token, $type = null)
- + {
- + throw new \yii\base\NotSupportedException();
- + }
- +
- + /**
- + * {@inheritdoc}
- + */
- + public function getId()
- + {
- + $prefix = $this->_user instanceof Admin ? 'admin' : 'employee';
- + return $prefix . '-' . $this->_user->getPrimaryKey();
- + }
- +
- + /**
- + * {@inheritdoc}
- + */
- + public function getAuthKey()
- + {
- + $secret = Yii::$app->request->cookieValidationKey;
- +
- + return sha1($secret . get_class($this->_user) . $this->_user->getPrimaryKey());
- + }
- +
- + /**
- + * {@inheritdoc}
- + */
- + public function validateAuthKey($authKey)
- + {
- + $secret = Yii::$app->request->cookieValidationKey;
- + $computedKey = sha1($secret . get_class($this->_user) . $this->_user->getPrimaryKey());
- +
- + return $authKey === $computedKey;
- + }
- +
- + public function setUser(\yii\db\BaseActiveRecord $user)
- + {
- + $this->_user = $user;
- + }
- +
- + public function getUsername()
- + {
- + return $this->_user->username;
- + }
- +}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement