duanjupiao4730 2013-07-23 11:40
浏览 25
已采纳

使用CakePHP中的不同用户角色登录?

I am making a system using CakePHP in which the users can be either A, B or C. Like student, teacher and some other role. Is it possible to let them all login via 1 link? so not /students/login and /teachers/login, but something like www.somewebsite/login for all of them?

  • 写回答

2条回答 默认 最新

  • doujin4031 2013-07-23 11:53
    关注

    Read this tutorial, it exactly covers what you're asking for. Also read this section.

    It does not make any sense at all to have different controllers for different types of users, you'll just duplicate code. If you need to take different actions based on the role you can do that within your login method by calling another method like afterStudentLogin() from within your login() method and do role specific things there. The reason for that is that a single method should always just do one task, so you decouple the role specific code from it in a separate method.

    public function login() {
        if ($this->Auth->user()) {
            /* ... */
            $callback = 'after' . $this->Auth->user('role') . 'Login');
            $this->{$callback}($this->Auth->user());
            /* ... */
        }
    }
    

    Even if the user types are very different, they all will share a common thing: The login. In this case have an users table and for example student_profils table and teacher_profiles table. If the difference is just a few fields I would put them all in one table like profiles.

    If you want to have /login instead of /users/login you should use routing.

    Router::connect(
        '/login',
        array(
            'controller' => 'users',
            'action' => 'login'
        )
    );
    

    You can also take a look at this Users plugin which covers a lot of the usual user related tasks. And here is a simple multi-role authorization adapter.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?