passman/js/app/controllers/edit_credential.js

274 lines
8.6 KiB
JavaScript
Raw Normal View History

2016-10-08 01:56:29 +08:00
(function () {
'use strict';
/**
* @ngdoc function
* @name passmanApp.controller:MainCtrl
* @description
* # MainCtrl
* Controller of the passmanApp
*/
angular.module('passmanApp')
.controller('CredentialEditCtrl', ['$scope', 'VaultService', 'CredentialService', 'SettingsService', '$location', '$routeParams', 'FileService', 'EncryptService', 'TagService', 'NotificationService', 'ShareService',
function ($scope, VaultService, CredentialService, SettingsService, $location, $routeParams, FileService, EncryptService, TagService, NotificationService, ShareService) {
$scope.active_vault = VaultService.getActiveVault();
if (!SettingsService.getSetting('defaultVault') || !SettingsService.getSetting('defaultVaultPass')) {
if (!$scope.active_vault) {
$location.path('/');
2016-10-08 21:48:42 +08:00
return;
2016-10-08 01:56:29 +08:00
}
} else {
if (SettingsService.getSetting('defaultVault') && SettingsService.getSetting('defaultVaultPass')) {
var _vault = angular.copy(SettingsService.getSetting('defaultVault'));
2016-10-08 21:48:42 +08:00
_vault.vaultKey = SettingsService.getSetting('defaultVaultPass');
VaultService.setActiveVault(_vault);
$scope.active_vault = _vault;
2016-10-08 01:56:29 +08:00
}
}
2016-10-08 21:48:42 +08:00
VaultService.getVault($scope.active_vault).then(function (vault) {
vault.vaultKey = VaultService.getActiveVault().vaultKey;
2016-10-08 21:48:42 +08:00
delete vault.credentials;
VaultService.setActiveVault(vault);
$scope.pwSettings = VaultService.getVaultSetting('pwSettings',
{
'length': 12,
'useUppercase': true,
'useLowercase': true,
'useDigits': true,
'useSpecialChars': true,
'minimumDigitCount': 3,
'avoidAmbiguousCharacters': false,
'requireEveryCharType': true,
'generateOnCreate': true
});
});
2016-10-08 01:56:29 +08:00
$scope.tabs = [{
title: 'General',
url: 'views/partials/forms/edit_credential/basics.html',
color: 'blue'
}, {
title: 'Password',
url: 'views/partials/forms/edit_credential/password.html',
color: 'green'
}, {
title: 'Custom fields',
url: 'views/partials/forms/edit_credential/custom_fields.html',
color: 'orange'
}, {
title: 'Files',
url: 'views/partials/forms/edit_credential/files.html',
color: 'yellow'
}, {
title: 'OTP',
url: 'views/partials/forms/edit_credential/otp.html',
color: 'purple'
}];
if ($scope.active_vault) {
$scope.$parent.selectedVault = true;
}
2016-10-08 01:56:29 +08:00
var storedCredential = SettingsService.getSetting('edit_credential');
2016-10-08 01:56:29 +08:00
if (!storedCredential) {
CredentialService.getCredential($routeParams.credential_id).then(function (result) {
$scope.storedCredential = CredentialService.decryptCredential(angular.copy(result));
});
} else {
$scope.storedCredential = CredentialService.decryptCredential(angular.copy(storedCredential));
$scope.storedCredential.password_repeat = angular.copy($scope.storedCredential.password);
$scope.storedCredential.expire_time = $scope.storedCredential.expire_time * 1000;
}
2016-10-08 01:56:29 +08:00
$scope.getTags = function ($query) {
return TagService.searchTag($query);
};
2016-10-08 01:56:29 +08:00
$scope.currentTab = {
title: 'General',
url: 'views/partials/forms/edit_credential/basics.html',
color: 'blue'
};
2016-10-08 01:56:29 +08:00
$scope.onClickTab = function (tab) {
$scope.currentTab = tab;
};
2016-10-08 01:56:29 +08:00
$scope.isActiveTab = function (tab) {
return tab.url === $scope.currentTab.url;
};
2016-09-14 05:03:12 +08:00
2016-10-08 01:56:29 +08:00
/**
* Below general edit functions
*/
2016-10-08 01:56:29 +08:00
$scope.pwGenerated = function (pass) {
$scope.storedCredential.password_repeat = pass;
};
2016-10-08 01:56:29 +08:00
var _customField = {
label: '',
value: '',
secret: false
};
$scope.new_custom_field = angular.copy(_customField);
2016-10-08 01:56:29 +08:00
$scope.addCustomField = function () {
if (!$scope.new_custom_field.label) {
NotificationService.showNotification('Please fill in a label', 3000);
}
if (!$scope.new_custom_field.value) {
NotificationService.showNotification('Please fill in a value!', 3000);
}
if (!$scope.new_custom_field.label || !$scope.new_custom_field.value) {
return;
}
$scope.storedCredential.custom_fields.push(angular.copy($scope.new_custom_field));
$scope.new_custom_field = angular.copy(_customField);
};
2016-10-06 01:41:47 +08:00
2016-10-08 01:56:29 +08:00
$scope.deleteCustomField = function (field) {
var idx = $scope.storedCredential.custom_fields.indexOf(field);
$scope.storedCredential.custom_fields.splice(idx, 1);
};
2016-10-06 01:41:47 +08:00
2016-10-08 01:56:29 +08:00
$scope.new_file = {
name: '',
data: null
};
2016-10-06 01:41:47 +08:00
2016-10-08 01:56:29 +08:00
$scope.deleteFile = function (file) {
var idx = $scope.storedCredential.files.indexOf(file);
FileService.deleteFile(file).then(function () {
$scope.storedCredential.files.splice(idx, 1);
});
};
2016-10-06 01:41:47 +08:00
2016-10-08 01:56:29 +08:00
$scope.fileLoaded = function (file) {
var key;
var _file = {
filename: file.name,
size: file.size,
mimetype: file.type,
data: file.data
};
2016-10-04 02:33:32 +08:00
2016-10-06 01:32:37 +08:00
if (!$scope.storedCredential.hasOwnProperty('acl') && $scope.storedCredential.hasOwnProperty('shared_key')) {
if ($scope.storedCredential.shared_key) {
key = EncryptService.decryptString(angular.copy($scope.storedCredential.shared_key));
}
2016-10-06 01:27:24 +08:00
}
2016-10-06 01:32:37 +08:00
if ($scope.storedCredential.hasOwnProperty('acl')) {
2016-10-06 01:27:24 +08:00
key = EncryptService.decryptString(angular.copy($scope.storedCredential.acl.shared_key));
}
2016-10-06 01:32:37 +08:00
2016-10-08 01:56:29 +08:00
FileService.uploadFile(_file, key).then(function (result) {
delete result.file_data;
result.filename = EncryptService.decryptString(result.filename, key);
$scope.storedCredential.files.push(result);
});
$scope.$digest();
};
$scope.fileLoadError = function (error, file) {
console.log(error, file);
};
$scope.selected_file = '';
$scope.fileprogress = [];
$scope.fileSelectProgress = function (progress) {
if (progress) {
$scope.fileprogress = progress;
$scope.$digest();
}
};
$scope.renewIntervalValue = 0;
$scope.renewIntervalModifier = '0';
$scope.updateInterval = function (renewIntervalValue, renewIntervalModifier) {
var value = parseInt(renewIntervalValue);
var modifier = parseInt(renewIntervalModifier);
if (value && modifier) {
$scope.storedCredential.renew_interval = value * modifier;
2016-10-06 01:27:24 +08:00
}
2016-10-08 01:56:29 +08:00
};
$scope.parseQR = function (QRCode) {
var re = /otpauth:\/\/(totp|hotp)\/(.*)\?(secret|issuer)=(.*)&(issuer|secret)=(.*)/, parsedQR, qrInfo;
parsedQR = (QRCode.qrData.match(re));
if (parsedQR)
qrInfo = {
type: parsedQR[1],
label: decodeURIComponent(parsedQR[2]),
qr_uri: QRCode
};
qrInfo[parsedQR[3]] = parsedQR[4];
qrInfo[parsedQR[5]] = parsedQR[6];
$scope.storedCredential.otp = qrInfo;
$scope.$digest();
};
2016-10-06 01:32:37 +08:00
2016-10-08 01:56:29 +08:00
$scope.saveCredential = function () {
2016-10-06 01:27:24 +08:00
2016-10-04 02:33:32 +08:00
2016-10-08 01:56:29 +08:00
if ($scope.new_custom_field.label && $scope.new_custom_field.value) {
$scope.storedCredential.custom_fields.push(angular.copy($scope.new_custom_field));
}
//@TODO validation
//@TODO When credential is expired and has renew interval set, calc new expire time.
delete $scope.storedCredential.password_repeat;
console.log($scope.storedCredential);
if (!$scope.storedCredential.credential_id) {
$scope.storedCredential.vault_id = $scope.active_vault.vault_id;
CredentialService.createCredential($scope.storedCredential).then(function (result) {
$location.path('/vault/' + $routeParams.vault_id);
NotificationService.showNotification('Credential created!', 5000);
});
} else {
var key, _credential;
if (!$scope.storedCredential.hasOwnProperty('acl') && $scope.storedCredential.hasOwnProperty('shared_key')) {
if ($scope.storedCredential.shared_key) {
key = EncryptService.decryptString(angular.copy($scope.storedCredential.shared_key));
}
}
if ($scope.storedCredential.hasOwnProperty('acl')) {
key = EncryptService.decryptString(angular.copy($scope.storedCredential.acl.shared_key));
}
2016-10-08 01:56:29 +08:00
if (key) {
_credential = ShareService.encryptSharedCredential($scope.storedCredential, key);
} else {
_credential = angular.copy($scope.storedCredential);
}
delete _credential.shared_key;
var _useKey = (key != null);
CredentialService.updateCredential(_credential, _useKey).then(function (result) {
SettingsService.setSetting('edit_credential', null);
$location.path('/vault/' + $routeParams.vault_id);
NotificationService.showNotification('Credential updated!', 5000);
});
}
};
$scope.cancel = function () {
$location.path('/vault/' + $routeParams.vault_id);
};
}]);
}());