Completed
Pull Request — master (#117)
by Sander
41s
created

angular.controller(ꞌAddAccountCtrlꞌ)   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

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