You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

225 lines
7.6 KiB

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class UserRole extends CI_Model {
private $_table = "user_role";
private $_privs_table = "user_role_privs";
private $_attributes = [
'role_id' => "",
'name' => "",
'privs' => [],
];
public function __construct() {
parent::__construct();
}
public function all($type = "normal")
{
$this->db->select("r.role_id, r.name, p.privs");
// $this->db->from("{$this->_table} r");
$this->db->join("{$this->_privs_table} p", "r.role_id = p.role_id");
$this->db->where_not_in("r.role_id", [
USER_ROLES['superadmin'],
USER_ROLES['regular'],
// USER_ROLES['student'],
// USER_ROLES['shareholder'],
// USER_ROLES['sponsored']
]);
$this->db->where('r.deletedAt IS NULL');
$this->db->where('p.deletedAt IS NULL');
// $query = ($this->db->query('SELECT * FROM user_role WHERE role_id NOT IN ? ORDER BY role_id DESC', [
// [USER_ROLES['superadmin'],
// USER_ROLES['subscriber']]
// ]));
if ($type == "normal") {
return ($this->db->get("{$this->_table} r"))->result();
} else {
$draw = intval($this->input->post("draw"));
$start = intval($this->input->post("start"));
$length = intval($this->input->post("length"));
$this->db->group_start();
if ($this->input->post("search")) {
$this->db->like("name", $this->input->post("search")["value"]);
$this->db->or_like("privs", $this->input->post("search")["value"]);
}
if ($this->input->post("order")) {
$this->db->order_by("role_id", "ASC");
}
$this->db->group_end();
$query = $this->db->get("{$this->_table} r");
$data = [];
foreach ($query->result() as $r) {
$data[] = [
"role_id" => $r->role_id,
"name" => $r->name,
"privs" => $r->privs
];
}
return [
"draw" => $draw,
"recordsTotal" => $query->num_rows(),
"recordsFiltered" => $query->num_rows(),
"data" => $data
];
}
}
public function __set($property, $value) {
$this->_attributes[$property] = $value;
}
public function attribute($property) {
return $this->_attributes[$property];
}
public function allAttributes() {
return $this->_attributes;
}
public function roles() {
// Get all roles
$this->db->select('role_id, name');
$this->db->where('isBOUser', 1);
$this->db->where('deletedAt IS NULL');
$this->db->where_not_in("role_id", [
USER_ROLES['superadmin'],
USER_ROLES['regular'],
// USER_ROLES['student'],
// USER_ROLES['shareholder'],
// USER_ROLES['sponsored']
]);
return $this->db->get($this->_table)->result();
}
public function save() {
if ($this->hasDuplicate()) {
return [
"mtype" => "error",
"message" => "Duplicate entry"
];
}
foreach (PAGE_CODE as $page => $code) {
if (!array_key_exists($code, $this->attribute('privs'))) {
$this->_attributes["privs"][$code] = [];
}
}
/* Save user role */
$saveRole = $this->db->query(
"INSERT INTO {$this->_table} (name) VALUES (?)", [
$this->attribute('name'),
]);
if ($saveRole) {
$role_id = $this->db->insert_id();
/* Set role privileges */
$saveRolePrivs = $this->db->query(
"INSERT INTO {$this->_privs_table} (role_id, privs) VALUES (?, ?)", [
$role_id,
json_encode($this->attribute('privs'))
]);
return true;
}
}
public function update() {
if ($this->hasDuplicate()) {
return [
"mtype" => "error",
"message" => "Duplicate entry"
];
}
foreach (PAGE_CODE as $page => $code) {
if (!array_key_exists($code, $this->attribute('privs'))) {
$this->_attributes["privs"][$code] = [];
}
}
/* Save user role */
$saveRole = $this->db->query(
"UPDATE {$this->_table} SET name = ? WHERE role_id = ?", [
$this->attribute('name'),
$this->attribute('role_id'),
]);
if ($saveRole) {
/* Update role privileges */
$saveRolePrivs = $this->db->query(
"UPDATE {$this->_privs_table} SET privs = ? WHERE role_id = ?", [
json_encode($this->attribute('privs')),
$this->attribute('role_id'),
]);
return true;
}
}
public function delete()
{
$this->db->set('deletedAt', 'NOW()', false);
$this->db->where('role_id', $this->attribute('role_id'));
$this->db->update($this->_table);
$this->db->set('deletedAt', 'NOW()', false);
$this->db->where('role_id', $this->attribute('role_id'));
$deleted = $this->db->update($this->_privs_table);
return true;
}
public function hasUserDependency() {
// Check user dependency before deleting
$this->db->select('role_id');
$this->db->where('role_id', $this->attribute('role_id'));
$this->db->where_not_in('status', [0]);
$this->db->limit(1);
return $this->db->get('user')->num_rows();
}
public function hasDuplicate() {
if ($this->attribute('role_id') != '') {
/* Do not let updates to superadmin account */
if ($this->attribute('role_id') == USER_ROLES['superadmin']) {
return false;
}
$record = $this->db->query(
"SELECT * FROM {$this->_table} WHERE name = ? AND role_id != ? AND deletedAt IS NULL", [
$this->attribute('name'),
$this->attribute('role_id'),
]);
} else {
$record = $this->db->query(
"SELECT * FROM {$this->_table} WHERE name = ? AND deletedAt IS NULL", [
$this->attribute('name'),
]);
}
return $record->num_rows() > 0 ? true:false;
}
public function allBORoles() {
return $this->db->query("SELECT role_id FROM {$this->_table} WHERE isBOUser = 1 AND deletedAt IS NULL")->result_array();
}
public function roleHasPrivelege($role_id) {
return $this->db->query("SELECT * FROM {$this->_privs_table} WHERE role_id = ? AND deletedAt IS NULL", [$role_id])->row_array();
}
public function allRolePriveleges() {
return $this->db->query("SELECT * FROM {$this->_privs_table} WHERE deletedAt IS NULL")->result_array();
}
public function deleteUserRolePrivs($role_id) {
$this->db->query("DELETE FROM {$this->_privs_table} WHERE role_id = ?", [$role_id]);
}
public function storeUserRolePrivs($role_id, $privs) {
$this->db->query(
"INSERT INTO {$this->_privs_table}(role_id, privs) VALUES(?, ?)", [
$role_id, $privs
]
);
}
}