diff --git a/database/Models/Users.js b/database/Models/Users.js index 7c00a38..45e8ec5 100644 --- a/database/Models/Users.js +++ b/database/Models/Users.js @@ -101,20 +101,19 @@ async function loginUsers(username, password) { * @param {String} username * @param {String} email * @param {String} password - * @param {String} roles * @param {Number} id * @returns {Object} */ -async function updateUser(username, email, password, roles, id) { +async function updateUser(username, email, password, id) { let sql, params - if (password !== null) { + if (password !== null || password !== '') { const hashedPassword = await bcrypt.hash(password, 10) - sql = `UPDATE users SET username = ?, email = ?, password = ?, roles = ? WHERE id = ?` - params = [username, email, hashedPassword, roles, id] + sql = `UPDATE users SET username = ?, email = ?, password = ? WHERE id = ?` + params = [username, email, hashedPassword, id] } else { - sql = `UPDATE users SET username = ?, email = ?, roles = ? WHERE id = ?` - params = [username, email, roles, id] + sql = `UPDATE users SET username = ?, email = ? WHERE id = ?` + params = [username, email, id] } try { @@ -126,10 +125,12 @@ async function updateUser(username, email, password, roles, id) { message: 'Utilisateur non trouvé ou aucune modification effectuée.' } } + const [rows] = await pool.query('SELECT * FROM users WHERE id = ?', [id]) return { success: true, - message: 'Utilisateur mis à jour avec succès.' + message: 'Utilisateur mis à jour avec succès.', + users: rows[0] } } catch (error) { return { success: false, error: 'Erreur veullez réeseyer' } diff --git a/database/function/System.js b/database/function/System.js index 6d371b2..c6b40fd 100644 --- a/database/function/System.js +++ b/database/function/System.js @@ -272,7 +272,7 @@ async function getNessesarytable() { try { const sql = 'SELECT * FROM nessesaryTable' const [rows] = await pool.query(sql) - return rows + return rows[0] } catch (error) { return error } diff --git a/src/main/index.js b/src/main/index.js index f724ac1..b452894 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -11,7 +11,7 @@ database .then(() => database.insertStatusesIfNotExist()) .catch(console.error) -const { loginUsers } = require('../../database/Models/Users') +const { loginUsers, insertUser, updateUser } = require('../../database/Models/Users') const { createConfigIp, updateIPConfig } = require('../../database/Models/IpConfig') const { importFileToDatabase } = require('../../database/import/Etudiants') const { @@ -78,9 +78,9 @@ const { showMoyenRepech } = require('../../database/Models/NoteRepechage') const { - updateCurrentYears - // updateStudents, - // updateNessesaryTable + updateCurrentYears, + updateStudents, + updateNessesaryTable } = require('../../database/function/System') const { autoUpdater } = require('electron-updater') const { URL } = require('../../database/api/Config') @@ -98,7 +98,7 @@ const { let mainWindow let tray = null updateCurrentYears() -// updateStudents() +updateStudents() autoUpdater.setFeedURL({ provider: 'generic', @@ -299,6 +299,23 @@ ipcMain.handle('login', async (event, credentials) => { return response }) + +ipcMain.handle('insertUser', async (event, credentials) => { + const { username, email, password, roles } = credentials + + const users = await insertUser(username, email, password, roles) + + return users +}) + +ipcMain.handle('updateUsers', async (event, credentials) => { + const { username, email, password, id } = credentials + + const update = await updateUser(username, email, password, id) + + return update +}) + // event for insert etudiants ipcMain.handle('insertEtudiant', async (event, credentials) => { const { diff --git a/src/renderer/src/components/Addadmin.jsx b/src/renderer/src/components/Addadmin.jsx index bc48d42..8ed4bd3 100644 --- a/src/renderer/src/components/Addadmin.jsx +++ b/src/renderer/src/components/Addadmin.jsx @@ -54,12 +54,13 @@ const Admin = () => { if (valid) { const response = await window.allUser.insertUsers(formData) console.log(response) - if (response.changes) { + if (response.success) { setOpen(true) setFormData({ username: '', email: '', - password: '' + password: '', + roles: 'Enseignant' }) } diff --git a/src/renderer/src/components/Param.jsx b/src/renderer/src/components/Param.jsx index 474e8eb..af91a41 100644 --- a/src/renderer/src/components/Param.jsx +++ b/src/renderer/src/components/Param.jsx @@ -1,17 +1,15 @@ -import React, { useRef, useState } from 'react' +import { useState } from 'react' import classe from '../assets/AllStyleComponents.module.css' import img from '../assets/para.png' import classeHome from '../assets/Home.module.css' import { Box, Button, InputAdornment, TextField, Grid, Modal, Typography } from '@mui/material' import classeAdd from '../assets/AddStudent.module.css' import { useAuthContext } from '../contexts/AuthContext' -import bcrypt from 'bcryptjs' import { FaEnvelope, FaLock, FaUser } from 'react-icons/fa' -import validationSetting from './validation/Setting' import svgSuccess from '../assets/success.svg' import svgError from '../assets/error.svg' import { Link } from 'react-router-dom' -import { GrConfigure } from "react-icons/gr"; +import { GrConfigure } from 'react-icons/gr' import IpConfig from './IpConfig' const Setting = () => { @@ -19,42 +17,13 @@ const Setting = () => { const userInfo = JSON.parse(token) - const [isUsername, setIsUsername] = useState(true) - - const changeUsernameRef = useRef() - const changeEmailRef = useRef() - - const changeEmail = (e) => { - if (changeEmailRef.current.checked) { - setIsUsername(false) - } - } - - const changeUsername = (e) => { - if (changeUsernameRef.current.checked) { - setIsUsername(true) - } - } - const [formData, setFormData] = useState({ username: userInfo.username, - newUsername: '', email: userInfo.email, - newEmail: '', - passwordVerif: '', password: '', id: userInfo.id }) - const usernameRef = useRef() - const emailRef = useRef() - const passwordRef = useRef() - const passwordChangeRef = useRef() - const usernameError = useRef() - const emailError = useRef() - const passwordError = useRef() - const passwordChangeError = useRef() - /** * function to set the data in state * @param {*} e @@ -70,79 +39,23 @@ const Setting = () => { const handleSubmit = async (e) => { e.preventDefault() // Handle form submission logic - let validation = validationSetting( - usernameRef.current, - emailRef.current, - passwordRef.current, - passwordChangeRef.current, - usernameError.current, - emailError.current, - passwordError.current, - passwordChangeError.current - ) - - if (validation) { - if (formData.username === formData.newUsername) { - if (usernameError.current) { - usernameError.current.textContent = - "Le nom d'utilisateur doit être différent de l'original" - } - } else { - if (usernameError.current) { - usernameError.current.textContent = '' // Clear the username error if condition is not met - } - } - - if (formData.email === formData.newEmail) { - if (emailError.current) { - emailError.current.textContent = "Email doit être différent de l'original" - } - } else { - if (emailError.current) { - emailError.current.textContent = '' // Clear the email error if condition is not met - } - } - if (formData.email !== formData.newEmail && formData.username !== formData.newUsername) { - // Password verification - bcrypt.compare(formData.passwordVerif, userInfo.password, async function (err, result) { - if (result) { - // Clear password error if password matches - if (passwordError.current) { - passwordError.current.textContent = '' - } - - // Update user information if password is verified - const response = await window.allUser.updateUsers(formData) - if (response.id) { - setOpen(true) - setToken(JSON.stringify(response)) - setFormData({ - passwordVerif: '', - username: response.username, - newUsername: '', - email: response.email, - newEmail: '', - password: '', - id: userInfo.id - }) - } - - // Handle specific response code for errors - if (response.code) { - setCode(422) - setOpen(true) - } - } else { - // Set error message if password verification fails - if (passwordError.current) { - passwordError.current.textContent = 'mot de passe ne correspond pas' - } - } - }) - } + const response = await window.allUser.updateUsers(formData) + console.log(response) + if (response.success) { + setOpen(true) + setCode(200) + setToken(JSON.stringify(response.users)) + setFormData({ + username: response.username, + email: response.email, + password: '', + id: userInfo.id + }) + } else { + setCode(422) + setOpen(true) } } - /** * hook to open modal */ @@ -264,182 +177,49 @@ const Setting = () => {
- - - - - - + {/* */} - {isUsername === true ? ( - - {/* username*/} - - - - - ) - }} - sx={{ - '& .MuiOutlinedInput-root': { - '&:hover fieldset': { - borderColor: '#ff9800' // Set the border color on hover - } - } - }} - /> - - - - - - ) - }} - inputRef={usernameRef} - sx={{ - '& .MuiOutlinedInput-root': { - '&:hover fieldset': { - borderColor: '#ff9800' // Set the border color on hover - } - } - }} - /> - - - - ) : ( - - {/* email */} - - - - - ) - }} - sx={{ - '& .MuiOutlinedInput-root': { - '&:hover fieldset': { - borderColor: '#ff9800' // Set the border color on hover - } - } - }} - /> - - - - - - ) - }} - inputRef={emailRef} - sx={{ - '& .MuiOutlinedInput-root': { - '&:hover fieldset': { - borderColor: '#ff9800' // Set the border color on hover - } - } - }} - /> - - - - )} - -

- Insérer le mot de passe pour confirmer le changement -

+ + + + + ) + }} + sx={{ + '& .MuiOutlinedInput-root': { + '&:hover fieldset': { + borderColor: '#ff9800' // Set the border color on hover + } + } + }} + /> - {/* matieres Mecanique general */} - + ) }} - inputRef={passwordRef} sx={{ '& .MuiOutlinedInput-root': { '&:hover fieldset': { @@ -448,26 +228,24 @@ const Setting = () => { } }} /> - - {/* Matieres Resistance Materiaux */} - + {/* matieres Mecanique general */} + + startAdornment: ( + + + + ) }} - inputRef={passwordChangeRef} sx={{ '& .MuiOutlinedInput-root': { '&:hover fieldset': { @@ -476,12 +254,8 @@ const Setting = () => { } }} /> - + {/* Matieres Resistance Materiaux */} {/* Submit Button */}