|
| 1 | +/* |
| 2 | + * Copyright (c) 2015-2017 Red Hat, Inc. |
| 3 | + * All rights reserved. This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v1.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * http://www.eclipse.org/legal/epl-v10.html |
| 7 | + * |
| 8 | + * Contributors: |
| 9 | + * Red Hat, Inc. - initial API and implementation |
| 10 | + */ |
| 11 | +'use strict'; |
| 12 | +import {AdminsUserManagementCtrl} from '../user-management.controller'; |
| 13 | + |
| 14 | +/** |
| 15 | + * This class is handling the controller for the add user |
| 16 | + * @author Oleksii Orel |
| 17 | + */ |
| 18 | +export class AdminsAddUserController { |
| 19 | + private $mdDialog: ng.material.IDialogService; |
| 20 | + private lodash: any; |
| 21 | + private cheNotification: any; |
| 22 | + private cheUser: any; |
| 23 | + private callbackController: AdminsUserManagementCtrl; |
| 24 | + private newUserName: string; |
| 25 | + private newUserEmail: string; |
| 26 | + private newUserPassword: string; |
| 27 | + private organizations: Array<string>; |
| 28 | + private organization: string; |
| 29 | + private cheOrganization: che.api.ICheOrganization; |
| 30 | + private chePermissions: che.api.IChePermissions; |
| 31 | + private organizationRoles: che.resource.ICheOrganizationRoles; |
| 32 | + |
| 33 | + /** |
| 34 | + * Default constructor. |
| 35 | + * @ngInject for Dependency injection |
| 36 | + */ |
| 37 | + constructor($mdDialog: ng.material.IDialogService, |
| 38 | + cheUser: any, |
| 39 | + cheNotification: any, |
| 40 | + lodash: any, |
| 41 | + cheOrganization: che.api.ICheOrganization, |
| 42 | + chePermissions: che.api.IChePermissions, |
| 43 | + resourcesService: che.service.IResourcesService) { |
| 44 | + this.$mdDialog = $mdDialog; |
| 45 | + this.lodash = lodash; |
| 46 | + this.cheUser = cheUser; |
| 47 | + this.cheNotification = cheNotification; |
| 48 | + this.cheOrganization = cheOrganization; |
| 49 | + this.chePermissions = chePermissions; |
| 50 | + this.organizationRoles = resourcesService.getOrganizationRoles(); |
| 51 | + |
| 52 | + this.organizations = []; |
| 53 | + |
| 54 | + this.cheOrganization.fetchOrganizations().then(() => { |
| 55 | + let organizations = this.cheOrganization.getOrganizations(); |
| 56 | + let rootOrganizations = organizations.filter((organization: any) => { |
| 57 | + return !organization.parent; |
| 58 | + }); |
| 59 | + this.organizations = lodash.pluck(rootOrganizations, 'name'); |
| 60 | + if (this.organizations.length > 0) { |
| 61 | + this.organization = this.organizations[0]; |
| 62 | + } |
| 63 | + }); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Callback of the cancel button of the dialog. |
| 68 | + */ |
| 69 | + abort(): void { |
| 70 | + this.$mdDialog.hide(); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Callback of the add button of the dialog(create new user). |
| 75 | + */ |
| 76 | + createUser(): void { |
| 77 | + let promise = this.cheUser.createUser(this.newUserName, this.newUserEmail, this.newUserPassword); |
| 78 | + |
| 79 | + promise.then((data: any) => { |
| 80 | + if (this.organization) { |
| 81 | + this.addUserToOrganization(data.id); |
| 82 | + } else { |
| 83 | + this.finish(); |
| 84 | + } |
| 85 | + }, (error: any) => { |
| 86 | + this.cheNotification.showError(error.data.message ? error.data.message : 'Failed to create user.'); |
| 87 | + }); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Finish user creation. |
| 92 | + */ |
| 93 | + private finish(): void { |
| 94 | + this.$mdDialog.hide(); |
| 95 | + this.callbackController.updateUsers(); |
| 96 | + this.cheNotification.showInfo('User successfully created.'); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Adds user to chosen organization. |
| 101 | + * |
| 102 | + * @param userId |
| 103 | + */ |
| 104 | + private addUserToOrganization(userId: string): void { |
| 105 | + let organizations = this.cheOrganization.getOrganizations(); |
| 106 | + let organization = this.lodash.find(organizations, (organization: any) => { |
| 107 | + return organization.name === this.organization; |
| 108 | + }); |
| 109 | + |
| 110 | + let actions = this.organizationRoles.MEMBER.actions; |
| 111 | + let permissions = { |
| 112 | + instanceId: organization.id, |
| 113 | + userId: userId, |
| 114 | + domainId: 'organization', |
| 115 | + actions: actions |
| 116 | + }; |
| 117 | + this.chePermissions.storePermissions(permissions).then(() => { |
| 118 | + this.finish(); |
| 119 | + }, (error: any) => { |
| 120 | + this.cheNotification.showError(error.data.message ? error.data.message : 'Failed to add user to organization' + this.organization + '.'); |
| 121 | + }); |
| 122 | + } |
| 123 | +} |
0 commit comments