diff --git a/src/app/organization/organizationCollectionsAddController.js b/src/app/organization/organizationCollectionsAddController.js index d58efe7011..6ac9e00ad1 100644 --- a/src/app/organization/organizationCollectionsAddController.js +++ b/src/app/organization/organizationCollectionsAddController.js @@ -2,11 +2,85 @@ .module('bit.organization') .controller('organizationCollectionsAddController', function ($scope, $state, $uibModalInstance, apiService, cipherService, - $analytics) { + $analytics, authService) { $analytics.eventTrack('organizationCollectionsAddController', { category: 'Modal' }); + $scope.groups = []; + $scope.selectedGroups = {}; + $scope.loading = true; + $scope.useGroups = false; + + $uibModalInstance.opened.then(function () { + return authService.getUserProfile(); + }).then(function (profile) { + if (profile.organizations) { + var org = profile.organizations[$state.params.orgId]; + $scope.useGroups = !!org.useGroups; + } + + if ($scope.useGroups) { + return apiService.groups.listOrganization({ orgId: $state.params.orgId }).$promise; + } + + return null; + }).then(function (groups) { + if (!groups) { + $scope.loading = false; + return; + } + + var groupsArr = []; + for (var i = 0; i < groups.Data.length; i++) { + groupsArr.push({ + id: groups.Data[i].Id, + name: groups.Data[i].Name + }); + } + + $scope.groups = groupsArr; + $scope.loading = false; + }); + + $scope.toggleGroupSelectionAll = function ($event) { + var groups = {}; + if ($event.target.checked) { + for (var i = 0; i < $scope.groups.length; i++) { + groups[$scope.groups[i].id] = true; + } + } + + $scope.selectedGroups = groups; + }; + + $scope.toggleGroupSelection = function (id) { + if (id in $scope.selectedGroups) { + delete $scope.selectedGroups[id]; + } + else { + $scope.selectedGroups[id] = true; + } + }; + + $scope.groupSelected = function (group) { + return group.id in $scope.selectedGroups; + }; + + $scope.allSelected = function () { + return Object.keys($scope.selectedGroups).length === $scope.groups.length; + }; $scope.submit = function (model) { var collection = cipherService.encryptCollection(model, $state.params.orgId); + + if ($scope.useGroups) { + collection.groupIds = []; + + for (var id in $scope.selectedGroups) { + if ($scope.selectedGroups.hasOwnProperty(id)) { + collection.groupIds.push(id); + } + } + } + $scope.submitPromise = apiService.collections.post({ orgId: $state.params.orgId }, collection, function (response) { $analytics.eventTrack('Created Collection'); var decCollection = cipherService.decryptCollection(response, $state.params.orgId, true); diff --git a/src/app/organization/organizationCollectionsEditController.js b/src/app/organization/organizationCollectionsEditController.js index 80ba1eabcd..af01c3a3fb 100644 --- a/src/app/organization/organizationCollectionsEditController.js +++ b/src/app/organization/organizationCollectionsEditController.js @@ -2,18 +2,98 @@ .module('bit.organization') .controller('organizationCollectionsEditController', function ($scope, $state, $uibModalInstance, apiService, cipherService, - $analytics, id) { + $analytics, id, authService) { $analytics.eventTrack('organizationCollectionsEditController', { category: 'Modal' }); $scope.collection = {}; + $scope.groups = []; + $scope.selectedGroups = {}; + $scope.loading = true; + $scope.useGroups = false; $uibModalInstance.opened.then(function () { - apiService.collections.get({ orgId: $state.params.orgId, id: id }, function (collection) { - $scope.collection = cipherService.decryptCollection(collection); - }); + return apiService.collections.getDetails({ orgId: $state.params.orgId, id: id }).$promise; + }).then(function (collection) { + $scope.collection = cipherService.decryptCollection(collection); + + var groups = {}; + if (collection.GroupIds) { + for (var i = 0; i < collection.GroupIds.length; i++) { + groups[collection.GroupIds[i]] = true; + } + } + $scope.selectedGroups = groups; + + return authService.getUserProfile(); + }).then(function (profile) { + if (profile.organizations) { + var org = profile.organizations[$state.params.orgId]; + $scope.useGroups = !!org.useGroups; + } + + if ($scope.useGroups) { + return apiService.groups.listOrganization({ orgId: $state.params.orgId }).$promise; + } + + return null; + }).then(function (groups) { + if (!groups) { + $scope.loading = false; + return; + } + + var groupsArr = []; + for (var i = 0; i < groups.Data.length; i++) { + groupsArr.push({ + id: groups.Data[i].Id, + name: groups.Data[i].Name + }); + } + + $scope.groups = groupsArr; + $scope.loading = false; }); + $scope.toggleGroupSelectionAll = function ($event) { + var groups = {}; + if ($event.target.checked) { + for (var i = 0; i < $scope.groups.length; i++) { + groups[$scope.groups[i].id] = true; + } + } + + $scope.selectedGroups = groups; + }; + + $scope.toggleGroupSelection = function (id) { + if (id in $scope.selectedGroups) { + delete $scope.selectedGroups[id]; + } + else { + $scope.selectedGroups[id] = true; + } + }; + + $scope.groupSelected = function (group) { + return group.id in $scope.selectedGroups; + }; + + $scope.allSelected = function () { + return Object.keys($scope.selectedGroups).length === $scope.groups.length; + }; + $scope.submit = function (model) { var collection = cipherService.encryptCollection(model, $state.params.orgId); + + if ($scope.useGroups) { + collection.groupIds = []; + + for (var id in $scope.selectedGroups) { + if ($scope.selectedGroups.hasOwnProperty(id)) { + collection.groupIds.push(id); + } + } + } + $scope.submitPromise = apiService.collections.put({ orgId: $state.params.orgId }, collection, function (response) { $analytics.eventTrack('Edited Collection'); var decCollection = cipherService.decryptCollection(response, $state.params.orgId, true); diff --git a/src/app/organization/views/organizationCollectionsAdd.html b/src/app/organization/views/organizationCollectionsAdd.html index 38799c4524..f749680169 100644 --- a/src/app/organization/views/organizationCollectionsAdd.html +++ b/src/app/organization/views/organizationCollectionsAdd.html @@ -24,6 +24,43 @@ +
+

Group Access

+
+ Loading groups... +
+
+

No groups for your organization.

+
+
+ + + + + + + + + + + + + +
+ + Name
+ + + {{group.name}} +
+
+
+
+

Group Access

+
+ Loading groups... +
+
+

No groups for your organization.

+
+
+ + + + + + + + + + + + + +
+ + Name
+ + + {{group.name}} +
+
+