dongwuli5105 2018-10-27 05:40
浏览 110

阻止普通用户进入管理页面

In model, User.php I set a function call isStatus()

 public function isStatus(){
    return $this->status; // 1 of mysql table column name status, let say 0 is admin and 1 is normal user.

}

In controller name LoginController.php, I want to make it if user status is 0 will go /admin page, other will go to / page which mean main page.

    protected function authenticated($request, $user){

    if($user->isStatus('0')){
        return redirect('/admin');
    }else{
        return redirect('/');
    }
}

User table with 'status' column are boolean but is not working.

  • 写回答

2条回答 默认 最新

  • dongpingwu8378 2018-10-27 06:44
    关注

    Since you are using Laravel, lets refactor a little bit of your code.

    You are passing a parameter to your function public function isStatus() with receives any parameter and returns a public value. Delete this function.

    auth()->user() will get the connected user for you and you can access the status property just like this

    auth()->user()->status

    Your function shoud look like this:

    return auth()->user()->status ? redirect('/admin'): redirect('/');
    

    You can put this validation in all your controllers that needs the role authentication.

    If you are interested you can also check some modules out of the box developed by external vendors that treats real profesional the validations and avoid repetitive code.

    Check them out

    and @Kuebel's favourite

    评论

报告相同问题?

悬赏问题

  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)