@firecms/user_management 3.0.0-3.0.0-canary.44.0 → 3.0.0-beta.11
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/LICENSE +3 -2
- package/README.md +2 -2
- package/dist/hooks/index.d.ts +1 -1
- package/dist/hooks/{useFirestoreUserManagement.d.ts → useBuildUserManagement.d.ts} +11 -8
- package/dist/index.es.js +2234 -1282
- package/dist/index.es.js.map +1 -1
- package/dist/index.umd.js +2313 -1
- package/dist/index.umd.js.map +1 -1
- package/dist/types/user_management.d.ts +5 -5
- package/dist/useUserManagementPlugin.d.ts +5 -5
- package/dist/utils/permissions.d.ts +2 -2
- package/package.json +14 -27
- package/src/components/roles/RolesDetailsForm.tsx +82 -33
- package/src/components/roles/RolesTable.tsx +6 -4
- package/src/components/roles/RolesView.tsx +3 -1
- package/src/components/users/UserDetailsForm.tsx +18 -19
- package/src/components/users/UsersTable.tsx +6 -6
- package/src/hooks/index.ts +1 -1
- package/src/hooks/useBuildUserManagement.tsx +349 -0
- package/src/types/user_management.tsx +7 -6
- package/src/useUserManagementPlugin.tsx +6 -6
- package/src/utils/permissions.ts +9 -8
- package/src/hooks/useFirestoreUserManagement.tsx +0 -274
@@ -1,274 +0,0 @@
|
|
1
|
-
import React, { useCallback, useEffect } from "react";
|
2
|
-
import {
|
3
|
-
addDoc,
|
4
|
-
collection,
|
5
|
-
deleteDoc,
|
6
|
-
doc,
|
7
|
-
DocumentSnapshot,
|
8
|
-
getFirestore,
|
9
|
-
onSnapshot,
|
10
|
-
setDoc
|
11
|
-
} from "firebase/firestore";
|
12
|
-
import { FirebaseApp } from "firebase/app";
|
13
|
-
import { UserManagement } from "../types";
|
14
|
-
import { Authenticator, PermissionsBuilder, Role, User } from "@firecms/core";
|
15
|
-
import { resolveUserRolePermissions } from "../utils";
|
16
|
-
|
17
|
-
type UserWithRoleIds = User & { roles: string[] };
|
18
|
-
|
19
|
-
export interface UserManagementParams {
|
20
|
-
/**
|
21
|
-
* The Firebase app to use for the user management. The config will be saved in the Firestore
|
22
|
-
* collection indicated by `configPath`.
|
23
|
-
*/
|
24
|
-
firebaseApp?: FirebaseApp;
|
25
|
-
/**
|
26
|
-
* Path where the plugin users configuration is stored.
|
27
|
-
* Default: __FIRECMS/config/users
|
28
|
-
* You can specify a different path if you want to store the user management configuration in a different place.
|
29
|
-
* Please keep in mind that the FireCMS users are not necessarily the same as the Firebase users (but they can be).
|
30
|
-
* The path should be relative to the root of the Firestore database, and should always have an odd number of segments.
|
31
|
-
*/
|
32
|
-
usersPath?: string;
|
33
|
-
|
34
|
-
/**
|
35
|
-
* Path where the plugin roles configuration is stored.
|
36
|
-
* Default: __FIRECMS/config/roles
|
37
|
-
*/
|
38
|
-
rolesPath?: string;
|
39
|
-
|
40
|
-
/**
|
41
|
-
* Maximum number of users that can be created.
|
42
|
-
*/
|
43
|
-
usersLimit?: number;
|
44
|
-
|
45
|
-
/**
|
46
|
-
* Can the logged user edit roles
|
47
|
-
*/
|
48
|
-
canEditRoles?: boolean;
|
49
|
-
|
50
|
-
/**
|
51
|
-
* If there are no roles in the database, provide a button to create the default roles.
|
52
|
-
*/
|
53
|
-
allowDefaultRolesCreation?: boolean;
|
54
|
-
|
55
|
-
/**
|
56
|
-
* Include the collection config permissions in the user management system.
|
57
|
-
*/
|
58
|
-
includeCollectionConfigPermissions?: boolean;
|
59
|
-
|
60
|
-
}
|
61
|
-
|
62
|
-
/**
|
63
|
-
* This hook is used to build a user management object that can be used to
|
64
|
-
* manage users and roles in a Firestore backend.
|
65
|
-
* @param backendFirebaseApp
|
66
|
-
* @param usersPath
|
67
|
-
* @param rolesPath
|
68
|
-
* @param usersLimit
|
69
|
-
* @param canEditRoles
|
70
|
-
*/
|
71
|
-
export function useFirestoreUserManagement({
|
72
|
-
firebaseApp,
|
73
|
-
usersPath = "__FIRECMS/config/users",
|
74
|
-
rolesPath = "__FIRECMS/config/roles",
|
75
|
-
usersLimit,
|
76
|
-
canEditRoles = true,
|
77
|
-
allowDefaultRolesCreation,
|
78
|
-
includeCollectionConfigPermissions
|
79
|
-
}: UserManagementParams): UserManagement {
|
80
|
-
|
81
|
-
const [rolesLoading, setRolesLoading] = React.useState<boolean>(true);
|
82
|
-
const [usersLoading, setUsersLoading] = React.useState<boolean>(true);
|
83
|
-
const [roles, setRoles] = React.useState<Role[]>([]);
|
84
|
-
const [usersWithRoleIds, setUsersWithRoleIds] = React.useState<UserWithRoleIds[]>([]);
|
85
|
-
|
86
|
-
const users = usersWithRoleIds.map(u => ({
|
87
|
-
...u,
|
88
|
-
roles: roles.filter(r => u.roles?.includes(r.id))
|
89
|
-
}) as User);
|
90
|
-
|
91
|
-
const [rolesError, setRolesError] = React.useState<Error | undefined>();
|
92
|
-
const [usersError, setUsersError] = React.useState<Error | undefined>();
|
93
|
-
|
94
|
-
const loading = rolesLoading || usersLoading;
|
95
|
-
|
96
|
-
useEffect(() => {
|
97
|
-
if (!firebaseApp || !rolesPath) return;
|
98
|
-
const firestore = getFirestore(firebaseApp);
|
99
|
-
|
100
|
-
return onSnapshot(collection(firestore, rolesPath),
|
101
|
-
{
|
102
|
-
next: (snapshot) => {
|
103
|
-
setRolesError(undefined);
|
104
|
-
try {
|
105
|
-
const newRoles = docsToRoles(snapshot.docs);
|
106
|
-
setRoles(newRoles);
|
107
|
-
} catch (e) {
|
108
|
-
console.error("Error loading roles", e);
|
109
|
-
setRolesError(e as Error);
|
110
|
-
}
|
111
|
-
setRolesLoading(false);
|
112
|
-
},
|
113
|
-
error: (e) => {
|
114
|
-
console.error("Error loading roles", e);
|
115
|
-
setRolesError(e);
|
116
|
-
setRolesLoading(false);
|
117
|
-
}
|
118
|
-
}
|
119
|
-
);
|
120
|
-
}, [firebaseApp, rolesPath]);
|
121
|
-
|
122
|
-
useEffect(() => {
|
123
|
-
if (!firebaseApp || !usersPath) return;
|
124
|
-
const firestore = getFirestore(firebaseApp);
|
125
|
-
|
126
|
-
return onSnapshot(collection(firestore, usersPath),
|
127
|
-
{
|
128
|
-
next: (snapshot) => {
|
129
|
-
setUsersError(undefined);
|
130
|
-
try {
|
131
|
-
const newUsers = docsToUsers(snapshot.docs);
|
132
|
-
setUsersWithRoleIds(newUsers);
|
133
|
-
} catch (e) {
|
134
|
-
console.error("Error loading users", e);
|
135
|
-
setUsersError(e as Error);
|
136
|
-
}
|
137
|
-
setUsersLoading(false);
|
138
|
-
},
|
139
|
-
error: (e) => {
|
140
|
-
console.error("Error loading users", e);
|
141
|
-
setUsersError(e);
|
142
|
-
setUsersLoading(false);
|
143
|
-
}
|
144
|
-
}
|
145
|
-
);
|
146
|
-
}, [firebaseApp, usersPath]);
|
147
|
-
|
148
|
-
const saveUser = useCallback(async (user: User): Promise<User> => {
|
149
|
-
if (!firebaseApp) throw Error("useFirestoreUserManagement Firebase not initialised");
|
150
|
-
const firestore = getFirestore(firebaseApp);
|
151
|
-
if (!firestore || !usersPath) throw Error("useFirestoreUserManagement Firestore not initialised");
|
152
|
-
console.debug("Persisting user", user);
|
153
|
-
const roleIds = user.roles?.map(r => r.id);
|
154
|
-
const {
|
155
|
-
uid,
|
156
|
-
...userData
|
157
|
-
} = user;
|
158
|
-
const data = {
|
159
|
-
...userData,
|
160
|
-
roles: roleIds
|
161
|
-
};
|
162
|
-
if (uid) {
|
163
|
-
return setDoc(doc(firestore, usersPath, uid), data, { merge: true }).then(() => user);
|
164
|
-
} else {
|
165
|
-
return addDoc(collection(firestore, usersPath), data).then(() => user);
|
166
|
-
}
|
167
|
-
}, [usersPath, firebaseApp]);
|
168
|
-
|
169
|
-
const saveRole = useCallback((role: Role): Promise<void> => {
|
170
|
-
if (!firebaseApp) throw Error("useFirestoreUserManagement Firebase not initialised");
|
171
|
-
const firestore = getFirestore(firebaseApp);
|
172
|
-
if (!firestore || !rolesPath) throw Error("useFirestoreUserManagement Firestore not initialised");
|
173
|
-
console.debug("Persisting role", role);
|
174
|
-
const {
|
175
|
-
id,
|
176
|
-
...roleData
|
177
|
-
} = role;
|
178
|
-
const ref = doc(firestore, rolesPath, id);
|
179
|
-
return setDoc(ref, roleData, { merge: true });
|
180
|
-
}, [rolesPath, firebaseApp]);
|
181
|
-
|
182
|
-
const deleteUser = useCallback(async (user: User): Promise<void> => {
|
183
|
-
if (!firebaseApp) throw Error("useFirestoreUserManagement Firebase not initialised");
|
184
|
-
const firestore = getFirestore(firebaseApp);
|
185
|
-
if (!firestore || !usersPath) throw Error("useFirestoreUserManagement Firestore not initialised");
|
186
|
-
console.debug("Deleting", user);
|
187
|
-
const { uid } = user;
|
188
|
-
return deleteDoc(doc(firestore, usersPath, uid));
|
189
|
-
}, [usersPath, firebaseApp]);
|
190
|
-
|
191
|
-
const deleteRole = useCallback((role: Role): Promise<void> => {
|
192
|
-
if (!firebaseApp) throw Error("useFirestoreUserManagement Firebase not initialised");
|
193
|
-
const firestore = getFirestore(firebaseApp);
|
194
|
-
if (!firestore || !rolesPath) throw Error("useFirestoreUserManagement Firestore not initialised");
|
195
|
-
console.debug("Deleting", role);
|
196
|
-
const { id } = role;
|
197
|
-
const ref = doc(firestore, rolesPath, id);
|
198
|
-
return deleteDoc(ref);
|
199
|
-
}, [rolesPath, firebaseApp]);
|
200
|
-
|
201
|
-
const collectionPermissions: PermissionsBuilder = useCallback(({
|
202
|
-
collection,
|
203
|
-
user
|
204
|
-
}) => resolveUserRolePermissions({
|
205
|
-
collection,
|
206
|
-
user
|
207
|
-
}), []);
|
208
|
-
|
209
|
-
const defineRolesFor: ((user: User) => Role[] | undefined) = useCallback((user) => {
|
210
|
-
if (!users) throw Error("Users not loaded");
|
211
|
-
const mgmtUser = users.find(u => u.email?.toLowerCase() === user?.email?.toLowerCase());
|
212
|
-
return mgmtUser?.roles;
|
213
|
-
}, [users]);
|
214
|
-
|
215
|
-
const authenticator: Authenticator = useCallback(({ user }) => {
|
216
|
-
console.debug("Authenticating user", user);
|
217
|
-
|
218
|
-
if (loading) {
|
219
|
-
console.warn("User management is still loading");
|
220
|
-
return false;
|
221
|
-
}
|
222
|
-
|
223
|
-
// This is an example of how you can link the access system to the user management plugin
|
224
|
-
if (users.length === 0) {
|
225
|
-
return true; // If there are no users created yet, we allow access to every user
|
226
|
-
}
|
227
|
-
|
228
|
-
const mgmtUser = users.find(u => u.email?.toLowerCase() === user?.email?.toLowerCase());
|
229
|
-
if (mgmtUser) {
|
230
|
-
return true;
|
231
|
-
}
|
232
|
-
|
233
|
-
throw Error("Could not find a user with the provided email in the user management system.");
|
234
|
-
}, [loading, users])
|
235
|
-
|
236
|
-
return {
|
237
|
-
loading,
|
238
|
-
roles,
|
239
|
-
users,
|
240
|
-
saveUser,
|
241
|
-
saveRole,
|
242
|
-
rolesError,
|
243
|
-
deleteUser,
|
244
|
-
deleteRole,
|
245
|
-
usersLimit,
|
246
|
-
usersError,
|
247
|
-
canEditRoles: canEditRoles === undefined ? true : canEditRoles,
|
248
|
-
allowDefaultRolesCreation: allowDefaultRolesCreation === undefined ? true : allowDefaultRolesCreation,
|
249
|
-
includeCollectionConfigPermissions: Boolean(includeCollectionConfigPermissions),
|
250
|
-
collectionPermissions,
|
251
|
-
defineRolesFor,
|
252
|
-
authenticator
|
253
|
-
}
|
254
|
-
}
|
255
|
-
|
256
|
-
const docsToUsers = (docs: DocumentSnapshot[]): (UserWithRoleIds)[] => {
|
257
|
-
return docs.map((doc) => {
|
258
|
-
const data = doc.data() as any;
|
259
|
-
const newVar = {
|
260
|
-
uid: doc.id,
|
261
|
-
...data,
|
262
|
-
created_on: data?.created_on?.toDate(),
|
263
|
-
updated_on: data?.updated_on?.toDate()
|
264
|
-
};
|
265
|
-
return newVar as (UserWithRoleIds);
|
266
|
-
});
|
267
|
-
}
|
268
|
-
|
269
|
-
const docsToRoles = (docs: DocumentSnapshot[]): Role[] => {
|
270
|
-
return docs.map((doc) => ({
|
271
|
-
id: doc.id,
|
272
|
-
...doc.data()
|
273
|
-
} as Role));
|
274
|
-
}
|