passman/js/app/controllers/credential.js

65 lines
2.1 KiB
JavaScript
Raw Normal View History

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-12 04:57:55 +08:00
.controller('CredentialCtrl', ['$scope', 'VaultService', 'SettingsService', '$location', 'CredentialService', '$rootScope', function ($scope, VaultService, SettingsService, $location, CredentialService, $rootScope) {
2016-09-12 02:47:29 +08:00
$scope.active_vault = VaultService.getActiveVault();
if (!SettingsService.getSetting('defaultVault') || !SettingsService.getSetting('defaultVaultPass')) {
if (!$scope.active_vault) {
2016-09-12 02:47:29 +08:00
$location.path('/')
}
} else {
if (SettingsService.getSetting('defaultVault') && SettingsService.getSetting('defaultVaultPass')) {
2016-09-14 05:03:12 +08:00
var _vault = angular.copy(SettingsService.getSetting('defaultVault'));
2016-09-12 02:47:29 +08:00
_vault.vaultKey = angular.copy(SettingsService.getSetting('defaultVaultPass'));
VaultService.setActiveVault(_vault);
$scope.active_vault = _vault;
}
}
$scope.addCredential = function(){
var new_credential = CredentialService.newCredential();
2016-09-14 05:03:12 +08:00
var enc_c = CredentialService.encryptCredential(new_credential);
SettingsService.setSetting('edit_credential',enc_c);
$location.path('/vault/'+ $scope.active_vault.vault_id +'/new')
};
$rootScope.$on('logout', function () {
2016-09-12 04:57:55 +08:00
console.log('Logout received, clean up');
$scope.credentials = [];
if ($scope.hasOwnProperty('$parent')) {
if ($scope.$parent.hasOwnProperty('selectedVault')) {
$scope.$parent.selectedVault = false;
}
}
2016-09-12 04:57:55 +08:00
$scope.active_vault = null;
});
var fetchCredentials = function () {
VaultService.getVault($scope.active_vault).then(function (credentials) {
2016-09-12 02:47:29 +08:00
var _credentials = [];
for (var i = 0; i < credentials.length; i++) {
2016-09-14 05:03:12 +08:00
var credential = angular.copy(credentials[i]);
/*var credential = CredentialService.decryptCredential(angular.copy(credentials[i]));*/
2016-09-12 02:47:29 +08:00
_credentials.push(credential);
}
$scope.credentials = _credentials;
2016-09-12 05:40:06 +08:00
//@Todo: Set last accessed
2016-09-12 02:47:29 +08:00
});
};
if ($scope.active_vault) {
2016-09-12 02:47:29 +08:00
$scope.$parent.selectedVault = true;
fetchCredentials();
}
}]);