Added roles and permissions system, as well as the ability to modify users and their roles
Downloads manager now uses device fingerprint as identifier rather than a randomly generated sessionIDpull/67/head
parent
e7b841c056
commit
b685b955df
@ -0,0 +1,19 @@
|
|||||||
|
<h4 *ngIf="role" mat-dialog-title><ng-container i18n="Manage role dialog title">Manage role</ng-container> - {{role.name}}</h4>
|
||||||
|
|
||||||
|
<mat-dialog-content *ngIf="role">
|
||||||
|
<mat-list>
|
||||||
|
<mat-list-item role="listitem" *ngFor="let permission of available_permissions">
|
||||||
|
<h3 matLine>{{permissionToLabel[permission] ? permissionToLabel[permission] : permission}}</h3>
|
||||||
|
<span matLine>
|
||||||
|
<mat-radio-group [disabled]="permission === 'settings' && role.name === 'admin'" (change)="changeRolePermissions($event, permission, permissions[permission])" [(ngModel)]="permissions[permission]" [attr.aria-label]="'Give role permission for ' + permission">
|
||||||
|
<mat-radio-button value="yes"><ng-container i18n="Yes">Yes</ng-container></mat-radio-button>
|
||||||
|
<mat-radio-button value="no"><ng-container i18n="No">No</ng-container></mat-radio-button>
|
||||||
|
</mat-radio-group>
|
||||||
|
</span>
|
||||||
|
</mat-list-item>
|
||||||
|
</mat-list>
|
||||||
|
</mat-dialog-content>
|
||||||
|
|
||||||
|
<mat-dialog-actions>
|
||||||
|
<button mat-button mat-dialog-close><ng-container i18n="Close">Close</ng-container></button>
|
||||||
|
</mat-dialog-actions>
|
@ -0,0 +1,4 @@
|
|||||||
|
.mat-radio-button {
|
||||||
|
margin-right: 10px;
|
||||||
|
margin-top: 5px;
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
||||||
|
|
||||||
|
import { ManageRoleComponent } from './manage-role.component';
|
||||||
|
|
||||||
|
describe('ManageRoleComponent', () => {
|
||||||
|
let component: ManageRoleComponent;
|
||||||
|
let fixture: ComponentFixture<ManageRoleComponent>;
|
||||||
|
|
||||||
|
beforeEach(async(() => {
|
||||||
|
TestBed.configureTestingModule({
|
||||||
|
declarations: [ ManageRoleComponent ]
|
||||||
|
})
|
||||||
|
.compileComponents();
|
||||||
|
}));
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
fixture = TestBed.createComponent(ManageRoleComponent);
|
||||||
|
component = fixture.componentInstance;
|
||||||
|
fixture.detectChanges();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should create', () => {
|
||||||
|
expect(component).toBeTruthy();
|
||||||
|
});
|
||||||
|
});
|
@ -0,0 +1,61 @@
|
|||||||
|
import { Component, OnInit, Inject } from '@angular/core';
|
||||||
|
import { MatDialogRef, MAT_DIALOG_DATA } from '@angular/material/dialog';
|
||||||
|
import { PostsService } from 'app/posts.services';
|
||||||
|
|
||||||
|
@Component({
|
||||||
|
selector: 'app-manage-role',
|
||||||
|
templateUrl: './manage-role.component.html',
|
||||||
|
styleUrls: ['./manage-role.component.scss']
|
||||||
|
})
|
||||||
|
export class ManageRoleComponent implements OnInit {
|
||||||
|
|
||||||
|
role = null;
|
||||||
|
available_permissions = null;
|
||||||
|
permissions = null;
|
||||||
|
|
||||||
|
permissionToLabel = {
|
||||||
|
'filemanager': 'File manager',
|
||||||
|
'settings': 'Settings access',
|
||||||
|
'subscriptions': 'Subscriptions',
|
||||||
|
'sharing': 'Share files',
|
||||||
|
'advanced_download': 'Use advanced download mode',
|
||||||
|
'downloads_manager': 'Use downloads manager'
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(public postsService: PostsService, private dialogRef: MatDialogRef<ManageRoleComponent>,
|
||||||
|
@Inject(MAT_DIALOG_DATA) public data: any) {
|
||||||
|
if (this.data) {
|
||||||
|
this.role = this.data.role;
|
||||||
|
this.available_permissions = this.postsService.available_permissions;
|
||||||
|
this.parsePermissions();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ngOnInit(): void {
|
||||||
|
}
|
||||||
|
|
||||||
|
parsePermissions() {
|
||||||
|
this.permissions = {};
|
||||||
|
for (let i = 0; i < this.available_permissions.length; i++) {
|
||||||
|
const permission = this.available_permissions[i];
|
||||||
|
if (this.role.permissions.includes(permission)) {
|
||||||
|
this.permissions[permission] = 'yes';
|
||||||
|
} else {
|
||||||
|
this.permissions[permission] = 'no';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
changeRolePermissions(change, permission) {
|
||||||
|
this.postsService.setRolePermission(this.role.name, permission, change.value).subscribe(res => {
|
||||||
|
if (res['success']) {
|
||||||
|
|
||||||
|
} else {
|
||||||
|
this.permissions[permission] = this.permissions[permission] === 'yes' ? 'no' : 'yes';
|
||||||
|
}
|
||||||
|
}, err => {
|
||||||
|
this.permissions[permission] = this.permissions[permission] === 'yes' ? 'no' : 'yes';
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
<h4 *ngIf="user" mat-dialog-title><ng-container i18n="Manage user dialog title">Manage user</ng-container> - {{user.name}}</h4>
|
||||||
|
|
||||||
|
<mat-dialog-content *ngIf="user">
|
||||||
|
<p><ng-container i18n="User UID">User UID:</ng-container> {{user.uid}}</p>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<mat-form-field style="margin-right: 15px;">
|
||||||
|
<input matInput [(ngModel)]="newPasswordInput" type="password" placeholder="New password" i18n-placeholder="New password placeholder">
|
||||||
|
</mat-form-field>
|
||||||
|
<button mat-raised-button color="accent" (click)="setNewPassword()" [disabled]="newPasswordInput.length === 0"><ng-container i18n="Set new password">Set new password</ng-container></button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div>
|
||||||
|
<mat-list>
|
||||||
|
<mat-list-item role="listitem" *ngFor="let permission of available_permissions">
|
||||||
|
<h3 matLine>{{permissionToLabel[permission] ? permissionToLabel[permission] : permission}}</h3>
|
||||||
|
<span matLine>
|
||||||
|
<mat-radio-group [disabled]="permission === 'settings' && postsService.user.uid === user.uid" (change)="changeUserPermissions($event, permission)" [(ngModel)]="permissions[permission]" [attr.aria-label]="'Give user permission for ' + permission">
|
||||||
|
<mat-radio-button value="default"><ng-container i18n="Use default">Use default</ng-container></mat-radio-button>
|
||||||
|
<mat-radio-button value="yes"><ng-container i18n="Yes">Yes</ng-container></mat-radio-button>
|
||||||
|
<mat-radio-button value="no"><ng-container i18n="No">No</ng-container></mat-radio-button>
|
||||||
|
</mat-radio-group>
|
||||||
|
</span>
|
||||||
|
</mat-list-item>
|
||||||
|
</mat-list>
|
||||||
|
</div>
|
||||||
|
</mat-dialog-content>
|
||||||
|
|
||||||
|
<mat-dialog-actions>
|
||||||
|
<button mat-button mat-dialog-close><ng-container i18n="Close">Close</ng-container></button>
|
||||||
|
</mat-dialog-actions>
|
@ -0,0 +1,4 @@
|
|||||||
|
.mat-radio-button {
|
||||||
|
margin-right: 10px;
|
||||||
|
margin-top: 5px;
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
||||||
|
|
||||||
|
import { ManageUserComponent } from './manage-user.component';
|
||||||
|
|
||||||
|
describe('ManageUserComponent', () => {
|
||||||
|
let component: ManageUserComponent;
|
||||||
|
let fixture: ComponentFixture<ManageUserComponent>;
|
||||||
|
|
||||||
|
beforeEach(async(() => {
|
||||||
|
TestBed.configureTestingModule({
|
||||||
|
declarations: [ ManageUserComponent ]
|
||||||
|
})
|
||||||
|
.compileComponents();
|
||||||
|
}));
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
fixture = TestBed.createComponent(ManageUserComponent);
|
||||||
|
component = fixture.componentInstance;
|
||||||
|
fixture.detectChanges();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should create', () => {
|
||||||
|
expect(component).toBeTruthy();
|
||||||
|
});
|
||||||
|
});
|
@ -0,0 +1,69 @@
|
|||||||
|
import { Component, OnInit, Inject } from '@angular/core';
|
||||||
|
import { PostsService } from 'app/posts.services';
|
||||||
|
import { MAT_DIALOG_DATA } from '@angular/material/dialog';
|
||||||
|
|
||||||
|
@Component({
|
||||||
|
selector: 'app-manage-user',
|
||||||
|
templateUrl: './manage-user.component.html',
|
||||||
|
styleUrls: ['./manage-user.component.scss']
|
||||||
|
})
|
||||||
|
export class ManageUserComponent implements OnInit {
|
||||||
|
|
||||||
|
user = null;
|
||||||
|
newPasswordInput = '';
|
||||||
|
available_permissions = null;
|
||||||
|
permissions = null;
|
||||||
|
|
||||||
|
permissionToLabel = {
|
||||||
|
'filemanager': 'File manager',
|
||||||
|
'settings': 'Settings access',
|
||||||
|
'subscriptions': 'Subscriptions',
|
||||||
|
'sharing': 'Share files',
|
||||||
|
'advanced_download': 'Use advanced download mode',
|
||||||
|
'downloads_manager': 'Use downloads manager'
|
||||||
|
}
|
||||||
|
|
||||||
|
settingNewPassword = false;
|
||||||
|
|
||||||
|
constructor(public postsService: PostsService, @Inject(MAT_DIALOG_DATA) public data: any) {
|
||||||
|
if (this.data) {
|
||||||
|
this.user = this.data.user;
|
||||||
|
this.available_permissions = this.postsService.available_permissions;
|
||||||
|
this.parsePermissions();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ngOnInit(): void {
|
||||||
|
}
|
||||||
|
|
||||||
|
parsePermissions() {
|
||||||
|
this.permissions = {};
|
||||||
|
for (let i = 0; i < this.available_permissions.length; i++) {
|
||||||
|
const permission = this.available_permissions[i];
|
||||||
|
if (this.user.permission_overrides.includes(permission)) {
|
||||||
|
if (this.user.permissions.includes(permission)) {
|
||||||
|
this.permissions[permission] = 'yes';
|
||||||
|
} else {
|
||||||
|
this.permissions[permission] = 'no';
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
this.permissions[permission] = 'default';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
changeUserPermissions(change, permission) {
|
||||||
|
this.postsService.setUserPermission(this.user.uid, permission, change.value).subscribe(res => {
|
||||||
|
// console.log(res);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
setNewPassword() {
|
||||||
|
this.settingNewPassword = true;
|
||||||
|
this.postsService.changeUserPassword(this.user.uid, this.newPasswordInput).subscribe(res => {
|
||||||
|
this.newPasswordInput = '';
|
||||||
|
this.settingNewPassword = false;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,107 @@
|
|||||||
|
<div *ngIf="dataSource; else loading">
|
||||||
|
<div style="padding: 15px">
|
||||||
|
<div class="row">
|
||||||
|
<div class="table table-responsive p-5">
|
||||||
|
<div class="example-header">
|
||||||
|
<mat-form-field>
|
||||||
|
<input matInput (keyup)="applyFilter($event.target.value)" placeholder="Search">
|
||||||
|
</mat-form-field>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="example-container mat-elevation-z8">
|
||||||
|
|
||||||
|
<mat-table #table [dataSource]="dataSource" matSort>
|
||||||
|
|
||||||
|
<!-- Name Column -->
|
||||||
|
<ng-container matColumnDef="name">
|
||||||
|
<mat-header-cell *matHeaderCellDef mat-sort-header><ng-container i18n="Username users table header"> User name </ng-container></mat-header-cell>
|
||||||
|
|
||||||
|
<mat-cell *matCellDef="let row">
|
||||||
|
<span *ngIf="editObject && editObject.uid === row.uid; else noteditingname">
|
||||||
|
<span style="width: 80%;">
|
||||||
|
<mat-form-field>
|
||||||
|
<input matInput [(ngModel)]="constructedObject['name']" type="text" style="font-size: 12px">
|
||||||
|
</mat-form-field>
|
||||||
|
</span>
|
||||||
|
</span>
|
||||||
|
<ng-template #noteditingname>
|
||||||
|
{{row.name}}
|
||||||
|
</ng-template>
|
||||||
|
</mat-cell>
|
||||||
|
</ng-container>
|
||||||
|
|
||||||
|
<!-- Email Column -->
|
||||||
|
<ng-container matColumnDef="role">
|
||||||
|
<mat-header-cell *matHeaderCellDef mat-sort-header><ng-container i18n="Role users table header"> Role </ng-container></mat-header-cell>
|
||||||
|
<mat-cell *matCellDef="let row">
|
||||||
|
<span *ngIf="editObject && editObject.uid === row.uid; else noteditingemail">
|
||||||
|
<span style="width: 80%;">
|
||||||
|
<mat-form-field>
|
||||||
|
<mat-select [(ngModel)]="constructedObject['role']">
|
||||||
|
<mat-option value="admin">Admin</mat-option>
|
||||||
|
<mat-option value="user">User</mat-option>
|
||||||
|
</mat-select>
|
||||||
|
</mat-form-field>
|
||||||
|
</span>
|
||||||
|
</span>
|
||||||
|
<ng-template #noteditingemail>
|
||||||
|
{{row.role}}
|
||||||
|
</ng-template>
|
||||||
|
</mat-cell>
|
||||||
|
</ng-container>
|
||||||
|
|
||||||
|
<!-- Actions Column -->
|
||||||
|
<ng-container matColumnDef="actions">
|
||||||
|
<mat-header-cell *matHeaderCellDef mat-sort-header><ng-container i18n="Actions users table header"> Actions </ng-container></mat-header-cell>
|
||||||
|
<mat-cell *matCellDef="let row">
|
||||||
|
<span *ngIf="editObject && editObject.uid === row.uid; else notediting">
|
||||||
|
<button mat-icon-button color="primary" (click)="finishEditing(row.uid)" matTooltip="Finish editing user">
|
||||||
|
<mat-icon>done</mat-icon>
|
||||||
|
</button>
|
||||||
|
<button mat-icon-button (click)="disableEditMode()" matTooltip="Cancel editing user">
|
||||||
|
<mat-icon>cancel</mat-icon>
|
||||||
|
</button>
|
||||||
|
</span>
|
||||||
|
<ng-template #notediting>
|
||||||
|
<button mat-icon-button (click)="enableEditMode(row.uid)" matTooltip="Edit user">
|
||||||
|
<mat-icon>edit</mat-icon>
|
||||||
|
</button>
|
||||||
|
</ng-template>
|
||||||
|
<button (click)="manageUser(row.uid)" mat-icon-button [disabled]="editObject && editObject.uid === row.uid" matTooltip="Manage user">
|
||||||
|
<mat-icon>settings</mat-icon>
|
||||||
|
</button>
|
||||||
|
<button mat-icon-button [disabled]="editObject && editObject.uid === row.uid || row.uid === postsService.user.uid" (click)="removeUser(row.uid)" matTooltip="Delete user">
|
||||||
|
<mat-icon>delete</mat-icon>
|
||||||
|
</button>
|
||||||
|
</mat-cell>
|
||||||
|
</ng-container>
|
||||||
|
|
||||||
|
<mat-header-row *matHeaderRowDef="displayedColumns"></mat-header-row>
|
||||||
|
<mat-row *matRowDef="let row; columns: displayedColumns;">
|
||||||
|
</mat-row>
|
||||||
|
</mat-table>
|
||||||
|
|
||||||
|
<mat-paginator #paginator [length]="length"
|
||||||
|
[pageSize]="pageSize"
|
||||||
|
[pageSizeOptions]="pageSizeOptions">
|
||||||
|
</mat-paginator>
|
||||||
|
|
||||||
|
<button color="primary" [disabled]="!this.users" mat-raised-button (click)="openAddUserDialog()" style="float: left; top: -45px; left: 15px">
|
||||||
|
<ng-container i18n="Add users button">Add Users</ng-container>
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<button color="primary" [matMenuTriggerFor]="edit_roles_menu" class="edit-role" mat-raised-button>Edit Role</button>
|
||||||
|
<mat-menu #edit_roles_menu="matMenu">
|
||||||
|
<button (click)="openModifyRole(role)" mat-menu-item *ngFor="let role of roles">{{role.name}}</button>
|
||||||
|
</mat-menu>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div style="position: absolute" class="centered">
|
||||||
|
<ng-template #loading>
|
||||||
|
<mat-spinner></mat-spinner>
|
||||||
|
</ng-template>
|
||||||
|
</div>
|
@ -0,0 +1,5 @@
|
|||||||
|
.edit-role {
|
||||||
|
position: relative;
|
||||||
|
top: -80px;
|
||||||
|
left: 35px;
|
||||||
|
}
|
@ -0,0 +1,25 @@
|
|||||||
|
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
||||||
|
|
||||||
|
import { ModifyUsersComponent } from './modify-users.component';
|
||||||
|
|
||||||
|
describe('ModifyUsersComponent', () => {
|
||||||
|
let component: ModifyUsersComponent;
|
||||||
|
let fixture: ComponentFixture<ModifyUsersComponent>;
|
||||||
|
|
||||||
|
beforeEach(async(() => {
|
||||||
|
TestBed.configureTestingModule({
|
||||||
|
declarations: [ ModifyUsersComponent ]
|
||||||
|
})
|
||||||
|
.compileComponents();
|
||||||
|
}));
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
fixture = TestBed.createComponent(ModifyUsersComponent);
|
||||||
|
component = fixture.componentInstance;
|
||||||
|
fixture.detectChanges();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should create', () => {
|
||||||
|
expect(component).toBeTruthy();
|
||||||
|
});
|
||||||
|
});
|
@ -0,0 +1,219 @@
|
|||||||
|
import { Component, OnInit, Input, ViewChild, AfterViewInit } from '@angular/core';
|
||||||
|
import { MatPaginator, PageEvent } from '@angular/material/paginator';
|
||||||
|
import { MatSort } from '@angular/material/sort';
|
||||||
|
import { MatTableDataSource } from '@angular/material/table';
|
||||||
|
import { PostsService } from 'app/posts.services';
|
||||||
|
import { MatSnackBar } from '@angular/material/snack-bar';
|
||||||
|
import { MatDialog, MatDialogRef } from '@angular/material/dialog';
|
||||||
|
import { AddUserDialogComponent } from 'app/dialogs/add-user-dialog/add-user-dialog.component';
|
||||||
|
import { ManageUserComponent } from '../manage-user/manage-user.component';
|
||||||
|
import { ManageRoleComponent } from '../manage-role/manage-role.component';
|
||||||
|
|
||||||
|
@Component({
|
||||||
|
selector: 'app-modify-users',
|
||||||
|
templateUrl: './modify-users.component.html',
|
||||||
|
styleUrls: ['./modify-users.component.scss']
|
||||||
|
})
|
||||||
|
export class ModifyUsersComponent implements OnInit, AfterViewInit {
|
||||||
|
|
||||||
|
displayedColumns = ['name', 'role', 'actions'];
|
||||||
|
dataSource = new MatTableDataSource();
|
||||||
|
|
||||||
|
deleteDialogContentSubstring = 'Are you sure you want delete user ';
|
||||||
|
|
||||||
|
@ViewChild(MatPaginator) paginator: MatPaginator;
|
||||||
|
@ViewChild(MatSort) sort: MatSort;
|
||||||
|
|
||||||
|
// MatPaginator Inputs
|
||||||
|
length = 100;
|
||||||
|
@Input() pageSize = 5;
|
||||||
|
pageSizeOptions: number[] = [5, 10, 25, 100];
|
||||||
|
|
||||||
|
// MatPaginator Output
|
||||||
|
pageEvent: PageEvent;
|
||||||
|
users: any;
|
||||||
|
editObject = null;
|
||||||
|
constructedObject = {};
|
||||||
|
roles = null;
|
||||||
|
|
||||||
|
|
||||||
|
constructor(public postsService: PostsService, public snackBar: MatSnackBar, public dialog: MatDialog,
|
||||||
|
private dialogRef: MatDialogRef<ModifyUsersComponent>) { }
|
||||||
|
|
||||||
|
ngOnInit() {
|
||||||
|
this.getArray();
|
||||||
|
this.getRoles();
|
||||||
|
}
|
||||||
|
|
||||||
|
ngAfterViewInit() {
|
||||||
|
this.dataSource.paginator = this.paginator;
|
||||||
|
this.dataSource.sort = this.sort;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the paginator and sort after the view init since this component will
|
||||||
|
* be able to query its view for the initialized paginator and sort.
|
||||||
|
*/
|
||||||
|
afterGetData() {
|
||||||
|
this.dataSource.sort = this.sort;
|
||||||
|
}
|
||||||
|
|
||||||
|
setPageSizeOptions(setPageSizeOptionsInput: string) {
|
||||||
|
this.pageSizeOptions = setPageSizeOptionsInput.split(',').map(str => +str);
|
||||||
|
}
|
||||||
|
|
||||||
|
applyFilter(filterValue: string) {
|
||||||
|
filterValue = filterValue.trim(); // Remove whitespace
|
||||||
|
filterValue = filterValue.toLowerCase(); // Datasource defaults to lowercase matches
|
||||||
|
this.dataSource.filter = filterValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
private getArray() {
|
||||||
|
this.postsService.getUsers().subscribe(res => {
|
||||||
|
this.users = res['users'];
|
||||||
|
this.createAndSortData();
|
||||||
|
this.afterGetData();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
getRoles() {
|
||||||
|
this.postsService.getRoles().subscribe(res => {
|
||||||
|
this.roles = [];
|
||||||
|
const roles = res['roles'];
|
||||||
|
const role_names = Object.keys(roles);
|
||||||
|
for (let i = 0; i < role_names.length; i++) {
|
||||||
|
const role_name = role_names[i];
|
||||||
|
this.roles.push({
|
||||||
|
name: role_name,
|
||||||
|
permissions: roles[role_name]['permissions']
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
openAddUserDialog() {
|
||||||
|
const dialogRef = this.dialog.open(AddUserDialogComponent);
|
||||||
|
dialogRef.afterClosed().subscribe(user => {
|
||||||
|
if (user && !user.error) {
|
||||||
|
this.openSnackBar('Successfully added user ' + user.name);
|
||||||
|
this.getArray();
|
||||||
|
} else if (user && user.error) {
|
||||||
|
this.openSnackBar('Failed to add user');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
finishEditing(user_uid) {
|
||||||
|
let has_finished = false;
|
||||||
|
if (this.constructedObject && this.constructedObject['name'] && this.constructedObject['role']) {
|
||||||
|
if (!isEmptyOrSpaces(this.constructedObject['name']) && !isEmptyOrSpaces(this.constructedObject['role'])) {
|
||||||
|
has_finished = true;
|
||||||
|
const index_of_object = this.indexOfUser(user_uid);
|
||||||
|
this.users[index_of_object] = this.constructedObject;
|
||||||
|
this.constructedObject = {};
|
||||||
|
this.editObject = null;
|
||||||
|
this.setUser(this.users[index_of_object]);
|
||||||
|
this.createAndSortData();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
enableEditMode(user_uid) {
|
||||||
|
if (this.uidInUserList(user_uid) && this.indexOfUser(user_uid) > -1) {
|
||||||
|
const users_index = this.indexOfUser(user_uid);
|
||||||
|
this.editObject = this.users[users_index];
|
||||||
|
this.constructedObject['name'] = this.users[users_index].name;
|
||||||
|
this.constructedObject['uid'] = this.users[users_index].uid;
|
||||||
|
this.constructedObject['role'] = this.users[users_index].role;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
disableEditMode() {
|
||||||
|
this.editObject = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// checks if user is in users array by name
|
||||||
|
uidInUserList(user_uid) {
|
||||||
|
for (let i = 0; i < this.users.length; i++) {
|
||||||
|
if (this.users[i].uid === user_uid) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// gets index of user in users array by name
|
||||||
|
indexOfUser(user_uid) {
|
||||||
|
for (let i = 0; i < this.users.length; i++) {
|
||||||
|
if (this.users[i].uid === user_uid) {
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
setUser(change_obj) {
|
||||||
|
this.postsService.changeUser(change_obj).subscribe(res => {
|
||||||
|
this.getArray();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
manageUser(user_uid) {
|
||||||
|
const index_of_object = this.indexOfUser(user_uid);
|
||||||
|
const user_obj = this.users[index_of_object];
|
||||||
|
this.dialog.open(ManageUserComponent, {
|
||||||
|
data: {
|
||||||
|
user: user_obj
|
||||||
|
},
|
||||||
|
width: '65vw'
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
removeUser(user_uid) {
|
||||||
|
this.postsService.deleteUser(user_uid).subscribe(res => {
|
||||||
|
this.getArray();
|
||||||
|
}, err => {
|
||||||
|
this.getArray();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
createAndSortData() {
|
||||||
|
// Sorts the data by last finished
|
||||||
|
this.users.sort((a, b) => b.name > a.name);
|
||||||
|
|
||||||
|
const filteredData = [];
|
||||||
|
for (let i = 0; i < this.users.length; i++) {
|
||||||
|
filteredData.push(JSON.parse(JSON.stringify(this.users[i])));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Assign the data to the data source for the table to render
|
||||||
|
this.dataSource.data = filteredData;
|
||||||
|
}
|
||||||
|
|
||||||
|
openModifyRole(role) {
|
||||||
|
const dialogRef = this.dialog.open(ManageRoleComponent, {
|
||||||
|
data: {
|
||||||
|
role: role
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
dialogRef.afterClosed().subscribe(success => {
|
||||||
|
this.getRoles();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
closeDialog() {
|
||||||
|
this.dialogRef.close();
|
||||||
|
}
|
||||||
|
|
||||||
|
public openSnackBar(message: string, action: string = '') {
|
||||||
|
this.snackBar.open(message, action, {
|
||||||
|
duration: 2000,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
function isEmptyOrSpaces(str){
|
||||||
|
return str === null || str.match(/^ *$/) !== null;
|
||||||
|
}
|
@ -0,0 +1,19 @@
|
|||||||
|
<h4 mat-dialog-title i18n="Register user dialog title">Register a user</h4>
|
||||||
|
|
||||||
|
<mat-dialog-content>
|
||||||
|
<div>
|
||||||
|
<mat-form-field>
|
||||||
|
<input matInput placeholder="User name" [(ngModel)]="usernameInput">
|
||||||
|
</mat-form-field>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<mat-form-field>
|
||||||
|
<input matInput placeholder="Password" [(ngModel)]="passwordInput" type="password">
|
||||||
|
</mat-form-field>
|
||||||
|
</div>
|
||||||
|
</mat-dialog-content>
|
||||||
|
|
||||||
|
<mat-dialog-actions>
|
||||||
|
<button color="accent" (click)="createUser()" mat-raised-button><ng-container i18n="Register user button">Register</ng-container></button>
|
||||||
|
<button mat-dialog-close mat-button><ng-container i18n="Close button">Close</ng-container></button>
|
||||||
|
</mat-dialog-actions>
|
@ -0,0 +1,25 @@
|
|||||||
|
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
|
||||||
|
|
||||||
|
import { AddUserDialogComponent } from './add-user-dialog.component';
|
||||||
|
|
||||||
|
describe('AddUserDialogComponent', () => {
|
||||||
|
let component: AddUserDialogComponent;
|
||||||
|
let fixture: ComponentFixture<AddUserDialogComponent>;
|
||||||
|
|
||||||
|
beforeEach(async(() => {
|
||||||
|
TestBed.configureTestingModule({
|
||||||
|
declarations: [ AddUserDialogComponent ]
|
||||||
|
})
|
||||||
|
.compileComponents();
|
||||||
|
}));
|
||||||
|
|
||||||
|
beforeEach(() => {
|
||||||
|
fixture = TestBed.createComponent(AddUserDialogComponent);
|
||||||
|
component = fixture.componentInstance;
|
||||||
|
fixture.detectChanges();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should create', () => {
|
||||||
|
expect(component).toBeTruthy();
|
||||||
|
});
|
||||||
|
});
|
@ -0,0 +1,32 @@
|
|||||||
|
import { Component, OnInit } from '@angular/core';
|
||||||
|
import { PostsService } from 'app/posts.services';
|
||||||
|
import { MatDialogRef } from '@angular/material/dialog';
|
||||||
|
|
||||||
|
@Component({
|
||||||
|
selector: 'app-add-user-dialog',
|
||||||
|
templateUrl: './add-user-dialog.component.html',
|
||||||
|
styleUrls: ['./add-user-dialog.component.scss']
|
||||||
|
})
|
||||||
|
export class AddUserDialogComponent implements OnInit {
|
||||||
|
|
||||||
|
usernameInput = '';
|
||||||
|
passwordInput = '';
|
||||||
|
|
||||||
|
constructor(private postsService: PostsService, public dialogRef: MatDialogRef<AddUserDialogComponent>) { }
|
||||||
|
|
||||||
|
ngOnInit(): void {
|
||||||
|
}
|
||||||
|
|
||||||
|
createUser() {
|
||||||
|
this.postsService.register(this.usernameInput, this.passwordInput).subscribe(res => {
|
||||||
|
if (res['user']) {
|
||||||
|
this.dialogRef.close(res['user']);
|
||||||
|
} else {
|
||||||
|
this.dialogRef.close({error: 'Unknown error'});
|
||||||
|
}
|
||||||
|
}, err => {
|
||||||
|
this.dialogRef.close({error: err});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue