nouveau SQL import / maj donnees XML
[mtweb] / mw / app / data / modules / xml / mw_data_users.php
1 <?php
2
3   class mw_data_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 del_user($login){
235       if(($user = $this->user($login)) !== false){
236         $sgbd = $this->sgbd();
237         if(!$sgbd->del_data("users", $user["id"])) return false;
238         if($rst = $sgbd->open_data("users_roles")){
239           $OK = true;
240           while($v_rst = $sgbd->fetch_data($rst)){
241             if(isset($v_rst)){
242               if(isset($v_rst["id"]) && isset($v_rst["id_user"]) && $v_rst["id_user"] == $user["id"]){
243                 if(!$sgbd->del_data("users_roles", $v_rst["id"])){
244                   $OK = false;
245                   break;
246                 }
247               }
248             }
249             else $OK = false;
250           }
251           $sgbd->close_data($rst);
252           return $OK;
253         }
254       }
255       return false;
256     }
257
258     # ----------------------------------------------------------------------------------------
259     #                                                                                    roles
260     #
261
262     function init_roles(){
263       $sgbd = $this->sgbd();
264       $this->roles = array();
265       if($rst = $sgbd->open_data("roles")){
266         while($v_rst = $sgbd->fetch_data($rst)){
267           if(isset($v_rst)){
268             $this->roles[$v_rst["id"]] = $v_rst;
269           }
270           else{
271             $this->roles = false;
272             break;
273           }
274         }
275         $sgbd->close_data($rst);
276       }
277       else $this->roles = false;
278       return $this->roles;
279     }
280
281     function roles(){
282       if(!isset($this->roles)) return false;
283       return $this->roles;
284     }
285
286     function add_role($nom, $intitule){
287       $sgbd = $this->sgbd();
288       $id_role = $sgbd->add_data(
289         "roles",
290         array(
291           "nom" => $nom,
292           "intitule" => $intitule
293         )
294       );
295       if(!isset($id_role)) return false;
296       return $id_role;
297     }
298
299     function get_role($id){
300       if($id === "0") return array(
301         "id" => 0,
302         "nom" => "",
303         "intitule" => ""
304       );
305       $sgbd = $this->sgbd();
306       $role = $sgbd->get_data("roles", $id);
307       if(!isset($role)) return false;
308       return $role ? $role : array();
309     }
310
311     function set_role($id, $nom, $intitule){
312       $sgbd = $this->sgbd();
313       if(
314         !$sgbd->set_data(
315           "roles",
316           $id,
317           array(
318             "nom" => $nom,
319             "intitule" => $intitule
320           )
321         )
322       ) return false;
323       return true;
324     }
325
326     function clear_role_actions($id_role){
327       $sgbd = $this->sgbd();
328       if($rst = $sgbd->open_data("actions_roles")){
329         $OK = true;
330         while($v_rst = $sgbd->fetch_data($rst)){
331           if(isset($v_rst)){
332             if(isset($v_rst["id"]) && isset($v_rst["id_role"]) && $v_rst["id_role"] == $id_role){
333               if(!$sgbd->del_data("actions_roles", $v_rst["id"])){
334                 $OK = false;
335                 break;
336               }
337             }
338           }
339           else $OK = false;
340         }
341         $sgbd->close_data($rst);
342         return $OK;
343       }
344       return false;
345     }
346
347     function clear_role_users($id_role){
348       $sgbd = $this->sgbd();
349       if($rst = $sgbd->open_data("users_roles")){
350         $OK = true;
351         while($v_rst = $sgbd->fetch_data($rst)){
352           if(isset($v_rst)){
353             if(isset($v_rst["id"]) && isset($v_rst["id_role"]) && $v_rst["id_role"] == $id_role){
354               if(!$sgbd->del_data("users_roles", $v_rst["id"])){
355                 $OK = false;
356                 break;
357               }
358             }
359           }
360           else $OK = false;
361         }
362         $sgbd->close_data($rst);
363         return $OK;
364       }
365       return false;
366     }
367
368     function add_role_action($id_role, $action){
369       $sgbd = $this->sgbd();
370       $id_action_role = $sgbd->add_data(
371         "actions_roles",
372         array(
373           "action" => $action,
374           "id_role" => $id_role
375         )
376       );
377       if(!isset($id_action_role)) return false;
378       return $id_action_role;
379     }
380
381     function del_role($id_role){
382       $sgbd = $this->sgbd();
383       return $sgbd->del_data("roles", $id_role) ? true : false;
384     }
385
386     function get_user_roles(){
387       $user_roles = array();
388       $user = $this->get_session_user();
389       if($user && isset($user["id"])){
390         $sgbd = $this->sgbd();
391         if($rst = $sgbd->open_data("users_roles")){
392           while($v_rst = $sgbd->fetch_data($rst)){
393             if(isset($v_rst)){
394               if(isset($v_rst["id_role"]) && isset($v_rst["id_user"]) && $v_rst["id_user"] == $user["id"]){
395                 $user_roles[] = $v_rst["id_role"];
396               }
397             }
398             else{
399               $user_roles = false;
400               break;
401             }
402           }
403           $sgbd->close_data($rst);
404         }
405         else $user_roles = false;
406         if($user_roles === false) return false;
407       }
408       else $user_roles[] = 0;
409       if(!$user_roles) $user_roles[] = 0;
410       return $user_roles;
411     }
412
413     function init_actions_roles(){
414       if(!isset($this->roles)) return false;
415       $this->actions_roles = $this->read_actions_roles();
416       return $this->actions_roles;
417     }
418
419     function read_actions_roles($params = array()){
420       if(!isset($this->roles)) return false;
421       $group_by_action = isset($params["group_by_action"]) ? $params["group_by_action"] : false;
422       $sgbd = $this->sgbd();
423       $actions_roles = array();
424       if($rst = $sgbd->open_data("actions_roles")){
425         while($v_rst = $sgbd->fetch_data($rst)){
426           if(isset($v_rst)){
427             if(isset($v_rst["action"]) && isset($v_rst["id_role"])){
428               if($group_by_action){
429                 if(!isset($actions_roles[$v_rst["action"]])) $actions_roles[$v_rst["action"]] = array();
430                 $actions_roles[$v_rst["action"]][] = $v_rst["id_role"];
431               }
432               else $actions_roles[$v_rst["id"]] = $v_rst;
433             }
434           }
435           else{
436             $actions_roles = false;
437             break;
438           }
439         }
440         $sgbd->close_data($rst);
441       }
442       else $actions_roles = false;
443       return $actions_roles;
444     }
445
446     function get_action_roles($mod, $controller = "index", $action = "index"){
447       $sgbd = $this->sgbd();
448       $roles = array();
449       if($rst = $sgbd->open_data("actions_roles")){
450         while($roles !==false && $v_rst = $sgbd->fetch_data($rst)){
451           if(isset($v_rst) && isset($v_rst["action"]) && isset($v_rst["id_role"])){
452             if(
453                  $v_rst["action"] == $mod
454               || $v_rst["action"] == $mod."/".$controller
455               || $v_rst["action"] == $mod."/".$controller."/".$action
456             ){
457               if(!isset($roles[$v_rst["action"]])) $roles[$v_rst["action"]] = array();
458               $roles[$v_rst["action"]][$v_rst["id_role"]] = true;
459             }
460           }
461           else $roles = false;
462         }
463         $sgbd->close_data($rst);
464       }
465       else $roles = false;
466       return $roles;
467     }
468
469     function get_actions($id_role = null){
470       $env = $this->env();
471       if($actions = $env->get_actions()){
472         if(($actions_roles = $this->read_actions_roles(array("group_by_action" => true))) !== false){
473           foreach($actions as $module_name => $module){
474             if(isset($id_role)) $actions[$module_name]["module_allowed"] =
475                 isset($actions_roles[$module_name])
476             &&  in_array($id_role, $actions_roles[$module_name]);
477             $actions[$module_name]["is_public"] =
478                 isset($actions_roles[$module_name])
479             &&  in_array(0, $actions_roles[$module_name]);
480             foreach($module["controleurs"] as $controleur_name => $controleur){
481               if(isset($id_role)) $actions[$module_name]["controleurs"][$controleur_name]["controleur_allowed"] =
482                   isset($actions_roles[$module_name."/".$controleur_name])
483               &&  in_array($id_role, $actions_roles[$module_name."/".$controleur_name]);
484               $actions[$module_name]["controleurs"][$controleur_name]["is_public"] =
485                   isset($actions_roles[$module_name."/".$controleur_name])
486               &&  in_array(0, $actions_roles[$module_name."/".$controleur_name]);
487               foreach($controleur["als"] as $index_als => $al){
488                 if($al["actions"]){
489                   if(isset($id_role)){
490                     $HAS_ACTION_NOT_ALLOWED = false;
491                     foreach($al["actions"] as $action_name){
492                       if(
493                           !isset($actions_roles[$module_name."/".$controleur_name."/".$action_name])
494                       ||  !in_array($id_role, $actions_roles[$module_name."/".$controleur_name."/".$action_name])
495                       ){
496                         $HAS_ACTION_NOT_ALLOWED = true;
497                         break;
498                       }
499                     }
500                     if(!$HAS_ACTION_NOT_ALLOWED){
501                       $actions[$module_name]["controleurs"][$controleur_name]["als"][$index_als]["action_allowed"] = true;
502                     }
503                   }
504                   $HAS_ACTION_NOT_ALLOWED = false;
505                   foreach($al["actions"] as $action_name){
506                     if(
507                         !isset($actions_roles[$module_name."/".$controleur_name."/".$action_name])
508                     ||  !in_array(0, $actions_roles[$module_name."/".$controleur_name."/".$action_name])
509                     ){
510                       $HAS_ACTION_NOT_ALLOWED = true;
511                       break;
512                     }
513                   }
514                   if(!$HAS_ACTION_NOT_ALLOWED){
515                     $actions[$module_name]["controleurs"][$controleur_name]["als"][$index_als]["is_public"] = true;
516                   }
517                 }
518               }
519             }
520           }
521           return $actions;
522         }
523       }
524       return array();
525     }
526
527   }
528
529 ?>