1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-10-10 06:08:34 +02:00
bitwarden-browser/src/app/vault/vaultShareLoginController.js

143 lines
5.1 KiB
JavaScript
Raw Normal View History

angular
.module('bit.vault')
.controller('vaultShareLoginController', function ($scope, apiService, $uibModalInstance, authService, cipherService,
2017-04-11 23:29:45 +02:00
loginId, $analytics, $state) {
$analytics.eventTrack('vaultShareLoginController', { category: 'Modal' });
$scope.model = {};
$scope.login = {};
2017-04-27 15:33:12 +02:00
$scope.collections = [];
$scope.selectedCollections = {};
$scope.organizations = [];
2017-04-27 15:33:12 +02:00
var organizationCollectionCounts = {};
$scope.loadingCollections = true;
$scope.loading = true;
2017-03-25 16:41:06 +01:00
$scope.readOnly = false;
2017-03-25 16:41:06 +01:00
apiService.logins.get({ id: loginId }).$promise.then(function (login) {
$scope.readOnly = !login.Edit;
if (login.Edit) {
$scope.login = cipherService.decryptLogin(login);
}
2017-03-25 16:41:06 +01:00
return login.Edit;
}).then(function (canEdit) {
$scope.loading = false;
2017-03-25 16:41:06 +01:00
if (!canEdit) {
return;
}
return authService.getUserProfile();
}).then(function (profile) {
if (profile && profile.organizations) {
2017-03-28 03:55:39 +02:00
var orgs = [],
setFirstOrg = false;
for (var i in profile.organizations) {
2017-04-11 23:29:45 +02:00
if (profile.organizations.hasOwnProperty(i) && profile.organizations[i].enabled) {
2017-03-28 03:55:39 +02:00
orgs.push({
id: profile.organizations[i].id,
name: profile.organizations[i].name
});
2017-04-27 15:33:12 +02:00
organizationCollectionCounts[profile.organizations[i].id] = 0;
2017-03-28 03:55:39 +02:00
if (!setFirstOrg) {
setFirstOrg = true;
$scope.model.organizationId = profile.organizations[i].id;
}
}
}
$scope.organizations = orgs;
2017-04-27 15:33:12 +02:00
apiService.collections.listMe(function (response) {
var collections = [];
for (var i = 0; i < response.Data.length; i++) {
if (response.Data[i].ReadOnly) {
continue;
}
2017-04-27 15:33:12 +02:00
var decCollection = cipherService.decryptCollection(response.Data[i]);
decCollection.organizationId = response.Data[i].OrganizationId;
collections.push(decCollection);
organizationCollectionCounts[decCollection.organizationId]++;
}
2017-04-27 15:33:12 +02:00
$scope.collections = collections;
$scope.loadingCollections = false;
});
}
});
2017-04-27 15:33:12 +02:00
$scope.toggleCollectionSelectionAll = function ($event) {
var collections = {};
2017-04-05 04:08:04 +02:00
if ($event.target.checked) {
2017-04-27 15:33:12 +02:00
for (var i = 0; i < $scope.collections.length; i++) {
2017-05-20 14:55:43 +02:00
if ($scope.model.organizationId && $scope.collections[i].organizationId === $scope.model.organizationId &&
!$scope.collections[i].readOnly) {
2017-04-27 15:33:12 +02:00
collections[$scope.collections[i].id] = true;
}
2017-04-05 04:08:04 +02:00
}
}
2017-04-27 15:33:12 +02:00
$scope.selectedCollections = collections;
2017-04-05 04:08:04 +02:00
};
2017-04-27 15:33:12 +02:00
$scope.toggleCollectionSelection = function (id) {
if (id in $scope.selectedCollections) {
delete $scope.selectedCollections[id];
2017-04-05 04:08:04 +02:00
}
else {
2017-04-27 15:33:12 +02:00
$scope.selectedCollections[id] = true;
2017-04-05 04:08:04 +02:00
}
};
2017-04-27 15:33:12 +02:00
$scope.collectionSelected = function (collection) {
return collection.id in $scope.selectedCollections;
2017-04-05 04:08:04 +02:00
};
$scope.allSelected = function () {
if (!$scope.model.organizationId) {
return false;
}
2017-04-27 15:33:12 +02:00
return Object.keys($scope.selectedCollections).length === organizationCollectionCounts[$scope.model.organizationId];
};
$scope.orgChanged = function () {
2017-04-27 15:33:12 +02:00
$scope.selectedCollections = {};
2017-04-05 04:08:04 +02:00
};
$scope.submitPromise = null;
$scope.submit = function (model) {
$scope.login.organizationId = model.organizationId;
var request = {
2017-04-27 15:33:12 +02:00
collectionIds: [],
cipher: cipherService.encryptLogin($scope.login)
};
2017-04-27 15:33:12 +02:00
for (var id in $scope.selectedCollections) {
if ($scope.selectedCollections.hasOwnProperty(id)) {
request.collectionIds.push(id);
2017-04-05 04:08:04 +02:00
}
}
2017-04-12 18:41:43 +02:00
$scope.submitPromise = apiService.ciphers.putShare({ id: loginId }, request, function (response) {
$analytics.eventTrack('Shared Login');
2017-04-15 05:36:11 +02:00
$uibModalInstance.close(model.organizationId);
}).$promise;
};
$scope.close = function () {
$uibModalInstance.dismiss('cancel');
};
2017-04-11 23:29:45 +02:00
$scope.createOrg = function () {
$state.go('backend.user.settingsCreateOrg').then(function () {
$uibModalInstance.dismiss('cancel');
});
};
});