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 226 227 228 229 230 231 232 233 234 235 | 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 13x 6x 13x 25x 25x 6x 6x 19x 25x 12x 36x 36x 25x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 24x 6x 3x 3x 15x 3x 6x 1x 1x 1x 1x 6x 3x 1x 1x 2x 6x 5x 6x 2x 2x 6x 3x 3x 6x 6x 6x 3x 3x 6x 6x 2x 2x 2x 1x 1x 1x 6x 4x 3x 1x 6x | import { Component, OnInit, TemplateRef, ViewChild } from '@angular/core'; import { FormControl, Validators } from '@angular/forms'; import { ActivatedRoute, Router } from '@angular/router'; import { I18n } from '@ngx-translate/i18n-polyfill'; import * as _ from 'lodash'; import { BsModalRef, BsModalService } from 'ngx-bootstrap/modal'; import { AuthService } from '../../../shared/api/auth.service'; import { RoleService } from '../../../shared/api/role.service'; import { UserService } from '../../../shared/api/user.service'; import { ConfirmationModalComponent } from '../../../shared/components/confirmation-modal/confirmation-modal.component'; import { SelectMessages } from '../../../shared/components/select/select-messages.model'; import { ActionLabelsI18n } from '../../../shared/constants/app.constants'; import { NotificationType } from '../../../shared/enum/notification-type.enum'; import { CdFormGroup } from '../../../shared/forms/cd-form-group'; import { CdValidators } from '../../../shared/forms/cd-validators'; import { AuthStorageService } from '../../../shared/services/auth-storage.service'; import { NotificationService } from '../../../shared/services/notification.service'; import { UserFormMode } from './user-form-mode.enum'; import { UserFormRoleModel } from './user-form-role.model'; import { UserFormModel } from './user-form.model'; @Component({ selector: 'cd-user-form', template: require('./user-form.component.html'), styles: [] }) export class UserFormComponent implements OnInit { @ViewChild('removeSelfUserReadUpdatePermissionTpl') removeSelfUserReadUpdatePermissionTpl: TemplateRef<any>; modalRef: BsModalRef; userForm: CdFormGroup; response: UserFormModel; userFormMode = UserFormMode; mode: UserFormMode; allRoles: Array<UserFormRoleModel>; messages = new SelectMessages({ empty: 'There are no roles.' }, this.i18n); action: string; resource: string; constructor( private authService: AuthService, private authStorageService: AuthStorageService, private route: ActivatedRoute, private router: Router, private modalService: BsModalService, private roleService: RoleService, private userService: UserService, private notificationService: NotificationService, private i18n: I18n, public actionLabels: ActionLabelsI18n ) { this.resource = this.i18n('user'); this.createForm(); this.messages = new SelectMessages({ empty: 'There are no roles.' }, this.i18n); } createForm() { this.userForm = new CdFormGroup( { username: new FormControl('', { validators: [Validators.required] }), name: new FormControl(''), password: new FormControl('', { validators: [] }), confirmpassword: new FormControl('', { updateOn: 'blur', validators: [] }), email: new FormControl('', { validators: [Validators.email] }), roles: new FormControl([]) }, { validators: [CdValidators.match('password', 'confirmpassword')] } ); } ngOnInit() { if (this.router.url.startsWith('/user-management/users/edit')) { this.mode = this.userFormMode.editing; this.action = this.actionLabels.EDIT; } else { this.action = this.actionLabels.CREATE; } this.roleService.list().subscribe((roles: Array<UserFormRoleModel>) => { this.allRoles = _.map(roles, (role) => { role.enabled = true; return role; }); }); if (this.mode === this.userFormMode.editing) { this.initEdit(); } } initEdit() { this.disableForEdit(); this.route.params.subscribe((params: { username: string }) => { const username = params.username; this.userService.get(username).subscribe((userFormModel: UserFormModel) => { this.response = _.cloneDeep(userFormModel); this.setResponse(userFormModel); }); }); } disableForEdit() { this.userForm.get('username').disable(); } setResponse(response: UserFormModel) { ['username', 'name', 'email', 'roles'].forEach((key) => this.userForm.get(key).setValue(response[key]) ); } getRequest(): UserFormModel { const userFormModel = new UserFormModel(); ['username', 'password', 'name', 'email', 'roles'].forEach( (key) => (userFormModel[key] = this.userForm.get(key).value) ); return userFormModel; } createAction() { const userFormModel = this.getRequest(); this.userService.create(userFormModel).subscribe( () => { this.notificationService.show( NotificationType.success, this.i18n('Created user "{{username}}"', { username: userFormModel.username }) ); this.router.navigate(['/user-management/users']); }, () => { this.userForm.setErrors({ cdSubmitButton: true }); } ); } editAction() { if (this.isUserRemovingNeededRolePermissions()) { const initialState = { titleText: this.i18n('Update user'), buttonText: this.i18n('Continue'), bodyTpl: this.removeSelfUserReadUpdatePermissionTpl, onSubmit: () => { this.modalRef.hide(); this.doEditAction(); }, onCancel: () => { this.userForm.setErrors({ cdSubmitButton: true }); this.userForm.get('roles').reset(this.userForm.get('roles').value); } }; this.modalRef = this.modalService.show(ConfirmationModalComponent, { initialState }); } else { this.doEditAction(); } } private isCurrentUser(): boolean { return this.authStorageService.getUsername() === this.userForm.getValue('username'); } private isUserChangingRoles(): boolean { const isCurrentUser = this.isCurrentUser(); return ( isCurrentUser && this.response && !_.isEqual(this.response.roles, this.userForm.getValue('roles')) ); } private isUserRemovingNeededRolePermissions(): boolean { const isCurrentUser = this.isCurrentUser(); return isCurrentUser && !this.hasUserReadUpdatePermissions(this.userForm.getValue('roles')); } private hasUserReadUpdatePermissions(Iroles: Array<string> = []) { for (const role of this.allRoles) { if (roles.indexOf(role.name) !== -1 && role.scopes_permissions['user']) { const userPermissions = role.scopes_permissions['user']; return ['read', 'update'].every((permission) => { return userPermissions.indexOf(permission) !== -1; }); } } return false; } private doEditAction() { const userFormModel = this.getRequest(); this.userService.update(userFormModel).subscribe( () => { if (this.isUserChangingRoles()) { this.authService.logout(() => { this.notificationService.show( NotificationType.info, this.i18n('You were automatically logged out because your roles have been changed.') ); }); } else { this.notificationService.show( NotificationType.success, this.i18n('Updated user "{{username}}"', { username: userFormModel.username }) ); this.router.navigate(['/user-management/users']); } }, () => { this.userForm.setErrors({ cdSubmitButton: true }); } ); } submit() { if (this.mode === this.userFormMode.editing) { this.editAction(); } else { this.createAction(); } } } |