我正在添加一些修改到我的應用程序,以便在我的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';
}
}
這是什麼發生在這個鏈接? https://github.com/EllisLab/CodeIgniter/wiki/Permission-Class – user2576961
@ user2576961上帝,我討厭CodeIgniter :)嗯,它是類似的東西。如果仔細查看鏈接的代碼,您可以在每個方法的上方看到每種方法的摘要。顯示的解決方案非常醜陋,不專業,但概念是相同的。您在服務之前攔截請求並首先詢問訪問控制設施,以決定是允許還是不允許用戶請求的操作。舉例來說,它在控制器內部執行。我會推薦它(和一般的CI),但它會成爲你品味的問題。 – Powerslave
夠公平的。我感謝您的建議和意見。 – user2576961