@addev-be/ui 0.6.24 → 0.6.25

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@addev-be/ui",
3
- "version": "0.6.24",
3
+ "version": "0.6.25",
4
4
  "type": "module",
5
5
  "scripts": {
6
6
  "watch": "tsc -b --watch",
@@ -248,15 +248,18 @@ export const sqlCheckboxColumn = <R extends Record<string, any>>(
248
248
  ): SqlRequestDataGridColumns<R> => ({
249
249
  [key]: {
250
250
  name: title,
251
- render: (row) => (
252
- <>
253
- <input type="checkbox" checked={row[key]} readOnly />
254
- <span>{row[key] ? ' Oui' : ' Non'}</span>
255
- </>
256
- ),
251
+ render: (row) => {
252
+ const value = !!+(row[key] ?? 0);
253
+ return (
254
+ <>
255
+ <input type="checkbox" checked={value} readOnly />
256
+ <span>{value ? ' Oui' : ' Non'}</span>
257
+ </>
258
+ );
259
+ },
257
260
  getter: (row) => row[key] ?? '',
258
261
  sortGetter: (row) => row[key] ?? '',
259
- filter: { ...numberFilter(key), getter: (value) => value[key] ?? 0 },
262
+ filter: { ...numberFilter(key), getter: (value) => +(value[key] ?? 0) },
260
263
  footer: (rows) => `${rows[0][key]} éléments`,
261
264
  ...options,
262
265
  },
@@ -0,0 +1,3 @@
1
+ export const basePermissions: Record<string, string> = {
2
+ 'users.manage': 'Gérer les utilisateurs',
3
+ };
@@ -18,6 +18,8 @@ import {
18
18
  useSendRecoveryKeyRequestHandler,
19
19
  } from '../../services';
20
20
 
21
+ import { basePermissions } from './helpers';
22
+
21
23
  export type AuthenticationContextProps = {
22
24
  token?: string;
23
25
  user?: UserDTO;
@@ -28,6 +30,8 @@ export type AuthenticationContextProps = {
28
30
  sendRecoveryKey: (email: string) => Promise<number>;
29
31
  checkRecoveryKey: (key: string) => Promise<number>;
30
32
  resetPassword: (key: string, password: string) => Promise<number>;
33
+ permissions: Record<string, string>;
34
+ hasPermission: (permission: string) => boolean;
31
35
  };
32
36
 
33
37
  export const AuthenticationContext = createContext<AuthenticationContextProps>({
@@ -38,18 +42,26 @@ export const AuthenticationContext = createContext<AuthenticationContextProps>({
38
42
  sendRecoveryKey: async () => -1,
39
43
  checkRecoveryKey: async () => -1,
40
44
  resetPassword: async () => -1,
45
+ permissions: {},
46
+ hasPermission: () => false,
41
47
  });
42
48
 
43
49
  export type AuthenticationProviderProps = PropsWithChildren<{
50
+ permissions?: Record<string, string>;
44
51
  loginComponent?: FC;
45
52
  requestHandlerName?: string;
46
53
  }>;
47
54
 
48
55
  export const AuthenticationProvider: FC<AuthenticationProviderProps> = ({
56
+ permissions,
49
57
  loginComponent: LoginComponent,
50
58
  children,
51
59
  requestHandlerName,
52
60
  }) => {
61
+ const allPermissions = useMemo(
62
+ () => ({ ...basePermissions, ...permissions }),
63
+ [permissions]
64
+ );
53
65
  const [, setWebSocketStatus] = useState<boolean | undefined>(false);
54
66
  const [token, setToken] = useState<string | undefined>(
55
67
  sessionStorage.getItem('authToken') || undefined
@@ -148,6 +160,14 @@ export const AuthenticationProvider: FC<AuthenticationProviderProps> = ({
148
160
  [sendCheckRecoveryKeyRequest]
149
161
  );
150
162
 
163
+ const hasPermission = useCallback(
164
+ (permission: string) =>
165
+ permission in allPermissions &&
166
+ !!allPermissions[permission] &&
167
+ (user?.isAdmin || (user?.permissions?.includes(permission) ?? false)),
168
+ [allPermissions, user?.isAdmin, user?.permissions]
169
+ );
170
+
151
171
  const contextValue = useMemo(
152
172
  () => ({
153
173
  token,
@@ -159,10 +179,14 @@ export const AuthenticationProvider: FC<AuthenticationProviderProps> = ({
159
179
  sendRecoveryKey,
160
180
  checkRecoveryKey,
161
181
  resetPassword,
182
+ permissions: allPermissions,
183
+ hasPermission,
162
184
  }),
163
185
  [
186
+ allPermissions,
164
187
  authenticate,
165
188
  checkRecoveryKey,
189
+ hasPermission,
166
190
  login,
167
191
  logout,
168
192
  resetPassword,
@@ -11,4 +11,3 @@ export * from './types/auth';
11
11
  export * from './types/base';
12
12
  export * from './types/users';
13
13
  export * from './types/userProfiles';
14
- export * from './types/userPermissions';
@@ -63,7 +63,7 @@ export type GetAllUserProfilesResponseDTO = t.TypeOf<
63
63
 
64
64
  export const saveUserProfileRequestDtoCodec = t.type(
65
65
  {
66
- userProfile: userProfileDtoCodec,
66
+ data: userProfileDtoCodec,
67
67
  },
68
68
  'SaveUserProfileRequestDTO'
69
69
  );
@@ -62,7 +62,13 @@ export type GetAllUsersResponseDTO = t.TypeOf<
62
62
 
63
63
  export const saveUserRequestDtoCodec = t.type(
64
64
  {
65
- user: userDtoCodec,
65
+ data: t.intersection([
66
+ userDtoCodec,
67
+ t.partial({
68
+ password: t.string,
69
+ }),
70
+ ]),
71
+ password: t.string,
66
72
  },
67
73
  'SaveUserRequestDTO'
68
74
  );
@@ -1,3 +0,0 @@
1
- export const userPermissions: Record<string, string> = {
2
- 'users:manage': 'Gérer les utilisateurs',
3
- };