// Vérifie email if (!Tools::verifyEmail($email)) { $formValid = false; $errors["email"] = "You must insert a valid email."; } else { if (User::findByEmail($email) != null) { $formValid = false; $errors["email"] = "Email address already used."; } } // Si mot de passe valide if (!Tools::verifyPassword($pwd)) { $formValid = false; $errors["pwd"] = "You must insert a valid password see password <a href=\"#\">hint</a>."; } if (Right::findByName($right) == null) { $formValid = false; $errors["right"] = "You must select a correct user right."; } if ($formValid) { try { User::create($email, $pwd, $right, $actif); } catch (Exception $e) { $formValid = false; $errors["other"] = $e->getMessage(); } } } } else { if (isset($get)) { if (Tools::isStringValid($get) && isDetail($get)) {
/** * * @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; } }