Completed
Push — master ( 5a0120...e4b8ca )
by Sander
01:10
created

angular.controller(ꞌSetupCtrlꞌ)   B

Complexity

Conditions 4
Paths 2

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 23
rs 8.7972
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', function ($scope, $timeout, $location, $rootScope, StepsService, notify) {
37
            $scope.settings = {
38
                nextcloud_host: '',
39
                nextcloud_username: '',
40
                nextcloud_password: '',
41
                ignoreProtocol: true,
42
                ignoreSubdomain: true,
43
                ignorePath: true,
44
                generatedPasswordLength: 12,
45
                remember_password: true,
46
                remember_vault_password: true,
47
                vault_password: '',
48
                refreshTime: 60,
49
                default_vault: {},
50
                master_password: '',
51
                disableAutoFill: false,
52
                disablePasswordPicker: false,
53
                disable_browser_autofill: true,
54
                debug: false
55
            };
56
            $scope.vaults = [];
57
58
            $rootScope.$broadcast('hideHeader');
59
            $rootScope.setup = true;
60
            $scope.gogo = function (to) {
61
                StepsService.steps().goTo(to);
62
            };
63
            notify.config({
64
                'position': 'left',
65
                'duration': 2500
66
            });
67
68
            $scope.check = {
69
                server: function (callback) {
70
                    if(!$scope.settings.nextcloud_host || !$scope.settings.nextcloud_username || !$scope.settings.nextcloud_password){
71
                        $scope.errors.push(API.i18n.getMessage('invalid_server_settings'));
72
                        callback(false);
73
                        return
74
                    }
75
                    PAPI.host = $scope.settings.nextcloud_host;
76
                    PAPI.username = $scope.settings.nextcloud_username;
77
                    PAPI.password = $scope.settings.nextcloud_password;
78
                    PAPI.getVaults(function (vaults) {
79
                        if (vaults.hasOwnProperty('error')) {
80
                            var errors = API.i18n.getMessage('invalid_response_from_server', [vaults.result.status, vaults.result.statusText]);
81
                            $scope.errors.push(errors);
82
                            notify(errors);
83
                            callback(false);
84
                        }
85
                        else {
86
                            $scope.vaults = vaults;
87
                            callback(true);
88
                        }
89
                        $scope.$apply();
90
                    });
91
                },
92
                vault: function (callback) {
93
                    try {
94
                        PAPI.decryptString($scope.settings.default_vault.challenge_password, $scope.settings.vault_password);
95
                        callback(true);
96
                    }
97
                    catch (e) {
98
                        $scope.errors.push();
99
                        notify(API.i18n.getMessage('invalid_vault_password'));
100
                        callback(false);
101
                    }
102
                },
103
                master: function (callback) {
104
                    if ($scope.settings.master_password.trim() !== '') {
105
                        callback(true);
106
                    } else {
107
                        notify(API.i18n.getMessage('empty_master_key'));
108
                        callback(false);
109
                    }
110
                }
111
            };
112
            $scope.saving = false;
113
            $scope.next = function () {
114
                $scope.saving = true;
115
                $scope.errors = [];
116
                $timeout(function () {
117
                    var step = StepsService.getCurrent().name;
118
                    var check = $scope.check[step];
119
                    if (typeof check === "function") {
120
                        check(function (result) {
121
                            $scope.saving = false;
122
                            if (result) {
123
                                $scope.errors = [];
124
                                $scope.$apply();
125
                                StepsService.steps().next();
126
                            }
127
                            $timeout(function () {
128
                                $scope.errors = [];
129
                                $scope.$apply();
130
                            }, 5000);
131
                        });
132
                    }
133
                    else {
134
                        $scope.saving = false;
135
                        StepsService.steps().next();
136
                    }
137
                }, 10);
138
            };
139
140
            $scope.finished = function () {
141
                var settings = angular.copy($scope.settings);
142
                var master_password = settings.master_password;
143
                var master_password_remember = settings.master_password_remember;
144
                delete settings.master_password;
145
                delete settings.master_password_remember;
146
                $scope.saving = true;
147
                API.runtime.sendMessage(API.runtime.id, {
148
                    method: "setMasterPassword",
149
                    args: {password: master_password, savePassword: master_password_remember}
150
                })
151
                    .then(function () {
152
                        API.runtime.sendMessage(API.runtime.id, {
153
                            method: "saveSettings",
154
                            args: settings
155
                        }).then(function () {
156
157
                            setTimeout(function () {
158
                                $rootScope.$broadcast('showHeader');
159
                                window.location = '#!/';
160
                                $scope.saving = false;
161
                            }, 750);
162
                        });
163
                    });
164
165
166
            };
167
        }]);
168
}());
169
170