Completed
Push — master ( 3d31b2...c9ad65 )
by Sander
8s
created

js/settings-admin.js   A

Complexity

Total Complexity 24
Complexity/F 1.33

Size

Lines of Code 99
Function Count 18

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 24
c 1
b 0
f 0
dl 0
loc 99
rs 10
cc 0
nc 32
mnd 1
bc 19
fnc 18
bpm 1.0555
cpm 1.3333
noi 0
1
/**
2
 * Nextcloud - passman
3
 *
4
 * @copyright Copyright (c) 2016, Sander Brand ([email protected])
5
 * @copyright Copyright (c) 2016, Marcos Zuriaga Miguel ([email protected])
6
 * @license GNU AGPL version 3 or any later version
7
 *
8
 * This program is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU Affero General Public License as
10
 * published by the Free Software Foundation, either version 3 of the
11
 * License, or (at your option) any later version.
12
 *
13
 * This program is distributed in the hope that it will be useful,
14
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
 * GNU Affero General Public License for more details.
17
 *
18
 * You should have received a copy of the GNU Affero General Public License
19
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 */
22
23
$(document).ready(function () {
24
25
	var Settings = function (baseUrl) {
26
		this._baseUrl = baseUrl;
27
		this._settings = [];
28
	};
29
30
	Settings.prototype = {
31
		load: function () {
32
			var deferred = $.Deferred();
33
			var self = this;
34
			$.ajax({
35
				url: this._baseUrl,
36
				method: 'GET',
37
				async: false
38
			}).done(function (settings) {
39
				self._settings = settings;
40
			}).fail(function () {
41
				deferred.reject();
42
			});
43
			return deferred.promise();
44
		},
45
46
		setUserKey: function (key, value) {
47
			var request = $.ajax({
48
				url: this._baseUrl + '/' + key + '/' + value,
49
				method: 'POST'
50
			});
51
			request.done(function () {
52
				$('.msg-passwords').removeClass("msg_error");
53
				$('.msg-passwords').text('');
54
			});
55
			request.fail(function () {
56
				$('.msg-passwords').addClass("msg_error");
57
				$('.msg-passwords').text(t('passwords', 'Error while saving field') + ' ' + key + '!');
58
			});
59
		},
60
61
		setAdminKey: function (key, value) {
62
			var request = $.ajax({
63
				url: this._baseUrl + '/' + key + '/' + value,
64
				method: 'POST'
65
			});
66
			request.done(function () {
67
				$('.msg-passwords').removeClass("msg_error");
68
				$('.msg-passwords').text('');
69
			});
70
			request.fail(function () {
71
				$('.msg-passwords').addClass("msg_error");
72
				$('.msg-passwords').text(t('passwords', 'Error while saving field') + ' ' + key + '!');
73
			});
74
		},
75
		getKey: function (key) {
76
			if(this._settings.hasOwnProperty(key)){
77
				return this._settings[key];
78
			}
79
			return false;
80
		},
81
		getAll: function () {
82
			return this._settings;
83
		}
84
	};
85
86
87
	var settings = new Settings(OC.generateUrl('apps/passman/api/internal/settings'));
88
	settings.load();
89
90
	// ADMIN SETTINGS
91
92
	// fill the boxes
93
	$('#passman_link_sharing_enabled').prop('checked', (settings.getKey('link_sharing_enabled').toString().toLowerCase() == '1'));
94
	$('#passman_sharing_enabled').prop('checked', (settings.getKey('user_sharing_enabled').toString().toLowerCase() == '1'));
95
	$('#passman_check_version').prop('checked', (settings.getKey('check_version').toString().toLowerCase() == '1'));
96
	$('#passman_https_check').prop('checked', (settings.getKey('https_check').toString().toLowerCase() == '1'));
97
	$('#passman_disable_contextmenu').prop('checked', (settings.getKey('disable_contextmenu').toString().toLowerCase() == '1'));
98
	$('#vault_key_strength').val(settings.getKey('vault_key_strength'));
99
100
101
	$('#passman_check_version').change(function () {
102
		settings.setAdminKey('check_version', ($(this).is(":checked")) ? 1 : 0);
103
	});
104
105
	$('#passman_https_check').change(function () {
106
		settings.setAdminKey('https_check', ($(this).is(":checked")) ? 1 : 0);
107
	});
108
109
	$('#passman_disable_contextmenu').change(function () {
110
		settings.setAdminKey('disable_contextmenu', ($(this).is(":checked")) ? 1 : 0);
111
	});
112
113
	$('#passman_sharing_enabled').change(function () {
114
		settings.setAdminKey('user_sharing_enabled', ($(this).is(":checked")) ? 1 : 0);
115
	});
116
117
	$('#passman_link_sharing_enabled').change(function () {
118
		settings.setAdminKey('link_sharing_enabled', ($(this).is(":checked")) ? 1 : 0);
119
	});
120
121
});
122