From c4882a6046934f1beea8dc1cc741e690cec357af Mon Sep 17 00:00:00 2001 From: Janicki Artur Date: Thu, 29 Nov 2018 11:58:40 +0100 Subject: [PATCH] add property isAdmin to the user interface Feature #14503_keep_services Arvados-DCO-1.1-Signed-off-by: Janicki Artur --- src/models/user.ts | 1 + src/services/auth-service/auth-service.ts | 14 ++++++++++++-- src/store/auth/auth-reducer.test.ts | 7 +++++-- 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/models/user.ts b/src/models/user.ts index c2f21e58..9f9c5347 100644 --- a/src/models/user.ts +++ b/src/models/user.ts @@ -10,6 +10,7 @@ export interface User { lastName: string; uuid: string; ownerUuid: string; + isAdmin: boolean; } export const getUserFullname = (user?: User) => { diff --git a/src/services/auth-service/auth-service.ts b/src/services/auth-service/auth-service.ts index 50760bb4..dd63de09 100644 --- a/src/services/auth-service/auth-service.ts +++ b/src/services/auth-service/auth-service.ts @@ -13,6 +13,7 @@ export const USER_FIRST_NAME_KEY = 'userFirstName'; export const USER_LAST_NAME_KEY = 'userLastName'; export const USER_UUID_KEY = 'userUuid'; export const USER_OWNER_UUID_KEY = 'userOwnerUuid'; +export const USER_IS_ADMIN = 'isAdmin'; export interface UserDetailsResponse { email: string; @@ -50,15 +51,20 @@ export class AuthService { return localStorage.getItem(USER_OWNER_UUID_KEY) || undefined; } + public getIsAdmin(): boolean { + return !!localStorage.getItem(USER_IS_ADMIN); + } + public getUser(): User | undefined { const email = localStorage.getItem(USER_EMAIL_KEY); const firstName = localStorage.getItem(USER_FIRST_NAME_KEY); const lastName = localStorage.getItem(USER_LAST_NAME_KEY); const uuid = localStorage.getItem(USER_UUID_KEY); const ownerUuid = localStorage.getItem(USER_OWNER_UUID_KEY); + const isAdmin = this.getIsAdmin(); return email && firstName && lastName && uuid && ownerUuid - ? { email, firstName, lastName, uuid, ownerUuid } + ? { email, firstName, lastName, uuid, ownerUuid, isAdmin } : undefined; } @@ -68,6 +74,7 @@ export class AuthService { localStorage.setItem(USER_LAST_NAME_KEY, user.lastName); localStorage.setItem(USER_UUID_KEY, user.uuid); localStorage.setItem(USER_OWNER_UUID_KEY, user.ownerUuid); + localStorage.setItem(USER_IS_ADMIN, JSON.stringify(user.isAdmin)); } public removeUser() { @@ -76,6 +83,7 @@ export class AuthService { localStorage.removeItem(USER_LAST_NAME_KEY); localStorage.removeItem(USER_UUID_KEY); localStorage.removeItem(USER_OWNER_UUID_KEY); + localStorage.removeItem(USER_IS_ADMIN); } public login() { @@ -95,12 +103,14 @@ export class AuthService { .get('/users/current') .then(resp => { this.actions.progressFn(reqId, false); + console.log('user:', resp.data); return { email: resp.data.email, firstName: resp.data.first_name, lastName: resp.data.last_name, uuid: resp.data.uuid, - ownerUuid: resp.data.owner_uuid + ownerUuid: resp.data.owner_uuid, + isAdmin: resp.data.is_admin }; }) .catch(e => { diff --git a/src/store/auth/auth-reducer.test.ts b/src/store/auth/auth-reducer.test.ts index 8cde3245..3dd486b3 100644 --- a/src/store/auth/auth-reducer.test.ts +++ b/src/store/auth/auth-reducer.test.ts @@ -29,7 +29,8 @@ describe('auth-reducer', () => { firstName: "John", lastName: "Doe", uuid: "uuid", - ownerUuid: "ownerUuid" + ownerUuid: "ownerUuid", + isAdmin: true }; const state = reducer(initialState, authActions.INIT({ user, token: "token" })); expect(state).toEqual({ @@ -58,7 +59,8 @@ describe('auth-reducer', () => { firstName: "John", lastName: "Doe", uuid: "uuid", - ownerUuid: "ownerUuid" + ownerUuid: "ownerUuid", + isAdmin: true }; const state = reducer(initialState, authActions.USER_DETAILS_SUCCESS(user)); @@ -71,6 +73,7 @@ describe('auth-reducer', () => { lastName: "Doe", uuid: "uuid", ownerUuid: "ownerUuid", + isAdmin: true } }); }); -- 2.30.2