2016-09-12 02:47:29 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ngdoc function
|
|
|
|
* @name passmanApp.controller:MainCtrl
|
|
|
|
* @description
|
|
|
|
* # MainCtrl
|
|
|
|
* Controller of the passmanApp
|
|
|
|
*/
|
|
|
|
angular.module('passmanApp')
|
2016-09-16 20:01:25 +08:00
|
|
|
.controller('CredentialCtrl', ['$scope', 'VaultService', 'SettingsService', '$location', 'CredentialService',
|
2016-09-25 23:36:03 +08:00
|
|
|
'$rootScope', 'FileService', 'EncryptService', 'TagService', '$timeout', 'NotificationService', 'CacheService',
|
|
|
|
function ($scope, VaultService, SettingsService, $location, CredentialService, $rootScope, FileService, EncryptService, TagService, $timeout, NotificationService, CacheService) {
|
2016-09-16 20:01:25 +08:00
|
|
|
$scope.active_vault = VaultService.getActiveVault();
|
|
|
|
if (!SettingsService.getSetting('defaultVault') || !SettingsService.getSetting('defaultVaultPass')) {
|
|
|
|
if (!$scope.active_vault) {
|
|
|
|
$location.path('/')
|
|
|
|
}
|
|
|
|
} 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;
|
|
|
|
//@TODO check if vault exists
|
|
|
|
}
|
|
|
|
|
2016-09-12 02:47:29 +08:00
|
|
|
}
|
|
|
|
|
2016-09-16 20:01:25 +08:00
|
|
|
$scope.addCredential = function () {
|
|
|
|
var new_credential = CredentialService.newCredential();
|
|
|
|
var enc_c = CredentialService.encryptCredential(new_credential);
|
|
|
|
SettingsService.setSetting('edit_credential', enc_c);
|
|
|
|
$location.path('/vault/' + $scope.active_vault.vault_id + '/new')
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.editCredential = function (credential) {
|
|
|
|
var _credential = angular.copy(credential);
|
|
|
|
$rootScope.$emit('app_menu', false);
|
|
|
|
SettingsService.setSetting('edit_credential', CredentialService.encryptCredential(_credential));
|
|
|
|
$location.path('/vault/' + $scope.active_vault.vault_id + '/edit/' + _credential.credential_id)
|
|
|
|
};
|
|
|
|
|
2016-09-24 16:58:52 +08:00
|
|
|
$scope.getRevisions = function (credential) {
|
|
|
|
var _credential = angular.copy(credential);
|
|
|
|
$rootScope.$emit('app_menu', false);
|
|
|
|
SettingsService.setSetting('revision_credential', CredentialService.encryptCredential(_credential));
|
|
|
|
$location.path('/vault/' + $scope.active_vault.vault_id + '/' + _credential.credential_id + '/revisions')
|
|
|
|
};
|
|
|
|
|
2016-09-21 02:03:37 +08:00
|
|
|
$scope.shareCredential = function (credential) {
|
|
|
|
var _credential = angular.copy(credential);
|
|
|
|
$rootScope.$emit('app_menu', false);
|
|
|
|
SettingsService.setSetting('share_credential', CredentialService.encryptCredential(_credential));
|
|
|
|
$location.path('/vault/' + $scope.active_vault.vault_id + '/' + _credential.credential_id +'/share')
|
|
|
|
};
|
|
|
|
|
2016-09-16 20:01:25 +08:00
|
|
|
var notification;
|
|
|
|
$scope.deleteCredential = function (credential) {
|
|
|
|
var _credential = angular.copy(credential);
|
|
|
|
try {
|
|
|
|
_credential = CredentialService.decryptCredential(angular.copy(credential));
|
|
|
|
} catch (e) {
|
|
|
|
|
2016-09-13 06:10:24 +08:00
|
|
|
}
|
2016-09-16 20:01:25 +08:00
|
|
|
_credential.delete_time = new Date().getTime() / 1000;
|
|
|
|
for (var i = 0; i < $scope.credentials.length; i++) {
|
|
|
|
if ($scope.credentials[i].credential_id == credential.credential_id) {
|
|
|
|
$scope.credentials[i].delete_time = _credential.delete_time;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$scope.closeSelected();
|
|
|
|
if (notification) {
|
|
|
|
NotificationService.hideNotification(notification);
|
|
|
|
}
|
|
|
|
notification = NotificationService.showNotification('Credential deleted <a class="undoDelete" data-item-id="' + credential.credential_id + '">[Undo]</a>', 5000,
|
|
|
|
function () {
|
|
|
|
CredentialService.updateCredential(_credential).then(function (result) {
|
|
|
|
if (result.delete_time > 0) {
|
|
|
|
notification = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.recoverCredential = function (credential) {
|
|
|
|
var _credential = angular.copy(credential);
|
|
|
|
try {
|
|
|
|
_credential = CredentialService.decryptCredential(angular.copy(credential));
|
|
|
|
} catch (e) {
|
|
|
|
|
|
|
|
}
|
|
|
|
for (var i = 0; i < $scope.credentials.length; i++) {
|
|
|
|
if ($scope.credentials[i].credential_id == credential.credential_id) {
|
|
|
|
$scope.credentials[i].delete_time = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_credential.delete_time = 0;
|
|
|
|
$scope.closeSelected();
|
|
|
|
if (notification) {
|
|
|
|
NotificationService.hideNotification(notification);
|
|
|
|
}
|
2016-09-24 03:30:07 +08:00
|
|
|
NotificationService.showNotification('Credential recovered <a class="undoRestore" data-item-id="' + credential.credential_id + '">[Undo]</a>', 5000,
|
2016-09-16 20:01:25 +08:00
|
|
|
function () {
|
|
|
|
CredentialService.updateCredential(_credential).then(function (result) {
|
|
|
|
notification = false;
|
|
|
|
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
};
|
2016-09-13 06:10:24 +08:00
|
|
|
|
2016-09-24 03:30:07 +08:00
|
|
|
$scope.destroyCredential = function(credential){
|
|
|
|
var _credential = angular.copy(credential);
|
|
|
|
CredentialService.destroyCredential(_credential.credential_id).then(function (result) {
|
|
|
|
for (var i = 0; i < $scope.credentials.length; i++) {
|
|
|
|
if ($scope.credentials[i].credential_id == credential.credential_id) {
|
|
|
|
$scope.credentials.splice(i,1);
|
|
|
|
NotificationService.showNotification('Credential destroyed', 5000);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2016-09-25 23:13:48 +08:00
|
|
|
|
|
|
|
$scope.filterOptions = {
|
|
|
|
filterText: '',
|
|
|
|
fields: ['label', 'username', 'email', 'password', 'custom_fields']
|
2016-09-16 20:01:25 +08:00
|
|
|
};
|
2016-09-25 23:13:48 +08:00
|
|
|
|
2016-09-16 20:01:25 +08:00
|
|
|
$scope.selectedtags = [];
|
2016-09-21 00:38:49 +08:00
|
|
|
var to;
|
2016-09-16 20:01:25 +08:00
|
|
|
$rootScope.$on('selected_tags_updated', function (evt, _sTags) {
|
2016-09-19 03:17:47 +08:00
|
|
|
var _selectedTags = [];
|
2016-09-16 20:01:25 +08:00
|
|
|
for(var x = 0; x < _sTags.length; x++){
|
|
|
|
_selectedTags.push(_sTags[x].text)
|
2016-09-12 02:47:29 +08:00
|
|
|
}
|
2016-09-16 20:01:25 +08:00
|
|
|
$scope.selectedtags = _selectedTags;
|
2016-09-21 00:38:49 +08:00
|
|
|
$timeout.cancel(to);
|
|
|
|
if(_selectedTags.length > 0) {
|
|
|
|
to = $timeout(function () {
|
|
|
|
if ($scope.filtered_credentials) {
|
|
|
|
var _filtered_tags = [];
|
|
|
|
for (var i = 0; i < $scope.filtered_credentials.length; i++) {
|
|
|
|
var tags = $scope.filtered_credentials[i].tags_raw;
|
|
|
|
for(var x = 0; x < tags.length; x++){
|
|
|
|
var tag = tags[x].text;
|
|
|
|
if(_filtered_tags.indexOf(tag) === -1){
|
|
|
|
_filtered_tags.push(tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$rootScope.$emit('limit_tags_in_list', _filtered_tags);
|
|
|
|
}
|
|
|
|
}, 50)
|
|
|
|
}
|
2016-09-12 02:47:29 +08:00
|
|
|
});
|
2016-09-16 20:01:25 +08:00
|
|
|
|
|
|
|
$scope.delete_time = 0;
|
|
|
|
$scope.showCredentialRow = function (credential) {
|
|
|
|
if ($scope.delete_time == 0) {
|
|
|
|
return credential.delete_time == 0
|
|
|
|
} else {
|
|
|
|
return credential.delete_time > $scope.delete_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
$rootScope.$on('set_delete_time', function (event, time) {
|
|
|
|
$scope.delete_time = time;
|
2016-09-16 03:21:34 +08:00
|
|
|
});
|
|
|
|
|
2016-09-16 20:01:25 +08:00
|
|
|
$scope.setDeleteTime = function (delete_time) {
|
|
|
|
$scope.delete_time = delete_time;
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.selectedCredential = false;
|
|
|
|
$scope.selectCredential = function (credential) {
|
2016-09-25 23:13:48 +08:00
|
|
|
$scope.selectedCredential = angular.copy(credential);
|
2016-09-16 20:01:25 +08:00
|
|
|
$rootScope.$emit('app_menu', true);
|
|
|
|
};
|
2016-09-15 06:11:59 +08:00
|
|
|
|
2016-09-16 20:01:25 +08:00
|
|
|
$scope.closeSelected = function () {
|
|
|
|
$rootScope.$emit('app_menu', false);
|
|
|
|
$scope.selectedCredential = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
$rootScope.$on('logout', function () {
|
|
|
|
console.log('Logout received, clean up');
|
2016-09-23 18:30:50 +08:00
|
|
|
$scope.active_vault = null;
|
2016-09-16 20:01:25 +08:00
|
|
|
$scope.credentials = [];
|
2016-09-23 18:30:50 +08:00
|
|
|
// $scope.$parent.selectedVault = false;
|
2016-09-16 20:01:25 +08:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2016-09-25 23:36:03 +08:00
|
|
|
|
|
|
|
try {
|
|
|
|
var credential_cache = JSON.parse(CacheService.get('credential_cache_'+ $scope.active_vault.vault_id));
|
|
|
|
if(credential_cache.length > 0){
|
|
|
|
$scope.credentials = credential_cache;
|
|
|
|
for (var i = 0; i < credential_cache.length; i++) {
|
|
|
|
TagService.addTags(credential_cache[i].tags_raw);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (e){
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-09-16 20:01:25 +08:00
|
|
|
var fetchCredentials = function () {
|
|
|
|
VaultService.getVault($scope.active_vault).then(function (credentials) {
|
|
|
|
var _credentials = [];
|
|
|
|
for (var i = 0; i < credentials.length; i++) {
|
2016-09-25 23:13:48 +08:00
|
|
|
var _c = CredentialService.decryptCredential(angular.copy(credentials[i]));
|
2016-09-25 23:15:59 +08:00
|
|
|
_c.tags_raw = _c.tags;
|
2016-09-25 23:36:03 +08:00
|
|
|
TagService.addTags( _c.tags);
|
2016-09-25 23:13:48 +08:00
|
|
|
_credentials.push(_c);
|
2016-09-16 20:01:25 +08:00
|
|
|
}
|
|
|
|
$scope.credentials = _credentials;
|
2016-09-25 23:36:03 +08:00
|
|
|
CacheService.set('credential_cache_'+ $scope.active_vault.vault_id, JSON.stringify(_credentials));
|
|
|
|
|
2016-09-16 20:01:25 +08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
$scope.downloadFile = function (file) {
|
|
|
|
FileService.getFile(file).then(function (result) {
|
|
|
|
var file_data = EncryptService.decryptString(result.file_data);
|
|
|
|
var uriContent = FileService.dataURItoBlob(file_data, file.mimetype), a = document.createElement("a");
|
|
|
|
a.style = "display: none";
|
|
|
|
a.href = uriContent;
|
|
|
|
a.download = escapeHTML(file.filename);
|
|
|
|
document.body.appendChild(a);
|
|
|
|
a.click();
|
|
|
|
window.URL.revokeObjectURL(uriContent);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
if ($scope.active_vault) {
|
|
|
|
$scope.$parent.selectedVault = true;
|
|
|
|
fetchCredentials();
|
|
|
|
}
|
|
|
|
}]);
|