@firecms/user_management 3.0.0-canary.259 → 3.0.0-canary.260

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.
@@ -1,5 +1,5 @@
1
1
  import { UserManagement } from "../types";
2
- import { AuthController, DataSourceDelegate, User } from "@firecms/core";
2
+ import { AuthController, DataSourceDelegate, Role, User } from "@firecms/core";
3
3
  export interface UserManagementParams<CONTROLLER extends AuthController<any> = AuthController<any>> {
4
4
  authController: CONTROLLER;
5
5
  /**
@@ -19,6 +19,11 @@ export interface UserManagementParams<CONTROLLER extends AuthController<any> = A
19
19
  * Default: __FIRECMS/config/roles
20
20
  */
21
21
  rolesPath?: string;
22
+ /**
23
+ * The roles that are available in the user management system.
24
+ * If you provide this, the user management system will not fetch the roles from the database.
25
+ */
26
+ roles?: Role[];
22
27
  /**
23
28
  * If there are no roles in the database, provide a button to create the default roles.
24
29
  */
@@ -35,7 +40,8 @@ export interface UserManagementParams<CONTROLLER extends AuthController<any> = A
35
40
  * @param dataSourceDelegate
36
41
  * @param usersPath
37
42
  * @param rolesPath
43
+ * @param roles
38
44
  * @param allowDefaultRolesCreation
39
45
  * @param includeCollectionConfigPermissions
40
46
  */
41
- export declare function useBuildUserManagement<CONTROLLER extends AuthController<any> = AuthController<any>, USER extends User = CONTROLLER extends AuthController<infer U> ? U : any>({ authController, dataSourceDelegate, usersPath, rolesPath, allowDefaultRolesCreation, includeCollectionConfigPermissions }: UserManagementParams<CONTROLLER>): UserManagement<USER> & CONTROLLER;
47
+ export declare function useBuildUserManagement<CONTROLLER extends AuthController<any> = AuthController<any>, USER extends User = CONTROLLER extends AuthController<infer U> ? U : any>({ authController, dataSourceDelegate, roles: rolesProp, usersPath, rolesPath, allowDefaultRolesCreation, includeCollectionConfigPermissions }: UserManagementParams<CONTROLLER>): UserManagement<USER> & CONTROLLER;
package/dist/index.es.js CHANGED
@@ -145,6 +145,7 @@ function hexToRgbaWithOpacity(hexColor, opacity = 10) {
145
145
  function useBuildUserManagement({
146
146
  authController,
147
147
  dataSourceDelegate,
148
+ roles: rolesProp,
148
149
  usersPath = "__FIRECMS/config/users",
149
150
  rolesPath = "__FIRECMS/config/roles",
150
151
  allowDefaultRolesCreation,
@@ -153,9 +154,10 @@ function useBuildUserManagement({
153
154
  if (!authController) {
154
155
  throw Error("useBuildUserManagement: You need to provide an authController since version 3.0.0-beta.11. Check https://firecms.co/docs/pro/migrating_from_v3_beta");
155
156
  }
156
- const [rolesLoading, setRolesLoading] = React.useState(true);
157
+ const rolesDefinedInCode = (rolesProp ?? [])?.length > 0;
158
+ const [rolesLoading, setRolesLoading] = React.useState(!rolesDefinedInCode);
157
159
  const [usersLoading, setUsersLoading] = React.useState(true);
158
- const [roles, setRoles] = React.useState([]);
160
+ const [roles, setRoles] = React.useState(rolesProp ?? []);
159
161
  const [usersWithRoleIds, setUsersWithRoleIds] = React.useState([]);
160
162
  const users = usersWithRoleIds.map((u) => ({
161
163
  ...u,
@@ -167,6 +169,7 @@ function useBuildUserManagement({
167
169
  const _rolesLoading = rolesLoading;
168
170
  const loading = _rolesLoading || _usersLoading;
169
171
  useEffect(() => {
172
+ if (rolesDefinedInCode) return;
170
173
  if (!dataSourceDelegate || !rolesPath) return;
171
174
  if (dataSourceDelegate.initialised !== void 0 && !dataSourceDelegate.initialised) return;
172
175
  if (authController?.initialLoading) return;
@@ -175,6 +178,7 @@ function useBuildUserManagement({
175
178
  path: rolesPath,
176
179
  onUpdate(entities) {
177
180
  setRolesError(void 0);
181
+ console.debug("Updating roles", entities);
178
182
  try {
179
183
  const newRoles = entityToRoles(entities);
180
184
  if (!equal(newRoles, roles)) {
@@ -194,7 +198,7 @@ function useBuildUserManagement({
194
198
  setRolesLoading(false);
195
199
  }
196
200
  });
197
- }, [dataSourceDelegate?.initialised, authController?.initialLoading, authController?.user?.uid, rolesPath]);
201
+ }, [rolesDefinedInCode, dataSourceDelegate?.initialised, authController?.initialLoading, authController?.user?.uid, rolesPath]);
198
202
  useEffect(() => {
199
203
  if (!dataSourceDelegate || !usersPath) return;
200
204
  if (dataSourceDelegate.initialised !== void 0 && !dataSourceDelegate.initialised) {
@@ -364,7 +368,10 @@ function useBuildUserManagement({
364
368
  const isAdmin = (userRoles ?? []).some((r_2) => r_2.id === "admin");
365
369
  const userRoleIds = userRoles?.map((r_3) => r_3.id);
366
370
  useEffect(() => {
367
- console.debug("Setting roles", userRoles);
371
+ console.debug("Setting user roles", {
372
+ userRoles,
373
+ roles
374
+ });
368
375
  authController.setUserRoles?.(userRoles ?? []);
369
376
  }, [userRoleIds]);
370
377
  const getUser = useCallback((uid_0) => {