Advertisement
Guest User

Untitled

a guest
Feb 14th, 2019
369
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 9.97 KB | None | 0 0
  1. diff --git a/app.db b/app.db
  2. new file mode 100644
  3. index 0000000..694f00e
  4. Binary files /dev/null and b/app.db differ
  5. diff --git a/config/db.php b/config/db.php
  6. index bc75e61..7fe328d 100644
  7. --- a/config/db.php
  8. +++ b/config/db.php
  9. @@ -2,9 +2,10 @@
  10.  
  11.  return [
  12.      'class' => 'yii\db\Connection',
  13. -    'dsn' => 'mysql:host=localhost;dbname=yii2basic',
  14. -    'username' => 'root',
  15. -    'password' => '',
  16. +    'dsn' => 'sqlite:' . dirname(__DIR__) . DIRECTORY_SEPARATOR . 'app.db',
  17. +    // 'dsn' => 'mysql:host=localhost;dbname=yii2basic',
  18. +    // 'username' => 'root',
  19. +    // 'password' => '',
  20.      'charset' => 'utf8',
  21.  
  22.      // Schema cache options (for production environment)
  23. diff --git a/config/web.php b/config/web.php
  24. index 88435f6..329b621 100644
  25. --- a/config/web.php
  26. +++ b/config/web.php
  27. @@ -20,7 +20,7 @@ $config = [
  28.              'class' => 'yii\caching\FileCache',
  29.          ],
  30.          'user' => [
  31. -            'identityClass' => 'app\models\User',
  32. +            'identityClass' => 'app\models\MultiUser',
  33.              'enableAutoLogin' => true,
  34.          ],
  35.          'errorHandler' => [
  36. @@ -61,14 +61,14 @@ if (YII_ENV_DEV) {
  37.      $config['modules']['debug'] = [
  38.          'class' => 'yii\debug\Module',
  39.          // uncomment the following to add your IP if you are not connecting from localhost.
  40. -        //'allowedIPs' => ['127.0.0.1', '::1'],
  41. +        'allowedIPs' => ['127.0.0.1', '[::1]'],
  42.      ];
  43.  
  44.      $config['bootstrap'][] = 'gii';
  45.      $config['modules']['gii'] = [
  46.          'class' => 'yii\gii\Module',
  47.          // uncomment the following to add your IP if you are not connecting from localhost.
  48. -        //'allowedIPs' => ['127.0.0.1', '::1'],
  49. +        'allowedIPs' => ['127.0.0.1', '[::1]'],
  50.      ];
  51.  }
  52.  
  53. diff --git a/migrations/m190214_180338_init.php b/migrations/m190214_180338_init.php
  54. new file mode 100644
  55. index 0000000..12fe258
  56. --- /dev/null
  57. +++ b/migrations/m190214_180338_init.php
  58. @@ -0,0 +1,61 @@
  59. +<?php
  60. +
  61. +use yii\db\Migration;
  62. +
  63. +/**
  64. + * Class m190214_180338_init
  65. + */
  66. +class m190214_180338_init extends Migration
  67. +{
  68. +    /**
  69. +     * {@inheritdoc}
  70. +     */
  71. +    public function safeUp()
  72. +    {
  73. +        $this->createTable('{{%admin}}', [
  74. +            'id' => $this->primaryKey(),
  75. +            'username' => $this->string(64)->notNull(),
  76. +            'password_hash' => $this->string(255)->notNull(),
  77. +        ]);
  78. +
  79. +        $this->createTable('{{%employee}}', [
  80. +            'id' => $this->primaryKey(),
  81. +            'username' => $this->string(64)->notNull(),
  82. +            'password_hash' => $this->string(255)->notNull(),
  83. +        ]);
  84. +
  85. +        $this->insert('{{%admin}}', [
  86. +            'username' => 'admin',
  87. +            'password_hash' => Yii::$app->security->generatePasswordHash('i-am-king'),
  88. +        ]);
  89. +
  90. +        $this->insert('{{%employee}}', [
  91. +            'username' => 'employee',
  92. +            'password_hash' => Yii::$app->security->generatePasswordHash('changeme'),
  93. +        ]);
  94. +    }
  95. +
  96. +    /**
  97. +     * {@inheritdoc}
  98. +     */
  99. +    public function safeDown()
  100. +    {
  101. +        $this->dropTable('{{%employee}}');
  102. +        $this->dropTable('{{%admin}}');
  103. +    }
  104. +
  105. +    /*
  106. +    // Use up()/down() to run migration code without a transaction.
  107. +    public function up()
  108. +    {
  109. +
  110. +    }
  111. +
  112. +    public function down()
  113. +    {
  114. +        echo "m190214_180338_init cannot be reverted.\n";
  115. +
  116. +        return false;
  117. +    }
  118. +    */
  119. +}
  120. diff --git a/models/Admin.php b/models/Admin.php
  121. new file mode 100644
  122. index 0000000..7d6a62d
  123. --- /dev/null
  124. +++ b/models/Admin.php
  125. @@ -0,0 +1,57 @@
  126. +<?php
  127. +
  128. +namespace app\models;
  129. +
  130. +use Yii;
  131. +
  132. +/**
  133. + * This is the model class for table "admin".
  134. + *
  135. + * @property int $id
  136. + * @property string $username
  137. + * @property string $password_hash
  138. + */
  139. +class Admin extends \yii\db\ActiveRecord
  140. +{
  141. +    /**
  142. +     * {@inheritdoc}
  143. +     */
  144. +    public static function tableName()
  145. +    {
  146. +        return 'admin';
  147. +    }
  148. +
  149. +    /**
  150. +     * {@inheritdoc}
  151. +     */
  152. +    public function rules()
  153. +    {
  154. +        return [
  155. +            [['username', 'password_hash'], 'required'],
  156. +            [['username'], 'string', 'max' => 64],
  157. +            [['password_hash'], 'string', 'max' => 255],
  158. +        ];
  159. +    }
  160. +
  161. +    /**
  162. +     * {@inheritdoc}
  163. +     */
  164. +    public function attributeLabels()
  165. +    {
  166. +        return [
  167. +            'id' => 'ID',
  168. +            'username' => 'Username',
  169. +            'password_hash' => 'Password Hash',
  170. +        ];
  171. +    }
  172. +
  173. +    public function findByUsername($name)
  174. +    {
  175. +        return static::find()->where(['username' => $name])->one();
  176. +    }
  177. +
  178. +    public function validatePassword($plaintextPassword)
  179. +    {
  180. +        return Yii::$app->security->validatePassword($plaintextPassword, $this->password_hash);
  181. +    }
  182. +}
  183. diff --git a/models/Employee.php b/models/Employee.php
  184. new file mode 100644
  185. index 0000000..b6e6e23
  186. --- /dev/null
  187. +++ b/models/Employee.php
  188. @@ -0,0 +1,57 @@
  189. +<?php
  190. +
  191. +namespace app\models;
  192. +
  193. +use Yii;
  194. +
  195. +/**
  196. + * This is the model class for table "employee".
  197. + *
  198. + * @property int $id
  199. + * @property string $username
  200. + * @property string $password_hash
  201. + */
  202. +class Employee extends \yii\db\ActiveRecord
  203. +{
  204. +    /**
  205. +     * {@inheritdoc}
  206. +     */
  207. +    public static function tableName()
  208. +    {
  209. +        return 'employee';
  210. +    }
  211. +
  212. +    /**
  213. +     * {@inheritdoc}
  214. +     */
  215. +    public function rules()
  216. +    {
  217. +        return [
  218. +            [['username', 'password_hash'], 'required'],
  219. +            [['username'], 'string', 'max' => 64],
  220. +            [['password_hash'], 'string', 'max' => 255],
  221. +        ];
  222. +    }
  223. +
  224. +    /**
  225. +     * {@inheritdoc}
  226. +     */
  227. +    public function attributeLabels()
  228. +    {
  229. +        return [
  230. +            'id' => 'ID',
  231. +            'username' => 'Username',
  232. +            'password_hash' => 'Password Hash',
  233. +        ];
  234. +    }
  235. +
  236. +    public function findByUsername($name)
  237. +    {
  238. +        return static::find()->where(['username' => $name])->one();
  239. +    }
  240. +
  241. +    public function validatePassword($plaintextPassword)
  242. +    {
  243. +        return Yii::$app->security->validatePassword($plaintextPassword, $this->password_hash);
  244. +    }
  245. +}
  246. diff --git a/models/LoginForm.php b/models/LoginForm.php
  247. index cc6af26..c3ed202 100644
  248. --- a/models/LoginForm.php
  249. +++ b/models/LoginForm.php
  250. @@ -17,7 +17,7 @@ class LoginForm extends Model
  251.      public $password;
  252.      public $rememberMe = true;
  253.  
  254. -    private $_user = false;
  255. +    private $_user;
  256.  
  257.  
  258.      /**
  259. @@ -47,7 +47,7 @@ class LoginForm extends Model
  260.          if (!$this->hasErrors()) {
  261.              $user = $this->getUser();
  262.  
  263. -            if (!$user || !$user->validatePassword($this->password)) {
  264. +            if (!isset($user) || !$user->validatePassword($this->password)) {
  265.                  $this->addError($attribute, 'Incorrect username or password.');
  266.              }
  267.          }
  268. @@ -60,7 +60,9 @@ class LoginForm extends Model
  269.      public function login()
  270.      {
  271.          if ($this->validate()) {
  272. -            return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0);
  273. +            $identity = new MultiUser(['user' => $this->getUser()]);
  274. +
  275. +            return Yii::$app->user->login($identity, $this->rememberMe ? 3600*24*30 : 0);
  276.          }
  277.          return false;
  278.      }
  279. @@ -68,12 +70,16 @@ class LoginForm extends Model
  280.      /**
  281.       * Finds user by [[username]]
  282.       *
  283. -     * @return User|null
  284. +     * @return MultiUser|null
  285.       */
  286.      public function getUser()
  287.      {
  288. -        if ($this->_user === false) {
  289. -            $this->_user = User::findByUsername($this->username);
  290. +        if (!isset($this->_user)) {
  291. +            $this->_user = Admin::findByUsername($this->username);
  292. +        }
  293. +
  294. +        if (!isset($this->_user)) {
  295. +            $this->_user = Employee::findByUsername($this->username);
  296.          }
  297.  
  298.          return $this->_user;
  299. diff --git a/models/MultiUser.php b/models/MultiUser.php
  300. new file mode 100644
  301. index 0000000..f25a402
  302. --- /dev/null
  303. +++ b/models/MultiUser.php
  304. @@ -0,0 +1,83 @@
  305. +<?php
  306. +
  307. +namespace app\models;
  308. +
  309. +use Yii;
  310. +
  311. +class MultiUser extends \yii\base\BaseObject implements \yii\web\IdentityInterface
  312. +{
  313. +    /**
  314. +     * @var \yii\db\BaseActiveRecord
  315. +     */
  316. +    private $_user;
  317. +
  318. +    /**
  319. +     * {@inheritdoc}
  320. +     */
  321. +    public static function findIdentity($id)
  322. +    {
  323. +        // $id will be something like "admin-1" or "employee-1"
  324. +        list($type, $pk) = explode('-', $id);
  325. +
  326. +        switch ($type) {
  327. +            case 'admin':
  328. +                $model = Admin::findOne($pk);
  329. +                break;
  330. +            case 'employee':
  331. +                $model = Employee::findOne($pk);
  332. +                break;
  333. +        }
  334. +
  335. +        if (isset($model)) {
  336. +            return new static(['user' => $model]);
  337. +        }
  338. +    }
  339. +
  340. +    /**
  341. +     * {@inheritdoc}
  342. +     */
  343. +    public static function findIdentityByAccessToken($token, $type = null)
  344. +    {
  345. +        throw new \yii\base\NotSupportedException();
  346. +    }
  347. +
  348. +    /**
  349. +     * {@inheritdoc}
  350. +     */
  351. +    public function getId()
  352. +    {
  353. +        $prefix = $this->_user instanceof Admin ? 'admin' : 'employee';
  354. +        return $prefix . '-' . $this->_user->getPrimaryKey();
  355. +    }
  356. +
  357. +    /**
  358. +     * {@inheritdoc}
  359. +     */
  360. +    public function getAuthKey()
  361. +    {
  362. +        $secret = Yii::$app->request->cookieValidationKey;
  363. +
  364. +        return sha1($secret . get_class($this->_user) . $this->_user->getPrimaryKey());
  365. +    }
  366. +
  367. +    /**
  368. +     * {@inheritdoc}
  369. +     */
  370. +    public function validateAuthKey($authKey)
  371. +    {
  372. +        $secret = Yii::$app->request->cookieValidationKey;
  373. +        $computedKey = sha1($secret . get_class($this->_user) . $this->_user->getPrimaryKey());
  374. +
  375. +        return $authKey === $computedKey;
  376. +    }
  377. +
  378. +    public function setUser(\yii\db\BaseActiveRecord $user)
  379. +    {
  380. +        $this->_user = $user;
  381. +    }
  382. +
  383. +    public function getUsername()
  384. +    {
  385. +        return $this->_user->username;
  386. +    }
  387. +}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement