maj version dans config.xml
[mtweb] / mw / app / data / modules / xml / mw_data_xml_users.php
1 <?php
2
3   class mw_data_xml_users extends mw_data{
4
5     public $users;
6     public $user;
7     public $roles;
8     public $actions_roles;
9
10     # ----------------------------------------------------------------------------------------
11     #                                                                                    users
12     #
13
14     public function users($start = 0, $alpha = null, $id_role = null){
15       $sgbd = $this->sgbd();
16       $env = $this->env();
17       $users = array("list" => array(), "total" => 0);
18       if(isset($id_role)){
19         $role_users = array();
20         if($rst = $sgbd->open_data("users_roles")){
21           while($v_rst = $sgbd->fetch_data($rst)){
22             if(isset($v_rst)){
23               if(($v_rst["id_user"]) && isset($v_rst["id_role"]) && $v_rst["id_role"] == $id_role){
24                 $role_users[] = $v_rst["id_user"];
25               }
26             }
27             else{
28               $role_users = false;
29               break;
30             }
31           }
32           $sgbd->close_data($rst);
33         }
34         else $role_users = false;
35         if($role_users === false) return false;
36       }
37       $res = array();
38       if($rst = $sgbd->open_data("users")){
39         while($v_rst = $sgbd->fetch_data($rst)){
40           if(isset($v_rst)){
41             if(!isset($alpha) || (isset($v_rst["login"]) && strtolower(substr($v_rst["login"], 0, 1)) == strtolower($alpha))){
42               if(!isset($id_role) || in_array($id_role, $role_users)){
43                 $res[$v_rst["id"]] = $v_rst;
44                 $users["total"]++;
45               }
46             }
47           }
48           else{
49             $res = false;
50             break;
51           }
52         }
53         $sgbd->close_data($rst);
54         if($res !== false){
55           $n = 0;
56           foreach($res as $id_user => $user){
57             $n++;
58             if(!$env->config("max_list") || ($n > $start && $n <= ($start + $env->config("max_list")))){
59               $users["list"][$user["id"]] = $user;
60               if(!isset($this->users)) $this->users = array();
61               $this->users[$user["id"]] = $user;
62             }
63           }
64           foreach($users["list"] as $id_user => $user){
65             if(($roles = $this->list_user_roles($id_user)) !== false){
66               $users["list"][$id_user]["roles"] = $roles;
67             }
68             else{
69               $users = false;
70               break;
71             }
72           }
73         }
74         else $users = false;
75       }
76       else $users = false;
77       return $users;
78     }
79
80     public function list_user_roles($id_user){
81       $sgbd = $this->sgbd();
82       $roles = array();
83       if($rst = $sgbd->open_data("users_roles")){
84         while($v_rst = $sgbd->fetch_data($rst)){
85           if(isset($v_rst)){
86             if(isset($v_rst["id_role"]) && isset($v_rst["id_user"]) && $v_rst["id_user"] == $id_user){
87               $roles[] = $v_rst["id_role"];
88             }
89           }
90           else{
91             $roles = false;
92             break;
93           }
94         }
95         $sgbd->close_data($rst);
96       }
97       else $roles = false;
98       return $roles;
99     }
100
101     public function user_by_id($id){
102       $env = $this->env();
103       $user = $env->get_model("users");
104       if($user->load("id", $id) === false) return false;
105       return $user->get_values();
106     }
107
108     public function user($login){
109       $env = $this->env();
110       $user = $env->get_model("users");
111       if($user->load("login", $login) === false) return false;
112       return $user->get_values();
113     }
114
115     public function user_exists($login){
116       $sgbd = $this->sgbd();
117       $EXISTS = 0;
118       if($rst = $sgbd->open_data("users")){
119         while($v_rst = $sgbd->fetch_data($rst)){
120           if(isset($v_rst)){
121             if(isset($v_rst["login"]) && $v_rst["login"] == $login){
122               $EXISTS++;
123             }
124           }
125           else{
126             $EXISTS = false;
127             break;
128           }
129         }
130         $sgbd->close_data($rst);
131       }
132       else $EXISTS = false;
133       return $EXISTS;
134     }
135
136     public function add_user($login, $password, $email, $roles){
137       $sgbd = $this->sgbd();
138       if(
139         (
140           $id_user = $sgbd->add_data(
141             "users",
142             array(
143               "login" => $login,
144               "password" => $password,
145               "email" => $email
146             )
147           )
148         ) === false
149       ) return false;
150       $OK = true;
151       foreach($roles as $id_role){
152         $OK = $sgbd->add_data(
153           "users_roles",
154           array(
155             "id_user" => $id_user,
156             "id_role" => $id_role
157           )
158         );
159         if(!$OK) break;
160       }
161       if(!$OK) return false;
162       return $id_user;
163     }
164
165     public function set_user($id, $login, $password, $email, $roles){
166       $sgbd = $this->sgbd();
167       if(
168         !$sgbd->set_data(
169           "users",
170           $id,
171           array(
172             "login" => $login,
173             "password" => $password,
174             "email" => $email
175           )
176         )
177       ) return false;
178       if($rst = $sgbd->open_data("users_roles")){
179         $OK = true;
180         while($v_rst = $sgbd->fetch_data($rst)){
181           if(isset($v_rst)){
182             if(isset($v_rst["id"]) && isset($v_rst["id_user"]) && $v_rst["id_user"] == $id){
183               if(!$sgbd->del_data("users_roles", $v_rst["id"])){
184                 $OK = false;
185                 break;
186               }
187             }
188           }
189           else $OK = false;
190         }
191         $sgbd->close_data($rst);
192         if(!$OK) return false;
193       }
194       else return false;
195       foreach($roles as $id_role){
196         $OK = $sgbd->add_data(
197           "users_roles",
198           array(
199             "id_user" => $id,
200             "id_role" => $id_role
201           )
202         );
203         if(!$OK) break;
204       }
205       if(!$OK) return false;
206       return true;
207     }
208
209     public function clear_user_roles($id_user){
210       $sgbd = $this->sgbd();
211       if($rst = $sgbd->open_data("users_roles")){
212         $OK = true;
213         while($v_rst = $sgbd->fetch_data($rst)){
214           if(isset($v_rst)){
215             if(isset($v_rst["id"]) && isset($v_rst["id_user"]) && ($v_rst["id_user"] == $id_user)){
216               if(!$sgbd->del_data("users_roles", $v_rst["id"])){
217                 $OK = false;
218                 break;
219               }
220             }
221           }
222           else $OK = false;
223         }
224         $sgbd->close_data($rst);
225       }
226       else $OK = false;
227       return $OK;
228     }
229
230     public function add_user_role($id_user, $id_role){
231       $sgbd = $this->sgbd();
232       $OK = $sgbd->add_data(
233         "users_roles",
234         array(
235           "id_user" => $id_user,
236           "id_role" => $id_role
237         )
238       );
239       if(!$OK) return false;
240       return true;
241     }
242
243     public function del_user($login){
244       if(($user = $this->user($login)) !== false){
245         $sgbd = $this->sgbd();
246         if(!$sgbd->del_data("users", $user["id"])) return false;
247         if($rst = $sgbd->open_data("users_roles")){
248           $OK = true;
249           while($v_rst = $sgbd->fetch_data($rst)){
250             if(isset($v_rst)){
251               if(isset($v_rst["id"]) && isset($v_rst["id_user"]) && $v_rst["id_user"] == $user["id"]){
252                 if(!$sgbd->del_data("users_roles", $v_rst["id"])){
253                   $OK = false;
254                   break;
255                 }
256               }
257             }
258             else $OK = false;
259           }
260           $sgbd->close_data($rst);
261           return $OK;
262         }
263       }
264       return false;
265     }
266
267     # ----------------------------------------------------------------------------------------
268     #                                                                                    roles
269     #
270
271     public function init_roles(){
272       $sgbd = $this->sgbd();
273       $this->roles = array();
274       if($rst = $sgbd->open_data("roles")){
275         while($v_rst = $sgbd->fetch_data($rst)){
276           if(isset($v_rst)){
277             $this->roles[$v_rst["id"]] = $v_rst;
278           }
279           else{
280             $this->roles = false;
281             break;
282           }
283         }
284         $sgbd->close_data($rst);
285       }
286       else $this->roles = false;
287       return $this->roles;
288     }
289
290     public function roles(){
291       if(!isset($this->roles)) return false;
292       return $this->roles;
293     }
294
295     public function add_role($nom, $intitule){
296       $sgbd = $this->sgbd();
297       $id_role = $sgbd->add_data(
298         "roles",
299         array(
300           "nom" => $nom,
301           "intitule" => $intitule
302         )
303       );
304       if(!isset($id_role)) return false;
305       return $id_role;
306     }
307
308     public function get_role($id){
309       if($id === "0") return array(
310         "id" => 0,
311         "nom" => "",
312         "intitule" => ""
313       );
314       $sgbd = $this->sgbd();
315       $role = $sgbd->get_data("roles", $id);
316       if(!isset($role)) return false;
317       return $role ? $role : array();
318     }
319
320     public function set_role($id, $nom, $intitule){
321       $sgbd = $this->sgbd();
322       if(
323         !$sgbd->set_data(
324           "roles",
325           $id,
326           array(
327             "nom" => $nom,
328             "intitule" => $intitule
329           )
330         )
331       ) return false;
332       return true;
333     }
334
335     public function clear_role_actions($id_role){
336       $sgbd = $this->sgbd();
337       if($rst = $sgbd->open_data("actions_roles")){
338         $OK = true;
339         while($v_rst = $sgbd->fetch_data($rst)){
340           if(isset($v_rst)){
341             if(isset($v_rst["id"]) && isset($v_rst["id_role"]) && $v_rst["id_role"] == $id_role){
342               if(!$sgbd->del_data("actions_roles", $v_rst["id"])){
343                 $OK = false;
344                 break;
345               }
346             }
347           }
348           else $OK = false;
349         }
350         $sgbd->close_data($rst);
351         return $OK;
352       }
353       return false;
354     }
355
356     public function clear_role_users($id_role){
357       $sgbd = $this->sgbd();
358       if($rst = $sgbd->open_data("users_roles")){
359         $OK = true;
360         while($v_rst = $sgbd->fetch_data($rst)){
361           if(isset($v_rst)){
362             if(isset($v_rst["id"]) && isset($v_rst["id_role"]) && $v_rst["id_role"] == $id_role){
363               if(!$sgbd->del_data("users_roles", $v_rst["id"])){
364                 $OK = false;
365                 break;
366               }
367             }
368           }
369           else $OK = false;
370         }
371         $sgbd->close_data($rst);
372         return $OK;
373       }
374       return false;
375     }
376
377     public function add_role_action($id_role, $action){
378       $sgbd = $this->sgbd();
379       $id_action_role = $sgbd->add_data(
380         "actions_roles",
381         array(
382           "action" => $action,
383           "id_role" => $id_role
384         )
385       );
386       if(!isset($id_action_role)) return false;
387       return $id_action_role;
388     }
389
390     function del_role($id_role){
391       $sgbd = $this->sgbd();
392       return $sgbd->del_data("roles", $id_role) ? true : false;
393     }
394
395     public function get_user_roles(){
396       $user_roles = array();
397       $user = $this->get_session_user();
398       if($user && isset($user["id"])){
399         $sgbd = $this->sgbd();
400         if($rst = $sgbd->open_data("users_roles")){
401           while($v_rst = $sgbd->fetch_data($rst)){
402             if(isset($v_rst)){
403               if(isset($v_rst["id_role"]) && isset($v_rst["id_user"]) && $v_rst["id_user"] == $user["id"]){
404                 $user_roles[] = $v_rst["id_role"];
405               }
406             }
407             else{
408               $user_roles = false;
409               break;
410             }
411           }
412           $sgbd->close_data($rst);
413         }
414         else $user_roles = false;
415         if($user_roles === false) return false;
416       }
417       else $user_roles[] = 0;
418       if(!$user_roles) $user_roles[] = 0;
419       return $user_roles;
420     }
421
422     public function init_actions_roles(){
423       if(!isset($this->roles)) return false;
424       $this->actions_roles = $this->read_actions_roles();
425       return $this->actions_roles;
426     }
427
428     public function read_actions_roles($params = array()){
429       if(!isset($this->roles)) return false;
430       $group_by_action = isset($params["group_by_action"]) ? $params["group_by_action"] : false;
431       $sgbd = $this->sgbd();
432       $actions_roles = array();
433       if($rst = $sgbd->open_data("actions_roles")){
434         while($v_rst = $sgbd->fetch_data($rst)){
435           if(isset($v_rst)){
436             if(isset($v_rst["action"]) && isset($v_rst["id_role"])){
437               if($group_by_action){
438                 if(!isset($actions_roles[$v_rst["action"]])) $actions_roles[$v_rst["action"]] = array();
439                 $actions_roles[$v_rst["action"]][] = $v_rst["id_role"];
440               }
441               else $actions_roles[$v_rst["id"]] = $v_rst;
442             }
443           }
444           else{
445             $actions_roles = false;
446             break;
447           }
448         }
449         $sgbd->close_data($rst);
450       }
451       else $actions_roles = false;
452       return $actions_roles;
453     }
454
455     public function get_action_roles($mod, $controller = "index", $action = "index"){
456       $sgbd = $this->sgbd();
457       $roles = array();
458       if($rst = $sgbd->open_data("actions_roles")){
459         while($roles !==false && $v_rst = $sgbd->fetch_data($rst)){
460           if(isset($v_rst) && isset($v_rst["action"]) && isset($v_rst["id_role"])){
461             if(
462                  $v_rst["action"] == $mod
463               || $v_rst["action"] == $mod."/".$controller
464               || $v_rst["action"] == $mod."/".$controller."/".$action
465             ){
466               if(!isset($roles[$v_rst["action"]])) $roles[$v_rst["action"]] = array();
467               $roles[$v_rst["action"]][$v_rst["id_role"]] = true;
468             }
469           }
470           else $roles = false;
471         }
472         $sgbd->close_data($rst);
473       }
474       else $roles = false;
475       return $roles;
476     }
477
478     public function get_actions($id_role = null){
479       $env = $this->env();
480       if($actions = $env->get_actions()){
481         if(($actions_roles = $this->read_actions_roles(array("group_by_action" => true))) !== false){
482           foreach($actions as $module_name => $module){
483             if(isset($id_role)) $actions[$module_name]["module_allowed"] =
484                 isset($actions_roles[$module_name])
485             &&  in_array($id_role, $actions_roles[$module_name]);
486             $actions[$module_name]["is_public"] =
487                 isset($actions_roles[$module_name])
488             &&  in_array(0, $actions_roles[$module_name]);
489             foreach($module["controleurs"] as $controleur_name => $controleur){
490               if(isset($id_role)) $actions[$module_name]["controleurs"][$controleur_name]["controleur_allowed"] =
491                   isset($actions_roles[$module_name."/".$controleur_name])
492               &&  in_array($id_role, $actions_roles[$module_name."/".$controleur_name]);
493               $actions[$module_name]["controleurs"][$controleur_name]["is_public"] =
494                   isset($actions_roles[$module_name."/".$controleur_name])
495               &&  in_array(0, $actions_roles[$module_name."/".$controleur_name]);
496               foreach($controleur["als"] as $index_als => $al){
497                 if($al["actions"]){
498                   if(isset($id_role)){
499                     $HAS_ACTION_NOT_ALLOWED = false;
500                     foreach($al["actions"] as $action_name){
501                       if(
502                           !isset($actions_roles[$module_name."/".$controleur_name."/".$action_name])
503                       ||  !in_array($id_role, $actions_roles[$module_name."/".$controleur_name."/".$action_name])
504                       ){
505                         $HAS_ACTION_NOT_ALLOWED = true;
506                         break;
507                       }
508                     }
509                     if(!$HAS_ACTION_NOT_ALLOWED){
510                       $actions[$module_name]["controleurs"][$controleur_name]["als"][$index_als]["action_allowed"] = true;
511                     }
512                   }
513                   $HAS_ACTION_NOT_ALLOWED = false;
514                   foreach($al["actions"] as $action_name){
515                     if(
516                         !isset($actions_roles[$module_name."/".$controleur_name."/".$action_name])
517                     ||  !in_array(0, $actions_roles[$module_name."/".$controleur_name."/".$action_name])
518                     ){
519                       $HAS_ACTION_NOT_ALLOWED = true;
520                       break;
521                     }
522                   }
523                   if(!$HAS_ACTION_NOT_ALLOWED){
524                     $actions[$module_name]["controleurs"][$controleur_name]["als"][$index_als]["is_public"] = true;
525                   }
526                 }
527               }
528             }
529           }
530           return $actions;
531         }
532       }
533       return array();
534     }
535
536   }