1window.angular && (function(angular) {
2  'use strict';
3
4  angular.module('app.common.directives').directive('ldapUserRoles', [
5    'APIUtils',
6    function(APIUtils) {
7      return {
8        restrict: 'E',
9        template: require('./ldap-user-roles.html'),
10        scope: {roleGroups: '=', enabled: '=', roleGroupType: '='},
11        controller: [
12          '$scope', 'APIUtils', 'toastService', '$q',
13          function($scope, APIUtils, toastService, $q) {
14            $scope.privileges = [];
15            $scope.loading = true;
16            $scope.newGroup = {};
17            $scope.selectedGroupIndex = '';
18            $scope.editGroup = false;
19            $scope.removeGroup = false;
20            $scope.removeMultipleGroups = false;
21            $scope.all = false;
22            $scope.sortPropertyName = 'id';
23            $scope.reverse = false;
24            $scope.addGroup = false;
25            $scope.hasSelectedGroup = false;
26
27            APIUtils.getAccountServiceRoles()
28                .then(
29                    (data) => {
30                      $scope.privileges = data;
31                    },
32                    (error) => {
33                      console.log(JSON.stringify(error));
34                    })
35                .finally(() => {
36                  $scope.loading = false;
37                });
38
39            $scope.addGroupFn = () => {
40              $scope.addGroup = true;
41            };
42
43            $scope.addRoleGroup = () => {
44              $scope.loading = true;
45
46              const newGroup = {};
47              newGroup.RemoteGroup = $scope.newGroup.RemoteGroup;
48              newGroup.LocalRole = $scope.newGroup.LocalRole;
49
50              const data = {};
51              const roleGroups = $scope.roleGroups;
52              const roleGroupType = $scope.roleGroupType;
53              data[roleGroupType] = {};
54              data[roleGroupType]['RemoteRoleMapping'] = roleGroups;
55              data[roleGroupType]['RemoteRoleMapping'][roleGroups.length] =
56                  newGroup;
57
58              APIUtils.saveLdapProperties(data)
59                  .then(
60                      (response) => {
61                        toastService.success(
62                            'Group has been created successfully.');
63                      },
64                      (error) => {
65                        toastService.error('Failed to create new group.');
66                      })
67                  .finally(() => {
68                    $scope.loading = false;
69                  });
70            };
71
72            $scope.editGroupFn = (group, role, index) => {
73              $scope.editGroup = true;
74              $scope.selectedGroupIndex = index;
75              $scope.newGroup.RemoteGroup = group;
76              $scope.newGroup.LocalRole = role;
77            };
78
79            $scope.editRoleGroup = () => {
80              $scope.loading = true;
81              const data = {};
82              const roleGroupType = $scope.roleGroupType;
83              const roleGroups = $scope.roleGroups;
84              const localRole = $scope.newGroup.LocalRole;
85              const selectedIndex = $scope.selectedGroupIndex;
86              data[roleGroupType] = {};
87              data[roleGroupType]['RemoteRoleMapping'] = roleGroups;
88              data[roleGroupType]['RemoteRoleMapping'][selectedIndex]['LocalRole'] =
89                  localRole;
90
91              APIUtils.saveLdapProperties(data)
92                  .then(
93                      (response) => {
94                        toastService.success(
95                            'Group has been edited successfully.');
96                      },
97                      (error) => {
98                        toastService.error('Failed to edit group.');
99                      })
100                  .finally(() => {
101                    $scope.loading = false;
102                  });
103              $scope.editGroup = false;
104            };
105
106            $scope.removeGroupFn = index => {
107              $scope.removeGroup = true;
108              $scope.selectedGroupIndex = index;
109            };
110
111            $scope.removeRoleGroup = () => {
112              $scope.loading = true;
113              const roleGroupType = $scope.roleGroupType;
114              const roleGroups = $scope.roleGroups;
115              const selectedGroupIndex = $scope.selectedGroupIndex;
116              const data = {};
117              data[roleGroupType] = {};
118              data[roleGroupType]['RemoteRoleMapping'] = roleGroups;
119              data[roleGroupType]['RemoteRoleMapping'][selectedGroupIndex] =
120                  null;
121
122              APIUtils.saveLdapProperties(data)
123                  .then(
124                      (response) => {
125                        toastService.success(
126                            'Group has been removed successfully.');
127                      },
128                      (error) => {
129                        toastService.error('Failed to remove group.');
130                      })
131                  .finally(() => {
132                    $scope.loading = false;
133                    $scope.$parent.loadLdap();
134                  });
135              $scope.removeGroup = false;
136            };
137
138            $scope.removeMultipleRoleGroupsFn = () => {
139              $scope.removeMultipleGroups = true;
140            };
141
142            $scope.roleGroupIsSelectedChanged = () => {
143              let groupSelected = false;
144              $scope.roleGroups.forEach(group => {
145                if (group && group['isSelected']) {
146                  groupSelected = true;
147                }
148              });
149              $scope.hasSelectedGroup = groupSelected;
150            };
151
152            $scope.removeMultipleRoleGroups = () => {
153              $scope.loading = true;
154              const roleGroupType = $scope.roleGroupType;
155              const roleGroups = $scope.roleGroups;
156              const data = {};
157              data[roleGroupType] = {};
158              data[roleGroupType]['RemoteRoleMapping'] =
159                  roleGroups.map((group) => {
160                    if (group['isSelected']) {
161                      return null;
162                    } else {
163                      return group;
164                    }
165                  });
166
167              APIUtils.saveLdapProperties(data)
168                  .then(
169                      (response) => {
170                        toastService.success(
171                            'Groups have been removed successfully.');
172                      },
173                      (error) => {
174                        toastService.error('Failed to remove groups.');
175                      })
176                  .finally(() => {
177                    $scope.loading = false;
178                    $scope.$parent.loadLdap();
179                  });
180              $scope.removeMultipleGroups = false;
181            };
182
183            $scope.toggleAll = () => {
184              let toggleStatus = !$scope.all;
185              $scope.roleGroups.forEach((group) => {
186                group.isSelected = toggleStatus;
187              });
188            };
189
190            $scope.optionToggled = () => {
191              $scope.all = $scope.roleGroups.every((group) => {
192                return group.isSelected;
193              });
194            };
195
196            $scope.sortBy = (propertyName, isReverse) => {
197              $scope.reverse = isReverse;
198              $scope.sortPropertyName = propertyName;
199            };
200          }
201        ]
202      };
203    }
204  ]);
205})(window.angular);
206