File manager - Edit - /home/wwwroot/camplus.hk/master.camplus.hk/public_html/application/libraries/Acl.php
Back
<?php class Acl { var $all_perms = array(); //Array : Stores the permissions for the user var $perms = array(); var $userID; //Integer : Stores the ID of the current user var $userRoles = array(); //Array : Stores the roles of the current user var $ci; var $perms_cb; function __construct() { $this->ci = &get_instance(); if($this->ci->session->userdata('id')) $config['id'] = $this->ci->session->userdata('id'); else $config = array(); $this->all_perms = json_decode(file_get_contents(APPPATH . "libraries/Permission.json")); $this->initAcl($config); } public function initAcl($config) { if(isset($config['id'])){ $this->userID = floatval($config['id']); $this->userRoles = $this->getUserRoles(); $this->buildACL(); } } function buildACL() { //first, get the rules for the user's role if (!empty($this->userRoles)) { $this->perms = array_merge($this->perms,$this->getRolePerms($this->userRoles)); } } function getPermKeyFromID($permID) { //$strSQL = "SELECT `permKey` FROM `".DB_PREFIX."permissions` WHERE `ID` = " . floatval($permID) . " LIMIT 1"; foreach ($this->all_perms as $permission) { if($permID == $permission->id) { return $permission->permission_key; } } } function getPermNameFromID($permID) { //$strSQL = "SELECT `permName` FROM `".DB_PREFIX."permissions` WHERE `ID` = " . floatval($permID) . " LIMIT 1"; foreach ($this->all_perms as $permission) { if($permID == $permission->id) { return $permission->permission_title; } } } function getPermNameFromKey($permKey) { //$strSQL = "SELECT `permName` FROM `".DB_PREFIX."permissions` WHERE `ID` = " . floatval($permID) . " LIMIT 1"; foreach ($this->all_perms as $permission) { if($permKey == $permission->permission_key) { return $permission->permission_title; } } } function getPermIDFromKey($permID) { //$strSQL = "SELECT `permName` FROM `".DB_PREFIX."permissions` WHERE `ID` = " . floatval($permID) . " LIMIT 1"; foreach ($this->all_perms as $permission) { if($permID == $permission->permission_key) { return $permission->id; } } } function getRoleNameFromID($roleID) { if($roleID == 0){ return 'Panel Head'; } //$strSQL = "SELECT `roleName` FROM `".DB_PREFIX."roles` WHERE `ID` = " . floatval($roleID) . " LIMIT 1"; $this->ci->db->select('role'); $this->ci->db->where('id',floatval($roleID),1); $sql = $this->ci->db->get('roles'); $data = $sql->result(); return $data[0]->role; } function getUserRoles() { //$strSQL = "SELECT * FROM `".DB_PREFIX."user_roles` WHERE `userID` = " . floatval($this->userID) . " ORDER BY `addDate` ASC"; $this->ci->db->where(array('user_id'=>floatval($this->userID))); $sql = $this->ci->db->get('users_roles'); $data = $sql->result(); //echo $this->ci->db->last_query(); $resp = array(); foreach( $data as $row ) { $resp[] = $row->role_id; } return $resp; } function getAllRoles($format='ids',$module='',$active_modules='') { $format = strtolower($format); //$strSQL = "SELECT * FROM `".DB_PREFIX."roles` ORDER BY `roleName` ASC"; $this->ci->db->order_by('role','asc'); $sql = $this->ci->db->get('roles'); $data = $sql->result(); $resp = array(); if ($format == 'full') { $resp[] = array("id" => 0 ,"name" => 'Panel Head', 'is_approval' => 1); } else { $resp[] = 0; } foreach( $data as $row ) { if ($format == 'full') { $resp[] = array("id" => $row->id,"name" => $row->role, 'is_approval' => $row->is_approval_role); } else { $resp[] = $row->id; } } return $resp; } function getAllPerms($format='ids', $module = "",$active_modules='') { $format = strtolower($format); $resp = array(); foreach ($this->all_perms as $perms) { if ($format == 'full') { $resp[$perms->permission_key] = array('id' => $perms->id, 'name' => $perms->permission_title, 'key' => $perms->permission_key, 'module' => $perms->module); } else { $resp[] = $perms->id; } } return $resp; } function getRolePerms($role) { if (is_array($role)) { if(empty($role)){return;} $this->ci->db->where_in('role_id',$role); } else { $this->ci->db->where(array('role_id'=>floatval($role))); } $this->ci->db->order_by('id','asc'); $sql = $this->ci->db->get('role_acl'); //$this->db->select($roleSQL); $roles = $sql->result(); $data_perm = array(); foreach($roles as $role) { $role_perm = json_decode($role->permission,1); $data_perm = array_merge($data_perm,$role_perm); } $perms = array(); foreach ($this->all_perms as $pK) { if(in_array($pK->permission_key,$data_perm)!==false) $hP = true; else $hP = false; $perms[$pK->permission_key] = array('perm' => $pK->permission_key,'inheritted' => true,'value' => $hP,'name' => $pK->permission_title,'id' => $pK->id); } return $perms; } function hasRole($roleID) { foreach($this->userRoles as $k => $v) { if (floatval($v) === floatval($roleID)) { return true; } } return false; } function hasPermission($permKey, $data= array()) { if($this->ci->session->userdata('id')==1 || $this->ci->session->userdata('username')=='admin') return true; if($this->perms[$permKey]['value']==1) { return true; } else { return false; } } function getPermsForRoles($module = ''){ $data = $this->ci->db->get('role_acl')->result_array(); $p_data = array(); foreach ($data as $role) { $permissions = json_decode($role['permission'],1); foreach($permissions as $perm) { $p_data[$this->getPermIDFromKey($perm)][$role['role_id']] = 1; } } return $p_data; } function createRole($data){ return $this->ci->db->insert('roles',$data); } function assignPermsToRole($perm_id, $role_id){ $data = $this->ci->db->get_where('role_acl',array('role_id'=>$role_id))->row(); $curr_perm = array(); if(!empty($data)) { $curr_perm = json_decode($data->permission,1); } $permKey = $this->getPermKeyFromID($perm_id); $curr_perm[] = $permKey; if(!empty($data)) { return $this->ci->db->update('role_acl',array('permission'=>json_encode($curr_perm)),array('role_id'=>$role_id)); } else { return $this->ci->db->insert('role_acl',array('permission'=>json_encode($curr_perm),'role_id'=>$role_id)); } } function action($id, $table, $action){ switch($action){ case 'delete': return $this->ci->db->delete($table, array('id' => $id)); break; } } }
| ver. 1.4 |
Github
|
.
| PHP 7.2.34 | Generation time: 0.19 |
proxy
|
phpinfo
|
Settings