1.2.3 - User roles were handled in useTuitioUserInfo hook.
parent
11775019df
commit
630f8472ce
|
@ -16,13 +16,13 @@ The package installation can be done in two ways:
|
||||||
## How to use the package
|
## How to use the package
|
||||||
|
|
||||||
```javascript!
|
```javascript!
|
||||||
const { TuitioProvider, useTuitioClient, useTuitioUser, useTuitioToken } = require("@flare/tuitio-client-react");
|
const { TuitioProvider, useTuitioClient, useTuitioUser, useTuitioToken, useTuitioUserInfo } = require("@flare/tuitio-client-react");
|
||||||
const TuitioProvider = require("@flare/tuitio-client-react");
|
const TuitioProvider = require("@flare/tuitio-client-react");
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
```javascript!
|
```javascript!
|
||||||
import { TuitioProvider, useTuitioClient, useTuitioUser, useTuitioToken } from "@flare/tuitio-client-react";
|
import { TuitioProvider, useTuitioClient, useTuitioUser, useTuitioToken, useTuitioUserInfo } from "@flare/tuitio-client-react";
|
||||||
import TuitioProvider from "@flare/tuitio-client-react";
|
import TuitioProvider from "@flare/tuitio-client-react";
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -41,3 +41,4 @@ All tests in the package can be executed by running: `npm test`.
|
||||||
1.2.0 - Has been implemented the "user-info" method exposed by the Tuitio API and the latest changes published by "@flare/tuitio-client".
|
1.2.0 - Has been implemented the "user-info" method exposed by the Tuitio API and the latest changes published by "@flare/tuitio-client".
|
||||||
1.2.1 - Added decodedToken in useTuitioToken hook. The token is obtained directly from TuitioProvider's react context.
|
1.2.1 - Added decodedToken in useTuitioToken hook. The token is obtained directly from TuitioProvider's react context.
|
||||||
1.2.2 - Upgraded @flare/tuitio-client and @flare/js-utils packages.
|
1.2.2 - Upgraded @flare/tuitio-client and @flare/js-utils packages.
|
||||||
|
1.2.3 - User roles were handled in useTuitioUserInfo hook.
|
||||||
|
|
|
@ -1,16 +1,16 @@
|
||||||
{
|
{
|
||||||
"name": "@flare/tuitio-client-react",
|
"name": "@flare/tuitio-client-react",
|
||||||
"version": "1.2.2",
|
"version": "1.2.3",
|
||||||
"lockfileVersion": 2,
|
"lockfileVersion": 2,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "@flare/tuitio-client-react",
|
"name": "@flare/tuitio-client-react",
|
||||||
"version": "1.2.2",
|
"version": "1.2.3",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@flare/js-utils": "^1.1.0",
|
"@flare/js-utils": "^1.1.0",
|
||||||
"@flare/tuitio-client": "^1.2.2"
|
"@flare/tuitio-client": "^1.2.3"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@testing-library/react": "^12.1.5",
|
"@testing-library/react": "^12.1.5",
|
||||||
|
@ -724,9 +724,9 @@
|
||||||
"integrity": "sha512-6NBXdZgRrHbLXw4EMgyqCIzOVAlUgr1+8QGHjlA+n5Iw2Lp/+dP3FTgAfPW/cHR/PBI3cj7gUDVUf/zD/qTPOQ=="
|
"integrity": "sha512-6NBXdZgRrHbLXw4EMgyqCIzOVAlUgr1+8QGHjlA+n5Iw2Lp/+dP3FTgAfPW/cHR/PBI3cj7gUDVUf/zD/qTPOQ=="
|
||||||
},
|
},
|
||||||
"node_modules/@flare/tuitio-client": {
|
"node_modules/@flare/tuitio-client": {
|
||||||
"version": "1.2.2",
|
"version": "1.2.3",
|
||||||
"resolved": "https://lab.code-rove.com/public-node-registry/@flare/tuitio-client/-/tuitio-client-1.2.2.tgz",
|
"resolved": "https://lab.code-rove.com/public-node-registry/@flare/tuitio-client/-/tuitio-client-1.2.3.tgz",
|
||||||
"integrity": "sha512-Rqz9seWFY5nUivMHrONphgYEeVriAq3yn8icAUQ+TFgf/ajoOqv0GlVfo2Nc//vqsLBJkZtHexmF92/5WHvvhQ==",
|
"integrity": "sha512-m1cJZNTLNXwviD2hkeAvs6I/jlC17GHkUraExfaEaYOUrmDpovE64sg6Xheb01mXnDADCdA2MYxdJgxODNJAqw==",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@flare/js-utils": "^1.1.0",
|
"@flare/js-utils": "^1.1.0",
|
||||||
"axios": "^1.3.2"
|
"axios": "^1.3.2"
|
||||||
|
@ -7661,9 +7661,9 @@
|
||||||
"integrity": "sha512-6NBXdZgRrHbLXw4EMgyqCIzOVAlUgr1+8QGHjlA+n5Iw2Lp/+dP3FTgAfPW/cHR/PBI3cj7gUDVUf/zD/qTPOQ=="
|
"integrity": "sha512-6NBXdZgRrHbLXw4EMgyqCIzOVAlUgr1+8QGHjlA+n5Iw2Lp/+dP3FTgAfPW/cHR/PBI3cj7gUDVUf/zD/qTPOQ=="
|
||||||
},
|
},
|
||||||
"@flare/tuitio-client": {
|
"@flare/tuitio-client": {
|
||||||
"version": "1.2.2",
|
"version": "1.2.3",
|
||||||
"resolved": "https://lab.code-rove.com/public-node-registry/@flare/tuitio-client/-/tuitio-client-1.2.2.tgz",
|
"resolved": "https://lab.code-rove.com/public-node-registry/@flare/tuitio-client/-/tuitio-client-1.2.3.tgz",
|
||||||
"integrity": "sha512-Rqz9seWFY5nUivMHrONphgYEeVriAq3yn8icAUQ+TFgf/ajoOqv0GlVfo2Nc//vqsLBJkZtHexmF92/5WHvvhQ==",
|
"integrity": "sha512-m1cJZNTLNXwviD2hkeAvs6I/jlC17GHkUraExfaEaYOUrmDpovE64sg6Xheb01mXnDADCdA2MYxdJgxODNJAqw==",
|
||||||
"requires": {
|
"requires": {
|
||||||
"@flare/js-utils": "^1.1.0",
|
"@flare/js-utils": "^1.1.0",
|
||||||
"axios": "^1.3.2"
|
"axios": "^1.3.2"
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "@flare/tuitio-client-react",
|
"name": "@flare/tuitio-client-react",
|
||||||
"version": "1.2.2",
|
"version": "1.2.3",
|
||||||
"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",
|
||||||
|
@ -49,7 +49,7 @@
|
||||||
],
|
],
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@flare/js-utils": "^1.1.0",
|
"@flare/js-utils": "^1.1.0",
|
||||||
"@flare/tuitio-client": "^1.2.2"
|
"@flare/tuitio-client": "^1.2.3"
|
||||||
},
|
},
|
||||||
"peerDependencies": {
|
"peerDependencies": {
|
||||||
"react": "^16.14.0"
|
"react": "^16.14.0"
|
||||||
|
|
|
@ -0,0 +1,11 @@
|
||||||
|
// 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"
|
||||||
|
};
|
|
@ -1,11 +1,47 @@
|
||||||
// Copyright (c) 2023 Tudor Stanciu
|
// Copyright (c) 2023 Tudor Stanciu
|
||||||
|
|
||||||
import { useContext, useCallback, useEffect } from "react";
|
import { useContext, useCallback, useEffect, useMemo } from "react";
|
||||||
import { TuitioClient } from "@flare/tuitio-client";
|
import { TuitioClient } from "@flare/tuitio-client";
|
||||||
import type { TuitioUserInfo } from "@flare/tuitio-client";
|
import type { TuitioUserInfo, UserRole } from "@flare/tuitio-client";
|
||||||
import { TuitioContext, TuitioDispatchContext } from "../contexts";
|
import { TuitioContext, TuitioDispatchContext } from "../contexts";
|
||||||
|
import { userRoles } from "../constants/userRoles";
|
||||||
|
|
||||||
const useTuitioUserInfo = () => {
|
type UserFlags = {
|
||||||
|
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;
|
||||||
|
};
|
||||||
|
|
||||||
|
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 state = useContext(TuitioContext);
|
const state = useContext(TuitioContext);
|
||||||
const dispatchActions = useContext(TuitioDispatchContext);
|
const dispatchActions = useContext(TuitioDispatchContext);
|
||||||
|
|
||||||
|
@ -22,8 +58,9 @@ const useTuitioUserInfo = () => {
|
||||||
}, [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 };
|
return { userInfo, ...flags };
|
||||||
};
|
};
|
||||||
|
|
||||||
export { useTuitioUserInfo };
|
export { useTuitioUserInfo };
|
||||||
|
|
|
@ -20,10 +20,25 @@ const userInfoMock = {
|
||||||
securityStamp: "mock",
|
securityStamp: "mock",
|
||||||
creationDate: "2023-03-08T11:46:31.453",
|
creationDate: "2023-03-08T11:46:31.453",
|
||||||
failedLoginAttempts: 0,
|
failedLoginAttempts: 0,
|
||||||
lastLoginDate: "2023-03-27T16:09:26.283"
|
lastLoginDate: "2023-03-27T16:09:26.283",
|
||||||
|
userRoles: [
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
code: "SYSTEM_ADMINISTRATOR",
|
||||||
|
name: "System administrator"
|
||||||
|
},
|
||||||
|
{ id: 2, code: "FULLSTACK_DEVELOPER", name: "Fullstack developer" }
|
||||||
|
],
|
||||||
|
userGroups: [
|
||||||
|
{
|
||||||
|
id: 1,
|
||||||
|
code: "ADMINISTRATORS",
|
||||||
|
name: "Administrators"
|
||||||
|
}
|
||||||
|
]
|
||||||
};
|
};
|
||||||
|
|
||||||
describe("useTuitioClient: dispatchActions", () => {
|
describe("useTuitioUserInfo: dispatchActions", () => {
|
||||||
const tuitioContextState = {
|
const tuitioContextState = {
|
||||||
auth: {
|
auth: {
|
||||||
userName: null,
|
userName: null,
|
||||||
|
@ -60,9 +75,14 @@ describe("useTuitioClient: dispatchActions", () => {
|
||||||
expect(onUserInfoLoadedSpy).toHaveBeenCalled();
|
expect(onUserInfoLoadedSpy).toHaveBeenCalled();
|
||||||
expect(onUserInfoLoadedSpy).toHaveBeenCalledWith(userInfoMock);
|
expect(onUserInfoLoadedSpy).toHaveBeenCalledWith(userInfoMock);
|
||||||
|
|
||||||
const { userInfo } = result.current;
|
const { userInfo, isSysAdmin, isDeveloper, isGuest } = 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?.userGroups).toHaveLength(1);
|
||||||
|
expect(isSysAdmin).toBe(true);
|
||||||
|
expect(isDeveloper).toBe(true);
|
||||||
|
expect(isGuest).toBe(false);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue