在Codeigniter中控制用户权限 [英] Controlling User Privileges in Codeigniter
问题描述
假设我正在使用Codeigniter构建Web应用程序。我有两个级别的用户- 1。管理员
和 2。经理
。我希望我的管理员能够添加,编辑,删除,因此在我的视图文件中,我具有以下内容:
Suppose I am building a web application using Codeigniter. I have two levels of user- 1. Admin
and 2. Manager
. I want my admin to be able to ADD,EDIT, DELETE, so in my view file I have the following:
<a href="somelink">ADD</a>
<a href="somelink">EDIT</a>
<a href="somelink">DELETE</a>
但是在某些特定页面中,我不希望我的Manager能够删除或编辑任何内容记录,他将只能添加数据。现在我的问题是,因为我有多种类型的用户和用户权限,是否必须为我的经理(以及其他每个用户级别)创建一个新的控制器和一个视图文件,或者有一些更好的方法可以在同一方法中处理我将用于管理员的控制器和视图文件?
But in some specific pages, I don't want my Manager to be able to delete or edit any record, he will be allowed to add data only . Now my question is since I have multiple types of user and user- privileges do I have to create a new controller and a view file for my manager(and for each other user levels) or there are some better ways to handle this in the same controller and view file that I will be using for the admin ?
目前,我正在为ADMIN和MANAGER创建单独的控制器,模型和视图文件,这非常痛苦。因此,我认为必须有一种更好的方法来做到这一点。
At present I am creating separate controller, model and view files for my ADMIN and MANAGER which is very painful. So I thought there got to be a better way to do this.
请问您关于如何处理此问题的一些专家建议?
Would you please give me some of your expert advice on how to handle this?
要弄清楚哪种类型的用户登录,我在控制器中使用以下代码:
To figure out what type of user is logged in I am using the following code in my controllers:
parent::__construct();
if ( !($this->session->userdata('user_type')== 'flex_admin'))
{
redirect('login');
}
在此先感谢:)
推荐答案
您绝对不需要创建单独的控制器和视图。
You most definitely do not need to create separate controllers and views.
您只需存储用户权限类型(1或2),然后在您的每个控制器或视图中进行检查,以检查用户的权限。
You can just store the users privilege type (1 or 2) in a session and then do a check in each of your controllers or views to check the privileges for the user.
查看文件
<div id="nav">
<ul>
<li>Home</li>
<li>Logout</li>
<?php if($this->session->userdata('privilege') == 1) : ?>
<li>Delete</li>
<?php endif; ?>
</ul>
</div>
您可以在控制器中执行相同类型的检查,以确保用户访问适合执行某些功能。
You can perform the same kind of check in your controller to make sure that the users access is appropriate to perform some function.
这篇关于在Codeigniter中控制用户权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!