+++ /dev/null
-<?php
-
- class mw_data_users extends mw_data{
-
- var $users;
- var $user;
- var $roles;
- var $actions_roles;
-
- # ----------------------------------------------------------------------------------------
- # users
- #
-
- function users($start = 0, $alpha = null, $id_role = null){
- $sgbd = $this->sgbd();
- $env = $this->env();
- $users = array("list" => array(), "total" => 0);
- if(isset($id_role)){
- $role_users = array();
- if($rst = $sgbd->open_data("users_roles")){
- while($v_rst = $sgbd->fetch_data($rst)){
- if(isset($v_rst)){
- if(($v_rst["id_user"]) && isset($v_rst["id_role"]) && $v_rst["id_role"] == $id_role){
- $role_users[] = $v_rst["id_user"];
- }
- }
- else{
- $role_users = false;
- break;
- }
- }
- $sgbd->close_data($rst);
- }
- else $role_users = false;
- if($role_users === false) return false;
- }
- $res = array();
- if($rst = $sgbd->open_data("users")){
- while($v_rst = $sgbd->fetch_data($rst)){
- if(isset($v_rst)){
- if(!isset($alpha) || (isset($v_rst["login"]) && strtolower(substr($v_rst["login"], 0, 1)) == strtolower($alpha))){
- if(!isset($id_role) || in_array($id_role, $role_users)){
- $res[$v_rst["id"]] = $v_rst;
- $users["total"]++;
- }
- }
- }
- else{
- $res = false;
- break;
- }
- }
- $sgbd->close_data($rst);
- if($res !== false){
- $n = 0;
- foreach($res as $id_user => $user){
- $n++;
- if(!$env->config("max_list") || ($n > $start && $n <= ($start + $env->config("max_list")))){
- $users["list"][$user["id"]] = $user;
- if(!isset($this->users)) $this->users = array();
- $this->users[$user["id"]] = $user;
- }
- }
- foreach($users["list"] as $id_user => $user){
- if(($roles = $this->list_user_roles($id_user)) !== false){
- $users["list"][$id_user]["roles"] = $roles;
- }
- else{
- $users = false;
- break;
- }
- }
- }
- else $users = false;
- }
- else $users = false;
- return $users;
- }
-
- function list_user_roles($id_user){
- $sgbd = $this->sgbd();
- $roles = 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){
- $roles[] = $v_rst["id_role"];
- }
- }
- else{
- $roles = false;
- break;
- }
- }
- $sgbd->close_data($rst);
- }
- else $roles = false;
- return $roles;
- }
-
- 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(($roles = $this->list_user_roles($user["id"])) !== false) $user["roles"] = $roles;
- else $user = false;
- }
- return $user;
- }
-
- function user($login){
- $sgbd = $this->sgbd();
- $user = array();
- if($rst = $sgbd->open_data("users")){
- while($v_rst = $sgbd->fetch_data($rst)){
- if(isset($v_rst)){
- if(isset($v_rst["login"]) && $v_rst["login"] == $login){
- $user = $v_rst;
- break;
- }
- }
- else $user = false;
- }
- $sgbd->close_data($rst);
- if($user){
- if(($roles = $this->list_user_roles($user["id"])) !== false) $user["roles"] = $roles;
- else $user = false;
- }
- }
- else $user = false;
- if($user !== false){
- if(!isset($this->users)) $this->users = array();
- if($user) $this->users[$user["id"]] = $user;
- }
- return $user;
- }
-
- function user_exists($login){
- $sgbd = $this->sgbd();
- $EXISTS = 0;
- if($rst = $sgbd->open_data("users")){
- while($v_rst = $sgbd->fetch_data($rst)){
- if(isset($v_rst)){
- if(isset($v_rst["login"]) && $v_rst["login"] == $login){
- $EXISTS++;
- }
- }
- else{
- $EXISTS = false;
- break;
- }
- }
- $sgbd->close_data($rst);
- }
- else $EXISTS = false;
- return $EXISTS;
- }
-
- function add_user($login, $password, $email, $roles){
- $sgbd = $this->sgbd();
- if(
- (
- $id_user = $sgbd->add_data(
- "users",
- array(
- "login" => $login,
- "password" => $password,
- "email" => $email
- )
- )
- ) === false
- ) return false;
- $OK = true;
- foreach($roles 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, $roles){
- $sgbd = $this->sgbd();
- 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($roles 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 clear_user_roles($id_user){
- $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_user"]) && ($v_rst["id_user"] == $id_user)){
- if(!$sgbd->del_data("users_roles", $v_rst["id"])){
- $OK = false;
- break;
- }
- }
- }
- else $OK = false;
- }
- $sgbd->close_data($rst);
- }
- else $OK = false;
- return $OK;
- }
-
- function add_user_role($id_user, $id_role){
- $sgbd = $this->sgbd();
- $OK = $sgbd->add_data(
- "users_roles",
- array(
- "id_user" => $id_user,
- "id_role" => $id_role
- )
- );
- if(!$OK) return false;
- return true;
- }
-
- function del_user($login){
- if(($user = $this->user($login)) !== false){
- $sgbd = $this->sgbd();
- 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;
- }
-
- # ----------------------------------------------------------------------------------------
- # roles
- #
-
- function init_roles(){
- $sgbd = $this->sgbd();
- $this->roles = array();
- if($rst = $sgbd->open_data("roles")){
- while($v_rst = $sgbd->fetch_data($rst)){
- if(isset($v_rst)){
- $this->roles[$v_rst["id"]] = $v_rst;
- }
- else{
- $this->roles = false;
- break;
- }
- }
- $sgbd->close_data($rst);
- }
- else $this->roles = false;
- return $this->roles;
- }
-
- function roles(){
- if(!isset($this->roles)) return false;
- return $this->roles;
- }
-
- 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("actions_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("actions_roles", $v_rst["id"])){
- $OK = false;
- break;
- }
- }
- }
- 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 $OK = false;
- }
- $sgbd->close_data($rst);
- return $OK;
- }
- return false;
- }
-
- function add_role_action($id_role, $action){
- $sgbd = $this->sgbd();
- $id_action_role = $sgbd->add_data(
- "actions_roles",
- array(
- "action" => $action,
- "id_role" => $id_role
- )
- );
- if(!isset($id_action_role)) return false;
- return $id_action_role;
- }
-
- function del_role($id_role){
- $sgbd = $this->sgbd();
- return $sgbd->del_data("roles", $id_role) ? true : false;
- }
-
- function get_user_roles(){
- $user_roles = 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_roles[] = $v_rst["id_role"];
- }
- }
- else{
- $user_roles = false;
- break;
- }
- }
- $sgbd->close_data($rst);
- }
- else $user_roles = false;
- if($user_roles === false) return false;
- }
- else $user_roles[] = 0;
- if(!$user_roles) $user_roles[] = 0;
- return $user_roles;
- }
-
- function init_actions_roles(){
- if(!isset($this->roles)) return false;
- $this->actions_roles = $this->read_actions_roles();
- return $this->actions_roles;
- }
-
- function read_actions_roles($params = array()){
- if(!isset($this->roles)) return false;
- $group_by_action = isset($params["group_by_action"]) ? $params["group_by_action"] : false;
- $sgbd = $this->sgbd();
- $actions_roles = array();
- if($rst = $sgbd->open_data("actions_roles")){
- while($v_rst = $sgbd->fetch_data($rst)){
- if(isset($v_rst)){
- if(isset($v_rst["action"]) && isset($v_rst["id_role"])){
- if($group_by_action){
- if(!isset($actions_roles[$v_rst["action"]])) $actions_roles[$v_rst["action"]] = array();
- $actions_roles[$v_rst["action"]][] = $v_rst["id_role"];
- }
- else $actions_roles[$v_rst["id"]] = $v_rst;
- }
- }
- else{
- $actions_roles = false;
- break;
- }
- }
- $sgbd->close_data($rst);
- }
- else $actions_roles = false;
- return $actions_roles;
- }
-
- function get_action_roles($mod, $controller = "index", $action = "index"){
- $sgbd = $this->sgbd();
- $roles = array();
- if($rst = $sgbd->open_data("actions_roles")){
- while($roles !==false && $v_rst = $sgbd->fetch_data($rst)){
- if(isset($v_rst) && isset($v_rst["action"]) && isset($v_rst["id_role"])){
- if(
- $v_rst["action"] == $mod
- || $v_rst["action"] == $mod."/".$controller
- || $v_rst["action"] == $mod."/".$controller."/".$action
- ){
- if(!isset($roles[$v_rst["action"]])) $roles[$v_rst["action"]] = array();
- $roles[$v_rst["action"]][$v_rst["id_role"]] = true;
- }
- }
- else $roles = false;
- }
- $sgbd->close_data($rst);
- }
- else $roles = false;
- return $roles;
- }
-
- function get_actions($id_role = null){
- $env = $this->env();
- if($actions = $env->get_actions()){
- if(($actions_roles = $this->read_actions_roles(array("group_by_action" => true))) !== false){
- foreach($actions as $module_name => $module){
- if(isset($id_role)) $actions[$module_name]["module_allowed"] =
- isset($actions_roles[$module_name])
- && in_array($id_role, $actions_roles[$module_name]);
- $actions[$module_name]["is_public"] =
- isset($actions_roles[$module_name])
- && in_array(0, $actions_roles[$module_name]);
- foreach($module["controleurs"] as $controleur_name => $controleur){
- if(isset($id_role)) $actions[$module_name]["controleurs"][$controleur_name]["controleur_allowed"] =
- isset($actions_roles[$module_name."/".$controleur_name])
- && in_array($id_role, $actions_roles[$module_name."/".$controleur_name]);
- $actions[$module_name]["controleurs"][$controleur_name]["is_public"] =
- isset($actions_roles[$module_name."/".$controleur_name])
- && in_array(0, $actions_roles[$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($actions_roles[$module_name."/".$controleur_name."/".$action_name])
- || !in_array($id_role, $actions_roles[$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($actions_roles[$module_name."/".$controleur_name."/".$action_name])
- || !in_array(0, $actions_roles[$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;
- }
- }
- }
- }
- }
- return $actions;
- }
- }
- return array();
- }
-
- }
-
-?>
\ No newline at end of file