KumbiaPHP  beta2
Framework PHP
 Todo Estructuras de Datos Namespaces Archivos Funciones Variables Páginas
acl.php
Ir a la documentación de este archivo.
1 <?php
23 include CORE_PATH . 'libs/acl/role/role.php';
24 
28 include CORE_PATH . 'libs/acl/resource/resource.php';
29 
52 class Acl
53 {
54 
60  private $roles_names = array();
66  private $roles = array();
72  private $resources = array();
78  public $access = array();
84  private $role_inherits = array();
90  private $resources_names = array('*');
96  private $access_list = array('*' => array('*'));
97 
111  public function add_role(AclRole $roleObject, $access_inherits='')
112  {
113  if (in_array($roleObject->name, $this->roles_names)) {
114  return false;
115  }
116  $this->roles[] = $roleObject;
117  $this->roles_names[] = $roleObject->name;
118  $this->access[$roleObject->name]['*']['*'] = 'A';
119  if ($access_inherits) {
120  $this->add_inherit($roleObject->name, $access_inherits);
121  }
122  }
123 
130  public function add_inherit($role, $role_to_inherit)
131  {
132  if (!in_array($role, $this->roles_names)) {
133  return false;
134  }
135  if ($role_to_inherit != '') {
136  if (is_array($role_to_inherit)) {
137  foreach ($role_to_inherit as $rol_in) {
138  if ($rol_in == $role) {
139  return false;
140  }
141  if (!in_array($rol_in, $this->roles_names)) {
142  throw new KumbiaException("El Rol '{$rol_in}' no existe en la lista");
143 
144  }
145  $this->role_inherits[$role][] = $role_in;
146  }
147  $this->rebuild_access_list();
148  } else {
149  if ($role_to_inherit == $role) {
150  return false;
151  }
152  if (!in_array($role_to_inherit, $this->roles_names)) {
153  throw new KumbiaException("El Rol '{$role_to_inherit}' no existe en la lista");
154 
155  }
156  $this->role_inherits[$role][] = $role_to_inherit;
157  $this->rebuild_access_list();
158  }
159  } else {
160  throw new KumbiaException("Debe especificar un rol a heredar en Acl::add_inherit");
161 
162  }
163  }
164 
172  public function is_role($role_name)
173  {
174  return in_array($role_name, $this->roles_names);
175  }
176 
184  public function is_resource($resource_name)
185  {
186  return in_array($resource_name, $this->resources_names);
187  }
188 
207  public function add_resource(AclResource $resource)
208  {
209  if (!in_array($resource->name, $this->resources)) {
210  $this->resources[] = $resource;
211  $this->access_list[$resource->name] = array();
212  $this->resources_names[] = $resource->name;
213  }
214  if (func_num_args() > 1) {
215  $access_list = func_get_args();
216  unset($access_list[0]);
217  $this->add_resource_access($resource->name, $access_list);
218  }
219  }
220 
227  public function add_resource_access($resource, $access_list)
228  {
229  if (is_array($access_list)) {
230  foreach ($access_list as $access_name) {
231  if (!in_array($access_name, $this->access_list[$resource])) {
232  $this->access_list[$resource][] = $access_name;
233  }
234  }
235  } else {
236  if (!in_array($access_list, $this->access_list[$resource])) {
237  $this->access_list[$resource][] = $access_list;
238  }
239  }
240  }
241 
248  public function drop_resource_access($resource, $access_list)
249  {
250  if (is_array($access_list)) {
251  foreach ($access_list as $access_name) {
252  if (in_array($access_name, $this->access_list[$resource])) {
253  foreach ($this->access_list[$resource] as $i => $access) {
254  if ($access == $access_name) {
255  unset($this->access_list[$resource][$i]);
256  }
257  }
258  }
259  }
260  } else {
261  if (in_array($access_list, $this->access_list[$resource])) {
262  foreach ($this->access_list[$resource] as $i => $access) {
263  if ($access == $access_list) {
264  unset($this->access_list[$resource][$i]);
265  }
266  }
267  }
268  }
269  $this->rebuild_access_list();
270  }
271 
296  public function allow($role, $resource, $access)
297  {
298  if (!in_array($role, $this->roles_names)) {
299  throw new KumbiaException("No existe el rol '$role' en la lista");
300 
301  }
302  if (!in_array($resource, $this->resources_names)) {
303  throw new KumbiaException("No existe el resource '$resource' en la lista");
304 
305  }
306  if (is_array($access)) {
307  foreach ($access as $acc) {
308  if (!in_array($acc, $this->access_list[$resource])) {
309  throw new KumbiaException("No existe el acceso '$acc' en el resource '$resource' de la lista");
310 
311  }
312  }
313  foreach ($access as $acc) {
314  $this->access[$role][$resource][$acc] = 'A';
315  }
316  } else {
317  if (!in_array($access, $this->access_list[$resource])) {
318  throw new KumbiaException("No existe el acceso '$access' en el resource '$resource' de la lista");
319 
320  }
321  $this->access[$role][$resource][$access] = 'A';
322  $this->rebuild_access_list();
323  }
324  }
325 
350  public function deny($role, $resource, $access)
351  {
352  if (!in_array($role, $this->roles_names)) {
353  throw new KumbiaException("No existe el rol '$role' en la lista");
354 
355  }
356  if (!in_array($resource, $this->resources_names)) {
357  throw new KumbiaException("No existe el resource '$resource' en la lista");
358 
359  }
360  if (is_array($access)) {
361  foreach ($access as $acc) {
362  if (!in_array($acc, $this->access_list[$resource])) {
363  throw new KumbiaException("No existe el acceso '$acc' en el resource '$resource' de la lista");
364 
365  }
366  }
367  foreach ($access as $acc) {
368  $this->access[$role][$resource][$acc] = 'D';
369  }
370  } else {
371  if (!in_array($access, $this->access_list[$resource])) {
372  throw new KumbiaException("No existe el acceso '$access' en el resource '$resource' de la lista");
373 
374  }
375  $this->access[$role][$resource][$access] = 'D';
376  $this->rebuild_access_list();
377  }
378  }
379 
399  public function is_allowed($role, $resource, $access_list)
400  {
401  if (!in_array($role, $this->roles_names)) {
402  throw new KumbiaException("El rol '$role' no existe en la lista en acl::is_allowed");
403 
404  }
405  if (!in_array($resource, $this->resources_names)) {
406  throw new KumbiaException("El resource '$resource' no existe en la lista en acl::is_allowed");
407 
408  }
409  if (is_array($access_list)) {
410  foreach ($access_list as $access) {
411  if (!in_array($access, $this->access_list[$resource])) {
412  throw new KumbiaException("No existe en acceso '$access' en el resource '$resource' en acl::is_allowed");
413 
414  }
415  }
416  } else {
417  if (!in_array($access_list, $this->access_list[$resource])) {
418  throw new KumbiaException("No existe en acceso '$access_list' en el resource '$resource' en acl::is_allowed");
419 
420  }
421  }
422 
423  /* foreach($this->access[$role] as ){
424 
425  } */
426  // FIXME: Por lo pronto hacemos esta validación, luego se mejorará
427  if (!isset($this->access[$role][$resource][$access_list]))
428  return false;
429  if ($this->access[$role][$resource][$access_list] == "A")
430  return true;
431  }
432 
439  private function rebuild_access_list()
440  {
441  for ($i = 0; $i <= ceil(count($this->roles) * count($this->roles) / 2); $i++) {
442  foreach ($this->roles_names as $role) {
443  if (isset($this->role_inherits[$role])) {
444  foreach ($this->role_inherits[$role] as $role_inherit) {
445  if (isset($this->access[$role_inherit])) {
446  foreach ($this->access[$role_inherit] as $resource_name => $access) {
447  foreach ($access as $access_name => $value) {
448  if (!in_array($access_name, $this->access_list[$resource_name])) {
449  unset($this->access[$role_inherit][$resource_name][$access_name]);
450  } else {
451  if (!isset($this->access[$role][$resource_name][$access_name])) {
452  $this->access[$role][$resource_name][$access_name] = $value;
453  }
454  }
455  }
456  }
457  }
458  }
459  }
460  }
461  }
462  }
463 
464 }