diff --git a/build/api/module/authenticationDefault.php b/build/api/module/authenticationDefault.php index 4dedfb7..9b58064 100755 --- a/build/api/module/authenticationDefault.php +++ b/build/api/module/authenticationDefault.php @@ -272,6 +272,70 @@ return [ 'error' => new Error(Err::Success) ]; } + + + + + + + + + + + + + /* CREATION D'UN NOUVEAL ADMINISTRATEUR + * + * @username Identifiant du nouvel administrateur + * + * @return password Mot de passe généré + * + */ + public function create_admin($params){ + extract($params); + + + + /* [1] On génère un mot de passe + =========================================================*/ + $password = secure_hash(uniqid(), uniqid()); + + + /* [2] On vérifie l'unicité de l'identifiant + =========================================================*/ + /* (1) On vérifie que l'identifiant n'existe pas */ + $checkPassword = new Repo('admin/getByUsername', [ + $_SESSION['WAREHOUSE']['id'], + $username + ]); + + $adminFetched = $checkPassword->answer(); + + // Si un résultat -> erreur + if( $adminFetched !== false ) + return [ 'error' => new Error(Err::AlreadyExists, 'username') ]; + + + /* [3] On crée l'administrateur + =========================================================*/ + /* (1) Requête */ + $create = new Repo('admin/create', [ + $_SESSION['WAREHOUSE']['id'], + $username, + $mail, + $password + ]); + + + + /* (3) Gestion erreur */ + if( !$create->answer() ) + return [ 'error' => new Error(Err::RepoError) ]; + + /* (4) Succès si tout ok */ + return [ 'password' => $password ]; + } + } diff --git a/config/modules.json b/config/modules.json index f4a563f..aa72320 100755 --- a/config/modules.json +++ b/config/modules.json @@ -81,7 +81,20 @@ "confirm": { "description": "Confirmation du nouveau mot de passe", "type": "text" } }, "output": {} + }, + + "POST::create_admin": { + "description": "Création d'un administrateur", + "permissions": [["admin"]], + "parameters": { + "username": { "description": "Identifiant", "type": "varchar(1,30,alphanumeric)" }, + "mail": { "description": "Adresse mail", "type": "mail" } + }, + "output": { + "password": { "description": "Mot de passe généré", "type": "text" } + } } + }, diff --git a/public_html/src/static/sub-menu-side/user.svg b/public_html/src/static/sub-menu-side/user.svg new file mode 100644 index 0000000..91232fc --- /dev/null +++ b/public_html/src/static/sub-menu-side/user.svg @@ -0,0 +1,50 @@ + +image/svg+xml \ No newline at end of file diff --git a/public_html/view/js/settings.js b/public_html/view/js/settings.js index 12735f1..3aa8dd4 100755 --- a/public_html/view/js/settings.js +++ b/public_html/view/js/settings.js @@ -8,6 +8,11 @@ var section = { password: { text: '#CONTAINER > section[data-sublink="password"] ', element: document.querySelector('#CONTAINER > section[data-sublink="password"]') + }, + + admin: { + text: '#CONTAINER > section[data-sublink="admin"] ', + element: document.querySelector('#CONTAINER > section[data-sublink="admin"]') } }; @@ -91,3 +96,70 @@ if( section.password.element != null ){ } + + + + +/* [3] admin -> Création d'administrateur +=========================================================*/ +if( section.admin.element != null ){ + + // On récupère les champs + section.admin.input = { + username: document.querySelector(section.admin.text + '#admin_username'), + mail: document.querySelector(section.admin.text + '#admin_mail'), + password: document.querySelector(section.admin.text + '#admin_password'), + + submit: document.querySelector(section.admin.text + '#admin_submit') + }; + + // On recupere tous les messages d'erreur + section.admin.errmsg = { + username: document.querySelector(section.admin.text + '.error-msg.admin_username'), + mail: document.querySelector(section.admin.text + '.error-msg.admin_mail') + }; + + /* (n) Gestion de l'envoi du formulaire */ + section.admin.input.submit.addEventListener('click', function(e){ + // On annule l'envoi de base (PHP) + e.preventDefault(); + + // On vide les messages erreurs + section.admin.errmsg.username.innerHTML = + section.admin.errmsg.mail.innerHTML = ''; + + var request = { + path: 'authenticationDefault/create_admin', // On veut créer un nouvel admin + username: section.admin.input.username.value, + mail: section.admin.input.mail.value + }; + + api.send(request, function(answer){ + if( answer.error == 0 ){ // Tout s'est bien deroule + console.log('Admin créé!'); + + section.admin.input.submit.anim('active', 1500); + + // on vide les champs + section.admin.input.username.value = ''; + section.admin.input.mail.value = ''; + section.admin.input.password.value = answer.password; + + }else{ // Erreur + console.error('ModuleError::'+answer.error); + + // if missing or incorrect param + if( answer.error == 16 || answer.error == 17 ){ + if( section.admin.errmsg[answer.ErrorArguments[0]] != null ) + section.admin.errmsg[answer.ErrorArguments[0]].innerHTML = 'Le champ est manquant ou incorrect !'; + } + } + + }); + + + + }, false); + +} + diff --git a/public_html/view/settings.php b/public_html/view/settings.php index 6b02ca1..35a6a16 100755 --- a/public_html/view/settings.php +++ b/public_html/view/settings.php @@ -16,6 +16,11 @@ Mot de passe + + + Administrateurs + + Mettre à jour"; echo ""; + echo ''; + + + + /* CREATION ADMINISTRATEUR + * + */ + echo "
"; + + echo "
"; + echo "
"; + echo "
"; + echo "
"; + echo "
"; + echo "
"; + + echo ""; + echo "
"; + echo '
'; \ No newline at end of file