3 class mw_data_xml_users extends mw_data{
10 # ----------------------------------------------------------------------------------------
14 function users($start = 0, $alpha = null, $id_role = null){
15 $sgbd = $this->sgbd();
17 $users = array("list" => array(), "total" => 0);
19 $role_users = array();
20 if($rst = $sgbd->open_data("users_roles")){
21 while($v_rst = $sgbd->fetch_data($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"];
32 $sgbd->close_data($rst);
34 else $role_users = false;
35 if($role_users === false) return false;
38 if($rst = $sgbd->open_data("users")){
39 while($v_rst = $sgbd->fetch_data($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;
53 $sgbd->close_data($rst);
56 foreach($res as $id_user => $user){
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;
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;
80 function list_user_roles($id_user){
81 $sgbd = $this->sgbd();
83 if($rst = $sgbd->open_data("users_roles")){
84 while($v_rst = $sgbd->fetch_data($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"];
95 $sgbd->close_data($rst);
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;
113 function user($login){
114 $sgbd = $this->sgbd();
116 if($rst = $sgbd->open_data("users")){
117 while($v_rst = $sgbd->fetch_data($rst)){
119 if(isset($v_rst["login"]) && $v_rst["login"] == $login){
126 $sgbd->close_data($rst);
128 if(($roles = $this->list_user_roles($user["id"])) !== false) $user["roles"] = $roles;
134 if(!isset($this->users)) $this->users = array();
135 if($user) $this->users[$user["id"]] = $user;
140 function user_exists($login){
141 $sgbd = $this->sgbd();
143 if($rst = $sgbd->open_data("users")){
144 while($v_rst = $sgbd->fetch_data($rst)){
146 if(isset($v_rst["login"]) && $v_rst["login"] == $login){
155 $sgbd->close_data($rst);
157 else $EXISTS = false;
161 function add_user($login, $password, $email, $roles){
162 $sgbd = $this->sgbd();
165 $id_user = $sgbd->add_data(
169 "password" => $password,
176 foreach($roles as $id_role){
177 $OK = $sgbd->add_data(
180 "id_user" => $id_user,
181 "id_role" => $id_role
186 if(!$OK) return false;
190 function set_user($id, $login, $password, $email, $roles){
191 $sgbd = $this->sgbd();
198 "password" => $password,
203 if($rst = $sgbd->open_data("users_roles")){
205 while($v_rst = $sgbd->fetch_data($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"])){
216 $sgbd->close_data($rst);
217 if(!$OK) return false;
220 foreach($roles as $id_role){
221 $OK = $sgbd->add_data(
225 "id_role" => $id_role
230 if(!$OK) return false;
234 function clear_user_roles($id_user){
235 $sgbd = $this->sgbd();
236 if($rst = $sgbd->open_data("users_roles")){
238 while($v_rst = $sgbd->fetch_data($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"])){
249 $sgbd->close_data($rst);
255 function add_user_role($id_user, $id_role){
256 $sgbd = $this->sgbd();
257 $OK = $sgbd->add_data(
260 "id_user" => $id_user,
261 "id_role" => $id_role
264 if(!$OK) return false;
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")){
274 while($v_rst = $sgbd->fetch_data($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"])){
285 $sgbd->close_data($rst);
292 # ----------------------------------------------------------------------------------------
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)){
302 $this->roles[$v_rst["id"]] = $v_rst;
305 $this->roles = false;
309 $sgbd->close_data($rst);
311 else $this->roles = false;
316 if(!isset($this->roles)) return false;
320 function add_role($nom, $intitule){
321 $sgbd = $this->sgbd();
322 $id_role = $sgbd->add_data(
326 "intitule" => $intitule
329 if(!isset($id_role)) return false;
333 function get_role($id){
334 if($id === "0") return array(
339 $sgbd = $this->sgbd();
340 $role = $sgbd->get_data("roles", $id);
341 if(!isset($role)) return false;
342 return $role ? $role : array();
345 function set_role($id, $nom, $intitule){
346 $sgbd = $this->sgbd();
353 "intitule" => $intitule
360 function clear_role_actions($id_role){
361 $sgbd = $this->sgbd();
362 if($rst = $sgbd->open_data("actions_roles")){
364 while($v_rst = $sgbd->fetch_data($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"])){
375 $sgbd->close_data($rst);
381 function clear_role_users($id_role){
382 $sgbd = $this->sgbd();
383 if($rst = $sgbd->open_data("users_roles")){
385 while($v_rst = $sgbd->fetch_data($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"])){
396 $sgbd->close_data($rst);
402 function add_role_action($id_role, $action){
403 $sgbd = $this->sgbd();
404 $id_action_role = $sgbd->add_data(
408 "id_role" => $id_role
411 if(!isset($id_action_role)) return false;
412 return $id_action_role;
415 function del_role($id_role){
416 $sgbd = $this->sgbd();
417 return $sgbd->del_data("roles", $id_role) ? true : false;
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)){
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"];
437 $sgbd->close_data($rst);
439 else $user_roles = false;
440 if($user_roles === false) return false;
442 else $user_roles[] = 0;
443 if(!$user_roles) $user_roles[] = 0;
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;
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)){
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"];
466 else $actions_roles[$v_rst["id"]] = $v_rst;
470 $actions_roles = false;
474 $sgbd->close_data($rst);
476 else $actions_roles = false;
477 return $actions_roles;
480 function get_action_roles($mod, $controller = "index", $action = "index"){
481 $sgbd = $this->sgbd();
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"])){
487 $v_rst["action"] == $mod
488 || $v_rst["action"] == $mod."/".$controller
489 || $v_rst["action"] == $mod."/".$controller."/".$action
491 if(!isset($roles[$v_rst["action"]])) $roles[$v_rst["action"]] = array();
492 $roles[$v_rst["action"]][$v_rst["id_role"]] = true;
497 $sgbd->close_data($rst);
503 function get_actions($id_role = null){
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){
524 $HAS_ACTION_NOT_ALLOWED = false;
525 foreach($al["actions"] as $action_name){
527 !isset($actions_roles[$module_name."/".$controleur_name."/".$action_name])
528 || !in_array($id_role, $actions_roles[$module_name."/".$controleur_name."/".$action_name])
530 $HAS_ACTION_NOT_ALLOWED = true;
534 if(!$HAS_ACTION_NOT_ALLOWED){
535 $actions[$module_name]["controleurs"][$controleur_name]["als"][$index_als]["action_allowed"] = true;
538 $HAS_ACTION_NOT_ALLOWED = false;
539 foreach($al["actions"] as $action_name){
541 !isset($actions_roles[$module_name."/".$controleur_name."/".$action_name])
542 || !in_array(0, $actions_roles[$module_name."/".$controleur_name."/".$action_name])
544 $HAS_ACTION_NOT_ALLOWED = true;
548 if(!$HAS_ACTION_NOT_ALLOWED){
549 $actions[$module_name]["controleurs"][$controleur_name]["als"][$index_als]["is_public"] = true;