335 lines
9.9 KiB
PHP
335 lines
9.9 KiB
PHP
<?php
|
|
defined('BASEPATH') or exit('No direct script access allowed');
|
|
|
|
class Acl extends Admin_Controller {
|
|
|
|
public $viewData = array();
|
|
|
|
public function __construct() {
|
|
parent::__construct();
|
|
|
|
$this->load->library('table');
|
|
$this->table->set_template($this->template);
|
|
$this->load->model('combo_model');
|
|
$this->load->model('Acl_model', 'acl');
|
|
// Load Pagination library
|
|
$this->load->library('pagination');
|
|
|
|
$controller = ($this->getController());
|
|
$options_array = array_combine($controller, $controller);
|
|
|
|
$this->viewData['controller_name'] = $this->combo_model->getControllerCombo('controller_name', $options_array, '');
|
|
$this->viewData['permission_level'] = $this->combo_model->getPermissionLevel('permission_level', '');
|
|
|
|
// filter
|
|
$options_array = array_merge($options_array, ['' => 'Select']);
|
|
ksort($options_array);
|
|
|
|
$this->viewData['card_class_filter'] = $this->combo_model->getControllerCombo('card_class_filter', $options_array, '');
|
|
$this->viewData['card_permission_level_filter'] = $this->combo_model->getPermissionLevel('card_permission_level_filter', '');
|
|
|
|
$this->viewData['msg'] = null;
|
|
}
|
|
|
|
protected function renderToolsPage($page_name, $data) {
|
|
$this->load->view('admin/view_admin_header', $data);
|
|
$this->load->view('acl/' . $page_name, $data);
|
|
$this->load->view('admin/view_admin_footer', $data);
|
|
}
|
|
|
|
private function getController() {
|
|
$path = __DIR__;
|
|
$controller = array();
|
|
|
|
$allFiles = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
|
|
$phpFiles = new RegexIterator($allFiles, '/\.php$/');
|
|
|
|
foreach ($phpFiles as $phpFile) {
|
|
$content = file_get_contents($phpFile->getRealPath());
|
|
$tokens = token_get_all($content);
|
|
$namespace = '';
|
|
|
|
for ($index = 0; isset($tokens[$index]); $index++) {
|
|
if (!isset($tokens[$index][0])) {
|
|
continue;
|
|
}
|
|
|
|
if (T_CLASS === $tokens[$index][0]) {
|
|
$index += 2; // Skip class keyword and whitespace
|
|
$controller[] = $tokens[$index][1];
|
|
}
|
|
}
|
|
}
|
|
sort($controller);
|
|
return $controller;
|
|
}
|
|
|
|
public function getMethodsByController() {
|
|
$controller = $this->input->post('controller');
|
|
|
|
$path = __DIR__;
|
|
$methods = array();
|
|
|
|
$phpFile = $path . '/' . $controller . '.php';
|
|
|
|
$content = file_get_contents($phpFile);
|
|
$tokens = token_get_all($content);
|
|
$namespace = '';
|
|
|
|
for ($index = 0; isset($tokens[$index]); $index++) {
|
|
if (!isset($tokens[$index][0])) {
|
|
continue;
|
|
}
|
|
|
|
if (T_FUNCTION === $tokens[$index][0]) {
|
|
$index += 2; // Skip class keyword and whitespace
|
|
|
|
if (!is_array($tokens[$index])) {
|
|
continue;
|
|
}
|
|
|
|
$methods[] = $tokens[$index][1];
|
|
}
|
|
}
|
|
|
|
sort($methods);
|
|
echo json_encode([
|
|
'methods' => $methods,
|
|
]);
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$this->renderToolsPage("view_acl", $this->viewData);
|
|
}
|
|
|
|
private function setFormRuleCreate()
|
|
{
|
|
$this->form_validation->set_rules('controller_name', 'Controller', 'required|max_length[50]');
|
|
$this->form_validation->set_rules('method_name', 'Method', 'required|max_length[50]');
|
|
$this->form_validation->set_rules('permission_level', 'Permission Level', 'required|callback_exists_permission_level|callback_check_duplicate_acl_and_permission_level');
|
|
}
|
|
|
|
private function getFormValue()
|
|
{
|
|
return [
|
|
'controller' => $this->input->post('controller_name'),
|
|
'method' => $this->input->post('method_name'),
|
|
'plevel' => $this->input->post('permission_level')
|
|
];
|
|
}
|
|
|
|
public function create() {
|
|
$this->load->helper(array('form', 'url'));
|
|
$this->load->database();
|
|
$this->load->library('form_validation');
|
|
$this->setFormRuleCreate();
|
|
|
|
$params = $this->getFormValue();
|
|
|
|
if ($this->form_validation->run() == false) {
|
|
$this->viewData['msg'] = validation_errors();
|
|
$this->renderToolsPage('view_acl', $this->viewData);
|
|
return;
|
|
}
|
|
|
|
$this->db->trans_start();
|
|
$this->db->trans_strict(FALSE);
|
|
|
|
$id = $this->acl->insert_acl($params);
|
|
$params = array_merge($params, [ 'bko_acl_id' => $id ]);
|
|
$this->acl->insert_acl_permission_level($params);
|
|
$this->acl->insert_acl_whitelist($params);
|
|
|
|
$this->db->trans_complete();
|
|
|
|
if ($this->db->trans_status() === FALSE) {
|
|
$this->db->trans_rollback();
|
|
$this->viewData['msg'] = "Insert Failed";
|
|
}
|
|
else {
|
|
$this->db->trans_commit();
|
|
$this->viewData['msg'] = "Insert Succesful";
|
|
}
|
|
|
|
$this->renderToolsPage('view_acl', $this->viewData);
|
|
}
|
|
|
|
public function check_duplicate_acl_and_permission_level() {
|
|
|
|
if ($this->acl->getRecordControllerMethodPlevel([
|
|
'controller' => $this->input->post('controller_name'),
|
|
'method' => $this->input->post('method_name'),
|
|
'plevel' => $this->input->post('permission_level')
|
|
])) {
|
|
$this->form_validation->set_message('check_duplicate_acl_and_permission_level', 'Oops !!! The value you entered is already in the list');
|
|
return FALSE;
|
|
} else {
|
|
return TRUE;
|
|
}
|
|
}
|
|
|
|
public function exists_permission_level() {
|
|
$permission_level = $this->input->post('permission_level');
|
|
|
|
if (!$permission_level) {
|
|
$this->form_validation->set_message('exists_permission_level', 'Please enter an existing permission');
|
|
return FALSE;
|
|
}
|
|
|
|
if (!$this->acl->getRecordByPermissionLevel([ 'plevel' => $permission_level])) {
|
|
$this->form_validation->set_message('exists_permission_level', 'Please enter an existing permission');
|
|
return FALSE;
|
|
} else {
|
|
return TRUE;
|
|
}
|
|
}
|
|
|
|
private function setFormRuleUpdate()
|
|
{
|
|
$this->form_validation->set_rules('id', 'Controller and Method', 'required|callback_exists_bko_acl');
|
|
$this->form_validation->set_rules('permission_level', 'Permission Level', 'callback_exists_permission_level');
|
|
}
|
|
|
|
public function exists_bko_acl($id)
|
|
{
|
|
if (!$id || !is_numeric($id)) {
|
|
$this->form_validation->set_message('exists_bko_acl', 'Please enter an existing controller and method');
|
|
return FALSE;
|
|
}
|
|
|
|
if (!$this->acl->gerRecordAclById(['id' => $id]))
|
|
{
|
|
$this->form_validation->set_message('exists_bko_acl', 'Please enter an existing controller and method');
|
|
return FALSE;
|
|
}
|
|
else
|
|
{
|
|
return TRUE;
|
|
}
|
|
}
|
|
|
|
public function update($bko_acl_id) {
|
|
|
|
$this->load->helper(array('form', 'url'));
|
|
$this->load->database();
|
|
$this->load->library('form_validation');
|
|
|
|
$this->form_validation->set_data(['id' => $bko_acl_id]);
|
|
$this->setFormRuleUpdate();
|
|
|
|
$params = $this->getFormValue();
|
|
$params = array_merge($params, ['bko_acl_id' => $bko_acl_id]);
|
|
|
|
if ($this->form_validation->run() == false) {
|
|
$this->viewData['msg'] = validation_errors();
|
|
$this->renderToolsPage('view_acl', $this->viewData);
|
|
return;
|
|
}
|
|
|
|
$this->viewData['msg'] = $this->acl->update_acl_permission_level($params) <> 1
|
|
? "Update Failed"
|
|
: "Update Successful" ;
|
|
|
|
$this->renderToolsPage('view_acl', $this->viewData);
|
|
}
|
|
|
|
private function setFormRuleDelete()
|
|
{
|
|
$this->form_validation->set_rules('id', 'Controller and Method', 'callback_exists_bko_acl');
|
|
}
|
|
|
|
public function destroy($id) {
|
|
|
|
$this->load->helper(array('form', 'url'));
|
|
$this->load->database();
|
|
$this->load->library('form_validation');
|
|
|
|
$this->form_validation->set_data(['id' => $id]);
|
|
$this->setFormRuleDelete();
|
|
|
|
if ($this->form_validation->run() == false) {
|
|
$this->viewData['msg'] = validation_errors();
|
|
$this->renderToolsPage('view_acl', $this->viewData);
|
|
return;
|
|
}
|
|
|
|
$this->viewData['msg'] = !$this->acl->deleteAclById($id)
|
|
? "Delete Failed"
|
|
: "Delete Successful" ;
|
|
|
|
$this->renderToolsPage('view_acl', $this->viewData);
|
|
}
|
|
|
|
private function setFormRuleSearchForm()
|
|
{
|
|
$this->form_validation->set_rules(
|
|
'card_permission_level_filter',
|
|
'Permission Level',
|
|
'numeric'
|
|
);
|
|
}
|
|
|
|
public function loadRecord(){
|
|
$rowno = $this->input->get('rowno');
|
|
parse_str($this->input->get('filters'), $filters);
|
|
$filters = array_filter($filters, function($val) {
|
|
return $val !== '';
|
|
});
|
|
|
|
$this->form_validation->set_data($filters);
|
|
$this->setFormRuleSearchForm();
|
|
$errors = [];
|
|
if ($this->form_validation->run() == false) {
|
|
$errors = $this->form_validation->error_array();
|
|
}
|
|
$filters = array_diff_key($filters, $errors);
|
|
|
|
// Row per page
|
|
$rowperpage = 10;
|
|
$cur_page = $rowno;
|
|
|
|
// Row position
|
|
if($rowno != 0){
|
|
$rowno = ($rowno-1) * $rowperpage;
|
|
}
|
|
|
|
// All records count
|
|
$allcount = $this->acl->getrecordCount($filters);
|
|
|
|
// Get records
|
|
$users_record = $this->acl->getData($rowno,$rowperpage,$filters);
|
|
|
|
// Pagination Configuration
|
|
$config['base_url'] = '/Acl/loadRecord';
|
|
$config['use_page_numbers'] = TRUE;
|
|
$config['total_rows'] = $allcount;
|
|
$config['per_page'] = $rowperpage;
|
|
$config['cur_page'] = $cur_page;
|
|
$config['full_tag_open'] = "<ul class='pagination'>";
|
|
$config['full_tag_close'] = "</ul>";
|
|
$config['num_tag_open'] = '<li>';
|
|
$config['num_tag_close'] = '</li>';
|
|
$config['cur_tag_open'] = "<li class='disabled'><li class='active'><a href='#'>";
|
|
$config['cur_tag_close'] = "<span class='sr-only'></span></a></li>";
|
|
$config['next_tag_open'] = "<li>";
|
|
$config['next_tagl_close'] = "</li>";
|
|
$config['prev_tag_open'] = "<li>";
|
|
$config['prev_tagl_close'] = "</li>";
|
|
$config['first_tag_open'] = "<li>";
|
|
$config['first_tagl_close'] = "</li>";
|
|
$config['last_tag_open'] = "<li>";
|
|
$config['last_tagl_close'] = "</li>";
|
|
|
|
// Initialize
|
|
$this->pagination->initialize($config);
|
|
|
|
// Initialize $data Array
|
|
$data['pagination'] = $this->pagination->create_links();
|
|
$data['result'] = $users_record;
|
|
$data['row'] = $rowno;
|
|
|
|
echo json_encode($data);
|
|
}
|
|
}
|