[#321] Gestion des rôles dans l'application (#2)
All checks were successful
Auto Tag Develop / tag (push) Successful in 6s
All checks were successful
Auto Tag Develop / tag (push) Successful in 6s
| Numéro du ticket | Titre du ticket | |------------------|-----------------| | #321 | Gestion des rôles dans l'application | ## Description de la PR [#321] Gestion des rôles dans l'application ## Modification du .env ## Check list - [x] Pas de régression - [ ] TU/TI/TF rédigée - [x] TU/TI/TF OK - [ ] CHANGELOG modifié Reviewed-on: #2 Co-authored-by: tristan <tristan@yuno.malio.fr> Co-committed-by: tristan <tristan@yuno.malio.fr>
This commit was merged in pull request #2.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
export type UserData = {
|
||||
id: number
|
||||
username: string
|
||||
roles: string[]
|
||||
}
|
||||
|
||||
8
frontend/services/dto/user.ts
Normal file
8
frontend/services/dto/user.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import type { Employee } from './employee'
|
||||
|
||||
export type User = {
|
||||
id: number
|
||||
username: string
|
||||
roles: string[]
|
||||
employee?: Employee | null
|
||||
}
|
||||
38
frontend/services/user-site-roles.ts
Normal file
38
frontend/services/user-site-roles.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { extractItems } from '~/utils/api'
|
||||
|
||||
export const createUserSiteRole = async (payload: {
|
||||
userId: number
|
||||
siteId: number
|
||||
role: string
|
||||
}) => {
|
||||
const api = useApi()
|
||||
return api.post('/user_site_roles', {
|
||||
user: `/api/users/${payload.userId}`,
|
||||
site: `/api/sites/${payload.siteId}`,
|
||||
role: payload.role
|
||||
}, {
|
||||
toast: false
|
||||
})
|
||||
}
|
||||
|
||||
export type UserSiteRole = {
|
||||
id: number
|
||||
user: { id: number }
|
||||
site: { id: number; name?: string }
|
||||
role: string
|
||||
}
|
||||
|
||||
export const listUserSiteRoles = async () => {
|
||||
const api = useApi()
|
||||
const data = await api.get<UserSiteRole[] | { 'hydra:member'?: UserSiteRole[] }>(
|
||||
'/user_site_roles',
|
||||
{},
|
||||
{ toast: false }
|
||||
)
|
||||
return extractItems<UserSiteRole>(data)
|
||||
}
|
||||
|
||||
export const deleteUserSiteRole = async (id: number) => {
|
||||
const api = useApi()
|
||||
return api.delete(`/user_site_roles/${id}`, {}, { toast: false })
|
||||
}
|
||||
57
frontend/services/users.ts
Normal file
57
frontend/services/users.ts
Normal file
@@ -0,0 +1,57 @@
|
||||
import type { User } from './dto/user'
|
||||
import { extractItems } from '~/utils/api'
|
||||
|
||||
export const listUsers = async () => {
|
||||
const api = useApi()
|
||||
const data = await api.get<User[] | { 'hydra:member'?: User[] }>(
|
||||
'/users',
|
||||
{},
|
||||
{ toast: false }
|
||||
)
|
||||
return extractItems<User>(data)
|
||||
}
|
||||
|
||||
export const createUser = async (payload: {
|
||||
username: string
|
||||
plainPassword: string
|
||||
roles: string[]
|
||||
employeeId?: number | null
|
||||
}) => {
|
||||
const api = useApi()
|
||||
return api.post<User>(
|
||||
'/users',
|
||||
{
|
||||
username: payload.username,
|
||||
plainPassword: payload.plainPassword,
|
||||
roles: payload.roles,
|
||||
employee: payload.employeeId ? `/api/employees/${payload.employeeId}` : null
|
||||
},
|
||||
{
|
||||
toastSuccessKey: 'success.user.create',
|
||||
toastErrorKey: 'errors.user.create'
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
export const updateUser = async (id: number, payload: {
|
||||
username: string
|
||||
plainPassword?: string
|
||||
roles: string[]
|
||||
employeeId?: number | null
|
||||
}) => {
|
||||
const api = useApi()
|
||||
const body: Record<string, unknown> = {
|
||||
username: payload.username,
|
||||
roles: payload.roles,
|
||||
employee: payload.employeeId ? `/api/employees/${payload.employeeId}` : null
|
||||
}
|
||||
|
||||
if (payload.plainPassword) {
|
||||
body.plainPassword = payload.plainPassword
|
||||
}
|
||||
|
||||
return api.patch<User>(`/users/${id}`, body, {
|
||||
toastSuccessKey: 'success.user.update',
|
||||
toastErrorKey: 'errors.user.update'
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user