passman/js/app/services/vaultservice.js

113 lines
3.1 KiB
JavaScript
Raw Normal View History

2016-09-11 05:30:17 +08:00
'use strict';
/**
* @ngdoc service
* @name passmanApp.VaultService
* @description
* # VaultService
* Service in the passmanApp.
*/
angular.module('passmanApp')
.service('VaultService', ['$http', function ($http) {
2016-09-11 05:30:17 +08:00
// AngularJS will instantiate a singleton by calling "new" on this function
2016-09-29 05:23:24 +08:00
var _this = this;
2016-09-12 01:45:20 +08:00
var _activeVault;
2016-09-29 05:23:24 +08:00
var service = {
2016-09-11 05:30:17 +08:00
getVaults: function(){
2016-09-12 01:45:20 +08:00
var queryUrl = OC.generateUrl('apps/passman/api/v2/vaults');
2016-09-11 05:30:17 +08:00
return $http.get(queryUrl).then(function (response) {
if(response.data){
return response.data;
} else {
return response;
}
});
},
2016-09-12 01:45:20 +08:00
setActiveVault: function(vault){
2016-09-29 05:23:24 +08:00
_activeVault = angular.copy(vault);
2016-09-12 01:45:20 +08:00
},
2016-09-29 05:40:33 +08:00
getActiveVault: function(){
2016-09-12 01:45:20 +08:00
return _activeVault;
},
2016-09-29 04:15:03 +08:00
getVaultSetting: function(key, default_value){
if(!_activeVault.vault_settings){
return default_value
} else {
2016-09-29 05:23:24 +08:00
return (_activeVault.vault_settings[key] !== undefined) ? _activeVault.vault_settings[key] : default_value
}
},
setVaultSetting: function(key, value){
if(!_activeVault.vault_settings){
return false;
} else {
_activeVault.vault_settings[key] = value;
_this.updateVault(_activeVault);
2016-09-29 04:15:03 +08:00
}
},
createVault: function (vaultName) {
2016-09-12 01:45:20 +08:00
var queryUrl = OC.generateUrl('apps/passman/api/v2/vaults');
return $http.post(queryUrl, { vault_name: vaultName }).then(function (response) {
if(response.data){
return response.data;
} else {
return response;
}
});
},
getVault: function (vault) {
2016-09-12 01:45:20 +08:00
var queryUrl = OC.generateUrl('apps/passman/api/v2/vaults/' + vault.vault_id);
return $http.get(queryUrl).then(function (response) {
if(response.data){
2016-09-29 04:05:09 +08:00
if(response.data.vault_settings){
response.data.vault_settings = JSON.parse(window.atob(response.data.vault_settings))
2016-09-29 05:23:24 +08:00
} else {
response.data.vault_settings = {};
2016-09-29 04:05:09 +08:00
}
return response.data;
} else {
return response;
}
});
},
updateVault: function (vault) {
2016-09-29 03:21:58 +08:00
var _vault = angular.copy(vault);
2016-10-01 03:15:28 +08:00
delete _vault.defaultVaultPass;
delete _vault.defaultVault;
delete _vault.vaultKey;
2016-09-29 05:23:24 +08:00
_vault.vault_settings = window.btoa(JSON.stringify(_vault.vault_settings))
2016-09-29 04:05:09 +08:00
var queryUrl = OC.generateUrl('apps/passman/api/v2/vaults/' + _vault.vault_id);
return $http.patch(queryUrl, _vault).then(function (response) {
if(response.data){
return response.data;
} else {
return response;
}
});
},
2016-09-26 00:34:33 +08:00
updateSharingKeys: function (vault) {
var queryUrl = OC.generateUrl('apps/passman/api/v2/vaults/' + vault.vault_id +'/sharing-keys');
return $http.post(queryUrl, vault).then(function (response) {
if(response.data){
return response.data;
} else {
return response;
}
});
},
deleteVault: function (vault) {
2016-09-12 01:45:20 +08:00
var queryUrl = OC.generateUrl('apps/passman/api/v2/vaults/' + vault.vault_id);
return $http.delete(queryUrl).then(function (response) {
if(response.data){
return response.data;
} else {
return response;
}
});
2016-09-11 05:30:17 +08:00
}
2016-09-29 05:23:24 +08:00
};
return service;
2016-09-11 05:30:17 +08:00
}]);