terminer
This commit is contained in:
parent
124d0eeead
commit
e5b4933076
@ -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' }
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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'
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@ -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,173 +177,67 @@ const Setting = () => {
|
||||
</span>
|
||||
|
||||
<form onSubmit={handleSubmit} style={{ marginTop: '5%' }}>
|
||||
<Grid container spacing={2} sx={{ marginBottom: '30px' }}>
|
||||
<Grid
|
||||
item
|
||||
xs={12}
|
||||
sm={12}
|
||||
sx={{
|
||||
display: 'flex',
|
||||
alignItems: 'center',
|
||||
justifyContent: 'center',
|
||||
gap: '20px'
|
||||
}}
|
||||
>
|
||||
<label htmlFor="usernameBlock">
|
||||
<input
|
||||
type="radio"
|
||||
name="option"
|
||||
id="usernameBlock"
|
||||
ref={changeUsernameRef}
|
||||
onClick={changeUsername}
|
||||
/>
|
||||
Username
|
||||
</label>
|
||||
<label htmlFor="emailBlock">
|
||||
<input
|
||||
type="radio"
|
||||
name="option"
|
||||
id="emailBlock"
|
||||
onClick={changeEmail}
|
||||
ref={changeEmailRef}
|
||||
/>
|
||||
Email
|
||||
</label>
|
||||
</Grid>
|
||||
</Grid>
|
||||
{/* */}
|
||||
<Grid container spacing={2}>
|
||||
{isUsername === true ? (
|
||||
<Grid container spacing={2}>
|
||||
{/* username*/}
|
||||
<Grid item xs={12} sm={6}>
|
||||
<TextField
|
||||
label="Nom d'utilisateur"
|
||||
name="username"
|
||||
color="warning"
|
||||
fullWidth
|
||||
disabled
|
||||
value={formData.username}
|
||||
onChange={handleInputChange}
|
||||
InputProps={{
|
||||
startAdornment: (
|
||||
<InputAdornment position="start">
|
||||
<FaUser />
|
||||
</InputAdornment>
|
||||
)
|
||||
}}
|
||||
sx={{
|
||||
'& .MuiOutlinedInput-root': {
|
||||
'&:hover fieldset': {
|
||||
borderColor: '#ff9800' // Set the border color on hover
|
||||
}
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</Grid>
|
||||
<Grid item xs={12} sm={6}>
|
||||
<TextField
|
||||
label="Nouveau nom d'utilisateur"
|
||||
name="newUsername"
|
||||
color="warning"
|
||||
fullWidth
|
||||
value={formData.newUsername}
|
||||
onChange={handleInputChange}
|
||||
InputProps={{
|
||||
startAdornment: (
|
||||
<InputAdornment position="start">
|
||||
<FaUser />
|
||||
</InputAdornment>
|
||||
)
|
||||
}}
|
||||
inputRef={usernameRef}
|
||||
sx={{
|
||||
'& .MuiOutlinedInput-root': {
|
||||
'&:hover fieldset': {
|
||||
borderColor: '#ff9800' // Set the border color on hover
|
||||
}
|
||||
}
|
||||
}}
|
||||
/>
|
||||
<span
|
||||
className="text-danger"
|
||||
ref={usernameError}
|
||||
style={{ fontSize: '13px' }}
|
||||
></span>
|
||||
</Grid>
|
||||
</Grid>
|
||||
) : (
|
||||
<Grid container spacing={2}>
|
||||
{/* email */}
|
||||
<Grid item xs={12} sm={6}>
|
||||
<TextField
|
||||
label="Email"
|
||||
name="email"
|
||||
fullWidth
|
||||
color="warning"
|
||||
value={formData.email}
|
||||
disabled
|
||||
onChange={handleInputChange}
|
||||
InputProps={{
|
||||
startAdornment: (
|
||||
<InputAdornment position="start">
|
||||
<FaEnvelope />
|
||||
</InputAdornment>
|
||||
)
|
||||
}}
|
||||
sx={{
|
||||
'& .MuiOutlinedInput-root': {
|
||||
'&:hover fieldset': {
|
||||
borderColor: '#ff9800' // Set the border color on hover
|
||||
}
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</Grid>
|
||||
<Grid item xs={12} sm={6}>
|
||||
<TextField
|
||||
label="Nouveau email"
|
||||
name="newEmail"
|
||||
fullWidth
|
||||
color="warning"
|
||||
value={formData.newEmail}
|
||||
onChange={handleInputChange}
|
||||
InputProps={{
|
||||
startAdornment: (
|
||||
<InputAdornment position="start">
|
||||
<FaEnvelope />
|
||||
</InputAdornment>
|
||||
)
|
||||
}}
|
||||
inputRef={emailRef}
|
||||
sx={{
|
||||
'& .MuiOutlinedInput-root': {
|
||||
'&:hover fieldset': {
|
||||
borderColor: '#ff9800' // Set the border color on hover
|
||||
}
|
||||
}
|
||||
}}
|
||||
/>
|
||||
<span
|
||||
className="text-danger"
|
||||
ref={emailError}
|
||||
style={{ fontSize: '13px' }}
|
||||
></span>
|
||||
</Grid>
|
||||
</Grid>
|
||||
)}
|
||||
<Grid item xs={12} sm={12}>
|
||||
<p>
|
||||
<b>Insérer le mot de passe pour confirmer le changement</b>
|
||||
</p>
|
||||
</Grid>
|
||||
{/* matieres Mecanique general */}
|
||||
<Grid item xs={12} sm={6}>
|
||||
<TextField
|
||||
label="Mot de passe"
|
||||
name="passwordVerif"
|
||||
fullWidth
|
||||
label="Nom d'utilisateur"
|
||||
name="username"
|
||||
color="warning"
|
||||
value={formData.passwordVerif}
|
||||
fullWidth
|
||||
value={formData.username}
|
||||
onChange={handleInputChange}
|
||||
required
|
||||
InputProps={{
|
||||
startAdornment: (
|
||||
<InputAdornment position="start">
|
||||
<FaUser />
|
||||
</InputAdornment>
|
||||
)
|
||||
}}
|
||||
sx={{
|
||||
'& .MuiOutlinedInput-root': {
|
||||
'&:hover fieldset': {
|
||||
borderColor: '#ff9800' // Set the border color on hover
|
||||
}
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</Grid>
|
||||
<Grid item xs={12} sm={6}>
|
||||
<TextField
|
||||
label="Email"
|
||||
name="email"
|
||||
fullWidth
|
||||
required
|
||||
color="warning"
|
||||
value={formData.email}
|
||||
onChange={handleInputChange}
|
||||
InputProps={{
|
||||
startAdornment: (
|
||||
<InputAdornment position="start">
|
||||
<FaEnvelope />
|
||||
</InputAdornment>
|
||||
)
|
||||
}}
|
||||
sx={{
|
||||
'& .MuiOutlinedInput-root': {
|
||||
'&:hover fieldset': {
|
||||
borderColor: '#ff9800' // Set the border color on hover
|
||||
}
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</Grid>
|
||||
{/* matieres Mecanique general */}
|
||||
<Grid item xs={12} sm={12}>
|
||||
<TextField
|
||||
label="Mot de passe"
|
||||
name="password"
|
||||
fullWidth
|
||||
helperText="À laisser vide si vous ne voulez pas le modifier"
|
||||
color="warning"
|
||||
value={formData.password}
|
||||
onChange={handleInputChange}
|
||||
InputProps={{
|
||||
startAdornment: (
|
||||
@ -439,7 +246,6 @@ const Setting = () => {
|
||||
</InputAdornment>
|
||||
)
|
||||
}}
|
||||
inputRef={passwordRef}
|
||||
sx={{
|
||||
'& .MuiOutlinedInput-root': {
|
||||
'&:hover fieldset': {
|
||||
@ -448,40 +254,8 @@ const Setting = () => {
|
||||
}
|
||||
}}
|
||||
/>
|
||||
<span
|
||||
className="text-danger"
|
||||
ref={passwordError}
|
||||
style={{ fontSize: '13px' }}
|
||||
></span>
|
||||
</Grid>
|
||||
{/* Matieres Resistance Materiaux */}
|
||||
<Grid item xs={12} sm={6}>
|
||||
<TextField
|
||||
label="Changer le mot de passe ?"
|
||||
name="password"
|
||||
fullWidth
|
||||
color="warning"
|
||||
placeholder="Nouveau mot de passe"
|
||||
value={formData.password}
|
||||
onChange={handleInputChange}
|
||||
InputProps={{
|
||||
startAdornment: <InputAdornment position="start"></InputAdornment>
|
||||
}}
|
||||
inputRef={passwordChangeRef}
|
||||
sx={{
|
||||
'& .MuiOutlinedInput-root': {
|
||||
'&:hover fieldset': {
|
||||
borderColor: '#ff9800' // Set the border color on hover
|
||||
}
|
||||
}
|
||||
}}
|
||||
/>
|
||||
<span
|
||||
className="text-danger"
|
||||
ref={passwordChangeError}
|
||||
style={{ fontSize: '13px' }}
|
||||
></span>
|
||||
</Grid>
|
||||
{/* Submit Button */}
|
||||
<Grid
|
||||
item
|
||||
|
||||
Loading…
Reference in New Issue
Block a user