codeigniter HMVC:检查控制器中各个功能的用户权限
codeigniter HMVC: Check user permissions for individual functions in controller
我正在使用 codeigniter 的 HMVC 模块扩展。阅读这篇精彩的文章 - Scalable Login System for CodeIgniter – Ion_Auth。
本文建议的是,我们可以创建多个自定义控制器,并根据登录用户的角色,将用户重定向到他各自的控制器。
例如,管理员将他重定向到扩展 Admin_Controller 的控制器,依此类推:
class Admin_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
if( ! $this->ion_auth->is_admin()) {
redirect('/');
}
}
}
因此,我们可以在基本控制器级别本身为该控制器中的所有 methods/views 应用通用规则。这就是我有疑问的地方:如果我不想对该控制器内的所有视图进行全局控制怎么办。
或者换句话说 - 如果我需要对个别视图进行特定控制,该怎么做。例如 - 我有一个名为 'Blog' 的控制器和名为 - 'view'、'add'、'edit' 的方法。现在,视图适用于所有类型的用户,而只有管理员才能添加或编辑博客。这意味着我不能放置控制器级别的逻辑..
class Blog_Controller extends Admin_Controller {
// should be accessible to both Admin and non-logged in user
public function view() {..}
// Following are only accessible to Admin
public function edit() {..}
public function create() {...}
}
我能想到的一种方法是在多个控制器中复制代码并处理特定于用户角色的视图。
您可以让您的 Admin_Blog_Controller
extend Blog_Controller
在用户构造函数中加载用户数据,并在 Admin 构造函数中检查管理员访问权限。
这样您的管理控制器也将包含 Blog_Controller 中的方法 view
。
class Admin_Blog_Controller extends Blog_Controller {
public function __construct() {
parent::__construct();
if( ! $this->ion_auth->is_admin()) {
redirect('/');
}
}
public function add() {
}
public function edit() {
}
// Optional, if you want to make custom behaviour
// in the view method of admin:
public function view() {
// Calling parent (Blog_Controller) view method.
parent::view();
// Doing our own stuff.
// ...
}
}
class Blog_Controller extends CI_Controller {
protected $the_user;
public function __construct() {
parent::__construct();
if($this->ion_auth->in_group('user') || $this->ion_auth->is_admin()) {
$this->the_user = $this->ion_auth->user()->row();
$data->the_user = $this->the_user;
$this->load->vars($data);
}
else {
redirect('/');
}
}
public function view() {
}
}
再次阅读您的 post,我意识到这可能不是您所要求的确切解决方案,因为您希望在一个控制器中实现所有功能?我不会删除我的答案,因为有人可能会觉得它仍然有用。
我正在使用 codeigniter 的 HMVC 模块扩展。阅读这篇精彩的文章 - Scalable Login System for CodeIgniter – Ion_Auth。 本文建议的是,我们可以创建多个自定义控制器,并根据登录用户的角色,将用户重定向到他各自的控制器。 例如,管理员将他重定向到扩展 Admin_Controller 的控制器,依此类推:
class Admin_Controller extends CI_Controller {
public function __construct() {
parent::__construct();
if( ! $this->ion_auth->is_admin()) {
redirect('/');
}
}
}
因此,我们可以在基本控制器级别本身为该控制器中的所有 methods/views 应用通用规则。这就是我有疑问的地方:如果我不想对该控制器内的所有视图进行全局控制怎么办。
或者换句话说 - 如果我需要对个别视图进行特定控制,该怎么做。例如 - 我有一个名为 'Blog' 的控制器和名为 - 'view'、'add'、'edit' 的方法。现在,视图适用于所有类型的用户,而只有管理员才能添加或编辑博客。这意味着我不能放置控制器级别的逻辑..
class Blog_Controller extends Admin_Controller {
// should be accessible to both Admin and non-logged in user
public function view() {..}
// Following are only accessible to Admin
public function edit() {..}
public function create() {...}
}
我能想到的一种方法是在多个控制器中复制代码并处理特定于用户角色的视图。
您可以让您的 Admin_Blog_Controller
extend Blog_Controller
在用户构造函数中加载用户数据,并在 Admin 构造函数中检查管理员访问权限。
这样您的管理控制器也将包含 Blog_Controller 中的方法 view
。
class Admin_Blog_Controller extends Blog_Controller {
public function __construct() {
parent::__construct();
if( ! $this->ion_auth->is_admin()) {
redirect('/');
}
}
public function add() {
}
public function edit() {
}
// Optional, if you want to make custom behaviour
// in the view method of admin:
public function view() {
// Calling parent (Blog_Controller) view method.
parent::view();
// Doing our own stuff.
// ...
}
}
class Blog_Controller extends CI_Controller {
protected $the_user;
public function __construct() {
parent::__construct();
if($this->ion_auth->in_group('user') || $this->ion_auth->is_admin()) {
$this->the_user = $this->ion_auth->user()->row();
$data->the_user = $this->the_user;
$this->load->vars($data);
}
else {
redirect('/');
}
}
public function view() {
}
}
再次阅读您的 post,我意识到这可能不是您所要求的确切解决方案,因为您希望在一个控制器中实现所有功能?我不会删除我的答案,因为有人可能会觉得它仍然有用。