1: <?php
2:
3: /**
4: * Pry Framework
5: *
6: * LICENSE
7: *
8: * This source file is subject to the new BSD license that is bundled
9: * with this package in the file LICENSE.txt.
10: *
11: */
12:
13: namespace Pry\Auth;
14:
15: /**
16: * Classe permettant la gestion de roles et de permissions.
17: *
18: * <code>
19: * $ACL = new ACL();
20: * $ACL->addRole('Writer',array('read','write'));
21: * $ACL->addPermission('Writer','delete');
22: *
23: * if($ACL->hasPermission('write'))
24: * echo 'ok';
25: * else
26: * echo 'ko';
27: * </code>
28: *
29: * @category Pry
30: * @package Auth
31: * @version 2.0.0
32: * @author Olivier ROGER <oroger.fr>
33: *
34: */
35: class ACL
36: {
37:
38: /** Liste des roles associé */
39: private $roles = array();
40:
41: /** Liste des permissions triées par roles */
42: private $permissions = array();
43:
44: public function __construct()
45: {
46:
47: }
48:
49: /**
50: * Ajout d'un role
51: * @param string $roleName Nom du role
52: * @param array $permissions Les permissions associées au role
53: */
54: public function addRole($roleName, array $permissions)
55: {
56: $this->roles[] = $roleName;
57: $this->permissions[$roleName] = array();
58: foreach ($permissions as $perm)
59: $this->permissions[$roleName][$perm] = true;
60: }
61:
62: /**
63: * Ajout d'une permission à un role spécifique
64: * @param string $roleName Nom du role concerné
65: * @param string $permission Nom de la permission
66: */
67: public function addPermission($roleName, $permission)
68: {
69: if (!isset($this->permissions[$roleName]))
70: $this->permissions[$roleName] = array();
71:
72: $this->permissions[$roleName][$permission] = true;
73: }
74:
75: /**
76: * Supprime un role et les permission associées
77: * @param type $name
78: * @return boolean
79: */
80: public function deleteRole($name)
81: {
82: $keyRole = array_search($name, $this->roles);
83: if ($keyRole !== false)
84: {
85: unset($this->roles[$keyRole]);
86: unset($this->permissions[$name]);
87: return true;
88: }
89:
90: return false;
91: }
92:
93: /**
94: * Vérifie que la permission est autorisée
95: * @param string $permName Nom de la permission à tester
96: * @return boolean true si autorisé false sinon
97: */
98: public function hasPermission($permName)
99: {
100: foreach ($this->roles as $role) {
101: if (isset($this->permissions[$role][$permName]) && $this->permissions[$role][$permName] == true)
102: return true;
103: }
104:
105: return false;
106: }
107:
108: /**
109: * Méthode magique pour sérialiser correctement les données
110: * @return array
111: */
112: public function __sleep()
113: {
114: return array('roles', 'permissions');
115: }
116:
117: }
118:
119: ?>