Completed
Pull Request — master (#164)
by Sander
58s
created

js/ui/popup/controllers/account/add.js   B

Complexity

Conditions 1
Paths 3

Size

Total Lines 141

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
c 2
b 0
f 0
nc 3
nop 0
dl 0
loc 141
rs 8.2857

1 Function

Rating   Name   Duplication   Size   Complexity  
B angular.controller(ꞌAddAccountCtrlꞌ) 0 128 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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('AddAccountCtrl', ['$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
            };
43
44
45
            $scope.vaults = [];
46
47
            $scope.gogo = function (to) {
48
                StepsService.steps().goTo(to);
49
            };
50
51
52
            notify.config({
53
                'position': 'left',
54
                'duration': 2500
55
            });
56
57
            $scope.check = {
58
                server: function (callback) {
59
                    if (!$scope.settings.nextcloud_host || !$scope.settings.nextcloud_username || !$scope.settings.nextcloud_password) {
60
                        $scope.errors.push(API.i18n.getMessage('invalid_server_settings'));
61
                        callback(false);
62
                        return;
63
                    }
64
                    $scope.settings.nextcloud_host = $scope.settings.nextcloud_host.replace(/\/$/, "");
65
                    PAPI.host = $scope.settings.nextcloud_host;
66
                    PAPI.username = $scope.settings.nextcloud_username;
67
                    PAPI.password = $scope.settings.nextcloud_password;
68
                    PAPI.getVaults(function (vaults) {
69
                        if (vaults.hasOwnProperty('error')) {
70
                            var errors = API.i18n.getMessage('invalid_response_from_server', [vaults.result.status, vaults.result.statusText]);
71
                            $scope.errors.push(errors);
72
                            notify(errors);
73
                            callback(false);
74
                        }
75
                        else {
76
                            $scope.vaults = vaults;
77
                            callback(true);
78
                        }
79
                        $scope.$apply();
80
                    });
81
                },
82
                vault: function (callback) {
83
                    try {
84
                        PAPI.decryptString($scope.settings.default_vault.challenge_password, $scope.settings.vault_password);
85
                        callback(true);
86
                    }
87
                    catch (e) {
88
                        $scope.errors.push();
89
                        notify(API.i18n.getMessage('invalid_vault_password'));
90
                        callback(false);
91
                    }
92
                }
93
            };
94
            $scope.saving = false;
95
            $scope.next = function () {
96
                $scope.saving = true;
97
                $scope.errors = [];
98
                $timeout(function () {
99
                    var step = StepsService.getCurrent().name;
100
                    var check = $scope.check[step];
101
                    if (typeof check === "function") {
102
                        check(function (result) {
103
                            $scope.saving = false;
104
                            if (result) {
105
                                $scope.errors = [];
106
                                $scope.$apply();
107
                                StepsService.steps().next();
108
                            }
109
                            $timeout(function () {
110
                                $scope.errors = [];
111
                                $scope.$apply();
112
                            }, 5000);
113
                        });
114
                    }
115
                    else {
116
                        $scope.saving = false;
117
                        StepsService.steps().next();
118
                    }
119
                }, 10);
120
            };
121
122
            var handleCheck = function (resultUrl) {
123
                $scope.settings.nextcloud_host = resultUrl;
124
            };
125
126
            $scope.isHTTP = function (url) {
127
                return HttpsTest.isHTTP(url);
128
            };
129
130
            $scope.checkHost = function () {
131
                HttpsTest.test($scope.settings.nextcloud_host).then(handleCheck, handleCheck);
132
            };
133
134
            $scope.cancelAdd = function () {
135
                window.location = '#!/settings/2';
136
            };
137
138
            $scope.finished = function () {
139
                var _settings = angular.copy($scope.settings);
140
141
                var account = {
142
                    nextcloud_host: _settings.nextcloud_host,
143
                    nextcloud_username: _settings.nextcloud_username,
144
                    nextcloud_password: _settings.nextcloud_password,
145
                    vault: _settings.default_vault,
146
                    vault_password: _settings.vault_password
147
                };
148
                $scope.saving = true;
149
                API.runtime.sendMessage(API.runtime.id, {'method': 'getRuntimeSettings'}).then(function (settings) {
150
                    settings.accounts.push(account);
151
                    API.runtime.sendMessage(API.runtime.id, {
152
                        method: "saveSettings",
153
                        args: settings
154
                    }).then(function () {
155
                        setTimeout(function () {
156
                            notify(API.i18n.getMessage('account_added'));
157
                            $scope.saving = false;
158
                            window.location = '#!/settings/2';
159
                        }, 750);
160
                    });
161
162
                });
163
            };
164
        }]);
165
}());
166
167