Skip to content

Commit

Permalink
Disallow ROLE_USER to login to backend
Browse files Browse the repository at this point in the history
  • Loading branch information
prabowomurti committed Aug 19, 2016
1 parent 54fd6f1 commit 9ef0e9d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
7 changes: 6 additions & 1 deletion backend/controllers/SiteController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use common\models\LoginForm;
use common\models\User;

/**
* Site controller
Expand All @@ -29,6 +30,10 @@ public function behaviors()
'actions' => ['logout', 'index'],
'allow' => true,
'roles' => ['@'],
'matchCallback' => function ($rule, $action)
{
return Yii::$app->user->identity['role'] == User::ROLE_ADMIN;
}
],
],
],
Expand Down Expand Up @@ -65,7 +70,7 @@ public function actionLogin()
}

$model = new LoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
if ($model->load(Yii::$app->request->post()) && $model->backendLogin()) {
return $this->goBack();
} else {
return $this->render('login', [
Expand Down
22 changes: 22 additions & 0 deletions common/models/LoginForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,28 @@ public function login()
}
}

/**
* Logs in a backend user using the provided email and password.
*
* @return boolean whether the user is logged in successfully
*/
public function backendLogin()
{
if ($this->validate()) {
$user = $this->getUser();
if ($user->role == User::ROLE_USER)
{
$this->_user = false;
$this->addError('email', 'Can not login as ' . User::ROLE_USER);
return false;
}

return Yii::$app->user->login($user, $this->rememberMe ? 3600 * 24 * 30 : 0);
} else {
return false;
}
}

/**
* Finds user by [[email]]
*
Expand Down

0 comments on commit 9ef0e9d

Please sign in to comment.