summaryrefslogtreecommitdiff
path: root/js/PrefUsers.js
blob: a6081f35f62a59923e694550191296053fe415c8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
'use strict'

/* global __, xhr, dijit, Notify, Tables, App, fox */

const	Users = {
	reload: function(sort) {
		return new Promise((resolve, reject) => {
			const user_search = App.byId("user_search");
			const search = user_search ? user_search.value : "";

			xhr.post("backend.php", { op: "pref-users", sort: sort, search: search }, (reply) => {
				dijit.byId('usersTab').attr('content', reply);
				Notify.close();
				resolve();
			}, (e) => { reject(e) });
		});
	},
	add: function() {
		const login = prompt(__("Please enter username:"), "");

		if (login) {
			Notify.progress("Adding user...");

			xhr.post("backend.php", {op: "pref-users", method: "add", login: login}, (reply) => {
				Users.reload().then(() => {
					Notify.info(reply);
				})
			});

		}
	},
	edit: function(id) {
		xhr.json('backend.php', {op: 'pref-users', method: 'edit', id: id}, (reply) => {
			const user = reply.user;
			const admin_disabled = (user.id == 1);

			const dialog = new fox.SingleUseDialog({
				id: "userEditDlg",
				title: __("Edit user"),
				execute: function () {
					if (this.validate()) {
						Notify.progress("Saving data...", true);

						xhr.post("backend.php", this.attr('value'), (reply) => {
							dialog.hide();
							Users.reload().then(() => {
								Notify.info(reply);
							});
						});
					}
				},
				content: `
					<form onsubmit='return false'>

						${App.FormFields.hidden_tag('id', user.id.toString())}
						${App.FormFields.hidden_tag('op', 'pref-users')}
						${App.FormFields.hidden_tag('method', 'editSave')}

						<div dojoType="dijit.layout.TabContainer" style="height : 400px">
							<div dojoType="dijit.layout.ContentPane" title="${__('Edit user')}">

								<section>
									<fieldset>
										<label>${__("Login:")}</label>
										<input style='font-size : 16px'
											${admin_disabled ? "disabled='1'" : ''}
											dojoType='dijit.form.ValidationTextBox' required='1'
											name='login' value="${App.escapeHtml(user.login)}">

										${admin_disabled ? App.FormFields.hidden_tag("login", user.login) : ''}
									</fieldset>

									<hr/>

									<fieldset>
										<label>${__('Access level: ')}</label>
										${App.FormFields.select_hash("access_level",
											user.access_level, reply.access_level_names, {disabled: admin_disabled.toString()}, "", {numeric_sort: true})}

										${admin_disabled ? App.FormFields.hidden_tag("access_level",
											user.access_level.toString()) : ''}
									</fieldset>
									<fieldset>
										<label>${__("New password:")}</label>
										<input dojoType='dijit.form.TextBox' type='password' size='20'
											placeholder='${__("Change password")}' name='password'>
									</fieldset>
									<fieldset>
									<label></label>
									<label class="checkbox">
										${App.FormFields.checkbox_tag("otp_enabled", user.otp_enabled)}
										${__('OTP enabled')}
									</fieldset>

									<hr/>

									<fieldset>
										<label>${__("E-mail:")}</label>
										<input dojoType='dijit.form.TextBox' size='30' name='email'
											value="${App.escapeHtml(user.email)}">
									</fieldset>
								</section>
							</div>
							<div dojoType="dijit.layout.ContentPane" title="${__('User details')}">
								<script type='dojo/method' event='onShow' args='evt'>
									if (this.domNode.querySelector('.loading')) {
										xhr.post("backend.php", {op: 'pref-users', method: 'userdetails', id: ${user.id}}, (reply) => {
											this.attr('content', reply);
										});
									}
								</script>
								<span class='loading'>${__("Loading, please wait...")}</span>
							</div>
						</div>

						<footer>
							<button dojoType='dijit.form.Button' class='alt-primary' type='submit' onclick='App.dialogOf(this).execute()'>
								${App.FormFields.icon("save")}
								${__('Save')}
							</button>
							<button dojoType='dijit.form.Button' onclick='App.dialogOf(this).hide()'>
								${__('Cancel')}
							</button>
						</footer>
					</form>
				`
			});

			dialog.show();
		});
	},
	resetSelected: function() {
		const rows = this.getSelection();

		if (rows.length == 0) {
			alert(__("No users selected."));
			return;
		}

		if (rows.length > 1) {
			alert(__("Please select one user."));
			return;
		}

		if (confirm(__("Reset password of selected user?"))) {
			Notify.progress("Resetting password for selected user...");

			const id = rows[0];

			xhr.post("backend.php", {op: "pref-users", method: "resetPass", id: id}, (reply) => {
				Notify.close();
				Notify.info(reply, true);
			});

		}
	},
	removeSelected: function() {
		const sel_rows = this.getSelection();

		if (sel_rows.length > 0) {
			if (confirm(__("Remove selected users? Neither default admin nor your account will be removed."))) {
				Notify.progress("Removing selected users...");

				const query = {
					op: "pref-users", method: "remove",
					ids: sel_rows.toString()
				};

				xhr.post("backend.php", query, () => {
					this.reload();
				});
			}

		} else {
			alert(__("No users selected."));
		}
	},
	getSelection :function() {
		return Tables.getSelected("users-list");
	}
}