Completed
Push — master ( ad7bcf...5907f1 )
by Sander
9s
created

angular.controller(ꞌSettingsCtrlꞌ)   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 22
rs 9.2
c 1
b 0
f 0
1
(function () {
2
	'use strict';
3
4
5
	/**
6
	 * @ngdoc function
7
	 * @name passmanApp.controller:SettingsCtrl
8
	 * @description
9
	 * # SettingsCtrl
10
	 * Controller of the passmanApp
11
	 */
12
	angular.module('passmanApp')
13
		.controller('SettingsCtrl', ['$scope', '$rootScope', 'SettingsService', 'VaultService', 'CredentialService', '$location', '$routeParams', '$http', 'EncryptService', 'NotificationService', '$sce',
14
			function ($scope, $rootScope, SettingsService, VaultService, CredentialService, $location, $routeParams, $http, EncryptService, NotificationService, $sce) {
15
				$scope.vault_settings = {};
16
				$scope.new_vault_name = '';
17
				$scope.active_vault = VaultService.getActiveVault();
18
				if (!SettingsService.getSetting('defaultVault') || !SettingsService.getSetting('defaultVaultPass')) {
19
					if (!$scope.active_vault) {
20
						$location.path('/');
21
						return;
22
					}
23
				} else {
24
					if (SettingsService.getSetting('defaultVault') && SettingsService.getSetting('defaultVaultPass')) {
25
						var _vault = angular.copy(SettingsService.getSetting('defaultVault'));
26
						_vault.vaultKey = SettingsService.getSetting('defaultVaultPass');
27
						VaultService.setActiveVault(_vault);
28
						$scope.active_vault = _vault;
29
					}
30
				}
31
32
				VaultService.getVault($scope.active_vault).then(function (vault) {
33
					vault.vaultKey = SettingsService.getSetting('defaultVaultPass');
34
					delete vault.credentials;
35
					VaultService.setActiveVault(vault);
36
					console.log(vault);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
37
					$scope.vault_settings = vault.vault_settings;
38
					if(!$scope.vault_settings.hasOwnProperty('pwSettings')){
39
						$scope.vault_settings.pwSettings = {
40
							'length': 12,
41
							'useUppercase': true,
42
							'useLowercase': true,
43
							'useDigits': true,
44
							'useSpecialChars': true,
45
							'minimumDigitCount': 3,
46
							'avoidAmbiguousCharacters': false,
47
							'requireEveryCharType': true,
48
							'generateOnCreate': true
49
						};
50
					}
51
				});
52
53
54
55
				var http = location.protocol, slashes = http.concat("//"), host = slashes.concat(window.location.hostname), complete = host + location.pathname;
56
				$scope.bookmarklet = $sce.trustAsHtml("<a class=\"button\" href=\"javascript:(function(){var a=window,b=document,c=encodeURIComponent,e=c(document.title),d=a.open('" + complete + "bookmarklet?url='+c(b.location)+'&title='+e,'bkmk_popup','left='+((a.screenX||a.screenLeft)+10)+',top='+((a.screenY||a.screenTop)+10)+',height=750px,width=475px,resizable=0,alwaysRaised=1');a.setTimeout(function(){d.focus()},300);})();\">Save in passman</a>");
57
58
59
				$scope.saveVaultSettings = function () {
60
					var _vault = $scope.active_vault;
61
					_vault.name = $scope.new_vault_name;
62
					_vault.vault_settings = angular.copy($scope.vault_settings);
63
					VaultService.updateVault(_vault).then(function () {
64
						//VaultService.setActiveVault(_vault);
65
						$scope.active_vault.name = angular.copy(_vault.name);
66
						NotificationService.showNotification('Settings saved', 5000);
67
					});
68
				};
69
70
71
				$scope.tabs = [
72
					{
73
						title: 'General settings',
74
						url: 'views/partials/forms/settings/general_settings.html'
75
					},
76
					{
77
						title: 'Password Audit',
78
						url: 'views/partials/forms/settings/tool.html'
79
80
					},
81
					{
82
						title: 'Password settings',
83
						url: 'views/partials/forms/settings/password_settings.html'
84
85
					},
86
					{
87
						title: 'Import credentials',
88
						url: 'views/partials/forms/settings/import.html'
89
90
					},
91
					{
92
						title: 'Export credentials',
93
						url: 'views/partials/forms/settings/export.html'
94
95
					},
96
					{
97
						title: 'Sharing',
98
						url: 'views/partials/forms/settings/sharing.html'
99
					}
100
				];
101
102
				$scope.currentTab = $scope.tabs[0];
103
104
				$scope.onClickTab = function (tab) {
105
					$scope.currentTab = tab;
106
				};
107
108
				$scope.isActiveTab = function (tab) {
109
					return tab.url === $scope.currentTab.url;
110
				};
111
112
				var getPassmanVersion = function () {
113
					var url = OC.generateUrl('apps/passman/api/internal/version');
114
					$http.get(url).then(function (result) {
115
						$scope.passman_version = result.data.version;
116
					});
117
				};
118
				getPassmanVersion();
119
120
				$scope.$watch(function () {
121
					return VaultService.getActiveVault();
122
				}, function (vault) {
123
					if (vault) {
124
						$scope.active_vault = vault;
125
					}
126
				});
127
128
				if ($scope.active_vault) {
0 ignored issues
show
Comprehensibility Documentation Best Practice introduced by
This code block is empty. Consider removing it or adding a comment to explain.
Loading history...
129
130
				}
131
132
				$rootScope.$on('logout', function () {
133
					$scope.selectedVault = false;
134
				});
135
				$scope.startScan = function (minStrength) {
136
					VaultService.getVault($scope.active_vault).then(function (vault) {
137
						var results = [];
138
						for (var i = 0; i < vault.credentials.length; i++) {
139
							var c = angular.copy(vault.credentials[i]);
140
							if (c.password && c.hidden === 0) {
141
								c = CredentialService.decryptCredential(c);
142
								if (c.password) {
143
									var zxcvbn_result = zxcvbn(c.password);
144
									if (zxcvbn_result.score <= minStrength) {
145
										results.push({
146
											credential_id: c.credential_id,
147
											label: c.label,
148
											password: c.password,
149
											password_zxcvbn_result: zxcvbn_result
150
										});
151
									}
152
								}
153
154
							}
155
							//@todo loop custom fields (if any and check secret fields
156
						}
157
						$scope.scan_result = results;
158
					});
159
				};
160
161
162
				$scope.cur_state = {};
163
164
				$scope.changeVaultPassword = function (oldVaultPass, newVaultPass, newVaultPass2) {
165
					if (oldVaultPass !== VaultService.getActiveVault().vaultKey) {
166
						$scope.error = 'Your old password is incorrect!';
167
						return;
168
					}
169
					if (newVaultPass !== newVaultPass2) {
170
						$scope.error = 'New passwords do not match!';
171
						return;
172
					}
173
					VaultService.getVault($scope.active_vault).then(function (vault) {
174
						var _selected_credentials = [];
175
						if (vault.credentials.length === 0) {
176
							$location.path('/');
177
						}
178
						for (var i = 0; i < vault.credentials.length; i++) {
179
							var _credential = vault.credentials[i];
180
							if (_credential.shared_key === null || _credential.shared_key === '') {
181
								_selected_credentials.push(_credential);
182
							}
183
						}
184
						$scope.change_pw = {
185
							percent: 0,
186
							done: 0,
187
							total: _selected_credentials.length
188
						};
189
						var changeCredential = function (index, oldVaultPass, newVaultPass) {
190
							CredentialService.reencryptCredential(_selected_credentials[index].guid, oldVaultPass, newVaultPass).progress(function (data) {
191
								$scope.cur_state = data;
192
							}).then(function (data) {
0 ignored issues
show
Unused Code introduced by
The parameter data is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
193
								var percent = index / _selected_credentials.length * 100;
194
								$scope.change_pw = {
195
									percent: percent,
196
									done: index + 1,
197
									total: _selected_credentials.length
198
								};
199
								if (index < _selected_credentials.length - 1) {
200
									changeCredential(index + 1, oldVaultPass, newVaultPass);
201
								} else {
202
									vault.private_sharing_key = EncryptService.decryptString(angular.copy(vault.private_sharing_key), oldVaultPass);
203
									vault.private_sharing_key = EncryptService.encryptString(vault.private_sharing_key, newVaultPass);
204
									VaultService.updateSharingKeys(vault).then(function (result) {
0 ignored issues
show
Unused Code introduced by
The parameter result is not used and could be removed.

This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.

Loading history...
205
										$rootScope.$broadcast('logout');
206
										NotificationService.showNotification('Please login with your new vault password', 5000);
207
									});
208
								}
209
							});
210
						};
211
						changeCredential(0, VaultService.getActiveVault().vaultKey, newVaultPass);
212
213
					});
214
				};
215
216
				$rootScope.$on('logout', function () {
217
					$scope.active_vault = null;
218
					VaultService.setActiveVault(null);
219
					$location.path('/');
220
221
				});
222
223
				$scope.cancel = function () {
224
					$location.path('/vault/' + $routeParams.vault_id);
225
				};
226
227
			}]);
228
229
}());