rsanchez
2017-03-22 60c65f2110f65221bc3a71b2887667e78c53c53e
securis/src/main/webapp/src/app/forms/user.form.component.ts
....@@ -9,6 +9,7 @@
99 import { TdMediaService } from '@covalent/core';
1010 import { FormBase, IComboOption } from './base';
1111 import { ActivatedRoute, Router } from '@angular/router';
12
+import { OrganizationsService } from "../resources/organizations";
1213
1314 var user_example = {
1415 username: 'rym',
....@@ -27,10 +28,14 @@
2728 templateUrl: 'src/app/forms/user.form.html'
2829 })
2930 export class UserFormComponent extends FormBase {
30
-
31
+ allOrganizations: IComboOption[];
32
+ orgNames: string[] = [];
33
+ allRoles: any[] = [{"id":1, "code": "advance", "label":"Advance"}, {"id":2, "code": "admin","label":"Admin"}];
34
+ user_orgs: string[] = [];
35
+ user_roles: any = {};
3136 constructor(private http: Http,
3237 private users: UsersService,
33
- private applications: ApplicationsService,
38
+ private organizations: OrganizationsService,
3439 router: Router,
3540 toaster: ToastsManager,
3641 route: ActivatedRoute,
....@@ -39,16 +44,62 @@
3944 super($L, router, route, toaster, users, $L.get('user'), dialogs);
4045 }
4146
42
-
47
+ save() : void {
48
+ this.data.organizations_ids = [];
49
+ this.data.roles = [];
50
+ this.user_orgs.forEach(orgName => {
51
+ var selectedOrg = this.allOrganizations.find(org => org.label === orgName);
52
+ this.data.organizations_ids.push(selectedOrg.id);
53
+ });
54
+ this.user_roles.advance && this.data.roles.push(1);
55
+ this.user_roles.admin && this.data.roles.push(2);
56
+ super.save('username');
57
+ }
58
+
59
+ canBeDeleted() {
60
+ return this.data && this.data.username !== 'admin' && this.data.username !== '_client';
61
+ }
62
+
63
+ loadCombos() : void {
64
+ this.organizations.get()
65
+ .map(list => list.map((org : any) => <IComboOption>{id: org.id, label: org.name}))
66
+ .subscribe(
67
+ data => {
68
+ this.allOrganizations = (<IComboOption[]>data).sort((e1, e2) => e1.label.localeCompare(e2.label));
69
+ this.orgNames = this.allOrganizations.map(org => org.label);
70
+ this._loadOrgs();
71
+ },
72
+ err => console.error('Error loading organizations')
73
+ );
74
+ }
75
+
4376 goBack(): void {
4477 this.router.navigate([`users`]);
4578 }
46
-
79
+ _loadOrgs() {
80
+ if (this.data && this.data.organizations_ids && this.allOrganizations && this.allOrganizations.length > 0) {
81
+ this.data.organizations_ids.forEach((orgId : number) => {
82
+ var selectedOrg = this.allOrganizations.find(org => org.id === orgId);
83
+ this.user_orgs.push(selectedOrg.label);
84
+ });
85
+ }
86
+ }
4787 init() : void {
88
+ this.loadCombos();
89
+ this.user_orgs = [];
90
+ this.user_roles = {};
4891 super.setFirstFocus();
4992 super.reset();
5093 super.prepareInitialData('username', {
51
- metadata: []
94
+ organizations_ids: [],
95
+ roles: []
96
+ }, (data) => {
97
+ this._loadOrgs();
98
+ data.roles.forEach((roleId : number) => {
99
+ var selectedRole = this.allRoles.find(r => r.id === roleId);
100
+ this.user_roles[selectedRole.code] = true;
101
+ });
102
+
52103 });
53104 }
54105