Lee*_*iss 1 php authorization cakephp
这是一个更多的问题,而不是解决特定问题的问题.
我第一次使用CakePHP,现在正在开发网站的管理部分.
作为MVC或CakePHP开发人员,您喜欢把管理功能放在哪里?
最初我将它们放在AdminController中,但后来改为将函数放在一个控制器中,该控制器用于被操作的数据类型.例如,我将用户列表/编辑放在UserController中.
对我来说这更有意义,因为UserController中可能存在可能有用的功能.
如果你留下回复,请你说几句话说明原因?也许这是一个有争议的问题.
小心,李
- 编辑
if ($this->Auth->user('user_type') == 1){//double-check the user is Admin
$this->Auth->allow('display');
$this->Auth->allow('watch');
Run Code Online (Sandbox Code Playgroud)
jos*_*ing 10
我认为这不是一个有争议的问题.将管理功能放在各自的控制器中(即,不是全部放在一个'admin'控制器中),并使用内置于Cake中的'admin'前缀路由来保证它们的安全.这是CakePHP批准的方法,CakePHP允许您通过Bake控制台以这种方式创建管理功能.
您可以使用AppController中的几行简单代码来保护所有以admin_为前缀的控制器功能,并且可以通过如下所示的整齐,一致的URL访问所有管理功能:http://www.example.com/admin/my_controller/my_function
这应该可以帮助您入门:http://book.cakephp.org/2.0/en/development/routing.html#prefix-routing
如果您需要更多帮助,请告诉我,我会更多信息更新我的回答.
编辑:更多信息......
以下是设置管理路由的一些步骤:
1 /在app/Config/core.php中,在第113行附近,确保此行存在并取消注释:
Configure::write('Routing.prefixes', array('admin'));
Run Code Online (Sandbox Code Playgroud)
2 /在app/Controller/AppController.php(即控制器超类)中,在beforeFilter方法中测试管理路由.不要在每个控制器的beforeFilter中执行此操作 - 这与DRY原则不一致.这是我之前的过滤方法作为示例:
function beforeFilter() {
if (isset($this->request->params['admin'])) {
// the user has accessed an admin function, so handle it accordingly.
$this->layout = 'admin';
$this->Auth->loginRedirect = array('controller'=>'users','action'=>'index');
$this->Auth->allow('login');
} else {
// the user has accessed a NON-admin function, so handle it accordingly.
$this->Auth->allow();
}
}
Run Code Online (Sandbox Code Playgroud)
3 /使用admin_前缀所有管理功能,它们应通过前缀路由自动提供.
例如.
function admin_dostuff () { echo 'hi from the admin function'; } // This will be available via http://www.example.com/admin/my_controller/dostuff
function dostuff () { echo 'hi from the NON-admin function'; } // This will be available via http://www.example.com/my_controller/dostuff
Run Code Online (Sandbox Code Playgroud)
一旦你完成了这个设置,你需要做的就是用admin_前缀管理功能,而Cake将为你处理所有这些.合理?
编辑2:
这是一些快速编写的示例代码,可以帮助您的情况.
function beforeFilter() {
if (isset($this->request->params['admin'])) {
// the user has accessed an admin_ function, so check if they are an admin.
if ($this->Auth->user('user_type') == 1){
// an Admin user has accessed an admin function. We can always allow that.
$this->Auth->allow();
} else {
// A non-admin user has accessed an admin function, so we shouldn't allow it.
// Here you can redirect them, or give an error message, or something
}
} else {
// the user has accessed a NON-admin function, so handle it however you want.
$this->Auth->allow(); // this example gives public access to all non-admin functions.
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2442 次 |
| 最近记录: |