我希望在我的应用程序中添加一些修改,以便在我的MY_Controller上检查是否允许用户访问当前页面。这是我的一个控制器的示例。我的所有人都有阅读,编辑,创建,删除功能。我只需要弄清楚如何全局设置权限以允许或禁止用户访问除了每个函数的if语句之外的函数。
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
class Content_pages extends MY_Controller
{
/**
* Account::__construct()
*
* Load the parent construct and any additional models, helper, libraries available.
*
* @return void
*/
public function __construct()
{
parent::__construct();
$this->load->model('content_page_model', 'content_page');
}
/**
* Content_pages::read()
*
* @return
*/
public function read()
{
//vardump($this->user_data);
// Checks to see if the user has a role id of four and if they do then it shows the admin dashboard and if not then shows the user dashboard.
if ($this->user_data->access_level_id >= 4)
{
// Retrieve all the users from the database that handle characters and assign it to the users variable.
$content_pages = $this->content_page->get_all();
// Place to dump the users array to verify it is the expected value.
// vardump($users);
// Checks to verify that there is data inside of the users array and that it is not empty.
if (!empty($content_pages))
{
$this->template->set('content_pages', $content_pages);
}
// Add the breadcrumbs to the view.
$this->breadcrumb->add_crumb('<li><a href="' . base_url() . 'wrestling-manager/control-panel" class="glyphicons home"><i></i> Control Panel</a></li>');
$this->breadcrumb->add_crumb('<li><i></i> Content Pages</li>');
$this->breadcrumb->change_link('<li class="divider"></li>');
// Sets all the properites for the template view.
$this->template
->set_theme('smashing')
->set_layout('control_panel_view')
->set_partial('header', 'partials/header')
->set_partial('sidebar','partials/sidebar')
->set_partial('footer', 'partials/footer')
->title('Content Pages')
->set('user_data', $this->user_data)
->build('content_pages_view');
}
else
{
echo 'haha';
//redirect('wrestling-manager/control-panel');
}
}
/**
* Content_pages::edit()
*
* @return void
*/
public function create()
{
echo 'testing for create function';
}
/**
* Content_pages::edit()
*
* @return void
*/
public function edit($content_page_id)
{
vardump($content_page_id);
}
public function delete($content_page_id)
{
vardump($content_page_id);
}
/**
* Content_pages::save()
*
* @return
*/
public function save()
{
echo 'testing for save function';
}
/**
* Content_pages::update()
*
* @return
*/
public function update()
{
echo 'testing for update function';
}
}
答案 0 :(得分:1)
嗯,这就是MY_Controller的全部内容 - 你必须在那里进行所有检查 - 在那里创建函数并根据URL和/或GET / POST / SESSION参数在构造函数中调用它们。如果用户无权访问它,只需在SESSION中添加一些标记或错误文本。然后在主控制器中仅检查该SESSION标志。
答案 1 :(得分:1)
您可以在配置文件或数据库中设置权限。
通过权限检查,您可能最好在调用任何控制器之前使用拦截器/过滤器。
对于控制器,我不得不说你做错了,因为它们通常不打算执行CRUD操作,而是更高级别的域特定操作(或者,在更低级别的情况下,一个常见的handleRequest
方法。)
然后,您可以通过AuthorizationService
检查当前用户是否被允许做某事。例如,该服务可以迭代所有权限,给定操作需要验证当前用户的角色是否具有这些权限;例如:
class AuthorizationFilter {
public function verifyAccess($user, $request) {
$role = $user->getRole();
$permissions = $authorization->getPermissionsFor($request);
$allowed = true; // true as a missing permission will later set it to false
for ($i = 0; $i < size($permissions); $i++) {
$allowed &= $role->hasPermission($permissions[$i]);
}
return $allowed;
}
}
之后,您可以根据授权结果调用原始请求的控制器或“后备”控制器,例如:
class RequestDispatcher {
public function dispatch() {
// ...
if ($authFilter->verifyAccess($user, $request)) {
// invoke proper request controller
} else {
// invoke "you're not allowed to do this" controller
}
// ...
}
}
警告:以上代码仅为示例代码,并非完整或适合生产环境!!!