在CakePHP中使用不同的用户角色登录? [英] Logging in using different user roles in CakePHP?

查看:246
本文介绍了在CakePHP中使用不同的用户角色登录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用CakePHP制作系统,其中用户可以是A,B或C.像学生,老师和一些其他角色。是否可以让他们通过1链接登录?

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?

推荐答案

对不同类型的用户使用不同的控制器没有任何意义,你只需要复制代码。如果你需要根据角色采取不同的行动,你可以在你的登录方法中通过从login()方法中调用另一个方法,如afterStudentLogin(),并在那里做角色特定的事情。这样做的原因是单个方法应该总是只执行一个任务,因此您可以在一个单独的方法中将角色特定代码与其分离。

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());
        /* ... */
    }
}

即使用户类型非常不同,他们都会分享一个共同的东西:登录。在这种情况下有一个users表,例如 student_profils 表和 teacher_profiles 表。如果差异只是几个字段,我会把它们都放在一个表中,如 profiles

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.

要拥有/ login而不是/ users / login,您应该使用路由

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

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

您也可以选择看看这个用户插件,其中涵盖了很多常见的用户相关任务。和这里是一个简单的多-role授权适配器

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.

这篇关于在CakePHP中使用不同的用户角色登录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆