xref: /openbmc/phosphor-webui/app/common/directives/ldap-user-roles.js (revision 7bdb91d4c4cd456ada08fc1dc684ff7aefa1ffa8)
15e258e43Sbeccabroekwindow.angular && (function(angular) {
25e258e43Sbeccabroek  'use strict';
35e258e43Sbeccabroek
45e258e43Sbeccabroek  angular.module('app.common.directives').directive('ldapUserRoles', [
55e258e43Sbeccabroek    'APIUtils',
65e258e43Sbeccabroek    function(APIUtils) {
75e258e43Sbeccabroek      return {
85e258e43Sbeccabroek        restrict: 'E',
95e258e43Sbeccabroek        template: require('./ldap-user-roles.html'),
105e258e43Sbeccabroek        scope: {roleGroups: '=', enabled: '=', roleGroupType: '='},
115e258e43Sbeccabroek        controller: [
125e258e43Sbeccabroek          '$scope', 'APIUtils', 'toastService', '$q',
135e258e43Sbeccabroek          function($scope, APIUtils, toastService, $q) {
145e258e43Sbeccabroek            $scope.privileges = [];
155e258e43Sbeccabroek            $scope.loading = true;
165e258e43Sbeccabroek            $scope.newGroup = {};
175e258e43Sbeccabroek            $scope.selectedGroupIndex = '';
185e258e43Sbeccabroek            $scope.editGroup = false;
195e258e43Sbeccabroek            $scope.removeGroup = false;
205e258e43Sbeccabroek            $scope.removeMultipleGroups = false;
215e258e43Sbeccabroek            $scope.all = false;
225e258e43Sbeccabroek            $scope.sortPropertyName = 'id';
235e258e43Sbeccabroek            $scope.reverse = false;
245e258e43Sbeccabroek            $scope.addGroup = false;
255e258e43Sbeccabroek            $scope.hasSelectedGroup = false;
265e258e43Sbeccabroek
275e258e43Sbeccabroek            APIUtils.getAccountServiceRoles()
285e258e43Sbeccabroek                .then(
295e258e43Sbeccabroek                    (data) => {
305e258e43Sbeccabroek                      $scope.privileges = data;
315e258e43Sbeccabroek                    },
325e258e43Sbeccabroek                    (error) => {
335e258e43Sbeccabroek                      console.log(JSON.stringify(error));
345e258e43Sbeccabroek                    })
355e258e43Sbeccabroek                .finally(() => {
365e258e43Sbeccabroek                  $scope.loading = false;
375e258e43Sbeccabroek                });
385e258e43Sbeccabroek
395e258e43Sbeccabroek            $scope.addGroupFn = () => {
405e258e43Sbeccabroek              $scope.addGroup = true;
415e258e43Sbeccabroek            };
425e258e43Sbeccabroek
435e258e43Sbeccabroek            $scope.addRoleGroup = () => {
44*7bdb91d4SDerick Montague              $scope.loading = true;
45*7bdb91d4SDerick Montague
465e258e43Sbeccabroek              const newGroup = {};
475e258e43Sbeccabroek              newGroup.RemoteGroup = $scope.newGroup.RemoteGroup;
485e258e43Sbeccabroek              newGroup.LocalRole = $scope.newGroup.LocalRole;
495e258e43Sbeccabroek
505e258e43Sbeccabroek              const data = {};
51*7bdb91d4SDerick Montague              const roleGroups = $scope.roleGroups;
52*7bdb91d4SDerick Montague              const roleGroupType = $scope.roleGroupType;
53*7bdb91d4SDerick Montague              data[roleGroupType] = {};
54*7bdb91d4SDerick Montague              data[roleGroupType]['RemoteRoleMapping'] = roleGroups;
55*7bdb91d4SDerick Montague              data[roleGroupType]['RemoteRoleMapping'][roleGroups.length] =
56*7bdb91d4SDerick Montague                  newGroup;
575e258e43Sbeccabroek
585e258e43Sbeccabroek              APIUtils.saveLdapProperties(data)
595e258e43Sbeccabroek                  .then(
605e258e43Sbeccabroek                      (response) => {
615e258e43Sbeccabroek                        toastService.success(
625e258e43Sbeccabroek                            'Group has been created successfully.');
635e258e43Sbeccabroek                      },
645e258e43Sbeccabroek                      (error) => {
655e258e43Sbeccabroek                        toastService.error('Failed to create new group.');
665e258e43Sbeccabroek                      })
675e258e43Sbeccabroek                  .finally(() => {
685e258e43Sbeccabroek                    $scope.loading = false;
695e258e43Sbeccabroek                  });
705e258e43Sbeccabroek            };
715e258e43Sbeccabroek
725e258e43Sbeccabroek            $scope.editGroupFn = (group, role, index) => {
735e258e43Sbeccabroek              $scope.editGroup = true;
745e258e43Sbeccabroek              $scope.selectedGroupIndex = index;
755e258e43Sbeccabroek              $scope.newGroup.RemoteGroup = group;
765e258e43Sbeccabroek              $scope.newGroup.LocalRole = role;
775e258e43Sbeccabroek            };
785e258e43Sbeccabroek
795e258e43Sbeccabroek            $scope.editRoleGroup = () => {
805e258e43Sbeccabroek              $scope.loading = true;
815e258e43Sbeccabroek              const data = {};
82*7bdb91d4SDerick Montague              const roleGroupType = $scope.roleGroupType;
83*7bdb91d4SDerick Montague              const roleGroups = $scope.roleGroups;
84*7bdb91d4SDerick Montague              const localRole = $scope.newGroup.LocalRole;
85*7bdb91d4SDerick Montague              const selectedIndex = $scope.selectedGroupIndex;
86*7bdb91d4SDerick Montague              data[roleGroupType] = {};
87*7bdb91d4SDerick Montague              data[roleGroupType]['RemoteRoleMapping'] = roleGroups;
88*7bdb91d4SDerick Montague              data[roleGroupType]['RemoteRoleMapping'][selectedIndex]['LocalRole'] =
89*7bdb91d4SDerick Montague                  localRole;
905e258e43Sbeccabroek
915e258e43Sbeccabroek              APIUtils.saveLdapProperties(data)
925e258e43Sbeccabroek                  .then(
935e258e43Sbeccabroek                      (response) => {
945e258e43Sbeccabroek                        toastService.success(
955e258e43Sbeccabroek                            'Group has been edited successfully.');
965e258e43Sbeccabroek                      },
975e258e43Sbeccabroek                      (error) => {
985e258e43Sbeccabroek                        toastService.error('Failed to edit group.');
995e258e43Sbeccabroek                      })
1005e258e43Sbeccabroek                  .finally(() => {
1015e258e43Sbeccabroek                    $scope.loading = false;
1025e258e43Sbeccabroek                  });
1035e258e43Sbeccabroek              $scope.editGroup = false;
1045e258e43Sbeccabroek            };
1055e258e43Sbeccabroek
106*7bdb91d4SDerick Montague            $scope.removeGroupFn = index => {
1075e258e43Sbeccabroek              $scope.removeGroup = true;
1085e258e43Sbeccabroek              $scope.selectedGroupIndex = index;
1095e258e43Sbeccabroek            };
1105e258e43Sbeccabroek
1115e258e43Sbeccabroek            $scope.removeRoleGroup = () => {
1125e258e43Sbeccabroek              $scope.loading = true;
113*7bdb91d4SDerick Montague              const roleGroupType = $scope.roleGroupType;
114*7bdb91d4SDerick Montague              const roleGroups = $scope.roleGroups;
115*7bdb91d4SDerick Montague              const selectedGroupIndex = $scope.selectedGroupIndex;
1165e258e43Sbeccabroek              const data = {};
117*7bdb91d4SDerick Montague              data[roleGroupType] = {};
118*7bdb91d4SDerick Montague              data[roleGroupType]['RemoteRoleMapping'] = roleGroups;
119*7bdb91d4SDerick Montague              data[roleGroupType]['RemoteRoleMapping'][selectedGroupIndex] =
120*7bdb91d4SDerick Montague                  null;
1215e258e43Sbeccabroek
1225e258e43Sbeccabroek              APIUtils.saveLdapProperties(data)
1235e258e43Sbeccabroek                  .then(
1245e258e43Sbeccabroek                      (response) => {
1255e258e43Sbeccabroek                        toastService.success(
1265e258e43Sbeccabroek                            'Group has been removed successfully.');
1275e258e43Sbeccabroek                      },
1285e258e43Sbeccabroek                      (error) => {
1295e258e43Sbeccabroek                        toastService.error('Failed to remove group.');
1305e258e43Sbeccabroek                      })
1315e258e43Sbeccabroek                  .finally(() => {
1325e258e43Sbeccabroek                    $scope.loading = false;
1335e258e43Sbeccabroek                    $scope.$parent.loadLdap();
1345e258e43Sbeccabroek                  });
1355e258e43Sbeccabroek              $scope.removeGroup = false;
1365e258e43Sbeccabroek            };
1375e258e43Sbeccabroek
1385e258e43Sbeccabroek            $scope.removeMultipleRoleGroupsFn = () => {
1395e258e43Sbeccabroek              $scope.removeMultipleGroups = true;
1405e258e43Sbeccabroek            };
1415e258e43Sbeccabroek
1425e258e43Sbeccabroek            $scope.roleGroupIsSelectedChanged = () => {
1435e258e43Sbeccabroek              let groupSelected = false;
1445e258e43Sbeccabroek              $scope.roleGroups.forEach(group => {
145*7bdb91d4SDerick Montague                if (group && group['isSelected']) {
1465e258e43Sbeccabroek                  groupSelected = true;
1475e258e43Sbeccabroek                }
1485e258e43Sbeccabroek              });
1495e258e43Sbeccabroek              $scope.hasSelectedGroup = groupSelected;
1505e258e43Sbeccabroek            };
1515e258e43Sbeccabroek
1525e258e43Sbeccabroek            $scope.removeMultipleRoleGroups = () => {
1535e258e43Sbeccabroek              $scope.loading = true;
154*7bdb91d4SDerick Montague              const roleGroupType = $scope.roleGroupType;
155*7bdb91d4SDerick Montague              const roleGroups = $scope.roleGroups;
1565e258e43Sbeccabroek              const data = {};
157*7bdb91d4SDerick Montague              data[roleGroupType] = {};
158*7bdb91d4SDerick Montague              data[roleGroupType]['RemoteRoleMapping'] =
159*7bdb91d4SDerick Montague                  roleGroups.map((group) => {
1605e258e43Sbeccabroek                    if (group['isSelected']) {
1615e258e43Sbeccabroek                      return null;
1625e258e43Sbeccabroek                    } else {
1635e258e43Sbeccabroek                      return group;
1645e258e43Sbeccabroek                    }
1655e258e43Sbeccabroek                  });
1665e258e43Sbeccabroek
1675e258e43Sbeccabroek              APIUtils.saveLdapProperties(data)
1685e258e43Sbeccabroek                  .then(
1695e258e43Sbeccabroek                      (response) => {
1705e258e43Sbeccabroek                        toastService.success(
17128e9ed8bSDixsie Wolmers                            'Groups have been removed successfully.');
1725e258e43Sbeccabroek                      },
1735e258e43Sbeccabroek                      (error) => {
1745e258e43Sbeccabroek                        toastService.error('Failed to remove groups.');
1755e258e43Sbeccabroek                      })
1765e258e43Sbeccabroek                  .finally(() => {
1775e258e43Sbeccabroek                    $scope.loading = false;
1785e258e43Sbeccabroek                    $scope.$parent.loadLdap();
1795e258e43Sbeccabroek                  });
1805e258e43Sbeccabroek              $scope.removeMultipleGroups = false;
1815e258e43Sbeccabroek            };
1825e258e43Sbeccabroek
1835e258e43Sbeccabroek            $scope.toggleAll = () => {
1845e258e43Sbeccabroek              let toggleStatus = !$scope.all;
1855e258e43Sbeccabroek              $scope.roleGroups.forEach((group) => {
1865e258e43Sbeccabroek                group.isSelected = toggleStatus;
1875e258e43Sbeccabroek              });
1885e258e43Sbeccabroek            };
1895e258e43Sbeccabroek
1905e258e43Sbeccabroek            $scope.optionToggled = () => {
1915e258e43Sbeccabroek              $scope.all = $scope.roleGroups.every((group) => {
1925e258e43Sbeccabroek                return group.isSelected;
1935e258e43Sbeccabroek              });
1945e258e43Sbeccabroek            };
1955e258e43Sbeccabroek
1965e258e43Sbeccabroek            $scope.sortBy = (propertyName, isReverse) => {
1975e258e43Sbeccabroek              $scope.reverse = isReverse;
1985e258e43Sbeccabroek              $scope.sortPropertyName = propertyName;
1995e258e43Sbeccabroek            };
2005e258e43Sbeccabroek          }
2015e258e43Sbeccabroek        ]
2025e258e43Sbeccabroek      };
2035e258e43Sbeccabroek    }
2045e258e43Sbeccabroek  ]);
2055e258e43Sbeccabroek})(window.angular);
206