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              const newGroup = {};
45              newGroup.RemoteGroup = $scope.newGroup.RemoteGroup;
46              newGroup.LocalRole = $scope.newGroup.LocalRole;
47
48              $scope.loading = true;
49              const data = {};
50
51              if ($scope.roleGroupType == 'ldap') {
52                data.LDAP = {};
53                data.LDAP.RemoteRoleMapping = $scope.roleGroups;
54                data.LDAP.RemoteRoleMapping.push(newGroup);
55              } else {
56                data.ActiveDirectory = {};
57                data.ActiveDirectory.RemoteRoleMapping = $scope.roleGroups;
58                data.ActiveDirectory.RemoteRoleMapping.push(newGroup);
59              }
60
61              APIUtils.saveLdapProperties(data)
62                  .then(
63                      (response) => {
64                        toastService.success(
65                            'Group has been created successfully.');
66                      },
67                      (error) => {
68                        toastService.error('Failed to create new group.');
69                      })
70                  .finally(() => {
71                    $scope.loading = false;
72                  });
73            };
74
75            $scope.editGroupFn = (group, role, index) => {
76              $scope.editGroup = true;
77              $scope.selectedGroupIndex = index;
78              $scope.newGroup.RemoteGroup = group;
79              $scope.newGroup.LocalRole = role;
80            };
81
82            $scope.editRoleGroup = () => {
83              $scope.loading = true;
84              const data = {};
85
86              if ($scope.roleGroupType == 'ldap') {
87                data.LDAP = {};
88                data.LDAP.RemoteRoleMapping = $scope.roleGroups;
89                data.LDAP.RemoteRoleMapping[$scope.selectedGroupIndex]
90                    .LocalRole = $scope.newGroup.LocalRole;
91              } else {
92                data.ActiveDirectory = {};
93                data.ActiveDirectory.RemoteRoleMapping = $scope.roleGroups;
94                data.ActiveDirectory
95                    .RemoteRoleMapping[$scope.selectedGroupIndex]
96                    .LocalRole = $scope.newGroup.LocalRole;
97              }
98
99              APIUtils.saveLdapProperties(data)
100                  .then(
101                      (response) => {
102                        toastService.success(
103                            'Group has been edited successfully.');
104                      },
105                      (error) => {
106                        toastService.error('Failed to edit group.');
107                      })
108                  .finally(() => {
109                    $scope.loading = false;
110                  });
111              $scope.editGroup = false;
112            };
113
114            $scope.removeGroupFn = (index) => {
115              $scope.removeGroup = true;
116              $scope.selectedGroupIndex = index;
117            };
118
119            $scope.removeRoleGroup = () => {
120              $scope.loading = true;
121              const data = {};
122
123              if ($scope.roleGroupType == 'ldap') {
124                data.LDAP = {};
125                data.LDAP.RemoteRoleMapping = $scope.roleGroups;
126                data.LDAP.RemoteRoleMapping[$scope.selectedGroupIndex] =
127                    $scope.newGroup;
128              } else {
129                data.ActiveDirectory = {};
130                data.ActiveDirectory.RemoteRoleMapping = $scope.roleGroups;
131                data.ActiveDirectory
132                    .RemoteRoleMapping[$scope.selectedGroupIndex] =
133                    $scope.newGroup;
134              }
135
136              $scope.roleGroups[$scope.selectedGroupIndex] = null;
137
138              APIUtils.saveLdapProperties(data)
139                  .then(
140                      (response) => {
141                        toastService.success(
142                            'Group has been removed successfully.');
143                      },
144                      (error) => {
145                        toastService.error('Failed to remove group.');
146                      })
147                  .finally(() => {
148                    $scope.loading = false;
149                    $scope.$parent.loadLdap();
150                  });
151              $scope.removeGroup = false;
152            };
153
154            $scope.removeMultipleRoleGroupsFn = () => {
155              $scope.removeMultipleGroups = true;
156            };
157
158            $scope.roleGroupIsSelectedChanged = () => {
159              let groupSelected = false;
160              $scope.roleGroups.forEach(group => {
161                if (group['isSelected']) {
162                  groupSelected = true;
163                }
164              });
165              $scope.hasSelectedGroup = groupSelected;
166            };
167
168            $scope.removeMultipleRoleGroups = () => {
169              $scope.loading = true;
170              const data = {};
171
172              if ($scope.roleGroupType == 'ldap') {
173                data.LDAP = {};
174                data.LDAP.RemoteRoleMapping = $scope.roleGroups.map((group) => {
175                  if (group['isSelected']) {
176                    return null;
177                  } else {
178                    return group;
179                  }
180                });
181              } else {
182                data.ActiveDirectory = {};
183                data.ActiveDirectory.RemoteRoleMapping =
184                    $scope.roleGroups.map((group) => {
185                      if (group['isSelected']) {
186                        return null;
187                      } else {
188                        return group;
189                      }
190                    });
191              }
192
193              APIUtils.saveLdapProperties(data)
194                  .then(
195                      (response) => {
196                        toastService.success(
197                            'Groups has been removed successfully.');
198                      },
199                      (error) => {
200                        toastService.error('Failed to remove groups.');
201                      })
202                  .finally(() => {
203                    $scope.loading = false;
204                    $scope.$parent.loadLdap();
205                  });
206              $scope.removeMultipleGroups = false;
207            };
208
209            $scope.toggleAll = () => {
210              let toggleStatus = !$scope.all;
211              $scope.roleGroups.forEach((group) => {
212                group.isSelected = toggleStatus;
213              });
214            };
215
216            $scope.optionToggled = () => {
217              $scope.all = $scope.roleGroups.every((group) => {
218                return group.isSelected;
219              });
220            };
221
222            $scope.sortBy = (propertyName, isReverse) => {
223              $scope.reverse = isReverse;
224              $scope.sortPropertyName = propertyName;
225            };
226          }
227        ]
228      };
229    }
230  ]);
231})(window.angular);
232