roles multiples possible par user, administrables
[mtweb] / mw / app / data / modules / xml / mw_data_users.php
index 70287a3..2f70095 100644 (file)
               $this->users[$user["id"]] = $user;
             }
           }
+          foreach($users["list"] as $id_user => $user){
+            if(($status = $this->list_user_status($id_user)) !== false){
+              $users["list"][$id_user]["status"] = $status;
+            }
+            else{
+              $users = false;
+              break;
+            }
+          }
         }
         else $users = false;
       }
       return $users;
     }
 
+    function list_user_status($id_user){
+      $sgbd = $this->sgbd();
+      $status = array();
+      if($rst = $sgbd->open_data("users_roles")){
+        while($v_rst = $sgbd->fetch_data($rst)){
+          if(isset($v_rst)){
+            if(isset($v_rst["id_role"]) && isset($v_rst["id_user"]) && $v_rst["id_user"] == $id_user){
+              $status[] = $v_rst["id_role"];
+            }
+          }
+          else{
+            $status = false;
+            break;
+          }
+        }
+        $sgbd->close_data($rst);
+      }
+      else $status = false;
+      return $status;
+    }
+
     function user_by_id($id){
       if(!isset($this->users)) $this->users = array();
       if(isset($this->users[$id])) return $this->users[$id];
       $sgbd = $this->sgbd();
       if(($user = $sgbd->get_data("users", $id)) !== false){
         $this->users[$id] = $user;
+        if(($status = $this->list_user_status($user["id"])) !== false) $user["status"] = $status;
+        else $user = false;
       }
       return $user;
     }
           else $user = false;
         }
         $sgbd->close_data($rst);
+        if($user){
+          if(($status = $this->list_user_status($user["id"])) !== false) $user["status"] = $status;
+          else $user = false;
+        }
       }
       else $user = false;
       if($user !== false){
 
     function add_user($login, $password, $email, $status){
       $sgbd = $this->sgbd();
-      return $sgbd->add_data(
-        "users",
-        array(
-          "login" => $login,
-          "password" => $password,
-          "email" => $email,
-          "status" => $status
-        )
-      );
+      if(
+        (
+          $id_user = $sgbd->add_data(
+            "users",
+            array(
+              "login" => $login,
+              "password" => $password,
+              "email" => $email
+            )
+          )
+        ) === false
+      ) return false;
+      $OK = true;
+      foreach($status as $id_role){
+        $OK = $sgbd->add_data(
+          "users_roles",
+          array(
+            "id_user" => $id_user,
+            "id_role" => $id_role
+          )
+        );
+        if(!$OK) break;
+      }
+      if(!$OK) return false;
+      return $id_user;
     }
 
     function set_user($id, $login, $password, $email, $status){
       $sgbd = $this->sgbd();
-      return $sgbd->set_data(
-        "users",
-        $id,
-        array(
-          "login" => $login,
-          "password" => $password,
-          "email" => $email,
-          "status" => $status
+      if(
+        !$sgbd->set_data(
+          "users",
+          $id,
+          array(
+            "login" => $login,
+            "password" => $password,
+            "email" => $email
+          )
         )
-      );
+      ) return false;
+      if($rst = $sgbd->open_data("users_roles")){
+        $OK = true;
+        while($v_rst = $sgbd->fetch_data($rst)){
+          if(isset($v_rst)){
+            if(isset($v_rst["id"]) && isset($v_rst["id_user"]) && $v_rst["id_user"] == $id){
+              if(!$sgbd->del_data("users_roles", $v_rst["id"])){
+                $OK = false;
+                break;
+              }
+            }
+          }
+          else $OK = false;
+        }
+        $sgbd->close_data($rst);
+        if(!$OK) return false;
+      }
+      else return false;
+      foreach($status as $id_role){
+        $OK = $sgbd->add_data(
+          "users_roles",
+          array(
+            "id_user" => $id,
+            "id_role" => $id_role
+          )
+        );
+        if(!$OK) break;
+      }
+      if(!$OK) return false;
+      return true;
     }
 
     function del_user($login){
       if(($user = $this->user($login)) !== false){
         $sgbd = $this->sgbd();
-        return $sgbd->del_data("users", $user["id"]);
+        if(!$sgbd->del_data("users", $user["id"])) return false;
+        if($rst = $sgbd->open_data("users_roles")){
+          $OK = true;
+          while($v_rst = $sgbd->fetch_data($rst)){
+            if(isset($v_rst)){
+              if(isset($v_rst["id"]) && isset($v_rst["id_user"]) && $v_rst["id_user"] == $user["id"]){
+                if(!$sgbd->del_data("users_roles", $v_rst["id"])){
+                  $OK = false;
+                  break;
+                }
+              }
+            }
+            else $OK = false;
+          }
+          $sgbd->close_data($rst);
+          return $OK;
+        }
       }
       return false;
     }
     #                                                                                   status
     #
 
-    function status(){
-      if(!isset($this->user_status)) return false;
-      return $this->user_status;
-    }
-
     function init_user_status($status = array()){
       $sgbd = $this->sgbd();
       $this->user_status = array();
-      if($rst = $sgbd->open_data("user_status")){
+      if($rst = $sgbd->open_data("roles")){
         while($v_rst = $sgbd->fetch_data($rst)){
           if(isset($v_rst)){
             $this->user_status[$v_rst["id"]] = $v_rst;
         $sgbd->close_data($rst);
       }
       else $this->user_status = false;
-      if($status && $this->user_status !== false){
-        foreach($status as $new_user_status){
-          $id_status = false;
-          foreach($this->user_status as $user_status) if($new_user_status["nom"] == $user_status["nom"]){
-            $id_status = $user_status["id"];
-            break;
-          }
-          if($id_status){
-            $SAME = true;
-            foreach($new_user_status as $status_key => $status_value){
-              if(!isset($this->user_status[$id_status][$status_key]) || $this->user_status[$id_status][$status_key] != $status_value){
-                $SAME = false;
+      return $this->user_status;
+    }
+
+    function add_role($nom, $intitule){
+      $sgbd = $this->sgbd();
+      $id_role = $sgbd->add_data(
+        "roles",
+        array(
+          "nom" => $nom,
+          "intitule" => $intitule
+        )
+      );
+      if(!isset($id_role)) return false;
+      return $id_role;
+    }
+
+    function get_role($id){
+      if($id === "0") return array(
+        "id" => 0,
+        "nom" => "",
+        "intitule" => ""
+      );
+      $sgbd = $this->sgbd();
+      $role = $sgbd->get_data("roles", $id);
+      if(!isset($role)) return false;
+      return $role ? $role : array();
+    }
+
+    function set_role($id, $nom, $intitule){
+      $sgbd = $this->sgbd();
+      if(
+        !$sgbd->set_data(
+          "roles",
+          $id,
+          array(
+            "nom" => $nom,
+            "intitule" => $intitule
+          )
+        )
+      ) return false;
+      return true;
+    }
+
+    function clear_role_actions($id_role){
+      $sgbd = $this->sgbd();
+      if($rst = $sgbd->open_data("action_status")){
+        $OK = true;
+        while($v_rst = $sgbd->fetch_data($rst)){
+          if(isset($v_rst)){
+            if(isset($v_rst["id"]) && isset($v_rst["id_status"]) && $v_rst["id_status"] == $id_role){
+              if(!$sgbd->del_data("action_status", $v_rst["id"])){
+                $OK = false;
                 break;
               }
             }
-            if(!$SAME){
-              if($sgbd->set_data("user_status", $id_status, $new_user_status)) $this->user_status[$id_status] = $new_user_status;
-              else{
-                $this->user_status = false;
+          }
+          else $OK = false;
+        }
+        $sgbd->close_data($rst);
+        return $OK;
+      }
+      return false;
+    }
+
+    function clear_role_users($id_role){
+      $sgbd = $this->sgbd();
+      if($rst = $sgbd->open_data("users_roles")){
+        $OK = true;
+        while($v_rst = $sgbd->fetch_data($rst)){
+          if(isset($v_rst)){
+            if(isset($v_rst["id"]) && isset($v_rst["id_role"]) && $v_rst["id_role"] == $id_role){
+              if(!$sgbd->del_data("users_roles", $v_rst["id"])){
+                $OK = false;
                 break;
               }
             }
           }
-          else{
-            if($id_status = $sgbd->add_data("user_status", $new_user_status)) $this->user_status[$id_status] = $new_user_status;
+          else $OK = false;
+        }
+        $sgbd->close_data($rst);
+        return $OK;
+      }
+      return false;
+    }
+
+    function add_role_action($id_role, $action){
+      $sgbd = $this->sgbd();
+      $id_action_status = $sgbd->add_data(
+        "action_status",
+        array(
+          "action" => $action,
+          "id_status" => $id_role
+        )
+      );
+      if(!isset($id_action_status)) return false;
+      return $id_action_status;
+    }
+
+    function del_role($id_role){
+      $sgbd = $this->sgbd();
+      return $sgbd->del_data("roles", $id_role) ? true : false;
+    }
+
+    function status(){
+      if(!isset($this->user_status)) return false;
+      return $this->user_status;
+    }
+
+    function get_user_status(){
+      $user_status = array();
+      $user = $this->get_session_user();
+      if($user && isset($user["id"])){
+        $sgbd = $this->sgbd();
+        if($rst = $sgbd->open_data("users_roles")){
+          while($v_rst = $sgbd->fetch_data($rst)){
+            if(isset($v_rst)){
+              if(isset($v_rst["id_role"]) && isset($v_rst["id_user"]) && $v_rst["id_user"] == $user["id"]){
+                $user_status[] = $v_rst["id_role"];
+              }
+            }
             else{
-              $this->user_status = false;
+              $user_status = false;
               break;
             }
           }
+          $sgbd->close_data($rst);
         }
+        else $user_status = false;
+        if($user_status === false) return false;
       }
-      return $this->user_status;
+      else $user_status[] = 0;
+      if(!$user_status) $user_status[] = 0;
+      return $user_status;
     }
 
     function init_action_status($status = array()){
       if(!isset($this->user_status)) return false;
+      $this->action_status = $this->read_action_status();
+      return $this->action_status;
+    }
+
+    function read_action_status($params = array()){
+      if(!isset($this->user_status)) return false;
+      $group_by_action = isset($params["group_by_action"]) ? $params["group_by_action"] : false;
       $sgbd = $this->sgbd();
-      $this->action_status = array();
+      $action_status = array();
       if($rst = $sgbd->open_data("action_status")){
         while($v_rst = $sgbd->fetch_data($rst)){
           if(isset($v_rst)){
-            $this->action_status[$v_rst["id"]] = $v_rst;
+            if(isset($v_rst["action"]) && isset($v_rst["id_status"])){
+              if($group_by_action){
+                if(!isset($action_status[$v_rst["action"]])) $action_status[$v_rst["action"]] = array();
+                $action_status[$v_rst["action"]][] = $v_rst["id_status"];
+              }
+              else $action_status[$v_rst["id"]] = $v_rst;
+            }
           }
           else{
-            $this->action_status = false;
+            $action_status = false;
             break;
           }
         }
         $sgbd->close_data($rst);
       }
-      else $this->action_status = false;
-      if($status && $this->action_status !== false){
-        $STATUS_OK = true;
-        foreach($status as $id_new_action_status => $new_action_status){
-          $FOUND = $new_action_status["id_status"] == "0";
-          if(!$FOUND) foreach($this->user_status as $user_status){
-            if($new_action_status["id_status"] == $user_status["nom"]){
-              $FOUND = true;
-              $status[$id_new_action_status]["id_status"] = $user_status["id"];
-            }
-          }
-          if(!$FOUND){
-            $STATUS_OK = false;
-            break;
-          }
-        }
-        if($STATUS_OK){
-          foreach($status as $new_action_status){
-            $id_status = false;
-            foreach($this->action_status as $action_status){
-              if(
-                   $new_action_status["action"] == $action_status["action"]
-                && $new_action_status["id_status"] == $action_status["id_status"]
-              ){
-                $id_status = $action_status["id"];
-                break;
-              }
-            }
-            if($id_status){
-              $SAME = true;
-              foreach($new_action_status as $status_key => $status_value){
-                if(!isset($this->action_status[$id_status][$status_key]) || $this->action_status[$id_status][$status_key] != $status_value){
-                  $SAME = false;
-                  break;
-                }
-              }
-              if(!$SAME){
-                if($id_status = $sgbd->add_data("action_status", $new_action_status)) $this->action_status[$id_status] = $new_action_status;
-                else{
-                  $this->action_status = false;
-                  break;
-                }
-              }
-            }
-            else{
-              if($id_status = $sgbd->add_data("action_status", $new_action_status)) $this->action_status[$id_status] = $new_action_status;
-              else{
-                $this->action_status = false;
-                break;
-              }
-            }
-          }
-        }
-        else $this->action_status = false;
-      }
-      return $this->action_status;
-    }
-
-    function get_user_status(){
-      $user = $this->get_session_user();
-      if($user && isset($user["status"])) return $user["status"];
-      return 0;
+      else $action_status = false;
+      return $action_status;
     }
 
     function get_action_status($mod, $controller = "index", $action = "index", $set_status = array()){
         $sgbd->close_data($rst);
       }
       else $status = false;
-      if($status !== false){
-        if($set_status){
-          foreach($set_status as $new_action_status){
-            $id_status = false;
-            foreach($status as $user_status) if($new_user_status["nom"] == $user_status["nom"]){
-              $id_status = $user_status["id"];
-              break;
-            }
-            if($id_status){
-              $SAME = true;
-              foreach($new_user_status as $status_key => $status_value){
-                if(!isset($status[$id_status][$status_key]) || $status[$id_status][$status_key] != $status_value){
-                  $SAME = false;
-                  break;
-                }
-              }
-              if(!$SAME){
-                if($sgbd->set_data("user_status", $id_status, $new_user_status)) $status[$id_status] = $new_user_status;
-                else{
-                  $status = false;
-                  break;
-                }
-              }
-            }
-            else{
-              if($id_status = $sgbd->add_data("user_status", $new_user_status)) $status[$id_status] = $new_user_status;
-              else{
-                $status = false;
-                break;
-              }
-            }
-          }
-        }
-      }
       return $status;
     }
 
-    function creation_default_status(){
-      $sgbd = $this->sgbd();
-      $default_status = 0;
-      if($rst = $sgbd->open_data("user_status")){
-        while($v_rst = $sgbd->fetch_data($rst)){
-          if(isset($v_rst)){
-            if(isset($v_rst["creation_default"]) && $v_rst["creation_default"] == 1){
-              $default_status = $v_rst["id"];
-              break;
+    function get_actions($id_role = null){
+      $env = $this->env();
+      if($actions = $env->get_actions()){
+        if(($action_status = $this->read_action_status(array("group_by_action" => true))) !== false){
+          foreach($actions as $module_name => $module){
+            if(isset($id_role)) $actions[$module_name]["module_allowed"] =
+                isset($action_status[$module_name])
+            &&  in_array($id_role, $action_status[$module_name]);
+            $actions[$module_name]["is_public"] =
+                isset($action_status[$module_name])
+            &&  in_array(0, $action_status[$module_name]);
+            foreach($module["controleurs"] as $controleur_name => $controleur){
+              if(isset($id_role)) $actions[$module_name]["controleurs"][$controleur_name]["controleur_allowed"] =
+                  isset($action_status[$module_name."/".$controleur_name])
+              &&  in_array($id_role, $action_status[$module_name."/".$controleur_name]);
+              $actions[$module_name]["controleurs"][$controleur_name]["is_public"] =
+                  isset($action_status[$module_name."/".$controleur_name])
+              &&  in_array(0, $action_status[$module_name."/".$controleur_name]);
+              foreach($controleur["als"] as $index_als => $al){
+                if($al["actions"]){
+                  if(isset($id_role)){
+                    $HAS_ACTION_NOT_ALLOWED = false;
+                    foreach($al["actions"] as $action_name){
+                      if(
+                          !isset($action_status[$module_name."/".$controleur_name."/".$action_name])
+                      ||  !in_array($id_role, $action_status[$module_name."/".$controleur_name."/".$action_name])
+                      ){
+                        $HAS_ACTION_NOT_ALLOWED = true;
+                        break;
+                      }
+                    }
+                    if(!$HAS_ACTION_NOT_ALLOWED){
+                      $actions[$module_name]["controleurs"][$controleur_name]["als"][$index_als]["action_allowed"] = true;
+                    }
+                  }
+                  $HAS_ACTION_NOT_ALLOWED = false;
+                  foreach($al["actions"] as $action_name){
+                    if(
+                        !isset($action_status[$module_name."/".$controleur_name."/".$action_name])
+                    ||  !in_array(0, $action_status[$module_name."/".$controleur_name."/".$action_name])
+                    ){
+                      $HAS_ACTION_NOT_ALLOWED = true;
+                      break;
+                    }
+                  }
+                  if(!$HAS_ACTION_NOT_ALLOWED){
+                    $actions[$module_name]["controleurs"][$controleur_name]["als"][$index_als]["is_public"] = true;
+                  }
+                }
+              }
             }
           }
-          else{
-            $default_status = false;
-            break;
-          }
+          return $actions;
         }
-        $sgbd->close_data($rst);
       }
-      else $default_status = false;
-      return $default_status;
+      return array();
     }
 
     # ----------------------------------------------------------------------------------------