Completed
Pull Request — master (#52)
by Sander
01:21
created

js/ui/popup/controllers/settings.js   B

Complexity

Conditions 1
Paths 27

Size

Total Lines 122

Duplication

Lines 0
Ratio 0 %

Importance

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

1 Function

Rating   Name   Duplication   Size   Complexity  
B angular.controller(ꞌSettingsCtrlꞌ) 0 110 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('SettingsCtrl', ['$scope', '$rootScope', 'Settings', '$location', function ($scope, $rootScope, Settings, $location) {
0 ignored issues
show
Unused Code introduced by
The parameter $rootScope 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...
Unused Code introduced by
The parameter $location 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...
Unused Code introduced by
The parameter Settings 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...
37
            $scope.settings = {
38
                nextcloud_host: '',
39
                nextcloud_username: '',
40
                nextcloud_password: '',
41
                ignoreProtocol: true,
42
                ignoreSubdomain: true,
43
                ignorePort: true,
44
                ignorePath: true,
45
                generatedPasswordLength: 12,
46
                remember_password: true,
47
                remember_vault_password: true,
48
                refreshTime: 60,
49
                debug: false
50
            };
51
            $scope.errors = [];
52
53
            API.runtime.sendMessage(API.runtime.id, {'method': 'getRuntimeSettings'}).then(function (settings) {
54
                $scope.errors = [];
55
                if (settings) {
56
                    $scope.settings = angular.copy(settings);
57
                }
58
59
                $scope.get_vaults = function () {
60
                    if (!$scope.settings.hasOwnProperty('nextcloud_host') || !$scope.settings.hasOwnProperty('nextcloud_password') || !$scope.settings.hasOwnProperty('nextcloud_username')) {
61
                        return;
62
                    }
63
                    PAPI.username = $scope.settings.nextcloud_username;
64
                    PAPI.password = $scope.settings.nextcloud_password;
65
                    PAPI.host = $scope.settings.nextcloud_host;
66
                    $scope.extension = API.runtime.getManifest().name + ' extension ' + API.runtime.getManifest().version;
67
                    PAPI.getVaults(function (vaults) {
68
                        $scope.errors = [];
69
                        var save_btn = jQuery('#save'),
70
                            login_required = jQuery('.login-req');
71
                        if (vaults.hasOwnProperty('error')) {
72
73
                            var errors = API.i18n.getMessage('invalid_response_from_server', [vaults.result.status, vaults.result.statusText]);
74
                            $scope.errors.push(errors);
75
                            login_required.hide();
76
                            save_btn.hide();
77
                            $scope.$apply();
78
                            return;
79
80
                        }
81
                        login_required.show();
82
                        save_btn.show();
83
                        $scope.vaults = vaults;
84
                        $scope.$apply();
85
86
                    });
87
                };
88
89
                $scope.$watch('[settings.nextcloud_host, settings.nextcloud_username, settings.nextcloud_password]', function () {
90
                    if ($scope.settings.nextcloud_host && $scope.settings.nextcloud_username && $scope.settings.nextcloud_password) {
91
                        $scope.get_vaults();
92
                    }
93
                }, true);
94
95
                if ($scope.settings.nextcloud_host && $scope.settings.nextcloud_username && $scope.settings.nextcloud_password) {
96
                    $scope.get_vaults();
97
                }
98
                $scope.$apply();
99
            });
100
101
            $scope.saving = false;
102
            $scope.saveSettings = function () {
103
                $scope.errors = [];
104
                var settings = angular.copy($scope.settings);
105
                for (var i =0; i < $scope.vaults.length; i++){
106
                    var vault = $scope.vaults[i];
107
                    if(vault.guid === settings.default_vault.guid){
108
                        settings.default_vault = angular.copy(vault);
109
                        break;
110
                    }
111
                }
112
113
                try {
114
                    /** global: PAPI */
115
                    PAPI.decryptString(settings.default_vault.challenge_password, settings.vault_password);
116
                } catch (e) {
117
                    $scope.errors.push(API.i18n.getMessage('invalid_vault_password'));
118
                    return;
119
                }
120
121
                $scope.saving = true;
122
                API.runtime.sendMessage(API.runtime.id, {method: "saveSettings", args: settings}).then(function () {
123
                    setTimeout(function () {
124
                        window.location = '#!/';
125
                        $scope.saving = false;
126
                    }, 750);
127
                });
128
            };
129
130
            $scope.removeSite = function (site) {
131
                var idx = $scope.settings.ignored_sites.indexOf(site);
132
                $scope.settings.ignored_sites.splice(idx, 1);
133
            };
134
135
            $scope.ignoreSite = '';
136
            $scope.addSite = function (site) {
137
                $scope.settings.ignored_sites.push(site);
138
                $scope.ignoreSite = '';
139
            };
140
141
142
            $scope.cancel = function () {
143
                window.location = '#!/';
144
            };
145
        }]);
146
}());
147
148