| Conditions | 1 |
| Paths | 16 |
| Total Lines | 130 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /** |
||
| 23 | (function () { |
||
| 24 | 'use strict'; |
||
| 25 | |||
| 26 | |||
| 27 | /** |
||
| 28 | * @ngdoc function |
||
| 29 | * @name passmanApp.controller:RevisionCtrl |
||
| 30 | * @description |
||
| 31 | * # RevisionCtrl |
||
| 32 | * Controller of the passmanApp |
||
| 33 | */ |
||
| 34 | angular.module('passmanApp') |
||
| 35 | .controller('RevisionCtrl', ['$scope', 'SettingsService', 'VaultService', 'CredentialService', '$location', '$routeParams', '$rootScope', 'NotificationService', '$filter', 'ShareService', 'EncryptService', '$translate', |
||
| 36 | function ($scope, SettingsService, VaultService, CredentialService, $location, $routeParams, $rootScope, NotificationService, $filter, ShareService, EncryptService, $translate) { |
||
| 37 | $scope.active_vault = VaultService.getActiveVault(); |
||
| 38 | if (!SettingsService.getSetting('defaultVault') || !SettingsService.getSetting('defaultVaultPass')) { |
||
| 39 | if (!$scope.active_vault) { |
||
| 40 | $location.path('/'); |
||
| 41 | } |
||
| 42 | } else { |
||
| 43 | if (SettingsService.getSetting('defaultVault') && SettingsService.getSetting('defaultVaultPass')) { |
||
| 44 | var _vault = angular.copy(SettingsService.getSetting('defaultVault')); |
||
| 45 | _vault.vaultKey = SettingsService.getSetting('defaultVaultPass'); |
||
| 46 | VaultService.setActiveVault(_vault); |
||
| 47 | VaultService.getVault(_vault).then(function (vault) { |
||
| 48 | vault.vaultKey = SettingsService.getSetting('defaultVaultPass'); |
||
| 49 | VaultService.setActiveVault(vault); |
||
| 50 | $scope.active_vault = vault; |
||
| 51 | $scope.$parent.selectedVault = true; |
||
| 52 | }); |
||
| 53 | } |
||
| 54 | } |
||
| 55 | |||
| 56 | if ($scope.active_vault) { |
||
| 57 | $scope.$parent.selectedVault = true; |
||
| 58 | } |
||
| 59 | var storedCredential = SettingsService.getSetting('revision_credential'); |
||
| 60 | |||
| 61 | var getRevisions = function () { |
||
| 62 | CredentialService.getRevisions($scope.storedCredential.guid).then(function (revisions) { |
||
| 63 | $scope.revisions = revisions; |
||
| 64 | }); |
||
| 65 | }; |
||
| 66 | |||
| 67 | if (!storedCredential) { |
||
| 68 | CredentialService.getCredential($routeParams.credential_id).then(function (result) { |
||
| 69 | $scope.storedCredential = CredentialService.decryptCredential(angular.copy(result)); |
||
| 70 | getRevisions(); |
||
| 71 | }); |
||
| 72 | } else { |
||
| 73 | $scope.storedCredential = CredentialService.decryptCredential(angular.copy(storedCredential)); |
||
| 74 | getRevisions(); |
||
| 75 | } |
||
| 76 | |||
| 77 | $scope.selectRevision = function (revision) { |
||
| 78 | var key; |
||
| 79 | $scope.selectedRevision = angular.copy(revision); |
||
| 80 | |||
| 81 | if (!$scope.storedCredential.hasOwnProperty('acl') && $scope.storedCredential.hasOwnProperty('shared_key')) { |
||
| 82 | if ($scope.storedCredential.shared_key) { |
||
| 83 | key = EncryptService.decryptString(angular.copy($scope.storedCredential.shared_key)); |
||
| 84 | } |
||
| 85 | } |
||
| 86 | if ($scope.storedCredential.hasOwnProperty('acl')) { |
||
| 87 | key = EncryptService.decryptString(angular.copy($scope.storedCredential.acl.shared_key)); |
||
| 88 | } |
||
| 89 | |||
| 90 | if (key) { |
||
| 91 | $scope.selectedRevision.credential_data = ShareService.decryptSharedCredential(angular.copy(revision.credential_data), key); |
||
| 92 | } else { |
||
| 93 | $scope.selectedRevision.credential_data = CredentialService.decryptCredential(angular.copy(revision.credential_data)); |
||
| 94 | } |
||
| 95 | |||
| 96 | $rootScope.$emit('app_menu', true); |
||
| 97 | }; |
||
| 98 | |||
| 99 | $scope.closeSelected = function () { |
||
| 100 | $rootScope.$emit('app_menu', false); |
||
| 101 | $scope.selectedRevision = false; |
||
| 102 | }; |
||
| 103 | |||
| 104 | $scope.deleteRevision = function (revision) { |
||
| 105 | CredentialService.deleteRevision($scope.storedCredential.guid, revision.revision_id).then(function () { |
||
| 106 | for (var i = 0; i < $scope.revisions.length; i++) { |
||
| 107 | if ($scope.revisions[i].revision_id === revision.revision_id) { |
||
| 108 | $scope.revisions.splice(i, 1); |
||
| 109 | NotificationService.showNotification($translate.instant('revision.deleted'), 5000); |
||
| 110 | break; |
||
| 111 | } |
||
| 112 | } |
||
| 113 | }); |
||
| 114 | }; |
||
| 115 | |||
| 116 | $scope.restoreRevision = function (revision) { |
||
| 117 | var key; |
||
| 118 | var _revision = angular.copy(revision); |
||
| 119 | var _credential = _revision.credential_data; |
||
| 120 | |||
| 121 | if (!$scope.storedCredential.hasOwnProperty('acl') && $scope.storedCredential.hasOwnProperty('shared_key')) { |
||
| 122 | if ($scope.storedCredential.shared_key) { |
||
| 123 | key = EncryptService.decryptString(angular.copy($scope.storedCredential.shared_key)); |
||
| 124 | } |
||
| 125 | } |
||
| 126 | if ($scope.storedCredential.hasOwnProperty('acl')) { |
||
| 127 | key = EncryptService.decryptString(angular.copy($scope.storedCredential.acl.shared_key)); |
||
| 128 | } |
||
| 129 | if (key) { |
||
| 130 | _credential = ShareService.encryptSharedCredential(_credential, key); |
||
| 131 | } |
||
| 132 | delete _credential.shared_key; |
||
| 133 | |||
| 134 | //Used in activity |
||
| 135 | _credential.revision_created = $filter('date')(_revision.created * 1000, "dd-MM-yyyy @ HH:mm:ss"); |
||
| 136 | CredentialService.updateCredential(_credential, (key)).then(function () { |
||
| 137 | SettingsService.setSetting('revision_credential', null); |
||
| 138 | $rootScope.$emit('app_menu', false); |
||
| 139 | $location.path('/vault/' + $routeParams.vault_id); |
||
| 140 | NotificationService.showNotification($translate.instant('revision.restored'), 5000); |
||
| 141 | }); |
||
| 142 | }; |
||
| 143 | |||
| 144 | $scope.cancelRevision = function () { |
||
| 145 | $location.path('/vault/' + $routeParams.vault_id); |
||
| 146 | $scope.storedCredential = null; |
||
| 147 | SettingsService.setSetting('revision_credential', null); |
||
| 148 | }; |
||
| 149 | |||
| 150 | }]); |
||
| 151 | |||
| 152 | }()); |