Completed
Push — phpunit ( 7f2080...75f541 )
by Marcos
14:28 queued 10:45
created

angular.controller(ꞌShareCtrlꞌ)   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 7
rs 9.4285
1
(function () {
2
	'use strict';
3
4
	/**
5
	 * @ngdoc function
6
	 * @name passmanApp.controller:MainCtrl
7
	 * @description
8
	 * # MainCtrl
9
	 * Controller of the passmanApp
10
	 * This file is part of passman, licensed under AGPLv3
11
	 */
12
	angular.module('passmanApp')
13
		.controller('ShareCtrl', ['$scope', 'VaultService', 'CredentialService', 'SettingsService', '$location', '$routeParams', 'ShareService', 'NotificationService', 'SharingACL', 'EncryptService',
14
			function ($scope, VaultService, CredentialService, SettingsService, $location, $routeParams, ShareService, NotificationService, SharingACL, EncryptService) {
15
				$scope.active_vault = VaultService.getActiveVault();
16
17
				$scope.tabs = [{
18
					title: 'Share with users and groups',
19
					url: 'views/partials/forms/share_credential/basics.html'
20
				}, {
21
					title: 'Share link',
22
					url: 'views/partials/forms/share_credential/link_sharing.html',
23
					color: 'green'
24
				}];
25
				$scope.currentTab = {
26
					title: 'General',
27
					url: 'views/partials/forms/share_credential/basics.html'
28
				};
29
30
				$scope.onClickTab = function (tab) {
31
					$scope.currentTab = tab;
32
				};
33
34
				$scope.isActiveTab = function (tab) {
35
					return tab.url === $scope.currentTab.url;
36
				};
37
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 = angular.copy(SettingsService.getSetting('defaultVaultPass'));
46
						VaultService.setActiveVault(_vault);
47
						$scope.active_vault = _vault;
48
49
					}
50
				}
51
				var storedCredential = SettingsService.getSetting('share_credential');
52
53
				if (!storedCredential) {
54
					$location.path('/vault/' + $routeParams.vault_id);
55
				} else {
56
					$scope.storedCredential = CredentialService.decryptCredential(angular.copy(storedCredential));
57
				}
58
59
				if ($scope.active_vault) {
60
					$scope.$parent.selectedVault = true;
61
				}
62
				$scope.cancel = function () {
63
					SettingsService.setSetting('share_credential', null);
64
					$location.path('/vault/' + $routeParams.vault_id);
65
				};
66
67
68
				$scope.default_permissions = new SharingACL(0);
69
				$scope.default_permissions.addPermission(
70
					$scope.default_permissions.permissions.READ |
71
					$scope.default_permissions.permissions.WRITE |
72
					$scope.default_permissions.permissions.FILES
73
				);
74
75
				var link_acl = angular.copy($scope.default_permissions);
76
				link_acl.removePermission($scope.default_permissions.permissions.WRITE);
77
78
				$scope.share_settings = {
79
					linkSharing: {
80
						enabled: false,
81
						settings: {
82
							expire_time: new Date("2999-12-31T22:59:59"),
83
							expire_views: 5,
84
							acl: link_acl
85
						}
86
					},
87
					credentialSharedWithUserAndGroup: [],
88
					cypher_progress: {
89
						done: 0,
90
						total: 0
91
					},
92
					upload_progress: {
93
						done: 0,
94
						total: 0
95
					}
96
				};
97
98
				var getAcl = function () {
99
					ShareService.getSharedCredentialACL($scope.storedCredential).then(function (aclList) {
100
						var _list = [];
101
						var enc_key = ($scope.storedCredential.shared_key) ? EncryptService.decryptString(angular.copy($scope.storedCredential.shared_key)) : false;
102
						for (var i = 0; i < aclList.length; i++) {
103
							var acl = aclList[i];
104
							if (acl.user_id === null) {
105
								$scope.share_settings.linkSharing = {
106
									enabled: true,
107
									settings: {
108
										expire_time: new Date(acl.expire * 1000),
109
										expire_views: acl.expire_views,
110
										acl: new SharingACL(acl.permissions)
111
									}
112
								};
113
								if (enc_key) {
114
									var hash = window.btoa($scope.storedCredential.guid + '<::>' + enc_key);
115
									$scope.share_link = $location.$$protocol + '://' + $location.$$host + OC.generateUrl('apps/passman/share/public#') + hash;
116
								}
117
							} else {
118
								var obj = {
119
									userId: acl.user_id,
120
									displayName: acl.user_id,
121
									type: 'user',
122
									acl: new SharingACL(acl.permissions),
123
									acl_id: acl.acl_id,
124
									pending: acl.pending,
125
									credential_guid: acl.item_guid,
126
									created: acl.created
127
								};
128
129
								_list.push(obj);
130
							}
131
132
						}
133
						$scope.share_settings.credentialSharedWithUserAndGroup = _list;
134
					});
135
				};
136
				getAcl();
137
				var acl = new SharingACL(0);
138
139
140
				$scope.$watch('share_settings.upload_progress.done', function () {
141
										if ($scope.share_settings.upload_progress.done === $scope.share_settings.upload_progress.total && $scope.share_settings.upload_progress.total > 0) {
142
						getAcl();
143
					}
144
				});
145
146
				$scope.inputSharedWith = [];
147
148
				$scope.searchUsers = function ($query) {
149
					return ShareService.search($query);
150
				};
151
152
				$scope.hasPermission = function (acl, permission) {
153
					return acl.hasPermission(permission);
154
				};
155
156
				$scope.setPermission = function (acl, permission) {
157
					acl.togglePermission(permission);
158
				};
159
				$scope.shareWith = function (shareWith) {
160
					$scope.inputSharedWith = [];
161
					if (shareWith.length > 0) {
162
						for (var i = 0; i < shareWith.length; i++) {
163
							var obj = {
164
								userId: shareWith[i].uid,
165
								displayName: shareWith[i].text,
166
								type: shareWith[i].type,
167
								acl: angular.copy($scope.default_permissions),
168
								pending: true,
169
								credential_guid: $scope.storedCredential.guid
170
							};
171
							var found = false;
172
							for (var z = 0; z < $scope.share_settings.credentialSharedWithUserAndGroup.length; z++) {
173
								if ($scope.share_settings.credentialSharedWithUserAndGroup[z].userId === shareWith[z].uid) {
174
									found = true;
175
								}
176
							}
177
							if (found === false) {
178
								$scope.share_settings.credentialSharedWithUserAndGroup.push(obj);
179
							}
180
						}
181
					}
182
				};
183
184
				$scope.unshareUser = function (user) {
185
					ShareService.unshareCredentialFromUser($scope.storedCredential, user.userId).then(function (result) {
186
						if (result.result === true) {
187
							var idx = $scope.share_settings.credentialSharedWithUserAndGroup.indexOf(user);
188
							$scope.share_settings.credentialSharedWithUserAndGroup.splice(idx, 1);
189
						}
190
					});
191
				};
192
193
				$scope.unshareCredential = function (credential) {
194
					ShareService.unshareCredential(credential);
195
					var _credential = angular.copy(credential);
196
					var old_key = EncryptService.decryptString(angular.copy(_credential.shared_key));
197
					var new_key = VaultService.getActiveVault().vaultKey;
198
					_credential.shared_key = null;
199
					_credential.unshare_action = true;
200
					_credential.skip_revision = true;
201
202
					_credential = CredentialService.encryptCredential(_credential, old_key);
203
					CredentialService.updateCredential(_credential, true).then(function () {
204
						NotificationService.showNotification('Credential unshared', 4000);
205
						CredentialService.reencryptCredential(_credential.guid, old_key, new_key).then(function () {
206
							getAcl();
207
						});
208
					});
209
				};
210
211
				/**
212
				 * Apply a share to a new user
213
				 * @param user A user object to who we should share the data
214
				 * @param enc_key The shared key we are going to ecnrypt with his public rsa key
215
				 */
216
				$scope.applyShareToUser = function (user, enc_key) {
217
					ShareService.getVaultsByUser(user.userId).then(function (data) {
218
						$scope.share_settings.cypher_progress.total += data.length;
219
220
						user.vaults = data;
221
						var start = new Date().getTime() / 1000;
222
						ShareService.cypherRSAStringWithPublicKeyBulkAsync(user.vaults, enc_key)
223
							.progress(function () {
224
								$scope.share_settings.cypher_progress.done++;
225
								$scope.share_settings.cypher_progress.percent = $scope.share_settings.cypher_progress.done / $scope.share_settings.cypher_progress.total * 100;
226
								$scope.$digest();
227
							})
228
							.then(function (result) {
229
																$scope.share_settings.cypher_progress.times.push({
230
									time: ((new Date().getTime() / 1000) - start),
231
									user: data[0].user_id
232
								});
233
								user.vaults = result;
234
								if (!user.hasOwnProperty('acl_id')) {
235
									$scope.uploadChanges(user);
236
								}
237
								$scope.$digest();
238
							});
239
					});
240
				};
241
242
243
244
				$scope.$on("$locationChangeStart", function(event) {
245
					if(!$scope.sharing_complete){
246
						if(!confirm("Are you sure you want to leave?\nThis will corrupt this credential")){
247
							event.preventDefault();
248
						}
249
					}
250
				});
251
252
				$scope.sharing_complete = true;
253
				$scope.applyShare = function () {
254
					$scope.sharing_complete = false;
255
					$scope.share_settings.cypher_progress.percent = 0;
256
					$scope.share_settings.cypher_progress.done = 0;
257
					$scope.share_settings.cypher_progress.total = 0;
258
					$scope.share_settings.cypher_progress.times = [];
259
					$scope.share_settings.cypher_progress.times_total = [];
260
					$scope.share_settings.upload_progress.done = 0;
261
					$scope.share_settings.upload_progress.total = 0;
262
					//Credential is already shared
263
					if ($scope.storedCredential.shared_key && $scope.storedCredential.shared_key !== '' && $scope.storedCredential.shared_key !== null) {
264
												var enc_key = EncryptService.decryptString(angular.copy($scope.storedCredential.shared_key));
265
						if ($scope.share_settings.linkSharing.enabled) {
266
							var expire_time = new Date(angular.copy($scope.share_settings.linkSharing.settings.expire_time)).getTime() / 1000;
267
							var shareObj = {
268
								item_id: $scope.storedCredential.credential_id,
269
								item_guid: $scope.storedCredential.guid,
270
								permissions: $scope.share_settings.linkSharing.settings.acl.getAccessLevel(),
271
								expire_timestamp: expire_time,
272
								expire_views: $scope.share_settings.linkSharing.settings.expire_views
273
							};
274
							ShareService.createPublicSharedCredential(shareObj).then(function () {
275
								var hash = window.btoa($scope.storedCredential.guid + '<::>' + enc_key);
276
								$scope.share_link = $location.$$protocol + '://' + $location.$$host + OC.generateUrl('apps/passman/share/public#') + hash;
277
							});
278
						}
279
280
						var list = $scope.share_settings.credentialSharedWithUserAndGroup;
281
282
						for (var i = 0; i < list.length; i++) {
283
							var iterator = i;
284
							var target_user = list[i];
285
							if (target_user.hasOwnProperty('created')) {
286
								var acl = {
287
									user_id: target_user.userId,
288
									permission: target_user.acl.getAccessLevel()
289
								};
290
								ShareService.updateCredentialAcl($scope.storedCredential, acl);
291
							} else {
292
								$scope.applyShareToUser(list[iterator], enc_key);
293
							}
294
						}
295
						NotificationService.showNotification('Saved!', 4000);
296
						$scope.sharing_complete = true;
297
					} else {
298
299
						ShareService.generateSharedKey(20).then(function (key) {
300
301
							var encryptedSharedCredential = angular.copy($scope.storedCredential);
302
							var old_key = VaultService.getActiveVault().vaultKey;
303
304
							CredentialService.reencryptCredential(encryptedSharedCredential.guid, old_key, key).progress(function () {
305
															}).then(function (data) {
306
								var _credential = data.cryptogram;
307
								_credential.set_share_key = true;
308
								_credential.skip_revision = true;
309
								_credential.shared_key = EncryptService.encryptString(key);
310
								CredentialService.updateCredential(_credential, true).then(function () {
311
									NotificationService.showNotification('Credential shared', 4000);
312
									$scope.sharing_complete = true;
313
								});
314
							});
315
316
							var list = $scope.share_settings.credentialSharedWithUserAndGroup;
317
							for (var i = 0; i < list.length; i++) {
318
								if (list[i].type === "user") {
319
									$scope.applyShareToUser(list[i], key);
320
								}
321
							}
322
323
							if ($scope.share_settings.linkSharing.enabled) {
324
								var expire_time = new Date(angular.copy($scope.share_settings.linkSharing.settings.expire_time)).getTime() / 1000;
325
								var shareObj = {
326
									item_id: $scope.storedCredential.credential_id,
327
									item_guid: $scope.storedCredential.guid,
328
									permissions: $scope.share_settings.linkSharing.settings.acl.getAccessLevel(),
329
									expire_timestamp: expire_time,
330
									expire_views: $scope.share_settings.linkSharing.settings.expire_views
331
								};
332
								ShareService.createPublicSharedCredential(shareObj).then(function () {
333
									var hash = window.btoa($scope.storedCredential.guid + '<::>' + key);
334
									$scope.share_link = $location.$$protocol + '://' + $location.$$host + OC.generateUrl('apps/passman/share/public#') + hash;
335
336
								});
337
							}
338
339
						});
340
					}
341
				};
342
343
				$scope.uploadChanges = function (user) {
344
					$scope.share_settings.upload_progress.total++;
345
346
					user.accessLevel = angular.copy(user.acl.getAccessLevel());
347
					ShareService.shareWithUser(storedCredential, user)
348
						.then(function () {
349
							$scope.share_settings.upload_progress.done++;
350
							$scope.share_settings.upload_progress.percent = $scope.share_settings.upload_progress.done / $scope.share_settings.upload_progress.total * 100;
351
						});
352
				};
353
354
				$scope.calculate_total_time = function () {
355
					$scope.share_settings.cypher_progress.times = $scope.share_settings.cypher_progress.times || [];
356
					var total = 0;
357
					for (var i = 0; i < $scope.share_settings.cypher_progress.times.length; i++) {
358
						total += $scope.share_settings.cypher_progress.times[i].time;
359
					}
360
					return total;
361
				};
362
			}]);
363
}());