angular/projects/researchdatabox/manage-roles/src/app/manage-roles.component.ts
selector | manage-roles |
styleUrls | ./manage-roles.component.scss |
templateUrl | ./manage-roles.component.html |
Properties |
|
Methods |
|
constructor(loggerService: LoggerService, translationService: TranslationService, userService: UserService)
|
||||||||||||
Parameters :
|
editUser | ||||||
editUser(username: string)
|
||||||
Parameters :
Returns :
void
|
hideDetailsModal |
hideDetailsModal()
|
Returns :
void
|
Protected Async initComponent |
initComponent()
|
Inherited from
BaseComponent
|
Defined in
BaseComponent:49
|
Returns :
Promise<void>
|
onDetailsModalHidden |
onDetailsModalHidden()
|
Returns :
void
|
onFilterChange | ||||||||
onFilterChange(roleFilter: any)
|
||||||||
Parameters :
Returns :
void
|
resetFilter |
resetFilter()
|
Returns :
void
|
Async saveCurrentUser | ||||||
saveCurrentUser($event: any)
|
||||||
Parameters :
Returns :
any
|
setSaveMessage |
setSaveMessage(msg: string, type: string)
|
Returns :
void
|
showDetailsModal |
showDetailsModal()
|
Returns :
void
|
getInitSubject |
getInitSubject()
|
Inherited from
BaseComponent
|
Defined in
BaseComponent:71
|
For those interested in the init from RXJS-land. Note that it returns a BehaviorSubject instance, and will have an initial value of false, so process the return value as needed.
Returns :
Subject<any>
|
isInitializing |
isInitializing()
|
Inherited from
BaseComponent
|
Defined in
BaseComponent:95
|
Main flag to indicate the init status
Returns :
boolean
|
ngOnInit |
ngOnInit()
|
Inherited from
BaseComponent
|
Defined in
BaseComponent:50
|
Returns :
void
|
Async waitForDeps |
waitForDeps()
|
Inherited from
BaseComponent
|
Defined in
BaseComponent:78
|
Wait for dependencies to initialise
Returns :
Promise<any>
|
Async waitForInit |
waitForInit()
|
Inherited from
BaseComponent
|
Defined in
BaseComponent:89
|
For those interested in the init from the Promise-land
Returns :
Promise<any>
|
currentUser |
Type : User
|
Default value : {username:'', name:'', email:'', roles:[]} as any
|
filteredUsers |
Type : any[]
|
Default value : []
|
hiddenUsers |
Type : []
|
Default value : ['admin']
|
isDetailsModalShown |
Type : boolean
|
Default value : false
|
Optional roleDetailsModal |
Type : ModalDirective
|
Decorators :
@ViewChild('roleDetailsModal', {static: false})
|
roles |
Type : Role[]
|
Default value : []
|
saveMsg |
Type : string
|
Default value : ""
|
saveMsgType |
Type : string
|
Default value : "info"
|
searchFilter |
Type : literal type
|
Default value : {
name: '',
role: null,
prevName: '',
prevRole:null,
roles: [ {value: null, label:'Any', checked:true}]
}
|
title |
Type : string
|
Default value : '@researchdatabox/manage-roles'
|
users |
Type : User[]
|
Default value : []
|
Protected brandingAndPortalUrl |
Type : string
|
Default value : ''
|
Inherited from
BaseComponent
|
Defined in
BaseComponent:42
|
Private filterFn |
Default value : function(initStat: boolean) { return initStat; }
|
Inherited from
BaseComponent
|
Defined in
BaseComponent:40
|
Protected initDependencies |
Type : Initable[]
|
Default value : []
|
Inherited from
BaseComponent
|
Defined in
BaseComponent:39
|
Protected initSubject |
Type : BehaviorSubject<any>
|
Default value : new BehaviorSubject(false)
|
Inherited from
BaseComponent
|
Defined in
BaseComponent:38
|
Protected isReady |
Type : boolean
|
Default value : false
|
Inherited from
BaseComponent
|
Defined in
BaseComponent:37
|
import { Component, Inject, ViewChild } from '@angular/core';
import { ModalDirective } from 'ngx-bootstrap/modal';
import { LoggerService, TranslationService, UserService, BaseComponent } from '@researchdatabox/portal-ng-common';
import { Role, User } from '@researchdatabox/portal-ng-common';
import * as _ from 'lodash';
@Component({
selector: 'manage-roles',
templateUrl: './manage-roles.component.html',
styleUrls: ['./manage-roles.component.scss']
})
export class ManageRolesComponent extends BaseComponent {
title = '@researchdatabox/manage-roles';
users: User[] = [];
filteredUsers: any[] = [];
searchFilter: {
name: string,
role: any,
prevName: string,
prevRole: any,
roles: any[] } = {
name: '',
role: null,
prevName: '',
prevRole:null,
roles: [ {value: null, label:'Any', checked:true}]
};
roles: Role[] = [];
hiddenUsers = ['admin'];
currentUser: User = {username:'', name:'', email:'', roles:[]} as any;
saveMsg = "";
saveMsgType ="info";
isDetailsModalShown: boolean = false;
@ViewChild('roleDetailsModal', { static: false }) roleDetailsModal?: ModalDirective;
constructor(
@Inject(LoggerService) private loggerService: LoggerService,
@Inject(TranslationService) private translationService: TranslationService,
@Inject(UserService) private userService: UserService
) {
super();
this.loggerService.debug(`Manage Roles waiting for deps to init...`);
this.initDependencies = [this.translationService, this.userService];
}
protected override async initComponent():Promise<void> {
let roles: any = await this.userService.getBrandRoles();
this.roles = roles;
_.forEach(roles, (role:any) => {
this.searchFilter.roles.push({value:role.name, label:role.name, checked:false});
_.forEach(role.users, (user:any) => {
if (!_.includes(this.hiddenUsers, user.username)) {
// flattening the tree, match by username
let existingUser: any = _.find(this.users, (existingUser:any) => { return existingUser.username == user.username});
if (_.isEmpty(existingUser)) {
existingUser = user;
existingUser.roles = [role.name];
this.users.push(existingUser);
} else {
existingUser.roles.push(role.name);
}
}
});
});
_.map(this.users, (user:any)=> {user.roleStr = _.join(user.roles, ', ')});
this.filteredUsers = this.users;
this.loggerService.debug(`Manage Roles initComponent done`);
}
editUser(username:string) {
this.setSaveMessage();
let currUser = _.find(this.users, (user:any)=>{return user.username == username});
if(!_.isUndefined(currUser)) {
this.currentUser = currUser;
this.currentUser.newRoles = _.map(this.roles, (r:any) => {
return {name: r.name, id:r.id, users: [], hasRole: _.includes(this.currentUser.roles, r.name)};
});
this.showDetailsModal();
}
}
showDetailsModal(): void {
this.isDetailsModalShown = true;
this.roleDetailsModal?.show();
}
hideDetailsModal(): void {
if(!_.isUndefined(this.roleDetailsModal)) {
this.roleDetailsModal.hide();
}
}
onDetailsModalHidden(): void {
this.isDetailsModalShown = false;
}
async saveCurrentUser($event:any) {
let hasRole:boolean = false;
let newRoles:any[] = [];
_.forEach(this.currentUser.newRoles, (role:any) => {
hasRole = hasRole || role.hasRole;
if (role.hasRole)
newRoles.push(role.name);
});
if (!hasRole) {
this.setSaveMessage("Please select at least one role!", "danger");
return;
}
this.setSaveMessage("Saving...", "primary");
let saveRes:any = await this.userService.updateUserRoles(this.currentUser.id, newRoles); //SaveResult
if (saveRes.status) {
this.currentUser.roles = newRoles;
this.currentUser.roleStr = _.join(this.currentUser.roles);
this.setSaveMessage();
this.hideDetailsModal();
} else {
this.setSaveMessage(saveRes.message, "danger");
}
}
setSaveMessage(msg:string="", type:string="primary") {
this.saveMsg = msg;
this.saveMsgType = type;
}
onFilterChange(roleFilter:any=null) {
if (roleFilter) {
roleFilter.checked = true;
this.searchFilter.role = roleFilter.value;
_.map(this.searchFilter.roles, (role:any)=> role.checked = roleFilter.value == role.value );
}
if (this.searchFilter.name != this.searchFilter.prevName || this.searchFilter.role != this.searchFilter.prevRole) {
this.searchFilter.prevName = this.searchFilter.name;
this.searchFilter.prevRole = this.searchFilter.role;
var nameFilter =_.isEmpty(this.searchFilter.name) ? "" : _.trim(this.searchFilter.name);
// run filter change...
this.filteredUsers = _.filter(this.users, (user:any) => {
var hasRole = this.searchFilter.role == null ? true : _.includes(user.roles, this.searchFilter.role);
var hasNameMatch = nameFilter == "" ? true : (_.toLower(user.name).indexOf(_.toLower(this.searchFilter.name)) >= 0);
return hasRole && hasNameMatch;
});
}
}
resetFilter() {
this.searchFilter.name = '';
this.searchFilter.role = null;
_.map(this.searchFilter.roles, (role:any)=> role.checked = role.value == null);
this.onFilterChange();
}
}
<div class="col-md-offset-2 col-md-8" *ngIf="isReady">
<div class="panel panel-default">
<div class="panel-heading">
<span class="panel-title">
{{ 'manage-roles-title' | i18next }}
</span>
</div>
<div class="panel-body" style="overflow:scroll" >
<div class="input-group">
<span class="input-group-addon" id="name-addon">{{ 'manage-roles-filter-name' | i18next }}</span>
<input type="text" (keyup)="onFilterChange(null)" [(ngModel)]="searchFilter.name" class="form-control" placeholder="{{ 'manage-roles-filter-name-placeholder' | i18next }}" [attr.aria-label]="'manage-roles-filter-name' | i18next ">
<span (click)="resetFilter()" class="input-group-btn"><button class="btn btn-primary" type='button'>Reset</button></span>
</div>
<div>
<br/>
</div>
<div class="row">
<div class="col-xs-3">
{{ 'manage-roles-filter-label' | i18next }}
</div>
<div class="col-xs-4" >
<fieldset>
<legend [hidden]="true"><span></span></legend>
<div *ngFor="let roleFilter of searchFilter.roles" >
<input [checked]="roleFilter.checked" (change)="onFilterChange(roleFilter)" id="role-{{roleFilter.value}}" name="role-filter" type="radio" >
<label for="role-{{roleFilter.value}}" > {{roleFilter.label}}</label>
</div>
</fieldset>
</div>
<div class="col-xs-4" ></div>
</div>
<div>
<br/>
</div>
<div class="table-responsive">
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th>{{ 'manage-roles-name' | i18next }}</th>
<th>{{ 'manage-roles-email' | i18next }}</th>
<th>{{ 'manage-roles-role' | i18next }}</th>
<th>{{ 'manage-roles-action' | i18next }}</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let user of filteredUsers">
<td>{{user.name}}</td>
<td>{{user.email}}</td>
<td>{{user.roleStr}}</td>
<td>
<a style="cursor:pointer" (click)="editUser(user.username)" >Edit</a>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<!-- Modal -->
<div *ngIf="isDetailsModalShown" [config]="{backdrop: 'static', show: true}" (onHidden)="onDetailsModalHidden()" bsModal #roleDetailsModal="bs-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="detailsform" >
<div class="modal-dialog modal-md">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<span class="modal-title h4-header">{{ 'manage-roles-edit' | i18next }}</span>
</div>
<div class="modal-body">
<form >
<div class="table-responsive">
<table class="table">
<tbody>
<tr>
<td>{{ 'manage-roles-name' | i18next }}</td>
<td>{{currentUser.name}}</td>
</tr>
<tr>
<td>{{ 'manage-roles-email' | i18next }}</td>
<td>{{currentUser.email}}</td>
</tr>
<tr>
<td>{{ 'manage-roles-role' | i18next }}</td>
<td>
<div *ngFor="let role of currentUser.newRoles">
<label class="checkbox-inline" for="cb_{{role.id}}">
<input id="cb_{{role.id}}" type="checkbox" value="{{role.id}}" [(ngModel)]="role.hasRole" [ngModelOptions]="{standalone: true}">
{{role.name}}
</label>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</form>
<div class="bg-{{saveMsgType}} center-block">{{saveMsg}}</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" (click)="saveCurrentUser($event)">{{ 'manage-roles-save' | i18next }}</button>
<button type="button" class="btn btn-default" (click)="hideDetailsModal()">{{ 'manage-roles-cancel' | i18next }}</button>
</div>
</div>
</div>
</div>
./manage-roles.component.scss