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条)

报告相同问题?

悬赏问题

  • ¥60 QQOP数据,什么是op数据号,怎么提取op数据!能不能大量提取(语言-c语言)
  • ¥15 matlab代码 关于微分方程和嵌套的分段函数。
  • ¥15 把VMware项目复制到另一台电脑
  • ¥15 onlyoffice编辑完后立即下载,下载的不是最新编辑的文档
  • ¥15 求caverdock使用教程
  • ¥15 Coze智能助手搭建过程中的问题请教
  • ¥15 12864只亮屏 不显示汉字
  • ¥20 三极管1000倍放大电路
  • ¥15 vscode报错如何解决
  • ¥15 前端vue CryptoJS Aes CBC加密后端java解密