예제 #1
0
 public function beforeAction($event)
 {
     /* Đăng nhập rồi nhưng quay lại trang đăng nhập */
     if ($event->controller->id == 'auth' && $event->id == 'login' && Yii::$app->user->getId() != null) {
         return $this->redirect(Url::base('http') . '/index');
     }
     /* Đăng nhập rồi nhưng quay lại trang đăng ký */
     if ($event->controller->id == 'auth' && $event->id == 'register' && Yii::$app->user->getId() != null) {
         return $this->redirect(Url::base('http') . '/index');
     }
     /* Chưa đăng nhập nhưng lại vào trang khác */
     if ($event->controller->id != 'auth' && $event->controller->id != 'index' && $event->controller->id != 'news' && Yii::$app->user->getId() == null) {
         return $this->redirect(Url::to(['auth/login']));
     }
     return parent::beforeAction($event);
 }
예제 #2
0
 public function beforeAction($event)
 {
     /* Đăng nhập rồi nhưng quay lại trang đăng nhập */
     if ($event->controller->id == 'auth' && $event->id == 'login' && Yii::$app->user->getId() != null) {
         return $this->redirect(Url::base());
     }
     /* Chưa đăng nhập nhưng lại vào trang khác */
     if (!($event->controller->id == 'auth' && $event->id == 'login') && Yii::$app->user->getId() == null) {
         return $this->redirect(Url::to(['auth/login']));
     }
     /* Bắt quyền có được cấp hay không */
     if (!in_array($event->controller->id, ["error", "auth", "home"]) && !Yii::$app->user->can($event->controller->id . "_" . $event->id)) {
         print_r('Chú làm éo có quyền mà vào đây');
         die;
     }
     return parent::beforeAction($event);
 }