Modify system admin role from has_admin_role to admin_role_in_auth and sysadmin_flag

Signed-off-by: Yogi_Wang <yawang@vmware.com>
This commit is contained in:
Yogi_Wang 2019-12-25 14:43:31 +08:00
parent 1855558739
commit fb2175e00a
6 changed files with 57 additions and 14 deletions

View File

@ -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;

View File

@ -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<any> {
@ -83,12 +84,11 @@ export class SessionService {
*
* @memberOf SessionService
*/
retrieveUser(): Observable<SessionUser> {
retrieveUser(): Observable<SessionUserBackend> {
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
*/

View File

@ -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();
});
});

View File

@ -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
};
}
}

View File

@ -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));
}
}

View File

@ -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;