1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
2
3 class Roles_Model extends CI_Model {
4 /**
5 * Class Constructor
6 */
7 public function __construct() {
8 parent::__construct();
9 }
10
11 /**
12 * Get the record id of a particular role.
13 *
14 * @param string $role_slug The selected role slug. Slugs are
15 * defined in the "application/config/constants.php" file.
16 * @return int Returns the database id of the roles record.
17 */
18 public function get_role_id($role_slug) {
19 return $this->db->get_where('ea_roles', array('slug' => $role_slug))->row()->id;
20 }
21
22 /**
23 * Returns all the privileges (bool values) of a role slug.
24 *
25 * The privilege numbers are converted into bool values of the four main actions (view,
26 * add, edit, delete). By checking each value you can know if the user is able to perform
27 * this action.
28 *
29 * @param string $slug The role slug.
30 * @return array Returns the privilege value.
31 */
32 public function get_privileges($slug) {
33 $privileges = $this->db->get_where('ea_roles', array('slug' => $slug))->row_array();
34 unset($privileges['id'], $privileges['name'], $privileges['slug'], $privileges['is_admin']);
35
36 // Convert the numeric values to bool so that is easier to check whether a
37 // user has the required privileges for a specific action.
38 foreach($privileges as &$value) {
39 $privileges_number = $value;
40
41 $value = array(
42 'view' => FALSE,
43 'add' => FALSE,
44 'edit' => FALSE,
45 'delete' => FALSE
46 );
47
48 if ($privileges_number > 0) {
49 if (intval($privileges_number / PRIV_DELETE) == 1) {
50 $value['delete'] = TRUE;
51 $privileges_number -= PRIV_DELETE;
52 }
53
54 if (intval($privileges_number / PRIV_EDIT) == 1) {
55 $value['edit'] = TRUE;
56 $privileges_number -= PRIV_EDIT;
57 }
58
59 if (intval($privileges_number / PRIV_ADD) == 1) {
60 $value['add'] = TRUE;
61 $privileges_number -= PRIV_ADD;
62 }
63
64 $value['view'] = TRUE;
65 }
66 }
67
68 return $privileges;
69 }
70 }
71
72 /* End of file roles_model.php */
73 /* Location: ./application/models/roles_model.php */