X-Git-Url: http://git.dj3c1t.com/?a=blobdiff_plain;f=mw%2Fapp%2Fdata%2Fmodels%2Fusers.php;fp=mw%2Fapp%2Fdata%2Fmodels%2Fusers.php;h=7a310db54a304d5a7955a5d61826d9e156154c5b;hb=422d883e3ed8ee55ee41e3b7826f32b79cea646d;hp=0000000000000000000000000000000000000000;hpb=e1b64e4088232b9d7b4acb2dc24279bb38fcafba;p=mtweb diff --git a/mw/app/data/models/users.php b/mw/app/data/models/users.php new file mode 100644 index 0000000..7a310db --- /dev/null +++ b/mw/app/data/models/users.php @@ -0,0 +1,115 @@ +roles = $roles; + } + + function get_roles(){ + return $this->roles; + } + + function clear_roles(){ + $this->roles = null; + } + + function get_values(){ + $values = array(); + if(isset($this->roles)) $values["roles"] = $this->roles; + foreach($this->attributs as $attribut_name => $attribut){ + $values[$attribut_name] = isset($attribut["value"]) ? $attribut["value"] : null; + } + return $values; + } + + function set_password($password){ + $this->attributs["password"]["value"] = $this->encode_password($password); + } + + function encode_password($password){ + return md5($password); + } + + // ---------------------------------------------------------------------------- + // validation + + function valid_email($email){ + if(!isset($email) || !$email){ + return "merci de préciser un email"; + } + if(!filter_var($email, FILTER_VALIDATE_EMAIL)){ + return "le mail ne semble pas être un email valide"; + } + return true; + } + + function valid_login($login){ + if(!isset($login) || !$login){ + return "merci de préciser un login"; + } + return true; + } + + function valid_password($password){ + if(!isset($password) || ($password == $this->encode_password(""))){ + return "merci de préciser un mot de passe"; + } + return true; + } + + // ---------------------------------------------------------------------------- + // CRUD + + function insert(){ + $data = $this->env->data(); + $roles = $this->get_roles(); + $this->clear_roles(); + $RETURN_INSERT_ID = $this->RETURN_INSERT_ID; + $this->RETURN_INSERT_ID = true; + if(!($id_user = parent::insert())) return false; + $this->set_id($id_user); + $this->set_roles($roles); + $this->RETURN_INSERT_ID = $RETURN_INSERT_ID; + foreach($this->roles as $id_role){ + if(!$data->add_user_role($this->get_id(), $id_role)) return false; + } + return $id_user; + } + + function load($index_name, $index_value){ + $data = $this->env->data(); + if(parent::load($index_name, $index_value) === false) return false; + if($this->get_id()){ + if(($this->roles = $data->list_user_roles($this->get_id())) === false) return false; + } + return true; + } + + function update($index_name, $index_value = null){ + $data = $this->env->data(); + $roles = $this->get_roles(); + $this->clear_roles(); + if(!($res = parent::update($index_name, $index_value))) return false; + $this->set_roles($roles); + if(!$data->clear_user_roles($this->get_id())) return false; + foreach($this->roles as $id_role){ + if(!$data->add_user_role($this->get_id(), $id_role)) return false; + } + return $res; + } + + function delete($index_name, $index_value = null){ + $data = $this->env->data(); + if(!$data->clear_user_roles($this->get_id())) return false; + return parent::delete($index_name, $index_value); + } + + } + +?> \ No newline at end of file