1 <?php
2
3 4 5 6 7 8 9
10 class rex_user
11 {
12 13 14 15 16
17 protected $sql;
18
19 20 21 22 23
24 protected $role;
25
26 27 28 29 30
31 protected static $roleClass;
32
33 34 35 36 37
38 public function __construct(rex_sql $sql)
39 {
40 $this->sql = $sql;
41 }
42
43 44 45 46 47 48 49
50 public function getValue($key)
51 {
52 return $this->sql->getValue($key);
53 }
54
55 56 57 58 59
60 public function getId()
61 {
62 return $this->sql->getValue('id');
63 }
64
65 66 67 68 69
70 public function getLogin()
71 {
72 return $this->sql->getValue('login');
73 }
74
75 76 77 78 79
80 public function getName()
81 {
82 return $this->sql->getValue('name');
83 }
84
85 86 87 88 89
90 public function getEmail()
91 {
92 return $this->sql->getValue('email');
93 }
94
95 96 97 98 99
100 public function isAdmin()
101 {
102 return (bool) $this->sql->getValue('admin');
103 }
104
105 106 107 108 109
110 public function getLanguage()
111 {
112 return $this->sql->getValue('language');
113 }
114
115 116 117 118 119
120 public function getStartPage()
121 {
122 return $this->sql->getValue('startpage');
123 }
124
125 126 127 128 129
130 public function hasRole()
131 {
132 if (self::$roleClass && !is_object($this->role) && ($role = $this->sql->getValue('role'))) {
133 $class = self::$roleClass;
134 $this->role = $class::get($role);
135 }
136 return is_object($this->role);
137 }
138
139 140 141 142 143 144 145
146 public function hasPerm($perm)
147 {
148 if ($this->isAdmin()) {
149 return true;
150 }
151 $result = false;
152 if (strpos($perm, '/') !== false) {
153 list($complexPerm, $method) = explode('/', $perm, 2);
154 $complexPerm = $this->getComplexPerm($complexPerm);
155 return $complexPerm ? $complexPerm->$method() : false;
156 }
157 if ($this->hasRole()) {
158 $result = $this->role->hasPerm($perm);
159 }
160 if (!$result && in_array($perm, ['isAdmin', 'admin', 'admin[]'])) {
161 return $this->isAdmin();
162 }
163 return $result;
164 }
165
166 167 168 169 170 171 172
173 public function getComplexPerm($key)
174 {
175 if ($this->hasRole()) {
176 return $this->role->getComplexPerm($this, $key);
177 }
178 return rex_complex_perm::get($this, $key);
179 }
180
181 182 183 184 185
186 public static function setRoleClass($class)
187 {
188 self::$roleClass = $class;
189 }
190 }
191