Completed
Push — master ( e4d488...d7a2df )
by Sander
30s
created

js/ui/popup/controllers/edit.js (1 issue)

1
/* global API */
2
3
/**
4
 * Nextcloud - passman
5
 *
6
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
7
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
8
 * @license GNU AGPL version 3 or any later version
9
 *
10
 * This program is free software: you can redistribute it and/or modify
11
 * it under the terms of the GNU Affero General Public License as
12
 * published by the Free Software Foundation, either version 3 of the
13
 * License, or (at your option) any later version.
14
 *
15
 * This program is distributed in the hope that it will be useful,
16
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18
 * GNU Affero General Public License for more details.
19
 *
20
 * You should have received a copy of the GNU Affero General Public License
21
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22
 *
23
 */
24
25
(function () {
26
    'use strict';
27
28
    /**
29
     * @ngdoc function
30
     * @name passmanApp.controller:MainCtrl
31
     * @description
32
     * # MainCtrl
33
     * Controller of the passmanApp
34
     */
35
    angular.module('passmanExtension')
36
        .controller('EditCtrl', ['$scope', '$routeParams', '$timeout', 'notify', function ($scope, $routeParams, $timeout, notify) {
37
            API.runtime.sendMessage(API.runtime.id, {
38
                method: "getCredentialByGuid",
39
                args: $routeParams.guid
40
            }).then(function (credential) {
41
                $scope.canEdit = true;
42
                if(credential.hasOwnProperty('acl')) {
43
                    var permissions = new SharingACL(credential.acl.permissions.permission);
44
                    $scope.canEdit = permissions.hasPermission(0x02);
45
                }
46
                $scope.credential = credential;
47
                $scope.credential.password_repeat = angular.copy(credential.password);
48
                $scope.$apply();
49
            });
50
51
            var storage = new API.Storage();
52
53
            $scope.tabActive = 1;
54
55
            function genPwd(settings) {
56
                /* jshint ignore:start */
57
                var password = generatePassword(settings['length'],
58
                    settings.useUppercase,
59
                    settings.useLowercase,
60
                    settings.useDigits,
61
                    settings.useSpecialChars,
62
                    settings.minimumDigitCount,
63
                    settings.avoidAmbiguousCharacters,
64
                    settings.requireEveryCharType);
65
                /* jshint ignore:end */
66
                return password;
67
            }
68
69
            $scope.pw_settings = null;
70
            function getPasswordGenerationSettings(cb) {
0 ignored issues
show
The parameter cb 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...
71
                var default_settings = {
72
                    'length': 12,
73
                    'useUppercase': true,
74
                    'useLowercase': true,
75
                    'useDigits': true,
76
                    'useSpecialChars': true,
77
                    'minimumDigitCount': 3,
78
                    'avoidAmbiguousCharacters': false,
79
                    'requireEveryCharType': true
80
                };
81
                storage.get('password_generator_settings').then(function (_settings) {
82
                    if (!_settings) {
83
                        _settings = default_settings;
84
                    }
85
86
                    $scope.pw_settings = _settings;
87
                }).error(function () {
88
                    $scope.pw_settings = default_settings;
89
                });
90
            }
91
92
            getPasswordGenerationSettings();
93
94
            var custom_field = {
95
                label: '',
96
                value: '',
97
                field_type: 'text',
98
                secret: false
99
            };
100
101
            $scope.new_custom_field = angular.copy(custom_field);
102
103
            $scope.addCustomField = function (_field) {
104
                var field = angular.copy(_field);
105
                if (!field.label || !field.value) {
106
                    return;
107
                }
108
                $scope.credential.custom_fields.push(field);
109
                $scope.new_custom_field = angular.copy(custom_field);
110
            };
111
112
            $scope.deleteCustomField = function (field) {
113
                var idx = $scope.credential.custom_fields.indexOf(field);
114
                $scope.credential.custom_fields.splice(idx, 1);
115
            };
116
117
            $scope.pwFieldShown = false;
118
119
            $scope.togglePwField = function () {
120
                $scope.pwFieldShown = !$scope.pwFieldShown;
121
            };
122
123
            var round = 0;
124
            $scope.generatePassword = function () {
125
                var new_password = genPwd($scope.pw_settings);
126
                $scope.credential.password = new_password;
127
                $scope.credential.password_repeat = new_password;
128
                $timeout(function () {
129
                    if (round < 10) {
130
                        $scope.generatePassword();
131
                        round++;
132
                    } else {
133
                        round = 0;
134
                    }
135
                }, 10);
136
            };
137
            $scope.saving = false;
138
            $scope.saveCredential = function () {
139
                if(!$scope.canEdit){
140
                    return;
141
                }
142
                if (!$scope.credential.label) {
143
                    notify(API.i18n.getMessage('label_required'));
144
                    return;
145
                }
146
147
                if ($scope.credential.password !== $scope.credential.password_repeat) {
148
                    notify(API.i18n.getMessage('no_password_match'));
149
                    return;
150
                }
151
152
                $scope.saving = true;
153
                if ($scope.new_custom_field.label && $scope.new_custom_field.value) {
154
                    $scope.credential.custom_fields.push(angular.copy($scope.new_custom_field));
155
                }
156
                delete $scope.credential.password_repeat;
157
158
                API.runtime.sendMessage(API.runtime.id, {
159
                    method: "saveCredential",
160
                    args: $scope.credential
161
                }).then(function () {
162
                    $scope.saving = false;
163
                    if (!$scope.credential.credential_id) {
164
                        notify(API.i18n.getMessage('credential_created'));
165
                    } else {
166
                        notify(API.i18n.getMessage('credential_updated'));
167
                    }
168
                    window.location = '#!/';
169
                });
170
171
            };
172
173
            $scope.deleteCredential = function () {
174
                $scope.credential.delete_time = new Date().getTime() / 1000;
175
                API.runtime.sendMessage(API.runtime.id, {
176
                    method: "saveCredential",
177
                    args: $scope.credential
178
                }).then(function () {
179
                    notify(API.i18n.getMessage('credential_deleted'));
180
                    API.runtime.sendMessage(API.runtime.id, {method: "getCredentials"}).then(function () {
181
                        setTimeout(function () {
182
                            window.location = '#!/';
183
                        }, 1900);
184
                    });
185
186
                });
187
            };
188
189
            $scope.cancel = function () {
190
                window.location = '#!/';
191
            };
192
193
194
        }]);
195
}());
196