1 <?php
2 3 4 5 6 7
8
9 namespace Hawk;
10
11
12 13 14 15 16
17 class User extends Model{
18 19 20 21 22
23 protected static $tablename = "User";
24
25
26 27 28 29 30
31 private $profile,
32
33 34 35 36 37
38 $permissions,
39
40 41 42 43 44
45 $options;
46
47 48 49
50 const GUEST_USER_ID = 0;
51
52 53 54
55 const ROOT_USER_ID = 1;
56
57 58 59 60 61
62 public function __construct($data = array()){
63 parent::__construct($data);
64 if(!empty($this->roleId)) {
65 $this->role = Role::getById($this->roleId);
66 }
67 }
68
69 70 71 72 73 74 75 76 77
78 public static function getAll($index = null, $fields = array(), $order = array()){
79 $example = array(
80 'id' => array(
81 '$ne' => self::GUEST_USER_ID
82 )
83 );
84 return self::getListByExample(new DBExample($example), $index, $fields, $order);
85 }
86
87
88 89 90 91 92 93 94
95 public static function getByUsername($username){
96 return self::getByExample(new DBExample(array('username' => $username)));
97 }
98
99
100 101 102 103 104 105 106
107 public static function getByEmail($email){
108 return self::getByExample(new DBExample(array('email' => $email)));
109 }
110
111 112 113
114 private function getPermissions(){
115 if(!isset($this->permissions)) {
116 $sql = 'SELECT P.plugin, P.key, P.id
117 FROM ' . RolePermission::getTable() . ' RP
118 INNER JOIN ' . Permission::getTable() . ' P ON RP.permissionId = P.id
119 INNER JOIN ' . self::getTable() . ' U ON U.roleId = RP.roleId
120 WHERE U.id = :id AND RP.value=1';
121
122 $permissions = App::db()->query($sql, array('id' => $this->id), array('return' => DB::RETURN_OBJECT));
123 $this->permissions = array();
124 foreach($permissions as $permission){
125
126 $this->permissions['byId'][$permission->id] = 1;
127
128
129 $this->permissions['byName'][$permission->plugin][$permission->key] = 1;
130 }
131 }
132 }
133
134
135 136 137 138 139 140 141 142
143 public function getProfileData($prop = ""){
144 if(!isset($this->profile)) {
145 $sql = 'SELECT Q.name, V.value
146 FROM ' . ProfileQuestionValue::getTable() . ' V
147 INNER JOIN ' . ProfileQuestion::getTable() . ' Q ON V.question = Q.name
148 WHERE V.userId = :id';
149
150 $data = App::db()->query(
151 $sql,
152 array(
153 'id' => $this->id
154 ),
155 array(
156 'return' => DB::RETURN_ARRAY,
157 'index' => 'name'
158 )
159 );
160
161 $this->profile = array_map(
162 function ($v) {
163 return $v['value'];
164 },
165 $data
166 );
167 }
168 return $prop ? (isset($this->profile[$prop]) ? $this->profile[$prop] : null) : $this->profile;
169 }
170
171
172 173 174 175 176 177
178 public function setProfileData($prop, $value){
179 $this->profile[$prop] = $value;
180 }
181
182
183 184 185
186 public function saveProfile(){
187 foreach($this->profile as $prop => $value){
188 $questionValue = new ProfileQuestionValue(
189 array(
190 'question' => $prop,
191 'userId' => $this->id,
192 'value' => $value
193 )
194 );
195 $questionValue->save();
196 }
197 }
198
199
200 201 202 203 204 205 206 207
208 public function getOptions($name = ''){
209 if(!isset($this->options)) {
210 $example = $this->isLogged() ? array('userId' => $this->id) : array('userIp' => App::request()->clientIp());
211
212 $options = App::db()->select(
213 array(
214 'from' => DB::getFullTablename('UserOption'),
215 'where' => new DBExample($example)
216 )
217 );
218
219 $this->options = array();
220 foreach($options as $option){
221 $this->options[$option['plugin'] . '.' . $option['key']] = $option['value'];
222 }
223 }
224
225 if($name) {
226 return isset($this->options[$name]) ? $this->options[$name] : null;
227 }
228 else{
229 return $this->options;
230 }
231 }
232
233
234 235 236 237 238 239 240
241 public function setOption($name, $value){
242 $this->getOptions();
243 $this->options[$name] = $value;
244
245 list($plugin, $key) = explode('.', $name, 2);
246 $data = array(
247 'plugin' => $plugin,
248 'key' => $key,
249 'value' => $value
250 );
251
252 if($this->isLogged()) {
253 $data['userId'] = $this->id;
254 }
255 else{
256 $data['userIp'] = App::request()->clientIp();
257 }
258 App::db()->replace(DB::getFullTablename('UserOption'), $data);
259 }
260
261
262
263 264 265 266 267 268 269 270 271
272 public function isAllowed($action){
273 if($this->roleId == Role::ADMIN_ROLE_ID) {
274
275 return true;
276 }
277 if($action !== Permission::ALL_PRIVILEGES_ID && $action !== Permission::ALL_PRIVILEGES_NAME && $this->isAllowed(Permission::ALL_PRIVILEGES_ID)) {
278
279 return true;
280 }
281
282
283 $this->getPermissions();
284
285 if(is_numeric($action)) {
286
287 return !empty($this->permissions['byId'][$action]);
288 }
289 else{
290
291 list($plugin, $key) = explode('.', $action);
292
293 return !empty($this->permissions['byName'][$plugin][$key]);
294 }
295 }
296
297
298 299 300 301 302
303 public function getUsername(){
304 return $this->id ? $this->username : Lang::get('main.guest-username');
305 }
306
307
308 309 310 311 312
313 public function getDisplayName(){
314 return $this->getProfileData('realname') ? $this->getProfileData('realname') : $this->getUsername();
315 }
316
317 318 319 320 321
322 public function isLogged(){
323 return $this->id && App::session()->getData('user.id') == $this->id && $this->active;
324 }
325
326
327 328 329 330 331
332 public function canAccessApplication(){
333 return $this->isLogged() || Option::get('main.allow-guest');
334 }
335
336 337 338 339 340 341
342 public function isRemovable(){
343 return $this->id != App::session()->getUser()->id &&
344 $this->id != self::ROOT_USER_ID &&
345 $this->id != self::GUEST_USER_ID;
346 }
347 }