简体   繁体   English

管理员和用户的 Codeigniter 角色

[英]Codeigniter roles for admin and user

users.php

I have made a login form which works now and goes to admin_view .我制作了一个登录表单,现在可以使用并转到admin_view I do need a user_login for users so the user_view should be loading if the user is a user .我确实需要一个user_login用户因此user_view应该是装载如果用户是一个user

I need to something with the database first maybe a type enumeration or a boolean (value 1 for admin and 0 for user)我首先需要对数据库进行一些处理,可能是类型枚举或布尔值(管理员为1 ,用户为0

public function login() {
    $this->form_validation->set_rules('username', 'Username', 'trim|required|min_length[3]');
    $this->form_validation->set_rules('password', 'Password', 'trim|required|min_length[3]');
    $this->form_validation->set_rules('confirm_password', 'Confirm Password', 'trim|required|min_length[3]|matches[password]');

    if ($this->form_validation->run() == FALSE) {
        $data = array(
            'errors' => validation_errors()
            );

        $this->session->set_flashdata($data);

        redirect('home');
    } else {
        $username = $this->input->post('username');
        $password = $this->input->post('password');
        $user_id = $this->user_model->login_user($username, $password);

        if($user_id) {
            $user_data = array(
                'user_id' => $user_id,
                'username' => $username,
                'logged_in' => true
                );

            $this->session->set_userdata($user_data);
            $this->session->set_flashdata('login_success', 'You are now logged in');
            // I need to load a user_view when the user have a user role.
            $data['main_view'] = "admin_view";
            $this->load->view('layouts/main', $data);
            // redirect('home/index');

            redirect('home/index');
        }
    }
}

Edit following rereading of the question: If you can set an admin role you can set a user role too.编辑以下重读问题:如果您可以设置管理员角色,您也可以设置用户角色。 It is the same methodology just with a user_role column.它与 user_role 列的方法相同。 You can set this to be anything from 1-9 or 1-20 or 1-999 if you need it.如果需要,您可以将其设置为 1-9 或 1-20 或 1-999 之间的任何值。

There are many ways to achieve this so no one way will answer your question.有很多方法可以实现这一目标,因此没有一种方法可以回答您的问题。 For instance you could set a column as you suggested in your user table to indicate if the user is an admin or not.例如,您可以按照您在用户表中的建议设置一列,以指示用户是否为管理员。 (How this gets set to admin is a different issue but for now you can manually change it.) (如何将其设置为 admin 是一个不同的问题,但现在您可以手动更改它。)

Once authenticated, you could set a session variable to indicate that the user is an admin, or when logging in, return an array that contains more than the user id, but the role setting as well.通过身份验证后,您可以设置会话变量以指示用户是管理员,或者在登录时返回一个数组,该数组包含的不仅仅是用户 ID,还包含角色设置。

Personally I like to have at least as a minimum a user role, and admin role and a god role that I reserve for system admins.就我个人而言,我喜欢至少拥有一个用户角色、管理员角色和我为系统管理员保留的上帝角色。 However normally roles would be set in a different table so many roles and responsibilities could be set.然而,通常角色会设置在不同的表中,因此可以设置许多角色和职责。 Even making roles editable to what they can do against certain permission settings, but that is far more complex.甚至使角色可编辑为他们可以针对某些权限设置执行的操作,但这要复杂得多。

When you authorize a user, ie are they logged in, you can then return not just that they are logged in but if they are an admin or not, or what level of permission they have.当您授权用户时,即他们是否已登录,您不仅可以返回他们已登录,还可以返回他们是否是管理员,或者他们拥有的权限级别。 If they are an admin, you can add to the menus the admin navigation, or a link to an entirely separate admin section, or redirect them immediately to the admin screens.如果他们是管理员,您可以将管理员导航添加到菜单中,或者添加一个指向完全独立的管理部分的链接,或者立即将他们重定向到管理屏幕。 That is up to you.那取决于你。

In you controller posted you seem to be loading an admin view, and then redirecting them to another controller.在您发布的控制器中,您似乎正在加载管理视图,然后将它们重定向到另一个控制器。 This does not make any sense to me.这对我来说没有任何意义。 It should be something like:它应该是这样的:

$user = $this->authentication_model->login_user($username, $password);
if ( (!empty($user)) AND (empty($user['error']))  // however your authentication works
{ 
    if ($user['is_admin'] > 0)
    {
        // is user AND is admin so deal with as you want
        // You might set session variables for instance
        // Or you might redirect to admin screens
        // Or you might redirect different admin level differently
        // Or you might just load additional menu views
    }
    else
    {
       // is user so do whatever
       // You might set a session variable
       // You might set a cookie value
       // You might redirect
    }
}
else
{
     // There was a login error message so deal with that here
}

Again, there are just so many different ways to do this.同样,有很多不同的方法可以做到这一点。 It might be worth taking a look at some of the existing authentication and authorization scripts already available, to use or to learn from.可能值得查看一些已经可用、使用或学习的现有身份验证和授权脚本。 There is a great summary of them here but it is not exhaustive of the possibilities.此处对它们进行了很好的总结,但并非详尽无遗。

http://webeasystep.com/blog/view_article/What_is_the_best_authentication_library_for_CodeIgniter http://webeasystep.com/blog/view_article/What_is_the_best_authentication_library_for_CodeIgniter

Personally I like ION Auth, which I find easy to customize and use in any way I want, in a limited fashion or full blown usage, but that is very much a personal preference.我个人喜欢 ION Auth,我发现它很容易以我想要的任何方式定制和使用,以有限的方式或完全成熟的使用,但这在很大程度上是个人喜好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM