Conditions | 1 |
Paths | 4 |
Total Lines | 225 |
Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 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 | * @ngdoc function |
||
28 | * @name passmanApp.controller:MainCtrl |
||
29 | * @description |
||
30 | * # MainCtrl |
||
31 | * Controller of the passmanApp |
||
32 | */ |
||
33 | angular.module('passmanApp') |
||
34 | .controller('VaultCtrl', ['$scope', 'VaultService', 'SettingsService', 'CredentialService', '$location', 'ShareService', 'EncryptService', '$translate', '$rootScope', '$interval', |
||
35 | function ($scope, VaultService, SettingsService, CredentialService, $location, ShareService, EncryptService, $translate, $rootScope, $interval) { |
||
36 | VaultService.getVaults().then(function (vaults) { |
||
37 | $scope.vaults = vaults; |
||
38 | if (SettingsService.getSetting('defaultVault') != null) { |
||
|
|||
39 | var default_vault = SettingsService.getSetting('defaultVault'); |
||
40 | |||
41 | /** |
||
42 | * Using a native for loop for preformance reasons. |
||
43 | * More info see http://stackoverflow.com/questions/13843972/angular-js-break-foreach |
||
44 | */ |
||
45 | for (var i = 0; i < vaults.length; i++) { |
||
46 | var vault = vaults[i]; |
||
47 | if (vault.guid === default_vault.guid) { |
||
48 | $scope.default_vault = true; |
||
49 | $scope.list_selected_vault = vault; |
||
50 | SettingsService.setSetting('defaultVault', vault); |
||
51 | if (SettingsService.getSetting('defaultVaultPass')) { |
||
52 | $location.path('/vault/' + vault.guid); |
||
53 | } |
||
54 | $scope.vault_tries[vault.guid] = { |
||
55 | tries: 0, |
||
56 | timeout: 0 |
||
57 | }; |
||
58 | break; |
||
59 | } |
||
60 | } |
||
61 | } |
||
62 | }); |
||
63 | |||
64 | |||
65 | var key_strengths = [ |
||
66 | 'password.poor', |
||
67 | 'password.poor', |
||
68 | 'password.weak', |
||
69 | 'password.good', |
||
70 | 'password.strong' |
||
71 | ]; |
||
72 | |||
73 | $scope.default_vault = false; |
||
74 | $scope.remember_vault_password = false; |
||
75 | $scope.auto_logout_timer = false; |
||
76 | $scope.logout_timer = '0'; |
||
77 | $scope.list_selected_vault = false; |
||
78 | $scope.minimal_value_key_strength = 3; |
||
79 | |||
80 | var settingsLoaded = function () { |
||
81 | $scope.minimal_value_key_strength = SettingsService.getSetting('vault_key_strength'); |
||
82 | $translate(key_strengths[SettingsService.getSetting('vault_key_strength')]).then(function (translation) { |
||
83 | $scope.required_score = {'strength': translation}; |
||
84 | }); |
||
85 | }; |
||
86 | |||
87 | if (!SettingsService.getSetting('settings_loaded')) { |
||
88 | $rootScope.$on('settings_loaded', function () { |
||
89 | settingsLoaded(); |
||
90 | }); |
||
91 | } else { |
||
92 | settingsLoaded(); |
||
93 | } |
||
94 | |||
95 | $scope.toggleDefaultVault = function () { |
||
96 | $scope.default_vault = !$scope.default_vault; |
||
97 | if ($scope.default_vault === true) { |
||
98 | SettingsService.setSetting('defaultVault', $scope.list_selected_vault); |
||
99 | } else { |
||
100 | SettingsService.setSetting('defaultVault', null); |
||
101 | } |
||
102 | }; |
||
103 | |||
104 | $scope.toggleRememberPassword = function () { |
||
105 | $scope.remember_vault_password = !$scope.remember_vault_password; |
||
106 | if ($scope.remember_vault_password) { |
||
107 | SettingsService.setSetting('defaultVault', $scope.list_selected_vault); |
||
108 | $scope.default_vault = true; |
||
109 | } |
||
110 | if ($scope.remember_vault_password !== true) { |
||
111 | SettingsService.setSetting('defaultVault', null); |
||
112 | } |
||
113 | }; |
||
114 | |||
115 | $scope.toggleAutoLogout = function () { |
||
116 | $scope.auto_logout_timer = !$scope.auto_logout_timer; |
||
117 | }; |
||
118 | |||
119 | $scope.clearState = function () { |
||
120 | $scope.list_selected_vault = false; |
||
121 | $scope.creating_vault = false; |
||
122 | $scope.error = false; |
||
123 | }; |
||
124 | |||
125 | $scope.selectVault = function (vault) { |
||
126 | $scope.list_selected_vault = vault; |
||
127 | if(!$scope.vault_tries[vault.guid]) { |
||
128 | $scope.vault_tries[vault.guid] = { |
||
129 | tries: 0, |
||
130 | timeout: 0 |
||
131 | }; |
||
132 | } |
||
133 | }; |
||
134 | $scope.sharing_keys = {}; |
||
135 | $scope.newVault = function () { |
||
136 | $scope.creating_vault = true; |
||
137 | var key_size = 1024; |
||
138 | ShareService.generateRSAKeys(key_size).progress(function (progress) { |
||
139 | var p = progress > 0 ? 2 : 1; |
||
140 | var msg = $translate.instant('generating.sharing.keys'); |
||
141 | msg = msg.replace('%step', p); |
||
142 | $scope.creating_keys = msg; |
||
143 | $scope.$digest(); |
||
144 | }).then(function (kp) { |
||
145 | var pem = ShareService.rsaKeyPairToPEM(kp); |
||
146 | $scope.creating_keys = false; |
||
147 | $scope.sharing_keys.private_sharing_key = pem.privateKey; |
||
148 | $scope.sharing_keys.public_sharing_key = pem.publicKey; |
||
149 | $scope.$digest(); |
||
150 | }); |
||
151 | |||
152 | }; |
||
153 | |||
154 | var _loginToVault = function (vault, vault_key) { |
||
155 | var _vault = angular.copy(vault); |
||
156 | _vault.vaultKey = angular.copy(vault_key); |
||
157 | delete _vault.credentials; |
||
158 | var timer = parseInt($scope.logout_timer); |
||
159 | if ($scope.auto_logout_timer && timer > 0) { |
||
160 | $rootScope.$broadcast('logout_timer_set', timer * 60); |
||
161 | } |
||
162 | |||
163 | VaultService.setActiveVault(_vault); |
||
164 | $location.path('/vault/' + vault.guid); |
||
165 | }; |
||
166 | |||
167 | $scope.selectLogoutTimer = function (time) { |
||
168 | $scope.auto_logout_timer = true; |
||
169 | $scope.logout_timer = time; |
||
170 | }; |
||
171 | |||
172 | var tickLockTimer = function (guid) { |
||
173 | $scope.vault_tries[guid].timeout = $scope.vault_tries[guid].timeout - 1; |
||
174 | if($scope.vault_tries[guid].timeout === 0){ |
||
175 | $interval.cancel($scope.vault_tries[guid].timer); |
||
176 | } |
||
177 | }; |
||
178 | |||
179 | $scope.vault_tries = {}; |
||
180 | |||
181 | $scope.vaultDecryptionKey = ''; |
||
182 | $scope.loginToVault = function (vault, vault_key) { |
||
183 | $scope.error = false; |
||
184 | var _vault = angular.copy(vault); |
||
185 | _vault.vaultKey = angular.copy(vault_key); |
||
186 | |||
187 | VaultService.setActiveVault(_vault); |
||
188 | try { |
||
189 | EncryptService.decryptString(vault.challenge_password); |
||
190 | if ($scope.remember_vault_password) { |
||
191 | SettingsService.setSetting('defaultVaultPass', vault_key); |
||
192 | } |
||
193 | _loginToVault(vault, vault_key); |
||
194 | |||
195 | } catch (e) { |
||
196 | $scope.error = $translate.instant('invalid.vault.key'); |
||
197 | |||
198 | $scope.vault_tries[vault.guid].tries = $scope.vault_tries[vault.guid].tries + 1; |
||
199 | |||
200 | if($scope.vault_tries[vault.guid].tries >= 3){ |
||
201 | var to = $scope.vault_tries[vault.guid].tries * $scope.vault_tries[vault.guid].tries * 2; |
||
202 | if(to < 30){ |
||
203 | to = 30; |
||
204 | } |
||
205 | $scope.vault_tries[vault.guid].timeout = to; |
||
206 | |||
207 | if($scope.vault_tries[vault.guid].hasOwnProperty('timer')){ |
||
208 | $interval.cancel($scope.vault_tries[vault.guid].timer); |
||
209 | } |
||
210 | |||
211 | $scope.vault_tries[vault.guid].timer = $interval(function () { |
||
212 | tickLockTimer(vault.guid); |
||
213 | } ,1000); |
||
214 | } |
||
215 | |||
216 | } |
||
217 | }; |
||
218 | |||
219 | |||
220 | $scope.createVault = function (vault_name, vault_key, vault_key2) { |
||
221 | if (vault_key !== vault_key2) { |
||
222 | $scope.error = $translate.instant('password.do.not.match'); |
||
223 | return; |
||
224 | } |
||
225 | VaultService.createVault(vault_name).then(function (vault) { |
||
226 | $scope.vaults.push(vault); |
||
227 | var _vault = angular.copy(vault); |
||
228 | _vault.vaultKey = angular.copy(vault_key); |
||
229 | VaultService.setActiveVault(_vault); |
||
230 | SettingsService.setSetting('defaultVaultPass', null); |
||
231 | SettingsService.setSetting('defaultVault', null); |
||
232 | var test_credential = CredentialService.newCredential(); |
||
233 | test_credential.label = 'Test key for vault ' + vault_name; |
||
234 | test_credential.hidden = true; |
||
235 | test_credential.vault_id = vault.vault_id; |
||
236 | test_credential.password = 'lorum ipsum'; |
||
237 | CredentialService.createCredential(test_credential).then(function () { |
||
238 | _vault.public_sharing_key = angular.copy($scope.sharing_keys.public_sharing_key); |
||
239 | _vault.private_sharing_key = EncryptService.encryptString(angular.copy($scope.sharing_keys.private_sharing_key)); |
||
240 | VaultService.updateSharingKeys(_vault).then(function () { |
||
241 | _loginToVault(vault, vault_key); |
||
242 | }); |
||
243 | }); |
||
244 | }); |
||
245 | }; |
||
246 | }]); |
||
247 | }()); |
Generally, it is recommended to use strict comparison whenever possible and not to rely on the weaker type-juggling comparison operator.
Read more about comparison operations.