Commit 7def50c62437bdde08092a2834323718c83bd254
1 parent
7c28815b
24.03.16 finish 1
Showing
7 changed files
with
145 additions
and
13 deletions
Show diff stats
backend/controllers/BlogController.php
| @@ -9,24 +9,37 @@ use common\models\BlogSearch; | @@ -9,24 +9,37 @@ use common\models\BlogSearch; | ||
| 9 | use yii\web\Controller; | 9 | use yii\web\Controller; |
| 10 | use yii\web\NotFoundHttpException; | 10 | use yii\web\NotFoundHttpException; |
| 11 | use yii\filters\VerbFilter; | 11 | use yii\filters\VerbFilter; |
| 12 | - | 12 | +use yii\filters\AccessControl; |
| 13 | /** | 13 | /** |
| 14 | * BlogController implements the CRUD actions for Blog model. | 14 | * BlogController implements the CRUD actions for Blog model. |
| 15 | */ | 15 | */ |
| 16 | class BlogController extends Controller | 16 | class BlogController extends Controller |
| 17 | { | 17 | { |
| 18 | 18 | ||
| 19 | - public $layout = '/admin'; | ||
| 20 | /** | 19 | /** |
| 21 | * @inheritdoc | 20 | * @inheritdoc |
| 22 | */ | 21 | */ |
| 23 | public function behaviors() | 22 | public function behaviors() |
| 24 | { | 23 | { |
| 25 | return [ | 24 | return [ |
| 25 | + 'access' => [ | ||
| 26 | + 'class' => AccessControl::className(), | ||
| 27 | + 'rules' => [ | ||
| 28 | + [ | ||
| 29 | + 'actions' => ['login', 'error'], | ||
| 30 | + 'allow' => true, | ||
| 31 | + ], | ||
| 32 | + [ | ||
| 33 | + 'actions' => ['logout', 'index'], | ||
| 34 | + 'allow' => true, | ||
| 35 | + 'roles' => ['@'], | ||
| 36 | + ], | ||
| 37 | + ], | ||
| 38 | + ], | ||
| 26 | 'verbs' => [ | 39 | 'verbs' => [ |
| 27 | 'class' => VerbFilter::className(), | 40 | 'class' => VerbFilter::className(), |
| 28 | 'actions' => [ | 41 | 'actions' => [ |
| 29 | - 'delete' => ['POST'], | 42 | + 'logout' => ['post'], |
| 30 | ], | 43 | ], |
| 31 | ], | 44 | ], |
| 32 | ]; | 45 | ]; |
backend/controllers/BrandController.php
| @@ -8,7 +8,7 @@ use common\modules\product\models\BrandSearch; | @@ -8,7 +8,7 @@ use common\modules\product\models\BrandSearch; | ||
| 8 | use yii\web\Controller; | 8 | use yii\web\Controller; |
| 9 | use yii\web\NotFoundHttpException; | 9 | use yii\web\NotFoundHttpException; |
| 10 | use yii\filters\VerbFilter; | 10 | use yii\filters\VerbFilter; |
| 11 | - | 11 | +use yii\filters\AccessControl; |
| 12 | /** | 12 | /** |
| 13 | * BrandController implements the CRUD actions for Brand model. | 13 | * BrandController implements the CRUD actions for Brand model. |
| 14 | */ | 14 | */ |
| @@ -20,10 +20,24 @@ class BrandController extends Controller | @@ -20,10 +20,24 @@ class BrandController extends Controller | ||
| 20 | public function behaviors() | 20 | public function behaviors() |
| 21 | { | 21 | { |
| 22 | return [ | 22 | return [ |
| 23 | + 'access' => [ | ||
| 24 | + 'class' => AccessControl::className(), | ||
| 25 | + 'rules' => [ | ||
| 26 | + [ | ||
| 27 | + 'actions' => ['login', 'error'], | ||
| 28 | + 'allow' => true, | ||
| 29 | + ], | ||
| 30 | + [ | ||
| 31 | + 'actions' => ['logout', 'index'], | ||
| 32 | + 'allow' => true, | ||
| 33 | + 'roles' => ['@'], | ||
| 34 | + ], | ||
| 35 | + ], | ||
| 36 | + ], | ||
| 23 | 'verbs' => [ | 37 | 'verbs' => [ |
| 24 | 'class' => VerbFilter::className(), | 38 | 'class' => VerbFilter::className(), |
| 25 | 'actions' => [ | 39 | 'actions' => [ |
| 26 | - 'delete' => ['POST'], | 40 | + 'logout' => ['post'], |
| 27 | ], | 41 | ], |
| 28 | ], | 42 | ], |
| 29 | ]; | 43 | ]; |
backend/controllers/CategoryController.php
| @@ -10,7 +10,7 @@ use yii\helpers\ArrayHelper; | @@ -10,7 +10,7 @@ use yii\helpers\ArrayHelper; | ||
| 10 | use yii\web\Controller; | 10 | use yii\web\Controller; |
| 11 | use yii\web\NotFoundHttpException; | 11 | use yii\web\NotFoundHttpException; |
| 12 | use yii\filters\VerbFilter; | 12 | use yii\filters\VerbFilter; |
| 13 | - | 13 | +use yii\filters\AccessControl; |
| 14 | /** | 14 | /** |
| 15 | * CategoryController implements the CRUD actions for Category model. | 15 | * CategoryController implements the CRUD actions for Category model. |
| 16 | */ | 16 | */ |
| @@ -22,10 +22,24 @@ class CategoryController extends Controller | @@ -22,10 +22,24 @@ class CategoryController extends Controller | ||
| 22 | public function behaviors() | 22 | public function behaviors() |
| 23 | { | 23 | { |
| 24 | return [ | 24 | return [ |
| 25 | + 'access' => [ | ||
| 26 | + 'class' => AccessControl::className(), | ||
| 27 | + 'rules' => [ | ||
| 28 | + [ | ||
| 29 | + 'actions' => ['login', 'error'], | ||
| 30 | + 'allow' => true, | ||
| 31 | + ], | ||
| 32 | + [ | ||
| 33 | + 'actions' => ['logout', 'index'], | ||
| 34 | + 'allow' => true, | ||
| 35 | + 'roles' => ['@'], | ||
| 36 | + ], | ||
| 37 | + ], | ||
| 38 | + ], | ||
| 25 | 'verbs' => [ | 39 | 'verbs' => [ |
| 26 | 'class' => VerbFilter::className(), | 40 | 'class' => VerbFilter::className(), |
| 27 | 'actions' => [ | 41 | 'actions' => [ |
| 28 | - 'delete' => ['POST'], | 42 | + 'logout' => ['post'], |
| 29 | ], | 43 | ], |
| 30 | ], | 44 | ], |
| 31 | ]; | 45 | ]; |
backend/controllers/OrdersController.php
| @@ -8,7 +8,7 @@ use common\models\OrdersSearch; | @@ -8,7 +8,7 @@ use common\models\OrdersSearch; | ||
| 8 | use yii\web\Controller; | 8 | use yii\web\Controller; |
| 9 | use yii\web\NotFoundHttpException; | 9 | use yii\web\NotFoundHttpException; |
| 10 | use yii\filters\VerbFilter; | 10 | use yii\filters\VerbFilter; |
| 11 | - | 11 | +use yii\filters\AccessControl; |
| 12 | /** | 12 | /** |
| 13 | * OrdersController implements the CRUD actions for Orders model. | 13 | * OrdersController implements the CRUD actions for Orders model. |
| 14 | */ | 14 | */ |
| @@ -20,15 +20,28 @@ class OrdersController extends Controller | @@ -20,15 +20,28 @@ class OrdersController extends Controller | ||
| 20 | public function behaviors() | 20 | public function behaviors() |
| 21 | { | 21 | { |
| 22 | return [ | 22 | return [ |
| 23 | + 'access' => [ | ||
| 24 | + 'class' => AccessControl::className(), | ||
| 25 | + 'rules' => [ | ||
| 26 | + [ | ||
| 27 | + 'actions' => ['login', 'error'], | ||
| 28 | + 'allow' => true, | ||
| 29 | + ], | ||
| 30 | + [ | ||
| 31 | + 'actions' => ['logout', 'index'], | ||
| 32 | + 'allow' => true, | ||
| 33 | + 'roles' => ['@'], | ||
| 34 | + ], | ||
| 35 | + ], | ||
| 36 | + ], | ||
| 23 | 'verbs' => [ | 37 | 'verbs' => [ |
| 24 | 'class' => VerbFilter::className(), | 38 | 'class' => VerbFilter::className(), |
| 25 | 'actions' => [ | 39 | 'actions' => [ |
| 26 | - 'delete' => ['POST'], | 40 | + 'logout' => ['post'], |
| 27 | ], | 41 | ], |
| 28 | ], | 42 | ], |
| 29 | ]; | 43 | ]; |
| 30 | } | 44 | } |
| 31 | - | ||
| 32 | /** | 45 | /** |
| 33 | * Lists all Orders models. | 46 | * Lists all Orders models. |
| 34 | * @return mixed | 47 | * @return mixed |
backend/controllers/SiteController.php
| @@ -4,7 +4,7 @@ namespace backend\controllers; | @@ -4,7 +4,7 @@ namespace backend\controllers; | ||
| 4 | use Yii; | 4 | use Yii; |
| 5 | use yii\filters\AccessControl; | 5 | use yii\filters\AccessControl; |
| 6 | use yii\web\Controller; | 6 | use yii\web\Controller; |
| 7 | -use common\models\LoginForm; | 7 | +use backend\models\LoginForm; |
| 8 | use yii\filters\VerbFilter; | 8 | use yii\filters\VerbFilter; |
| 9 | use common\models\Blog; | 9 | use common\models\Blog; |
| 10 | /** | 10 | /** |
| @@ -13,7 +13,6 @@ use common\models\Blog; | @@ -13,7 +13,6 @@ use common\models\Blog; | ||
| 13 | class SiteController extends Controller | 13 | class SiteController extends Controller |
| 14 | { | 14 | { |
| 15 | 15 | ||
| 16 | - public $layout = '/admin'; | ||
| 17 | 16 | ||
| 18 | /** | 17 | /** |
| 19 | * @inheritdoc | 18 | * @inheritdoc |
| 1 | +<?php | ||
| 2 | +namespace backend\models; | ||
| 3 | + | ||
| 4 | +use common\models\User; | ||
| 5 | +use Yii; | ||
| 6 | +use yii\base\Model; | ||
| 7 | + | ||
| 8 | +/** | ||
| 9 | + * Login form | ||
| 10 | + */ | ||
| 11 | +class LoginForm extends Model | ||
| 12 | +{ | ||
| 13 | + public $username; | ||
| 14 | + public $password; | ||
| 15 | + public $rememberMe = true; | ||
| 16 | + | ||
| 17 | + private $_user; | ||
| 18 | + | ||
| 19 | + | ||
| 20 | + /** | ||
| 21 | + * @inheritdoc | ||
| 22 | + */ | ||
| 23 | + public function rules() | ||
| 24 | + { | ||
| 25 | + return [ | ||
| 26 | + // username and password are both required | ||
| 27 | + [['username', 'password'], 'required'], | ||
| 28 | + // rememberMe must be a boolean value | ||
| 29 | + ['rememberMe', 'boolean'], | ||
| 30 | + // password is validated by validatePassword() | ||
| 31 | + ['password', 'validatePassword'], | ||
| 32 | + ]; | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + /** | ||
| 36 | + * Validates the password. | ||
| 37 | + * This method serves as the inline validation for password. | ||
| 38 | + * | ||
| 39 | + * @param string $attribute the attribute currently being validated | ||
| 40 | + * @param array $params the additional name-value pairs given in the rule | ||
| 41 | + */ | ||
| 42 | + public function validatePassword($attribute, $params) | ||
| 43 | + { | ||
| 44 | + if (!$this->hasErrors()) { | ||
| 45 | + $user = $this->getUser(); | ||
| 46 | + if (!$user || !$user->validatePassword($this->password)) { | ||
| 47 | + $this->addError($attribute, 'Incorrect username or password.'); | ||
| 48 | + } | ||
| 49 | + } | ||
| 50 | + } | ||
| 51 | + | ||
| 52 | + /** | ||
| 53 | + * Logs in a user using the provided username and password. | ||
| 54 | + * | ||
| 55 | + * @return boolean whether the user is logged in successfully | ||
| 56 | + */ | ||
| 57 | + public function login() | ||
| 58 | + { | ||
| 59 | + if ($this->validate()) { | ||
| 60 | + return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0); | ||
| 61 | + } else { | ||
| 62 | + return false; | ||
| 63 | + } | ||
| 64 | + } | ||
| 65 | + | ||
| 66 | + /** | ||
| 67 | + * Finds user by [[username]] | ||
| 68 | + * | ||
| 69 | + * @return User|null | ||
| 70 | + */ | ||
| 71 | + protected function getUser() | ||
| 72 | + { | ||
| 73 | + if ($this->_user === null) { | ||
| 74 | + $this->_user = User::findByUsername($this->username); | ||
| 75 | + } | ||
| 76 | + | ||
| 77 | + return $this->_user; | ||
| 78 | + } | ||
| 79 | +} |
backend/views/site/login.php
| @@ -19,7 +19,7 @@ $this->params['breadcrumbs'][] = $this->title; | @@ -19,7 +19,7 @@ $this->params['breadcrumbs'][] = $this->title; | ||
| 19 | <div class="col-lg-5"> | 19 | <div class="col-lg-5"> |
| 20 | <?php $form = ActiveForm::begin(['id' => 'login-form']); ?> | 20 | <?php $form = ActiveForm::begin(['id' => 'login-form']); ?> |
| 21 | 21 | ||
| 22 | - <?= $form->field($model, 'email')->textInput(['autofocus' => true]) ?> | 22 | + <?= $form->field($model, 'username')->textInput(['autofocus' => true]) ?> |
| 23 | 23 | ||
| 24 | <?= $form->field($model, 'password')->passwordInput() ?> | 24 | <?= $form->field($model, 'password')->passwordInput() ?> |
| 25 | 25 |