3 class mw_data_xml_users extends mw_data{
10 # ----------------------------------------------------------------------------------------
14 public 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 public 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 public function user_by_id($id){
103 $user = $env->get_model("users");
104 if($user->load("id", $id) === false) return false;
105 return $user->get_values();
108 public function user($login){
110 $user = $env->get_model("users");
111 if($user->load("login", $login) === false) return false;
112 return $user->get_values();
115 public function user_exists($login){
116 $sgbd = $this->sgbd();
118 if($rst = $sgbd->open_data("users")){
119 while($v_rst = $sgbd->fetch_data($rst)){
121 if(isset($v_rst["login"]) && $v_rst["login"] == $login){
130 $sgbd->close_data($rst);
132 else $EXISTS = false;
136 public function add_user($login, $password, $email, $roles){
137 $sgbd = $this->sgbd();
140 $id_user = $sgbd->add_data(
144 "password" => $password,
151 foreach($roles as $id_role){
152 $OK = $sgbd->add_data(
155 "id_user" => $id_user,
156 "id_role" => $id_role
161 if(!$OK) return false;
165 public function set_user($id, $login, $password, $email, $roles){
166 $sgbd = $this->sgbd();
173 "password" => $password,
178 if($rst = $sgbd->open_data("users_roles")){
180 while($v_rst = $sgbd->fetch_data($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"])){
191 $sgbd->close_data($rst);
192 if(!$OK) return false;
195 foreach($roles as $id_role){
196 $OK = $sgbd->add_data(
200 "id_role" => $id_role
205 if(!$OK) return false;
209 public function clear_user_roles($id_user){
210 $sgbd = $this->sgbd();
211 if($rst = $sgbd->open_data("users_roles")){
213 while($v_rst = $sgbd->fetch_data($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"])){
224 $sgbd->close_data($rst);
230 public function add_user_role($id_user, $id_role){
231 $sgbd = $this->sgbd();
232 $OK = $sgbd->add_data(
235 "id_user" => $id_user,
236 "id_role" => $id_role
239 if(!$OK) return false;
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")){
249 while($v_rst = $sgbd->fetch_data($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"])){
260 $sgbd->close_data($rst);
267 # ----------------------------------------------------------------------------------------
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)){
277 $this->roles[$v_rst["id"]] = $v_rst;
280 $this->roles = false;
284 $sgbd->close_data($rst);
286 else $this->roles = false;
290 public function roles(){
291 if(!isset($this->roles)) return false;
295 public function add_role($nom, $intitule){
296 $sgbd = $this->sgbd();
297 $id_role = $sgbd->add_data(
301 "intitule" => $intitule
304 if(!isset($id_role)) return false;
308 public function get_role($id){
309 if($id === "0") return array(
314 $sgbd = $this->sgbd();
315 $role = $sgbd->get_data("roles", $id);
316 if(!isset($role)) return false;
317 return $role ? $role : array();
320 public function set_role($id, $nom, $intitule){
321 $sgbd = $this->sgbd();
328 "intitule" => $intitule
335 public function clear_role_actions($id_role){
336 $sgbd = $this->sgbd();
337 if($rst = $sgbd->open_data("actions_roles")){
339 while($v_rst = $sgbd->fetch_data($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"])){
350 $sgbd->close_data($rst);
356 public function clear_role_users($id_role){
357 $sgbd = $this->sgbd();
358 if($rst = $sgbd->open_data("users_roles")){
360 while($v_rst = $sgbd->fetch_data($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"])){
371 $sgbd->close_data($rst);
377 public function add_role_action($id_role, $action){
378 $sgbd = $this->sgbd();
379 $id_action_role = $sgbd->add_data(
383 "id_role" => $id_role
386 if(!isset($id_action_role)) return false;
387 return $id_action_role;
390 function del_role($id_role){
391 $sgbd = $this->sgbd();
392 return $sgbd->del_data("roles", $id_role) ? true : false;
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)){
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"];
412 $sgbd->close_data($rst);
414 else $user_roles = false;
415 if($user_roles === false) return false;
417 else $user_roles[] = 0;
418 if(!$user_roles) $user_roles[] = 0;
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;
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)){
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"];
441 else $actions_roles[$v_rst["id"]] = $v_rst;
445 $actions_roles = false;
449 $sgbd->close_data($rst);
451 else $actions_roles = false;
452 return $actions_roles;
455 public function get_action_roles($mod, $controller = "index", $action = "index"){
456 $sgbd = $this->sgbd();
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"])){
462 $v_rst["action"] == $mod
463 || $v_rst["action"] == $mod."/".$controller
464 || $v_rst["action"] == $mod."/".$controller."/".$action
466 if(!isset($roles[$v_rst["action"]])) $roles[$v_rst["action"]] = array();
467 $roles[$v_rst["action"]][$v_rst["id_role"]] = true;
472 $sgbd->close_data($rst);
478 public function get_actions($id_role = null){
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){
499 $HAS_ACTION_NOT_ALLOWED = false;
500 foreach($al["actions"] as $action_name){
502 !isset($actions_roles[$module_name."/".$controleur_name."/".$action_name])
503 || !in_array($id_role, $actions_roles[$module_name."/".$controleur_name."/".$action_name])
505 $HAS_ACTION_NOT_ALLOWED = true;
509 if(!$HAS_ACTION_NOT_ALLOWED){
510 $actions[$module_name]["controleurs"][$controleur_name]["als"][$index_als]["action_allowed"] = true;
513 $HAS_ACTION_NOT_ALLOWED = false;
514 foreach($al["actions"] as $action_name){
516 !isset($actions_roles[$module_name."/".$controleur_name."/".$action_name])
517 || !in_array(0, $actions_roles[$module_name."/".$controleur_name."/".$action_name])
519 $HAS_ACTION_NOT_ALLOWED = true;
523 if(!$HAS_ACTION_NOT_ALLOWED){
524 $actions[$module_name]["controleurs"][$controleur_name]["als"][$index_als]["is_public"] = true;