/application/models/rule_model.php

https://gitlab.com/fredec/ionizecms-1.0.8.x · PHP · 461 lines · 257 code · 89 blank · 115 comment · 36 complexity · f0e04bbadd80c08501feaaa6d605e786 MD5 · raw file

  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * Ionize
  4. *
  5. * @package Ionize
  6. * @author Ionize Dev Team
  7. * @license http://doc.ionizecms.com/en/basic-infos/license-agreement
  8. * @link http://ionizecms.com
  9. * @since Version 1.0.0
  10. */
  11. // ------------------------------------------------------------------------
  12. /**
  13. * Ionize Permission Model
  14. *
  15. * @package Ionize
  16. * @subpackage Models
  17. * @category Authority
  18. * @author Ionize Dev Team
  19. *
  20. */
  21. class rule_model extends Base_model
  22. {
  23. /**
  24. * Constructor
  25. *
  26. * @access public
  27. */
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. $this->table = 'rule';
  32. }
  33. // ------------------------------------------------------------------------
  34. /**
  35. * @param $role
  36. *
  37. * @return array
  38. */
  39. public function get_from_role($role)
  40. {
  41. $where = array(
  42. 'id_role' => $role['id_role'],
  43. 'id_user' => 0
  44. );
  45. $rules = $this->get_list($where);
  46. return $rules;
  47. }
  48. // ------------------------------------------------------------------------
  49. /**
  50. * @param $user
  51. *
  52. * @return array
  53. */
  54. public function get_from_user($user)
  55. {
  56. $rules = array();
  57. $role = $user->get_role();
  58. $this->{$this->db_group}->where(
  59. array(
  60. 'id_role' => $role['id_role'],
  61. 'id_user' => 0
  62. )
  63. );
  64. $role_rules = array();
  65. $this->{$this->db_group}->select('resource,actions,permission,id_element', FALSE);
  66. $query = $this->{$this->db_group}->get($this->get_table());
  67. if ( $query->num_rows() > 0 ) $role_rules = $query->result_array();
  68. $this->{$this->db_group}->where(
  69. array(
  70. 'id_role' => 0,
  71. 'id_user' => $user->getId()
  72. )
  73. );
  74. $user_rules = array();
  75. $this->{$this->db_group}->select('resource,actions,permission,id_element', FALSE);
  76. $query = $this->{$this->db_group}->get($this->get_table());
  77. if ( $query->num_rows() > 0 ) $user_rules = $query->result_array();
  78. // Process Role's rules
  79. foreach($role_rules as $rr)
  80. {
  81. $found = FALSE;
  82. foreach ($user_rules as $ur)
  83. {
  84. if ($rr['resource'] == $ur['resource'] && $rr['id_element'] == $ur['id_element'])
  85. {
  86. $found = TRUE;
  87. if ($ur['permission'] == 1) $rules[] = $ur;
  88. }
  89. }
  90. if ( ! $found) $rules[] = $rr;
  91. }
  92. // Process User's Rules : Add missing to rules list
  93. foreach ($user_rules as $ur)
  94. {
  95. $found = FALSE;
  96. foreach($rules as $r)
  97. {
  98. if ($r['resource'] == $ur['resource'] && $r['id_element'] == $ur['id_element'])
  99. $found = TRUE;
  100. }
  101. if ( ! $found) $rules[] = $ur;
  102. }
  103. return $rules;
  104. }
  105. // ------------------------------------------------------------------------
  106. /**
  107. * @param null $type
  108. *
  109. * @return array
  110. */
  111. public function get_from_type($type=NULL)
  112. {
  113. $where = NULL;
  114. if ( ! is_null($type))
  115. {
  116. $where = array("resource LIKE '".$type."/%'");
  117. }
  118. return $this->get_list($where);
  119. }
  120. // ------------------------------------------------------------------------
  121. /**
  122. * Returns array of Roles IDs which have access to this resource
  123. *
  124. * HERE
  125. * HERE
  126. *
  127. * @TODO : Be more detailed : Include actions.
  128. * @param $element
  129. * @param $element_id
  130. * @param string $type
  131. * @param int $permission
  132. *
  133. * @return array
  134. */
  135. public function get_element_role_ids($element, $element_id, $type='frontend', $permission=1)
  136. {
  137. $resource = $type . '/' . $element . '/' . $element_id;
  138. $where = array(
  139. 'resource' => $resource,
  140. 'permission' => $permission
  141. );
  142. return $this->get_group_concat_array(
  143. 'id_role',
  144. $where
  145. );
  146. }
  147. // ------------------------------------------------------------------------
  148. /**
  149. * @param $rules
  150. *
  151. * @return array
  152. */
  153. public function format_rules($rules)
  154. {
  155. $data = array();
  156. foreach($rules as $rule)
  157. {
  158. $resource = $rule['resource'];
  159. $actions = explode(',', $rule['actions']);
  160. // TODO : Check roles priority...
  161. if ( ! in_array($resource, array_keys($data)))
  162. {
  163. $data[$resource] = array(
  164. 'actions' => $actions,
  165. 'permission' => $rule['permission']
  166. );
  167. }
  168. }
  169. return $data;
  170. }
  171. // ------------------------------------------------------------------------
  172. /**
  173. * @param $id_role
  174. */
  175. public function set_all_permissions($id_role)
  176. {
  177. $this->delete(array('id_role'=>$id_role));
  178. $data = array(
  179. 'id_role' => $id_role,
  180. 'resource' => 'all',
  181. 'permission' => 1,
  182. );
  183. $this->insert($data);
  184. }
  185. // ------------------------------------------------------------------------
  186. /**
  187. * Saves rules for one element
  188. *
  189. * @param $resource
  190. * @param $rules
  191. */
  192. public function save_element_roles_rules($resource, $rules)
  193. {
  194. $data = array();
  195. if ( ! empty($rules))
  196. {
  197. foreach($rules as $id_role => $role_rules)
  198. {
  199. $resource_actions = array();
  200. foreach($role_rules as $rule)
  201. {
  202. $array = explode(':', $rule);
  203. $resource = $array[0];
  204. $action = isset($array[1]) ? $array[1] : NULL;
  205. // Resource / Actions array
  206. $actions = isset($resource_actions[$resource]) ? $resource_actions[$resource] : array();
  207. if ( ! is_null($action))
  208. $actions[]=$action;
  209. $resource_actions[$resource] = $actions;
  210. }
  211. foreach($resource_actions as $resource => $actions)
  212. {
  213. $data[] = array(
  214. 'id_role' => $id_role,
  215. 'resource' => $resource,
  216. 'actions' => implode(',', $actions),
  217. 'permission' => 1,
  218. );
  219. }
  220. }
  221. }
  222. $this->delete_element_roles_rules($resource);
  223. if ( ! empty($data))
  224. {
  225. $this->{$this->db_group}->insert_batch($this->get_table(), $data);
  226. }
  227. }
  228. // ------------------------------------------------------------------------
  229. /**
  230. * Save admin & module rules.
  231. *
  232. * @param $id_role
  233. * @param $rules
  234. * @param $type
  235. */
  236. public function save_rules($id_role, $rules, $type)
  237. {
  238. self::$ci->load->model('resource_model', '', TRUE);
  239. $this->_delete_role_rules($id_role, $type);
  240. $data = $resource_actions = array();
  241. if ( ! empty($rules))
  242. {
  243. foreach($rules as $rule)
  244. {
  245. // Only if type is found
  246. if (strpos($rule, $type) === 0)
  247. {
  248. $array = explode(':', $rule);
  249. $resource = $array[0];
  250. $action = isset($array[1]) ? $array[1] : NULL;
  251. // Resource / Actions array
  252. $actions = isset($resource_actions[$resource]) ? $resource_actions[$resource] : array();
  253. if ( ! is_null($action))
  254. $actions[]=$action;
  255. $resource_actions[$resource] = $actions;
  256. }
  257. }
  258. }
  259. foreach($resource_actions as $resource => $actions)
  260. {
  261. $data[] = array(
  262. 'id_role' => $id_role,
  263. 'resource' => $resource,
  264. 'actions' => implode(',', $actions),
  265. 'permission' => 1,
  266. );
  267. }
  268. $all_resources = self::$ci->resource_model->get_all_resources();
  269. $this->_add_parent_resources_for_save($data, array_keys($resource_actions), $all_resources, $id_role);
  270. if ( ! empty($data))
  271. {
  272. $this->{$this->db_group}->insert_batch($this->get_table(), $data);
  273. }
  274. }
  275. // ------------------------------------------------------------------------
  276. /**
  277. * Deletes all rules concerning all roles for one resource,
  278. * depending on the current User's level.
  279. *
  280. * @param $resource
  281. * ex : 'backend/page/8'
  282. *
  283. * @return mixed
  284. */
  285. public function delete_element_roles_rules($resource)
  286. {
  287. // First get all roles_ids from roles under the current logged in user
  288. $role_ids = $this->get_group_concat_array(
  289. 'id_role',
  290. array(
  291. 'role_level <=' => User()->get('role_level'),
  292. ),
  293. 'role'
  294. );
  295. // Filter on roles
  296. if ( ! empty($role_ids))
  297. $this->{$this->db_group}->where_in('id_role', $role_ids);
  298. $this->{$this->db_group}->where('resource', $resource);
  299. return $this->{$this->db_group}->delete($this->get_table());
  300. }
  301. // ------------------------------------------------------------------------
  302. /**
  303. * @param $data
  304. * @param $resources
  305. * @param $all
  306. * @param $id_role
  307. */
  308. protected function _add_parent_resources_for_save(&$data, $resources, $all, $id_role)
  309. {
  310. $new_resources = array();
  311. foreach($resources as $resource)
  312. {
  313. foreach($all as $rec)
  314. {
  315. if ($resource == $rec['resource'] && ! is_null($rec['id_parent']))
  316. {
  317. foreach($all as $recParent)
  318. {
  319. if ($rec['id_parent'] == $recParent['id_resource'])
  320. {
  321. $new_resources[] = $recParent['resource'];
  322. $found = FALSE;
  323. foreach($data as $d)
  324. {
  325. if ($d['resource'] == $recParent['resource'])
  326. {
  327. $found = TRUE;
  328. break;
  329. }
  330. }
  331. if ( ! $found)
  332. {
  333. $data[] = array(
  334. 'id_role' => $id_role,
  335. 'resource' => $recParent['resource'],
  336. 'actions' => '',
  337. 'permission' => 1,
  338. );
  339. }
  340. }
  341. }
  342. }
  343. }
  344. $this->_add_parent_resources_for_save($data, $new_resources, $all, $id_role);
  345. }
  346. }
  347. // ------------------------------------------------------------------------
  348. /**
  349. * @param $id_role
  350. * @param $type
  351. *
  352. * @return mixed
  353. */
  354. protected function _delete_role_rules($id_role, $type)
  355. {
  356. $this->{$this->db_group}->where('id_role', $id_role);
  357. if ($type != 'all')
  358. {
  359. // Everything but starting with 'admin/' or 'module/'
  360. switch ($type)
  361. {
  362. case 'admin':
  363. $this->{$this->db_group}->where("substr(resource, 1, 5) = 'admin'");
  364. break;
  365. default:
  366. $this->{$this->db_group}->where("resource LIKE '".$type."/%'");
  367. break;
  368. }
  369. }
  370. return $this->{$this->db_group}->delete($this->get_table());
  371. }
  372. }