passman/js/app/controllers/share.js

294 lines
10 KiB
JavaScript
Raw Normal View History

2016-09-21 02:03:37 +08:00
'use strict';
/**
* @ngdoc function
* @name passmanApp.controller:MainCtrl
* @description
* # MainCtrl
* Controller of the passmanApp
* This file is part of passman, licensed under AGPLv3
2016-09-21 02:03:37 +08:00
*/
angular.module('passmanApp')
2016-10-04 21:08:24 +08:00
.controller('ShareCtrl', ['$scope', 'VaultService', 'CredentialService', 'SettingsService', '$location', '$routeParams', 'ShareService', 'NotificationService', 'SharingACL','EncryptService',
function ($scope, VaultService, CredentialService, SettingsService, $location, $routeParams, ShareService, NotificationService, SharingACL, EncryptService) {
2016-10-03 23:53:49 +08:00
$scope.active_vault = VaultService.getActiveVault();
$scope.tabs = [{
title: 'Share with users and groups',
url: 'views/partials/forms/share_credential/basics.html',
}, {
title: 'Share link',
2016-10-04 02:41:30 +08:00
url: 'views/partials/forms/share_credential/link_sharing.html',
2016-10-03 23:53:49 +08:00
color: 'green'
}];
$scope.currentTab = {
title: 'General',
url: 'views/partials/forms/share_credential/basics.html'
};
$scope.onClickTab = function (tab) {
$scope.currentTab = tab;
};
$scope.isActiveTab = function (tab) {
return tab.url == $scope.currentTab.url;
};
if (!SettingsService.getSetting('defaultVault') || !SettingsService.getSetting('defaultVaultPass')) {
if (!$scope.active_vault) {
$location.path('/')
2016-10-02 19:35:49 +08:00
}
2016-10-03 23:53:49 +08:00
} else {
if (SettingsService.getSetting('defaultVault') && SettingsService.getSetting('defaultVaultPass')) {
var _vault = angular.copy(SettingsService.getSetting('defaultVault'));
_vault.vaultKey = angular.copy(SettingsService.getSetting('defaultVaultPass'));
VaultService.setActiveVault(_vault);
$scope.active_vault = _vault;
2016-10-03 23:53:49 +08:00
}
}
2016-10-03 23:53:49 +08:00
var storedCredential = SettingsService.getSetting('share_credential');
2016-10-05 00:15:40 +08:00
if(!storedCredential) {
2016-10-03 23:53:49 +08:00
$location.path('/vault/' + $routeParams.vault_id);
} else {
$scope.storedCredential = CredentialService.decryptCredential(angular.copy(storedCredential));
2016-09-21 02:03:37 +08:00
}
2016-10-05 00:15:40 +08:00
2016-10-03 23:53:49 +08:00
if ($scope.active_vault) {
$scope.$parent.selectedVault = true;
}
$scope.cancel = function () {
SettingsService.setSetting('share_credential', null);
$location.path('/vault/' + $scope.storedCredential.vault_id);
};
2016-10-04 01:19:28 +08:00
$scope.default_permissions = new SharingACL(0);
$scope.default_permissions.addPermission(
$scope.default_permissions.permissions.READ |
$scope.default_permissions.permissions.WRITE |
$scope.default_permissions.permissions.FILES
);
var link_acl = angular.copy($scope.default_permissions);
link_acl.removePermission($scope.default_permissions.permissions.WRITE)
2016-10-03 23:53:49 +08:00
$scope.share_settings = {
linkSharing: {
enabled: false,
settings: {
expire_time: new Date("2999-12-31T22:59:59"),
expire_views: 5,
2016-10-04 01:19:28 +08:00
acl: link_acl
2016-10-03 23:53:49 +08:00
}
},
credentialSharedWithUserAndGroup: [],
cypher_progress: {
done: 0,
total: 0
}
};
2016-10-04 20:50:49 +08:00
ShareService.getSharedCredentialACL($scope.storedCredential).then(function (aclList) {
2016-10-05 00:15:40 +08:00
var enc_key = ($scope.storedCredential.shared_key) ? EncryptService.decryptString(angular.copy($scope.storedCredential.shared_key)) : false;
2016-10-04 21:08:24 +08:00
for(var i = 0; i < aclList.length; i++){
var acl = aclList[i];
if(acl.user_id === null){
$scope.share_settings.linkSharing ={
enabled: true,
settings: {
expire_time: new Date(acl.expire * 1000),
expire_views: acl.expire_views,
acl: new SharingACL(acl.permissions)
}
};
2016-10-05 00:15:40 +08:00
if(enc_key) {
var hash = window.btoa($scope.storedCredential.guid + '<::>' + enc_key)
$scope.share_link = $location.$$protocol + '://' + $location.$$host + OC.generateUrl('apps/passman/share/public#') + hash;
}
2016-10-04 21:08:24 +08:00
} else {
var obj = {
userId: acl.user_id,
displayName: acl.user_id,
type: 'user',
2016-10-04 21:14:31 +08:00
acl: new SharingACL(acl.permissions),
acl_id: acl.acl_id
2016-10-04 21:08:24 +08:00
};
2016-10-04 21:14:31 +08:00
2016-10-04 21:08:24 +08:00
$scope.share_settings.credentialSharedWithUserAndGroup.push(obj);
}
}
});
2016-10-04 20:50:49 +08:00
2016-10-03 23:56:28 +08:00
var acl = new SharingACL(0);
2016-10-04 00:14:18 +08:00
$scope.inputSharedWith = [];
2016-10-03 23:53:49 +08:00
$scope.selectedAccessLevel = '1';
$scope.searchUsers = function ($query) {
return ShareService.search($query)
};
2016-10-04 00:14:18 +08:00
$scope.hasPermission = function(acl, permission){
return acl.hasPermission(permission);
2016-10-04 00:14:18 +08:00
};
2016-10-04 00:44:49 +08:00
$scope.setPermission = function(acl, permission){
2016-10-04 01:06:25 +08:00
acl.togglePermission(permission);
2016-10-04 00:44:49 +08:00
};
2016-10-03 23:53:49 +08:00
$scope.shareWith = function (shareWith, selectedAccessLevel) {
//@TODO Improve this so we can add, edit and remove users and permissions.
$scope.inputSharedWith = [];
if (shareWith.length > 0) {
for (var i = 0; i < shareWith.length; i++) {
var obj = {
userId: shareWith[i].uid,
displayName: shareWith[i].text,
type: shareWith[i].type,
acl: angular.copy($scope.default_permissions)
2016-10-03 23:53:49 +08:00
};
if ($scope.share_settings.credentialSharedWithUserAndGroup.indexOf(obj) === -1) {
$scope.share_settings.credentialSharedWithUserAndGroup.push(obj)
}
}
}
2016-10-03 23:53:49 +08:00
};
$scope.unshareCredential = function (credential) {
2016-10-04 06:19:46 +08:00
ShareService.unshareCredential(credential);
var _credential = angular.copy(credential);
_credential.shared_key = null;
CredentialService.updateCredential(_credential).then(function () {
NotificationService.showNotification('Credential unshared', 4000)
2016-10-03 23:23:03 +08:00
})
2016-10-04 01:19:28 +08:00
};
2016-10-05 00:15:40 +08:00
/**
* Apply a share to a new user
* @param user A user object to who we should share the data
* @param enc_key The shared key we are going to ecnrypt with his public rsa key
*/
$scope.applyShareToUser = function (user, enc_key) {
ShareService.getVaultsByUser(user.userId).then(function (data) {
$scope.share_settings.cypher_progress.total += data.length;
user.vaults = data;
var start = new Date().getTime() / 1000;
console.log('Begin messing with permissions');
ShareService.cypherRSAStringWithPublicKeyBulkAsync(user.vaults, enc_key)
.progress(function (data) {
$scope.share_settings.cypher_progress.done++;
$scope.share_settings.cypher_progress.percent = $scope.share_settings.cypher_progress.done / $scope.share_settings.cypher_progress.total * 100;
$scope.$digest();
})
.then(function (result) {
console.log("Took: " + ((new Date().getTime() / 1000) - start) + "s to cypher the string for user [" + data[0].user_id + "]");
$scope.share_settings.cypher_progress.times.push({
time: ((new Date().getTime() / 1000) - start),
user: data[0].user_id
});
user.vaults = result;
console.log(user)
$scope.uploadChanges(user);
$scope.$digest();
});
});
};
2016-10-03 23:53:49 +08:00
$scope.applyShare = function () {
$scope.share_settings.cypher_progress.percent = 0;
$scope.share_settings.cypher_progress.done = 0;
$scope.share_settings.cypher_progress.total = 0;
$scope.share_settings.cypher_progress.times = [];
$scope.share_settings.cypher_progress.times_total = [];
2016-10-05 00:15:40 +08:00
console.log($scope.storedCredential);
2016-10-04 22:20:55 +08:00
//Credential is already shared
2016-10-05 00:15:40 +08:00
if($scope.storedCredential.shared_key && $scope.storedCredential.shared_key != '' && $scope.storedCredential.shared_key != null){
2016-10-04 22:20:55 +08:00
console.log('Shared key found');
if($scope.share_settings.linkSharing.enabled){
var expire_time = new Date(angular.copy( $scope.share_settings.linkSharing.settings.expire_time)).getTime()/1000;
var shareObj = {
item_id: $scope.storedCredential.credential_id,
item_guid: $scope.storedCredential.guid,
permissions: $scope.share_settings.linkSharing.settings.acl.getAccessLevel(),
expire_timestamp: expire_time,
expire_views: $scope.share_settings.linkSharing.settings.expire_views
};
//ShareService.createPublicSharedCredential(shareObj);
}
2016-10-03 23:53:49 +08:00
var list = $scope.share_settings.credentialSharedWithUserAndGroup;
console.log(list);
2016-10-04 22:20:55 +08:00
var enc_key = EncryptService.decryptString(angular.copy($scope.storedCredential.shared_key));
2016-10-03 23:53:49 +08:00
for (var i = 0; i < list.length; i++) {
2016-10-04 22:20:55 +08:00
var iterator = i;
var target_user = list[i];
console.log(target_user)
if(target_user.hasOwnProperty('acl_id')){
var acl = {
user_id: target_user.userId,
permission: target_user.acl.getAccessLevel()
};
ShareService.updateCredentialAcl($scope.storedCredential, acl);
} else {
$scope.applyShareToUser(list[iterator], enc_key);
2016-10-03 23:53:49 +08:00
}
2016-09-29 07:12:39 +08:00
}
2016-10-04 05:53:11 +08:00
2016-10-04 22:20:55 +08:00
} else {
ShareService.generateSharedKey(20).then(function (key) {
var encryptedSharedCredential = ShareService.encryptSharedCredential($scope.storedCredential, key);
CredentialService.updateCredential(encryptedSharedCredential, true);
var list = $scope.share_settings.credentialSharedWithUserAndGroup;
console.log(list);
for (var i = 0; i < list.length; i++) {
if (list[i].type == "user") {
$scope.applyShareToUser(list[i], key);
2016-10-04 22:20:55 +08:00
}
}
2016-10-04 05:53:11 +08:00
2016-10-04 22:20:55 +08:00
if($scope.share_settings.linkSharing.enabled){
var expire_time = new Date(angular.copy( $scope.share_settings.linkSharing.settings.expire_time)).getTime()/1000;
var shareObj = {
item_id: $scope.storedCredential.credential_id,
item_guid: $scope.storedCredential.guid,
permissions: $scope.share_settings.linkSharing.settings.acl.getAccessLevel(),
expire_timestamp: expire_time,
expire_views: $scope.share_settings.linkSharing.settings.expire_views
};
ShareService.createPublicSharedCredential(shareObj).then(function(){
var hash = window.btoa($scope.storedCredential.guid + '<::>'+ key)
$scope.share_link = $location.$$protocol + '://' + $location.$$host + OC.generateUrl('apps/passman/share/public#') + hash;
});
}
NotificationService.showNotification('Credential shared', 4000)
})
}
2016-10-03 23:53:49 +08:00
};
$scope.uploadChanges = function (user) {
2016-10-04 01:31:59 +08:00
user.accessLevel = angular.copy(user.acl.getAccessLevel());
2016-10-03 23:53:49 +08:00
ShareService.shareWithUser(storedCredential, user);
};
$scope.calculate_total_time = function () {
$scope.share_settings.cypher_progress.times = $scope.share_settings.cypher_progress.times || [];
var total = 0;
for (var i = 0; i < $scope.share_settings.cypher_progress.times.length; i++) {
total += $scope.share_settings.cypher_progress.times[i].time;
2016-09-27 04:36:05 +08:00
}
2016-10-03 23:53:49 +08:00
return total;
}
2016-10-03 23:53:49 +08:00
}]);