Example #1
0
 /**
  *
  * @param string $email        	
  * @param string $password        	
  * @param string $right        	
  * @param boolean $actif        	
  */
 public static function create($email, $password, $right, $actif)
 {
     // Vérifie que les champs soit du bon format.
     if (!Tools::isStringValid($email) || !Tools::isStringValid($password) || !Tools::isStringValid($right) || !Tools::isBooleanValid($actif)) {
         throw new Exception("Invalid user arguments type.");
     }
     // Vérifie s'il s'agit d'un email.
     if (!Tools::verifyEmail($email)) {
         throw new Exception("Invalid email format.");
     }
     // Vérifie que l'utilisateur n'existe pas déjà.
     if (User::findByEmail($email) != null) {
         throw new Exception("Invalid email, user already exist.");
     }
     // Crée un nouvel utilisateur.
     $user = new User();
     // Récupère le droit utilisateur.
     $user->right = Right::findByName($right);
     if ($user->right == null) {
         throw new Exception("Invalid right.");
     }
     // Set les autres paramètres
     $user->id = null;
     $user->email = $email;
     $user->photo = "";
     $user->salt = Tools::randomSalt();
     $user->password = Tools::doHash($password, $user->salt);
     $user->actif = $actif;
     try {
         // Effectue une connexion à la DB.
         $pdo = DBconnect();
         // Commence la transaction.
         $pdo->beginTransaction();
         // Prépare la requête.
         $stmt = $pdo->prepare("INSERT INTO t_user(email, password, salt, actif, fk_right) VALUES (:email, :password, :salt, :actif, :fk_right)");
         $stmt->bindParam(":email", $user->email);
         $stmt->bindParam(":password", $user->password);
         $stmt->bindParam(":salt", $user->salt);
         $stmt->bindParam(":actif", $user->actif);
         $stmt->bindParam(":fk_right", $user->right->getId());
         if ($stmt->execute()) {
             $pdo->commit();
         } else {
             throw new Exception();
         }
     } catch (Exception $e) {
         // Revient en arrière si une erreur c'est produite.
         $pdo->rollBack();
         // Envoie à nouveau une exception.
         throw new Exception("Create user aborted , an error occurated.");
     } finally {
         // Ferme le traitement.
         $stmt->closeCursor();
         // Ferme la connexion vers la base de donnée.
         $pdo = null;
     }
 }
Example #2
0
 public function setDeletedReceiver($deletedReceiver, $email)
 {
     // Vérifie que l'argument est de format booléen et qu'il soit valide.
     if (!Tools::isBooleanValid($deletedReceiver)) {
         throw new Exception("Invalid deleted receiver.");
     }
     if (!Tools::isStringValid($email)) {
         throw new Exception("Invalid email");
     }
     if ($this->receiver != $email) {
         throw new Exception("Incorrect action");
     }
     // Met à jour l'attribut.
     if ($deletedReceiver) {
         $this->deletedReceiver = 1;
     } else {
         $this->deletedReceiver = 0;
     }
     // Retourne l'objet pour le chainage.
     return $this;
 }