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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 6x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 6x 15x 6x 29x 29x 4x 4x 25x 29x 4x 25x 25x 25x 6x 4x 4x 4x 4x 4x 4x 4x 4x 12x 15x 15x 16x 16x 22x 22x 22x 14x 30x 22x 16x 6x 3x 5x 3x 1x 2x 153x 153x 153x 30x 153x 243x 5x 10x 5x 5x 5x 2x 5x 2x 5x 3x 2x 2x 1x 5x 6x 2x 2x 2x 2x 2x 8x 16x 8x 8x 8x 3x 2x 6x 2x 2x 6x 2x 6x 1x 1x 1x 1x 6x 1x 1x 1x 1x 6x 2x 1x 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 } from 'ngx-bootstrap/modal'; import { forkJoin as observableForkJoin } from 'rxjs'; import { RoleService } from '../../../shared/api/role.service'; import { ScopeService } from '../../../shared/api/scope.service'; 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 { CdTableColumn } from '../../../shared/models/cd-table-column'; import { NotificationService } from '../../../shared/services/notification.service'; import { RoleFormMode } from './role-form-mode.enum'; import { RoleFormModel } from './role-form.model'; @Component({ selector: 'cd-role-form', template: require('./role-form.component.html'), styles: [] }) export class RoleFormComponent implements OnInit { @ViewChild('headerPermissionCheckboxTpl') headerPermissionCheckboxTpl: TemplateRef<any>; @ViewChild('cellScopeCheckboxTpl') cellScopeCheckboxTpl: TemplateRef<any>; @ViewChild('cellPermissionCheckboxTpl') cellPermissionCheckboxTpl: TemplateRef<any>; modalRef: BsModalRef; roleForm: CdFormGroup; response: RoleFormModel; columns: CdTableColumn[]; scopes: Array<string> = []; scopes_permissions: Array<any> = []; roleFormMode = RoleFormMode; mode: RoleFormMode; action: string; resource: string; constructor( private route: ActivatedRoute, private router: Router, private roleService: RoleService, private scopeService: ScopeService, private notificationService: NotificationService, private i18n: I18n, public actionLabels: ActionLabelsI18n ) { this.resource = this.i18n('role'); this.createForm(); this.listenToChanges(); } createForm() { this.roleForm = new CdFormGroup({ name: new FormControl('', { validators: [Validators.required], asyncValidators: [CdValidators.unique(this.roleService.exists, this.roleService)] }), description: new FormControl(''), scopes_permissions: new FormControl({}) }); } ngOnInit() { this.columns = [ { prop: 'scope', name: this.i18n('All'), flexGrow: 2, cellTemplate: this.cellScopeCheckboxTpl, headerTemplate: this.headerPermissionCheckboxTpl }, { prop: 'read', name: this.i18n('Read'), flexGrow: 1, cellClass: 'text-center', cellTemplate: this.cellPermissionCheckboxTpl, headerTemplate: this.headerPermissionCheckboxTpl }, { prop: 'create', name: this.i18n('Create'), flexGrow: 1, cellClass: 'text-center', cellTemplate: this.cellPermissionCheckboxTpl, headerTemplate: this.headerPermissionCheckboxTpl }, { prop: 'update', name: this.i18n('Update'), flexGrow: 1, cellClass: 'text-center', cellTemplate: this.cellPermissionCheckboxTpl, headerTemplate: this.headerPermissionCheckboxTpl }, { prop: 'delete', name: this.i18n('Delete'), flexGrow: 1, cellClass: 'text-center', cellTemplate: this.cellPermissionCheckboxTpl, headerTemplate: this.headerPermissionCheckboxTpl } ]; if (this.router.url.startsWith('/user-management/roles/edit')) { this.mode = this.roleFormMode.editing; this.action = this.actionLabels.EDIT; } else { this.action = this.actionLabels.CREATE; } if (this.mode === this.roleFormMode.editing) { this.initEdit(); } else { this.initCreate(); } } initCreate() { // Load the scopes and initialize the default scopes/permissions data. this.scopeService.list().subscribe((scopes: Array<string>) => { this.scopes = scopes; this.roleForm.get('scopes_permissions').setValue({}); }); } initEdit() { // Disable the 'Name' input field. this.roleForm.get('name').disable(); // Load the scopes and the role data. this.route.params.subscribe((params: { name: string }) => { const observables = []; observables.push(this.scopeService.list()); observables.push(this.roleService.get(params.name)); observableForkJoin(observables).subscribe((resp: any[]) => { this.scopes = resp[0]; ['name', 'description', 'scopes_permissions'].forEach((key) => this.roleForm.get(key).setValue(resp[1][key]) ); }); }); } listenToChanges() { // Create/Update the data which is used by the data table to display the // scopes/permissions every time the form field value has been changed. this.roleForm.get('scopes_permissions').valueChanges.subscribe((value) => { const scopes_permissions = []; _.each(this.scopes, (scope) => { // Set the defaults values. const scope_permission = { read: false, create: false, update: false, delete: false }; scope_permission['scope'] = scope; // Apply settings from the given value if they exist. if (scope in value) { _.each(value[scope], (permission) => { scope_permission[permission] = true; }); } scopes_permissions.push(scope_permission); }); this.scopes_permissions = scopes_permissions; }); } /** * Checks if the specified row checkbox needs to be rendered as checked. * @param {string} scope The scope to be checked, e.g. 'cephfs', 'grafana', * 'osd', 'pool' ... * @return Returns true if all permissions (read, create, update, delete) * are checked for the specified scope, otherwise false. */ isRowChecked(scope: string) { const scope_permission = _.find(this.scopes_permissions, (o) => { return o['scope'] === scope; }); if (_.isUndefined(scope_permission)) { return false; } return ( scope_permission['read'] && scope_permission['create'] && scope_permission['update'] && scope_permission['delete'] ); } /** * Checks if the specified header checkbox needs to be rendered as checked. * @param {string} property The property/permission (read, create, * update, delete) to be checked. If 'scope' is given, all permissions * are checked. * @return Returns true if specified property/permission is selected * for all scopes, otherwise false. */ isHeaderChecked(property: string) { let permissions = [property]; if ('scope' === property) { permissions = ['read', 'create', 'update', 'delete']; } return permissions.every((permission) => { return this.scopes_permissions.every((scope_permission) => { return scope_permission[permission]; }); }); } onClickCellCheckbox(scope: string, property: string, Eevent: Event = null) { // Use a copy of the form field data to do not trigger the redrawing of the // data table with every change. const scopes_permissions = _.cloneDeep(this.roleForm.getValue('scopes_permissions')); let permissions = [property]; if ('scope' === property) { permissions = ['read', 'create', 'update', 'delete']; } if (!(scope in scopes_permissions)) { scopes_permissions[scope] = []; } // Add or remove the given permission(s) depending on the click event or if no // click event is given then add/remove them if they are absent/exist. if ( (event && event.target['checked']) || !_.isEqual(permissions.sort(), _.intersection(scopes_permissions[scope], permissions).sort()) ) { scopes_permissions[scope] = _.union(scopes_permissions[scope], permissions); } else { scopes_permissions[scope] = _.difference(scopes_permissions[scope], permissions); if (_.isEmpty(scopes_permissions[scope])) { _.unset(scopes_permissions, scope); } } this.roleForm.get('scopes_permissions').setValue(scopes_permissions); } onClickHeaderCheckbox(property: 'scope' | 'read' | 'create' | 'update' | 'delete', event: Event) { // Use a copy of the form field data to do not trigger the redrawing of the // data table with every change. const scopes_permissions = _.cloneDeep(this.roleForm.getValue('scopes_permissions')); let permissions = [property]; Eif ('scope' === property) { permissions = ['read', 'create', 'update', 'delete']; } _.each(permissions, (permission) => { _.each(this.scopes, (scope) => { if (event.target['checked']) { scopes_permissions[scope] = _.union(scopes_permissions[scope], [permission]); } else { scopes_permissions[scope] = _.difference(scopes_permissions[scope], [permission]); if (_.isEmpty(scopes_permissions[scope])) { _.unset(scopes_permissions, scope); } } }); }); this.roleForm.get('scopes_permissions').setValue(scopes_permissions); } getRequest(): RoleFormModel { const roleFormModel = new RoleFormModel(); ['name', 'description', 'scopes_permissions'].forEach( (key) => (roleFormModel[key] = this.roleForm.get(key).value) ); return roleFormModel; } createAction() { const roleFormModel = this.getRequest(); this.roleService.create(roleFormModel).subscribe( () => { this.notificationService.show( NotificationType.success, this.i18n(`Created role '{{role_name}}'`, { role_name: roleFormModel.name }) ); this.router.navigate(['/user-management/roles']); }, () => { this.roleForm.setErrors({ cdSubmitButton: true }); } ); } editAction() { const roleFormModel = this.getRequest(); this.roleService.update(roleFormModel).subscribe( () => { this.notificationService.show( NotificationType.success, this.i18n(`Updated role '{{role_name}}'`, { role_name: roleFormModel.name }) ); this.router.navigate(['/user-management/roles']); }, () => { this.roleForm.setErrors({ cdSubmitButton: true }); } ); } submit() { if (this.mode === this.roleFormMode.editing) { this.editAction(); } else { this.createAction(); } } } |