Completed
Push — master ( 576d64...22e707 )
by Sander
01:42 queued 33s
created

angular.controller(ꞌEditCtrlꞌ)   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 8
rs 9.4285
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', '$mdToast','$timeout', function ($scope, $routeParams, $mdToast, $timeout) {
37
            API.runtime.sendMessage(API.runtime.id, {
38
                method: "getCredentialByGuid",
39
                args: $routeParams.guid
40
            }).then(function (credential) {
41
                $scope.credential = credential;
42
                $scope.credential.password_repeat = angular.copy(credential.password);
43
                $scope.$apply();
44
            });
45
46
            var storage = new API.Storage();
47
48
            function genPwd(settings) {
49
                /* jshint ignore:start */
50
                var password = generatePassword(settings['length'],
51
                    settings.useUppercase,
52
                    settings.useLowercase,
53
                    settings.useDigits,
54
                    settings.useSpecialChars,
55
                    settings.minimumDigitCount,
56
                    settings.avoidAmbiguousCharacters,
57
                    settings.requireEveryCharType);
58
                /* jshint ignore:end */
59
                return password;
60
            }
61
62
            $scope.pw_settings = null;
63
            function getPasswordGenerationSettings(cb) {
0 ignored issues
show
Unused Code introduced by
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...
64
                var default_settings = {
65
                    'length': 12,
66
                    'useUppercase': true,
67
                    'useLowercase': true,
68
                    'useDigits': true,
69
                    'useSpecialChars': true,
70
                    'minimumDigitCount': 3,
71
                    'avoidAmbiguousCharacters': false,
72
                    'requireEveryCharType': true
73
                };
74
                storage.get('password_generator_settings').then(function (_settings) {
75
                    if (!_settings) {
76
                        _settings = default_settings;
77
                    }
78
79
                    $scope.pw_settings = _settings;
80
                }).error(function () {
81
                    $scope.pw_settings = default_settings;
82
                });
83
            }
84
85
            getPasswordGenerationSettings();
86
87
            var custom_field = {
88
                label: '',
89
                value: '',
90
                field_type: 'text',
91
                secret: false
92
            };
93
94
            $scope.new_custom_field = angular.copy(custom_field);
95
96
            $scope.addCustomField = function (_field) {
97
                var field = angular.copy(_field);
98
                if (!field.label || !field.value) {
99
                    return;
100
                }
101
                $scope.credential.custom_fields.push(field);
102
                $scope.new_custom_field = angular.copy(custom_field);
103
            };
104
105
            $scope.deleteCustomField = function (field) {
106
                var idx = $scope.credential.custom_fields.indexOf(field);
107
                $scope.credential.custom_fields.splice(idx, 1);
108
            };
109
110
            $scope.pwFieldShown = false;
111
112
            $scope.togglePwField = function () {
113
                $scope.pwFieldShown = !$scope.pwFieldShown;
114
            };
115
116
117
            function generate_pass(inputId) {
0 ignored issues
show
Unused Code introduced by
The parameter inputId 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...
introduced by
The function generate_pass does not seem to be used and can be removed.
Loading history...
118
119
            }
120
            var round = 0;
121
            $scope.generatePassword = function () {
122
                var new_password = genPwd($scope.pw_settings);
123
                $scope.credential.password = new_password;
124
                $scope.credential.password_repeat = new_password;
125
                $timeout(function () {
126
                    if (round < 10) {
127
                        $scope.generatePassword();
128
                        round++;
129
                    } else {
130
                        round = 0;
131
                    }
132
                }, 10);
133
            };
134
135
            $scope.saveCredential = function () {
136
                if (!$scope.credential.label) {
137
                    $mdToast.showSimple(API.i18n.getMessage('label_required'));
138
                    return;
139
                }
140
141
                if ($scope.credential.password !== $scope.credential.password_repeat) {
142
                    $mdToast.showSimple(API.i18n.getMessage('no_password_match'));
143
                    return;
144
                }
145
146
                if ($scope.new_custom_field.label && $scope.new_custom_field.value) {
147
                    $scope.credential.custom_fields.push(angular.copy($scope.new_custom_field));
148
                }
149
                delete $scope.credential.password_repeat;
150
151
                API.runtime.sendMessage(API.runtime.id, {
152
                    method: "saveCredential",
153
                    args: $scope.credential
154
                }).then(function (credential) {
155
                    if (!$scope.credential.credential_id) {
156
                        $mdToast.showSimple(API.i18n.getMessage('credential_created'));
157
                    } else {
158
                        $mdToast.showSimple(API.i18n.getMessage('credential_updated'));
159
                    }
160
                    window.location = '#!/';
161
                });
162
163
            };
164
165
            $scope.cancel = function () {
166
                window.location = '#!/';
167
            };
168
169
170
        }]);
171
}());
172
173