js/ui/popup/controllers/setup.js   A
last analyzed

Complexity

Total Complexity 27
Complexity/F 1.5

Size

Lines of Code 182
Function Count 18

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 0
nc 3
dl 0
loc 182
rs 10
c 2
b 0
f 0
wmc 27
mnd 2
bc 29
fnc 18
bpm 1.6111
cpm 1.5
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B angular.controller(ꞌSetupCtrlꞌ) 0 169 1
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('SetupCtrl', ['$scope', '$timeout', '$location', '$rootScope', 'StepsService', 'notify', 'HttpsTest',
37
            function ($scope, $timeout, $location, $rootScope, StepsService, notify, HttpsTest) {
38
            $scope.settings = {
39
                nextcloud_host: '',
40
                nextcloud_username: '',
41
                nextcloud_password: '',
42
                ignoreProtocol: true,
43
                ignoreSubdomain: true,
44
                ignorePath: true,
45
                generatedPasswordLength: 12,
46
                remember_password: true,
47
                vault_password: '',
48
                refreshTime: 60,
49
                default_vault: {},
50
                master_password: '',
51
                master_password_repeat: '',
52
                enableAutoFill: true,
53
                enablePasswordPickerr: true,
54
                enableAutoSubmit: false,
55
                debug: false,
56
                accounts: []
57
            };
58
            $scope.vaults = [];
59
60
            $rootScope.$broadcast('hideHeader');
61
            $rootScope.setup = true;
62
            $scope.gogo = function (to) {
63
                StepsService.steps().goTo(to);
64
            };
65
            notify.config({
66
                'position': 'left',
67
                'duration': 2500
68
            });
69
70
            $scope.check = {
71
                server: function (callback) {
72
                    if(!$scope.settings.nextcloud_host || !$scope.settings.nextcloud_username || !$scope.settings.nextcloud_password){
73
                        $scope.errors.push(API.i18n.getMessage('invalid_server_settings'));
74
                        callback(false);
75
                        return;
76
                    }
77
                    $scope.settings.nextcloud_host = $scope.settings.nextcloud_host.replace(/\/$/, "");
78
                    PAPI.host = $scope.settings.nextcloud_host;
79
                    PAPI.username = $scope.settings.nextcloud_username;
80
                    PAPI.password = $scope.settings.nextcloud_password;
81
                    PAPI.getVaults(function (vaults) {
82
                        if (vaults.hasOwnProperty('error')) {
83
                            var errors = API.i18n.getMessage('invalid_response_from_server', [vaults.result.status, vaults.result.statusText]);
84
                            $scope.errors.push(errors);
85
                            notify(errors);
86
                            callback(false);
87
                        }
88
                        else {
89
                            $scope.vaults = vaults;
90
                            callback(true);
91
                        }
92
                        $scope.$apply();
93
                    });
94
                },
95
                vault: function (callback) {
96
                    try {
97
                        PAPI.decryptString($scope.settings.default_vault.challenge_password, $scope.settings.vault_password);
98
                        callback(true);
99
                    }
100
                    catch (e) {
101
                        $scope.errors.push();
102
                        notify(API.i18n.getMessage('invalid_vault_password'));
103
                        callback(false);
104
                    }
105
                },
106
                master: function (callback) {
107
                    if($scope.settings.master_password !== $scope.settings.master_password_repeat){
108
                        notify(API.i18n.getMessage('no_password_match'));
109
                        callback(false);
110
                        return;
111
                    }
112
113
                    if ($scope.settings.master_password.trim() !== '') {
114
                        callback(true);
115
                    } else {
116
                        notify(API.i18n.getMessage('empty_master_key'));
117
                        callback(false);
118
                    }
119
                }
120
            };
121
            $scope.saving = false;
122
            $scope.next = function () {
123
                $scope.saving = true;
124
                $scope.errors = [];
125
                $timeout(function () {
126
                    var step = StepsService.getCurrent().name;
127
                    var check = $scope.check[step];
128
                    if (typeof check === "function") {
129
                        check(function (result) {
130
                            $scope.saving = false;
131
                            if (result) {
132
                                $scope.errors = [];
133
                                $scope.$apply();
134
                                StepsService.steps().next();
135
                            }
136
                            $timeout(function () {
137
                                $scope.errors = [];
138
                                $scope.$apply();
139
                            }, 5000);
140
                        });
141
                    }
142
                    else {
143
                        $scope.saving = false;
144
                        StepsService.steps().next();
145
                    }
146
                }, 10);
147
            };
148
149
            var handleCheck = function (resultUrl) {
150
                $scope.settings.nextcloud_host = resultUrl;
151
            };
152
153
            $scope.isHTTP = function (url) {
154
                return HttpsTest.isHTTP(url);
155
            };
156
157
            $scope.checkHost = function () {
158
                HttpsTest.test($scope.settings.nextcloud_host).then(handleCheck, handleCheck);
159
            };
160
161
            $scope.finished = function () {
162
                var settings = angular.copy($scope.settings);
163
                var master_password = settings.master_password;
164
                var master_password_remember = settings.master_password_remember;
165
                var account = {
166
                    nextcloud_host: settings.nextcloud_host,
167
                    nextcloud_username: settings.nextcloud_username,
168
                    nextcloud_password: settings.nextcloud_password,
169
                    vault: settings.default_vault,
170
                    vault_password: settings.vault_password
171
                };
172
                settings.accounts.push(account);
173
                delete settings.master_password;
174
                delete settings.master_password_remember;
175
                delete settings.nextcloud_host;
176
                delete settings.nextcloud_username;
177
                delete settings.nextcloud_password;
178
                delete settings.vault_password;
179
                delete settings.master_password_repeat;
180
                delete settings.default_vault;
181
182
                $scope.saving = true;
183
184
                API.runtime.sendMessage(API.runtime.id, {
185
                    method: "setMasterPassword",
186
                    args: {password: master_password, savePassword: master_password_remember}
187
                })
188
                    .then(function () {
189
                        API.runtime.sendMessage(API.runtime.id, {
190
                            method: "saveSettings",
191
                            args: settings
192
                        }).then(function () {
193
                            setTimeout(function () {
194
                                $rootScope.setup = false;
195
                                 API.runtime.sendMessage(API.runtime.id, {
196
                                    method: "closeSetupTab"
197
                                });
198
                                $scope.saving = false;
199
                            }, 750);
200
                        });
201
                    });
202
203
204
            };
205
        }]);
206
}());
207
208