diff --git a/build/api/module/admin.php b/build/api/module/admin.php index da950d1..0687fae 100644 --- a/build/api/module/admin.php +++ b/build/api/module/admin.php @@ -68,4 +68,50 @@ } + + + /* (3) Updates an existing new administrator + * + * @id_admin UID de l'administrateur + * @mail [OPT] Adresse mail de l'administrateur + * @password [OPT] Mot de passe de l'administrateur + * + * @return admin The new administrator data + * + ---------------------------------------------------------*/ + public static function put($args){ + extract($args); + debug(); + + /* (1) If @mail given + ---------------------------------------------------------*/ + if( !is_null($mail) ){ + + /* (1) Update mail address */ + $updated = Repo::request('admin', 'setMail', $id_admin, $mail); + + /* (2) Gestion erreur */ + if( $updated === false ) + return [ 'error' => new Error(Err::RepoError) ]; + + } + + /* (2) If @password given + ---------------------------------------------------------*/ + if( !is_null($password) ){ + + /* (1) Update password */ + $updated = Repo::request('admin', 'setPassword', $id_admin, $password); + + /* (2) Gestion erreur */ + if( $updated === false ) + return [ 'error' => new Error(Err::RepoError) ]; + + } + + /* (3) Renvoi @id_admin */ + return [ 'id_admin' => Repo::request('admin', 'getById', $id_admin) ]; + + } + } diff --git a/build/database/repo/admin.php b/build/database/repo/admin.php index 48e56dc..9d3d2e1 100644 --- a/build/database/repo/admin.php +++ b/build/database/repo/admin.php @@ -181,7 +181,40 @@ } - /* (7) Creates a new admin + + /* (7) Set the mail address for a admin + * + * @id_admin The admin UID + * @mail The mail address to set + * + * @return set Whether the mail address has been set or not + * + ---------------------------------------------------------*/ + public function setMail(int $id_admin, String $mail){ + + /* (1) Check @mail is unique + ---------------------------------------------------------*/ + /* (1) If @mail already exists -> abort */ + if( is_array($this->getByMail($mail)) ) + return false; + + + /* (2) Update mail address + ---------------------------------------------------------*/ + /* (1) Prepare Statement */ + $pst = $this->pdo->prepare("UPDATE `admin` SET `mail` = :mail WHERE `id_admin` = :id_admin"); + + /* (3) Bind variables */ + $pst->bindParam(':mail', $mail, \PDO::PARAM_STR, 50); + $pst->bindParam(':id_admin', $id_admin, \PDO::PARAM_INT); + + /* (4) Execute -> dispatch status */ + return $pst->execute(); + + } + + + /* (8) Creates a new admin * * @username The username (must be unique) * @mail The mail address (must be unique) diff --git a/config/modules.json b/config/modules.json index 146ef2a..7c60ae8 100755 --- a/config/modules.json +++ b/config/modules.json @@ -13,18 +13,21 @@ }, "PUT": { - "description": "Gets an administrator's data", + "description": "Updates an existing administrator's data", "permissions": [], "parameters": { - "URL0": { "description": "Some string token", "type": "text", "rename": "id_article" }, - "postdata": { "description": "Some string token", "type": "text" } + "URL0": { "description": "The UID of the wanted administrator.", "type": "id", "rename": "id_admin" }, + "mail": { "description": "The new administrator email address", "type": "mail", "optional": true }, + "password": { "description": "The new administrator passowrd", "type": "text", "optional": true } } }, "DELETE": { "description": "Deletes an administrator", "permissions": [["admin"]], - "parameters": {} + "parameters": { + "URL0": { "description": "The UID of the wanted administrator.", "type": "id", "optional": true, "rename": "id_admin" } + } }, "GET": {