1.2.6 - Removed specific user groups and roles. Records must be dynamic.

master
Tudor Stanciu 2023-04-12 19:03:09 +03:00
parent 7b22385a02
commit 90f934671b
6 changed files with 10 additions and 56 deletions

View File

@ -44,3 +44,4 @@ All tests in the package can be executed by running: `npm test`.
1.2.3 - User roles were handled in useTuitioUserInfo hook. 1.2.3 - User roles were handled in useTuitioUserInfo hook.
1.2.4 - Upgraded @flare/tuitio-client. 1.2.4 - Upgraded @flare/tuitio-client.
1.2.5 - Upgraded @flare/tuitio-client. 1.2.5 - Upgraded @flare/tuitio-client.
1.2.6 - Removed specific user groups and roles. Records must be dynamic.

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{ {
"name": "@flare/tuitio-client-react", "name": "@flare/tuitio-client-react",
"version": "1.2.5", "version": "1.2.6",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@flare/tuitio-client-react", "name": "@flare/tuitio-client-react",
"version": "1.2.5", "version": "1.2.6",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"@flare/js-utils": "^1.1.0", "@flare/js-utils": "^1.1.0",

View File

@ -1,6 +1,6 @@
{ {
"name": "@flare/tuitio-client-react", "name": "@flare/tuitio-client-react",
"version": "1.2.5", "version": "1.2.6",
"description": "Tuitio client react is an npm package written in typescript that facilitates the integration of a react application with Tuitio.", "description": "Tuitio client react is an npm package written in typescript that facilitates the integration of a react application with Tuitio.",
"main": "./dist/cjs/index.js", "main": "./dist/cjs/index.js",
"module": "./dist/esm/index.js", "module": "./dist/esm/index.js",

View File

@ -1,11 +0,0 @@
// Copyright (c) 2023 Tudor Stanciu
export const userRoles = {
SYS_ADMIN: "SYSTEM_ADMINISTRATOR",
DEVELOPER: "FULLSTACK_DEVELOPER",
POWER_USER: "POWER_USER",
REGULAR_USER: "REGULAR_USER",
VIEWER: "READONLY_USER",
GUEST: "ANONYMOUS_USER",
DEMO: "DEMO_USER"
};

View File

@ -1,46 +1,14 @@
// Copyright (c) 2023 Tudor Stanciu // Copyright (c) 2023 Tudor Stanciu
import { useContext, useCallback, useEffect, useMemo } from "react"; import { useContext, useCallback, useEffect } from "react";
import { TuitioClient } from "@flare/tuitio-client"; import { TuitioClient } from "@flare/tuitio-client";
import type { TuitioUserInfo, UserRole } from "@flare/tuitio-client"; import type { TuitioUserInfo } from "@flare/tuitio-client";
import { TuitioContext, TuitioDispatchContext } from "../contexts"; import { TuitioContext, TuitioDispatchContext } from "../contexts";
import { userRoles } from "../constants/userRoles";
type UserFlags = { export type TuitioUserInfoHookResult = {
isSysAdmin: boolean;
isDeveloper: boolean;
isPowerUser: boolean;
isRegularUser: boolean;
isViewer: boolean;
isGuest: boolean;
isDemoGuest: boolean;
};
const defaultUserFlags: UserFlags = {
isSysAdmin: false,
isDeveloper: false,
isPowerUser: false,
isRegularUser: false,
isViewer: false,
isGuest: false,
isDemoGuest: false
};
export type TuitioUserInfoHookResult = UserFlags & {
userInfo?: TuitioUserInfo; userInfo?: TuitioUserInfo;
}; };
const getFlags = (roles: UserRole[]): UserFlags => {
const isSysAdmin = roles.some(role => role.code === userRoles.SYS_ADMIN);
const isDeveloper = roles.some(role => role.code === userRoles.DEVELOPER);
const isPowerUser = roles.some(role => role.code === userRoles.POWER_USER);
const isRegularUser = roles.some(role => role.code === userRoles.REGULAR_USER);
const isViewer = roles.some(role => role.code === userRoles.VIEWER);
const isGuest = roles.some(role => role.code === userRoles.GUEST);
const isDemoGuest = roles.some(role => role.code === userRoles.DEMO);
return { isSysAdmin, isDeveloper, isPowerUser, isRegularUser, isViewer, isGuest, isDemoGuest };
};
const useTuitioUserInfo = (): TuitioUserInfoHookResult => { const useTuitioUserInfo = (): TuitioUserInfoHookResult => {
const state = useContext(TuitioContext); const state = useContext(TuitioContext);
const dispatchActions = useContext(TuitioDispatchContext); const dispatchActions = useContext(TuitioDispatchContext);
@ -58,9 +26,8 @@ const useTuitioUserInfo = (): TuitioUserInfoHookResult => {
}, [getUserInfo, state.userInfo]); }, [getUserInfo, state.userInfo]);
const userInfo = state.userInfo; const userInfo = state.userInfo;
const flags = useMemo(() => (userInfo ? getFlags(userInfo.userRoles) : defaultUserFlags), [userInfo]);
return { userInfo, ...flags }; return { userInfo };
}; };
export { useTuitioUserInfo }; export { useTuitioUserInfo };

View File

@ -75,14 +75,11 @@ describe("useTuitioUserInfo: dispatchActions", () => {
expect(onUserInfoLoadedSpy).toHaveBeenCalled(); expect(onUserInfoLoadedSpy).toHaveBeenCalled();
expect(onUserInfoLoadedSpy).toHaveBeenCalledWith(userInfoMock); expect(onUserInfoLoadedSpy).toHaveBeenCalledWith(userInfoMock);
const { userInfo, isSysAdmin, isDeveloper, isGuest } = result.current; const { userInfo } = result.current;
expect(userInfo).toBeDefined(); expect(userInfo).toBeDefined();
expect(userInfo?.userId).toBe(1); expect(userInfo?.userId).toBe(1);
expect(userInfo?.userName).toBe("tuitio.test"); expect(userInfo?.userName).toBe("tuitio.test");
expect(userInfo?.userRoles).toHaveLength(2); expect(userInfo?.userRoles).toHaveLength(2);
expect(userInfo?.userGroups).toHaveLength(1); expect(userInfo?.userGroups).toHaveLength(1);
expect(isSysAdmin).toBe(true);
expect(isDeveloper).toBe(true);
expect(isGuest).toBe(false);
}); });
}); });