All files / src/app/superadmin/users users.component.ts

28.23% Statements 24/85
0% Branches 0/28
20.83% Functions 5/24
28.57% Lines 24/84

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225                                            1x 1x 1x 1x 1x 1x   1x 1x 1x   1x     1x 1x 1x 1x 1x 1x 1x   1x                             1x 1x                                                                                                                                                                                                                                                                                                                 1x 1x 1x 1x                
import { MatTableDataSource } from '@angular/material/table';
import { ViewChild, Component, OnInit } from '@angular/core';
import { MatDialog } from '@angular/material/dialog';
import { MatSnackBar } from '@angular/material/snack-bar';
import { MatSort } from '@angular/material/sort';
import { SelectionModel } from '@angular/cdk/collections';
import {
  ConfirmDialogComponent, ConfirmDialogData, MessageDialogComponent,
  MessageDialogData
} from '../../shared/shared.module';
import { IdRoleData, UserData } from '../superadmin.interfaces';
import {
  SuperadminPasswordRequestComponent
} from '../superadmin-password-request/superadmin-password-request.component';
import { BackendService } from '../backend.service';
import { NewUserComponent } from './newuser/new-user.component';
import { PasswordChangeService } from '../../shared/services/password-change/password-change.service';
 
@Component({
  templateUrl: './users.component.html',
  styleUrls: ['./users.component.css']
})
export class UsersComponent implements OnInit {
  objectsDatasource: MatTableDataSource<UserData> = new MatTableDataSource<UserData>();
  displayedColumns = ['name'];
  tableSelectionRow = new SelectionModel<UserData>(false, []);
  selectedUser = -1;
  selectedUserName = '';
 
  pendingWorkspaceChanges = false;
  workspacelistDatasource: MatTableDataSource<IdRoleData> = new MatTableDataSource<IdRoleData>();
  displayedWorkspaceColumns = ['selectCheckbox', 'label'];
 
  @ViewChild(MatSort) sort: MatSort = new MatSort();
 
  constructor(
    private bs: BackendService,
    private newuserDialog: MatDialog,
    private confirmDialog: MatDialog,
    private superadminPasswordDialog: MatDialog,
    private messsageDialog: MatDialog,
    private snackBar: MatSnackBar,
    private newpasswordService: PasswordChangeService
  ) {
    this.tableSelectionRow.changed.subscribe(
      r => {
        if (r.added.length > 0) {
          this.selectedUser = r.added[0].id;
          this.selectedUserName = r.added[0].name;
        } else {
          this.selectedUser = -1;
          this.selectedUserName = '';
        }
        this.updateWorkspaceList();
      }
    );
  }
 
  ngOnInit(): void {
    setTimeout(() => {
      this.updateObjectList();
    });
  }
 
  addObject(): void {
    const dialogRef = this.newuserDialog.open(NewUserComponent, {
      width: '600px'
    });
 
    dialogRef.afterClosed().subscribe(result => {
      Iif (typeof result !== 'undefined') {
        Iif (result !== false) {
          this.bs
            .addUser(result.get('name').value, result.get('pw').value)
            .subscribe(() => { this.updateObjectList(); });
        }
      }
    });
  }
 
  changeSuperadminStatus(): void {
    const selectedRows = this.tableSelectionRow.selected;
    Iif (selectedRows.length === 0) {
      this.messsageDialog.open(MessageDialogComponent, {
        width: '400px',
        data: <MessageDialogData>{
          title: 'Superadmin-Status ändern',
          content: 'Bitte markieren Sie erst eine Administrator:in!',
          type: 'error'
        }
      });
      return;
    }
 
    const userObject = <UserData>selectedRows[0];
    const passwdDialogRef = this.superadminPasswordDialog.open(SuperadminPasswordRequestComponent, {
      width: '600px',
      data: `Superadmin-Status ${userObject.isSuperadmin ? 'entziehen' : 'setzen'}`
    });
 
    passwdDialogRef.afterClosed().subscribe(afterClosedResult => {
      Iif (!afterClosedResult) {
        return;
      }
      this.bs.setSuperUserStatus(
        selectedRows[0].id,
        !userObject.isSuperadmin,
        afterClosedResult.get('pw').value
      )
        .subscribe(() => {
          this.snackBar.open('Status geändert', '', { duration: 1000 });
          this.updateObjectList();
        });
    });
  }
 
  changePassword(): void {
    const selectedRows = this.tableSelectionRow.selected;
    if (selectedRows.length === 0) {
      this.messsageDialog.open(MessageDialogComponent, {
        width: '400px',
        data: <MessageDialogData>{
          title: 'Kennwort ändern',
          content: 'Bitte markieren Sie erst eine Administrator:in!',
          type: 'error'
        }
      });
    } else {
      this.newpasswordService.showPasswordChangeDialog(selectedRows[0]).subscribe(errorCode => {
        Iif (!errorCode) {
          this.snackBar.open('Kennwort geändert', '', { duration: 3000 });
        }
      });
    }
  }
 
  deleteObject(): void {
    const selectedRows = this.tableSelectionRow.selected;
    if (selectedRows.length === 0) {
      this.messsageDialog.open(MessageDialogComponent, {
 
        width: '400px',
        data: <MessageDialogData>{
          title: 'Löschen von Administrator:innen',
          content: 'Bitte markieren Sie erst eine Administrator:in!',
          type: 'error'
        }
      });
    } else {
      let prompt;
      if (selectedRows.length > 1) {
        prompt = `Sollen ${selectedRows.length} Administrator:innen gelöscht werden?`;
      } else {
        prompt = `Soll Administrator:in "${selectedRows[0].name}" gelöscht werden?`;
      }
      const dialogRef = this.confirmDialog.open(ConfirmDialogComponent, {
        width: '400px',
        data: <ConfirmDialogData>{
          title: 'Löschen von Administrator:innen',
          content: prompt,
          confirmbuttonlabel: 'Administrator:in löschen',
          showcancel: true
        }
      });
 
      dialogRef.afterClosed().subscribe(result => {
        Iif (result !== false) {
          const usersToDelete: string[] = [];
          selectedRows.forEach((r: UserData) => usersToDelete.push(r.id.toString(10)));
          this.bs.deleteUsers(usersToDelete).subscribe(
            () => {
              this.snackBar.open('Administrator:in gelöscht', '', { duration: 1000 });
              this.updateObjectList();
            }
          );
        }
      });
    }
  }
 
  updateWorkspaceList(): void {
    this.pendingWorkspaceChanges = false;
    Iif (this.selectedUser > -1) {
      this.workspacelistDatasource = new MatTableDataSource<IdRoleData>();
      this.bs.getWorkspacesByUser(this.selectedUser)
        .subscribe(dataresponse => {
          this.workspacelistDatasource = new MatTableDataSource(dataresponse);
        });
    }
  }
 
  selectPermissions(user: IdRoleData, role: string): void {
    if (role === 'RW') {
      user.role = (user.role === 'RW') ? 'RO' : 'RW';
    } else Iif (role === 'RO') {
      user.role = (user.role === 'RO' || user.role === 'RW') ? '' : 'RO';
    }
    this.pendingWorkspaceChanges = true;
  }
 
  saveWorkspaces(): void {
    this.pendingWorkspaceChanges = false;
    if (this.selectedUser > -1) {
      this.bs.setWorkspacesByUser(this.selectedUser, this.workspacelistDatasource.data)
        .subscribe(() => {
          this.snackBar.open('Zugriffsrechte geändert', '', { duration: 1000 });
        });
    } else {
      this.workspacelistDatasource = new MatTableDataSource<IdRoleData>();
    }
  }
 
  updateObjectList(): void {
    this.tableSelectionRow.clear();
    this.bs.getUsers().subscribe(dataresponse => {
      this.objectsDatasource = new MatTableDataSource(dataresponse);
      this.objectsDatasource.sort = this.sort;
    });
  }
 
  selectRow(row: UserData): void {
    this.tableSelectionRow.select(row);
  }
}