From fb2175e00a11cf51fbe474f52d3582bfa8f1c151 Mon Sep 17 00:00:00 2001 From: Yogi_Wang Date: Wed, 25 Dec 2019 14:43:31 +0800 Subject: [PATCH] Modify system admin role from has_admin_role to admin_role_in_auth and sysadmin_flag Signed-off-by: Yogi_Wang --- src/portal/src/app/shared/session-user.ts | 10 ++++++-- src/portal/src/app/shared/session.service.ts | 12 ++++----- .../shared/session.viewmodel.factory.spec.ts | 12 +++++++++ .../app/shared/session.viewmodel.factory.ts | 25 +++++++++++++++++++ src/portal/src/app/user/user.component.ts | 10 ++++---- src/portal/src/app/user/user.ts | 2 +- 6 files changed, 57 insertions(+), 14 deletions(-) create mode 100644 src/portal/src/app/shared/session.viewmodel.factory.spec.ts create mode 100644 src/portal/src/app/shared/session.viewmodel.factory.ts diff --git a/src/portal/src/app/shared/session-user.ts b/src/portal/src/app/shared/session-user.ts index cc762673f..5f53b78bf 100644 --- a/src/portal/src/app/shared/session-user.ts +++ b/src/portal/src/app/shared/session-user.ts @@ -12,17 +12,23 @@ // See the License for the specific language governing permissions and // limitations under the License. // Define the session user -export class SessionUser { +export class SessionUserBase { user_id: number; username: string; email: string; realname: string; role_name?: string; role_id?: number; - has_admin_role?: boolean; comment: string; oidc_user_meta?: OidcUserMeta; } +export class SessionUser extends SessionUserBase { + has_admin_role?: boolean; +} +export class SessionUserBackend extends SessionUserBase { + admin_role_in_auth?: boolean; + sysadmin_flag?: boolean; +} export class OidcUserMeta { id: number; user_id: number; diff --git a/src/portal/src/app/shared/session.service.ts b/src/portal/src/app/shared/session.service.ts index c5737e18f..a6206c9b9 100644 --- a/src/portal/src/app/shared/session.service.ts +++ b/src/portal/src/app/shared/session.service.ts @@ -15,11 +15,12 @@ import { Injectable } from '@angular/core'; import { HttpClient, HttpParams } from '@angular/common/http'; import { map, catchError } from "rxjs/operators"; import { Observable, throwError as observableThrowError } from "rxjs"; -import { SessionUser } from './session-user'; +import { SessionUser, SessionUserBackend } from './session-user'; import { Member } from '../project/member/member'; import { SignInCredential } from './sign-in-credential'; import { enLang } from './shared.const'; -import { HTTP_FORM_OPTIONS, HTTP_GET_OPTIONS, HTTP_JSON_OPTIONS } from "../../lib/utils/utils"; +import { SessionViewmodelFactory } from './session.viewmodel.factory'; +import { HTTP_FORM_OPTIONS, HTTP_GET_OPTIONS, HTTP_JSON_OPTIONS, clone } from "../../lib/utils/utils"; import { FlushAll } from "../../lib/utils/cache-util"; const signInUrl = '/c/login'; @@ -50,7 +51,7 @@ export class SessionService { "Content-Type": 'application/x-www-form-urlencoded' });*/ - constructor(private http: HttpClient) { } + constructor(private http: HttpClient, public sessionViewmodel: SessionViewmodelFactory) { } // Handle the related exceptions handleError(error: any): Observable { @@ -83,12 +84,11 @@ export class SessionService { * * @memberOf SessionService */ - retrieveUser(): Observable { + retrieveUser(): Observable { return this.http.get(currentUserEndpoint, HTTP_GET_OPTIONS) - .pipe(map(response => this.currentUser = response as SessionUser) + .pipe(map((response: SessionUserBackend) => this.currentUser = this.sessionViewmodel.getCurrentUser(response) as SessionUser) , catchError(error => this.handleError(error))); } - /** * For getting info */ diff --git a/src/portal/src/app/shared/session.viewmodel.factory.spec.ts b/src/portal/src/app/shared/session.viewmodel.factory.spec.ts new file mode 100644 index 000000000..10d2e8860 --- /dev/null +++ b/src/portal/src/app/shared/session.viewmodel.factory.spec.ts @@ -0,0 +1,12 @@ +import { TestBed } from '@angular/core/testing'; + +import { SessionViewmodelFactory } from './session.viewmodel.factory'; + +describe('SessionViewmodelFactory', () => { + beforeEach(() => TestBed.configureTestingModule({})); + + it('should be created', () => { + const service: SessionViewmodelFactory = TestBed.get(SessionViewmodelFactory); + expect(service).toBeTruthy(); + }); +}); diff --git a/src/portal/src/app/shared/session.viewmodel.factory.ts b/src/portal/src/app/shared/session.viewmodel.factory.ts new file mode 100644 index 000000000..46f647005 --- /dev/null +++ b/src/portal/src/app/shared/session.viewmodel.factory.ts @@ -0,0 +1,25 @@ +import { Injectable } from '@angular/core'; +import { SessionUser, SessionUserBackend } from './session-user'; +import { clone } from "../../lib/utils/utils"; + +@Injectable({ + providedIn: 'root' +}) +export class SessionViewmodelFactory { + + constructor() { } + // view model need + getCurrentUser(currentUser: SessionUserBackend): SessionUser { + return { + user_id: currentUser.user_id, + username: currentUser.username, + email: currentUser.email, + realname: currentUser.realname, + role_name: currentUser.role_name, + role_id: currentUser.role_id, + comment: currentUser.comment, + oidc_user_meta: currentUser.oidc_user_meta, + has_admin_role: currentUser.admin_role_in_auth || currentUser.sysadmin_flag + }; + } +} diff --git a/src/portal/src/app/user/user.component.ts b/src/portal/src/app/user/user.component.ts index 1d3bb06b4..529ff4b0b 100644 --- a/src/portal/src/app/user/user.component.ts +++ b/src/portal/src/app/user/user.component.ts @@ -115,7 +115,7 @@ export class UserComponent implements OnInit, OnDestroy { if (user.user_id === 0 || this.isMySelf(user.user_id)) { return false; } - if (user.has_admin_role) { + if (user.sysadmin_flag) { usersRole.push(1); } else { usersRole.push(0); @@ -136,7 +136,7 @@ export class UserComponent implements OnInit, OnDestroy { if (!u) { return "{{MISS}}"; } - let key: string = u.has_admin_role ? "USER.IS_ADMIN" : "USER.IS_NOT_ADMIN"; + let key: string = u.sysadmin_flag ? "USER.IS_ADMIN" : "USER.IS_NOT_ADMIN"; this.translate.get(key).subscribe((res: string) => this.adminColumn = res); return this.adminColumn; } @@ -145,7 +145,7 @@ export class UserComponent implements OnInit, OnDestroy { if (!u) { return "{{MISS}}"; } - let key: string = u.has_admin_role ? "USER.DISABLE_ADMIN_ACTION" : "USER.ENABLE_ADMIN_ACTION"; + let key: string = u.sysadmin_flag ? "USER.DISABLE_ADMIN_ACTION" : "USER.ENABLE_ADMIN_ACTION"; this.translate.get(key).subscribe((res: string) => this.adminMenuText = res); return this.adminMenuText; } @@ -196,7 +196,7 @@ export class UserComponent implements OnInit, OnDestroy { let updatedUser: User = new User(); updatedUser.user_id = this.selectedRow[i].user_id; - updatedUser.has_admin_role = true; // Set as admin + updatedUser.sysadmin_flag = true; // Set as admin observableLists.push(this.userService.updateUserRole(updatedUser)); } } @@ -209,7 +209,7 @@ export class UserComponent implements OnInit, OnDestroy { let updatedUser: User = new User(); updatedUser.user_id = this.selectedRow[i].user_id; - updatedUser.has_admin_role = false; // Set as none admin + updatedUser.sysadmin_flag = false; // Set as none admin observableLists.push(this.userService.updateUserRole(updatedUser)); } } diff --git a/src/portal/src/app/user/user.ts b/src/portal/src/app/user/user.ts index 2f552b9ce..b3fb3d450 100644 --- a/src/portal/src/app/user/user.ts +++ b/src/portal/src/app/user/user.ts @@ -27,7 +27,7 @@ export class User { deleted?: boolean; role_name?: string; role_id?: number; - has_admin_role?: boolean; + sysadmin_flag?: boolean; reset_uuid?: string; creation_time?: string; update_time?: string;