plugin mw_minimal fourni avec mtweb
[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     var $users;
6     var $user;
7     var $roles;
8     var $actions_roles;
9
10     # ----------------------------------------------------------------------------------------
11     #                                                                                    users
12     #
13
14     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     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     function user_by_id($id){
102       if(!isset($this->users)) $this->users = array();
103       if(isset($this->users[$id])) return $this->users[$id];
104       $sgbd = $this->sgbd();
105       if(($user = $sgbd->get_data("users", $id)) !== false){
106         $this->users[$id] = $user;
107         if(($roles = $this->list_user_roles($user["id"])) !== false) $user["roles"] = $roles;
108         else $user = false;
109       }
110       return $user;
111     }
112
113     function user($login){
114       $sgbd = $this->sgbd();
115       $user = array();
116       if($rst = $sgbd->open_data("users")){
117         while($v_rst = $sgbd->fetch_data($rst)){
118           if(isset($v_rst)){
119             if(isset($v_rst["login"]) && $v_rst["login"] == $login){
120               $user = $v_rst;
121               break;
122             }
123           }
124           else $user = false;
125         }
126         $sgbd->close_data($rst);
127         if($user){
128           if(($roles = $this->list_user_roles($user["id"])) !== false) $user["roles"] = $roles;
129           else $user = false;
130         }
131       }
132       else $user = false;
133       if($user !== false){
134         if(!isset($this->users)) $this->users = array();
135         if($user) $this->users[$user["id"]] = $user;
136       }
137       return $user;
138     }
139
140     function user_exists($login){
141       $sgbd = $this->sgbd();
142       $EXISTS = 0;
143       if($rst = $sgbd->open_data("users")){
144         while($v_rst = $sgbd->fetch_data($rst)){
145           if(isset($v_rst)){
146             if(isset($v_rst["login"]) && $v_rst["login"] == $login){
147               $EXISTS++;
148             }
149           }
150           else{
151             $EXISTS = false;
152             break;
153           }
154         }
155         $sgbd->close_data($rst);
156       }
157       else $EXISTS = false;
158       return $EXISTS;
159     }
160
161     function add_user($login, $password, $email, $roles){
162       $sgbd = $this->sgbd();
163       if(
164         (
165           $id_user = $sgbd->add_data(
166             "users",
167             array(
168               "login" => $login,
169               "password" => $password,
170               "email" => $email
171             )
172           )
173         ) === false
174       ) return false;
175       $OK = true;
176       foreach($roles as $id_role){
177         $OK = $sgbd->add_data(
178           "users_roles",
179           array(
180             "id_user" => $id_user,
181             "id_role" => $id_role
182           )
183         );
184         if(!$OK) break;
185       }
186       if(!$OK) return false;
187       return $id_user;
188     }
189
190     function set_user($id, $login, $password, $email, $roles){
191       $sgbd = $this->sgbd();
192       if(
193         !$sgbd->set_data(
194           "users",
195           $id,
196           array(
197             "login" => $login,
198             "password" => $password,
199             "email" => $email
200           )
201         )
202       ) return false;
203       if($rst = $sgbd->open_data("users_roles")){
204         $OK = true;
205         while($v_rst = $sgbd->fetch_data($rst)){
206           if(isset($v_rst)){
207             if(isset($v_rst["id"]) && isset($v_rst["id_user"]) && $v_rst["id_user"] == $id){
208               if(!$sgbd->del_data("users_roles", $v_rst["id"])){
209                 $OK = false;
210                 break;
211               }
212             }
213           }
214           else $OK = false;
215         }
216         $sgbd->close_data($rst);
217         if(!$OK) return false;
218       }
219       else return false;
220       foreach($roles as $id_role){
221         $OK = $sgbd->add_data(
222           "users_roles",
223           array(
224             "id_user" => $id,
225             "id_role" => $id_role
226           )
227         );
228         if(!$OK) break;
229       }
230       if(!$OK) return false;
231       return true;
232     }
233
234     function clear_user_roles($id_user){
235       $sgbd = $this->sgbd();
236       if($rst = $sgbd->open_data("users_roles")){
237         $OK = true;
238         while($v_rst = $sgbd->fetch_data($rst)){
239           if(isset($v_rst)){
240             if(isset($v_rst["id"]) && isset($v_rst["id_user"]) && ($v_rst["id_user"] == $id_user)){
241               if(!$sgbd->del_data("users_roles", $v_rst["id"])){
242                 $OK = false;
243                 break;
244               }
245             }
246           }
247           else $OK = false;
248         }
249         $sgbd->close_data($rst);
250       }
251       else $OK = false;
252       return $OK;
253     }
254
255     function add_user_role($id_user, $id_role){
256       $sgbd = $this->sgbd();
257       $OK = $sgbd->add_data(
258         "users_roles",
259         array(
260           "id_user" => $id_user,
261           "id_role" => $id_role
262         )
263       );
264       if(!$OK) return false;
265       return true;
266     }
267
268     function del_user($login){
269       if(($user = $this->user($login)) !== false){
270         $sgbd = $this->sgbd();
271         if(!$sgbd->del_data("users", $user["id"])) return false;
272         if($rst = $sgbd->open_data("users_roles")){
273           $OK = true;
274           while($v_rst = $sgbd->fetch_data($rst)){
275             if(isset($v_rst)){
276               if(isset($v_rst["id"]) && isset($v_rst["id_user"]) && $v_rst["id_user"] == $user["id"]){
277                 if(!$sgbd->del_data("users_roles", $v_rst["id"])){
278                   $OK = false;
279                   break;
280                 }
281               }
282             }
283             else $OK = false;
284           }
285           $sgbd->close_data($rst);
286           return $OK;
287         }
288       }
289       return false;
290     }
291
292     # ----------------------------------------------------------------------------------------
293     #                                                                                    roles
294     #
295
296     function init_roles(){
297       $sgbd = $this->sgbd();
298       $this->roles = array();
299       if($rst = $sgbd->open_data("roles")){
300         while($v_rst = $sgbd->fetch_data($rst)){
301           if(isset($v_rst)){
302             $this->roles[$v_rst["id"]] = $v_rst;
303           }
304           else{
305             $this->roles = false;
306             break;
307           }
308         }
309         $sgbd->close_data($rst);
310       }
311       else $this->roles = false;
312       return $this->roles;
313     }
314
315     function roles(){
316       if(!isset($this->roles)) return false;
317       return $this->roles;
318     }
319
320     function add_role($nom, $intitule){
321       $sgbd = $this->sgbd();
322       $id_role = $sgbd->add_data(
323         "roles",
324         array(
325           "nom" => $nom,
326           "intitule" => $intitule
327         )
328       );
329       if(!isset($id_role)) return false;
330       return $id_role;
331     }
332
333     function get_role($id){
334       if($id === "0") return array(
335         "id" => 0,
336         "nom" => "",
337         "intitule" => ""
338       );
339       $sgbd = $this->sgbd();
340       $role = $sgbd->get_data("roles", $id);
341       if(!isset($role)) return false;
342       return $role ? $role : array();
343     }
344
345     function set_role($id, $nom, $intitule){
346       $sgbd = $this->sgbd();
347       if(
348         !$sgbd->set_data(
349           "roles",
350           $id,
351           array(
352             "nom" => $nom,
353             "intitule" => $intitule
354           )
355         )
356       ) return false;
357       return true;
358     }
359
360     function clear_role_actions($id_role){
361       $sgbd = $this->sgbd();
362       if($rst = $sgbd->open_data("actions_roles")){
363         $OK = true;
364         while($v_rst = $sgbd->fetch_data($rst)){
365           if(isset($v_rst)){
366             if(isset($v_rst["id"]) && isset($v_rst["id_role"]) && $v_rst["id_role"] == $id_role){
367               if(!$sgbd->del_data("actions_roles", $v_rst["id"])){
368                 $OK = false;
369                 break;
370               }
371             }
372           }
373           else $OK = false;
374         }
375         $sgbd->close_data($rst);
376         return $OK;
377       }
378       return false;
379     }
380
381     function clear_role_users($id_role){
382       $sgbd = $this->sgbd();
383       if($rst = $sgbd->open_data("users_roles")){
384         $OK = true;
385         while($v_rst = $sgbd->fetch_data($rst)){
386           if(isset($v_rst)){
387             if(isset($v_rst["id"]) && isset($v_rst["id_role"]) && $v_rst["id_role"] == $id_role){
388               if(!$sgbd->del_data("users_roles", $v_rst["id"])){
389                 $OK = false;
390                 break;
391               }
392             }
393           }
394           else $OK = false;
395         }
396         $sgbd->close_data($rst);
397         return $OK;
398       }
399       return false;
400     }
401
402     function add_role_action($id_role, $action){
403       $sgbd = $this->sgbd();
404       $id_action_role = $sgbd->add_data(
405         "actions_roles",
406         array(
407           "action" => $action,
408           "id_role" => $id_role
409         )
410       );
411       if(!isset($id_action_role)) return false;
412       return $id_action_role;
413     }
414
415     function del_role($id_role){
416       $sgbd = $this->sgbd();
417       return $sgbd->del_data("roles", $id_role) ? true : false;
418     }
419
420     function get_user_roles(){
421       $user_roles = array();
422       $user = $this->get_session_user();
423       if($user && isset($user["id"])){
424         $sgbd = $this->sgbd();
425         if($rst = $sgbd->open_data("users_roles")){
426           while($v_rst = $sgbd->fetch_data($rst)){
427             if(isset($v_rst)){
428               if(isset($v_rst["id_role"]) && isset($v_rst["id_user"]) && $v_rst["id_user"] == $user["id"]){
429                 $user_roles[] = $v_rst["id_role"];
430               }
431             }
432             else{
433               $user_roles = false;
434               break;
435             }
436           }
437           $sgbd->close_data($rst);
438         }
439         else $user_roles = false;
440         if($user_roles === false) return false;
441       }
442       else $user_roles[] = 0;
443       if(!$user_roles) $user_roles[] = 0;
444       return $user_roles;
445     }
446
447     function init_actions_roles(){
448       if(!isset($this->roles)) return false;
449       $this->actions_roles = $this->read_actions_roles();
450       return $this->actions_roles;
451     }
452
453     function read_actions_roles($params = array()){
454       if(!isset($this->roles)) return false;
455       $group_by_action = isset($params["group_by_action"]) ? $params["group_by_action"] : false;
456       $sgbd = $this->sgbd();
457       $actions_roles = array();
458       if($rst = $sgbd->open_data("actions_roles")){
459         while($v_rst = $sgbd->fetch_data($rst)){
460           if(isset($v_rst)){
461             if(isset($v_rst["action"]) && isset($v_rst["id_role"])){
462               if($group_by_action){
463                 if(!isset($actions_roles[$v_rst["action"]])) $actions_roles[$v_rst["action"]] = array();
464                 $actions_roles[$v_rst["action"]][] = $v_rst["id_role"];
465               }
466               else $actions_roles[$v_rst["id"]] = $v_rst;
467             }
468           }
469           else{
470             $actions_roles = false;
471             break;
472           }
473         }
474         $sgbd->close_data($rst);
475       }
476       else $actions_roles = false;
477       return $actions_roles;
478     }
479
480     function get_action_roles($mod, $controller = "index", $action = "index"){
481       $sgbd = $this->sgbd();
482       $roles = array();
483       if($rst = $sgbd->open_data("actions_roles")){
484         while($roles !==false && $v_rst = $sgbd->fetch_data($rst)){
485           if(isset($v_rst) && isset($v_rst["action"]) && isset($v_rst["id_role"])){
486             if(
487                  $v_rst["action"] == $mod
488               || $v_rst["action"] == $mod."/".$controller
489               || $v_rst["action"] == $mod."/".$controller."/".$action
490             ){
491               if(!isset($roles[$v_rst["action"]])) $roles[$v_rst["action"]] = array();
492               $roles[$v_rst["action"]][$v_rst["id_role"]] = true;
493             }
494           }
495           else $roles = false;
496         }
497         $sgbd->close_data($rst);
498       }
499       else $roles = false;
500       return $roles;
501     }
502
503     function get_actions($id_role = null){
504       $env = $this->env();
505       if($actions = $env->get_actions()){
506         if(($actions_roles = $this->read_actions_roles(array("group_by_action" => true))) !== false){
507           foreach($actions as $module_name => $module){
508             if(isset($id_role)) $actions[$module_name]["module_allowed"] =
509                 isset($actions_roles[$module_name])
510             &&  in_array($id_role, $actions_roles[$module_name]);
511             $actions[$module_name]["is_public"] =
512                 isset($actions_roles[$module_name])
513             &&  in_array(0, $actions_roles[$module_name]);
514             foreach($module["controleurs"] as $controleur_name => $controleur){
515               if(isset($id_role)) $actions[$module_name]["controleurs"][$controleur_name]["controleur_allowed"] =
516                   isset($actions_roles[$module_name."/".$controleur_name])
517               &&  in_array($id_role, $actions_roles[$module_name."/".$controleur_name]);
518               $actions[$module_name]["controleurs"][$controleur_name]["is_public"] =
519                   isset($actions_roles[$module_name."/".$controleur_name])
520               &&  in_array(0, $actions_roles[$module_name."/".$controleur_name]);
521               foreach($controleur["als"] as $index_als => $al){
522                 if($al["actions"]){
523                   if(isset($id_role)){
524                     $HAS_ACTION_NOT_ALLOWED = false;
525                     foreach($al["actions"] as $action_name){
526                       if(
527                           !isset($actions_roles[$module_name."/".$controleur_name."/".$action_name])
528                       ||  !in_array($id_role, $actions_roles[$module_name."/".$controleur_name."/".$action_name])
529                       ){
530                         $HAS_ACTION_NOT_ALLOWED = true;
531                         break;
532                       }
533                     }
534                     if(!$HAS_ACTION_NOT_ALLOWED){
535                       $actions[$module_name]["controleurs"][$controleur_name]["als"][$index_als]["action_allowed"] = true;
536                     }
537                   }
538                   $HAS_ACTION_NOT_ALLOWED = false;
539                   foreach($al["actions"] as $action_name){
540                     if(
541                         !isset($actions_roles[$module_name."/".$controleur_name."/".$action_name])
542                     ||  !in_array(0, $actions_roles[$module_name."/".$controleur_name."/".$action_name])
543                     ){
544                       $HAS_ACTION_NOT_ALLOWED = true;
545                       break;
546                     }
547                   }
548                   if(!$HAS_ACTION_NOT_ALLOWED){
549                     $actions[$module_name]["controleurs"][$controleur_name]["als"][$index_als]["is_public"] = true;
550                   }
551                 }
552               }
553             }
554           }
555           return $actions;
556         }
557       }
558       return array();
559     }
560
561   }
562
563 ?>