forked from Chocobozzz/OpenVPN-Admin
-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
366 lines (300 loc) · 13.3 KB
/
index.js
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*jslint browser: true*/
/*global $, Slick, grid, jQuery, alert*/
$(function () {
"use strict";
// Selection des users
$.ajax({
type: "POST",
url: "include/ajax.php",
dataType: 'json',
data: "select=user",
success: function (json) {
// Bouton pour formater la cellule pour supprimer un user
function buttonFormatter(row, cell, value, columnDef, dataContext) {
var button = "<img src='images/drop.png' class='delete del_user' data-row='" + row + "' id='" + dataContext.user_id + "' />";
return button;
}
var
i,
columns = [
{id: "user_id", name: "ID", field: "user_id", width: 120, cssClass: "cell-title", editor: Slick.Editors.Text},
{id: "user_pass", name: "Pass", field: "user_pass", width: 120, cssClass: "cell-title", editor: Slick.Editors.Text},
{id: "user_mail", name: "Mail", field: "user_mail", width: 120, cssClass: "cell-title", editor: Slick.Editors.Text},
{id: "user_phone", name: "Phone", field: "user_phone", width: 120, cssClass: "cell-title", editor: Slick.Editors.Text},
{id: "user_online", name: "Online", field: "user_online", width: 120, cssClass: "cell-title", editor: Slick.Editors.Text},
{id: "user_enable", name: "Enabled", field: "user_enable", width: 120, cssClass: "cell-title", editor: Slick.Editors.Text},
{id: "user_start_date", name: "Start Date", field: "user_start_date", width: 120, cssClass: "cell-title", editor: Slick.Editors.Date},
{id: "user_end_date", name: "End Date", field: "user_end_date", width: 120, cssClass: "cell-title", editor: Slick.Editors.Date},
{id: "user_del", name: 'Delete', field: "user_del", width: 250, formatter: buttonFormatter}
],
// Options de la grid
options = {
editable: true,
enableAddRow: true,
enableCellNavigation: true,
asyncEditorLoading: false,
autoEdit: false,
autoHeight: true
},
// Création des données
data = [],
// Permet de sauvegarder l'ancien user_id lorsque l'admin modifie un utilisateur du VPN
save = null,
grid = null;
// Action lorsqu'on veut supprimer un user
$('.del_user').live('click', function () {
// Suppression dans la bdd
var me = $(this), id = me.attr('id'),
data = grid.getData();
$.ajax({
type: "POST",
url: "include/ajax.php",
dataType: "json",
data: {del_user_id: id},
error: function () {
alert("Erreur dans la suppression de la donnée...");
}
});
// Suppression de la ligne en question
data.splice(me.attr('data-row'), 1);
grid.setData(data);
grid.render();
});
for (i = 0; i < json.length; i += 1) {
data[i] = {
user_id: json[i].user_id,
user_pass: json[i].user_pass,
user_mail: json[i].user_mail,
user_phone: json[i].user_phone,
user_online: json[i].user_online,
user_enable: json[i].user_enable,
user_start_date: json[i].user_start_date,
user_end_date: json[i].user_end_date
};
}
// Grille des utilisateurs du VPN
grid = new Slick.Grid($("#grid_user"), data, columns, options);
$("#grid_user").on('click', function () {
var $active = grid.getActiveCell();
if ($active !== undefined && $active.cell === 0) {
save = $(grid.getActiveCellNode()).html();
} else {
save = null;
}
});
// Lorsqu'on modifie une cellule d'un utilisateur
grid.onCellChange.subscribe(function (e, args) {
var item = args.item;
// On sauvegarde l'ancien user_id
if (save) {
item.set_user = save;
} else {
item.set_user = item.user_id;
}
// Suppression dans la bdd
$.ajax({
type: "POST",
url: "include/ajax.php",
dataType: "json",
data: item,
success: function () {
// Transformation en sha1 du mdp
if(args.cell == 1) {
grid.invalidateRow(args.row);
data[args.row][grid.getColumns()[args.cell].field] = hex_sha1(data[args.row][grid.getColumns()[args.cell].field]);
grid.render();
}
},
error: function () {
alert("Erreur dans la modification des données...");
}
});
delete item.set_user;
});
// Ajout d'un nouvel utilisateur
grid.onAddNewRow.subscribe(function (e, args) {
var item = args.item;
// On ne peut ajouter un utilisateur qu'en saisissant son id
if (!item.user_id) {
return;
}
// Modification dans la bdd
item.add_user = true;
$.ajax({
type: "POST",
url: "include/ajax.php",
dataType: "json",
data: item,
error: function () {
alert("Erreur dans l'insertion des données...");
}
});
delete item.add_user;
// Maj de la grille
grid.invalidateRow(data.length);
data.push(item);
grid.updateRowCount();
grid.render();
});
grid.autosizeColumns();
},
error: function () {
alert('Erreur dans la récupération des données...');
}
});
// Selection des admins
$.ajax({
type: "POST",
url: "include/ajax.php",
dataType: 'json',
data: "select=admin",
success: function (json) {
// Fonction créant la cellule pour supprimer un admin
function buttonFormatter(row, cell, value, columnDef, dataContext) {
var button = "<img src='images/drop.png' class='delete del_admin' data-row='" + row + "' id='" + dataContext.admin_id + "' />";
return button;
}
var
i,
// Header des colonnes
columns = [
{id: "admin_id", name: "Admin ID", field: "admin_id", width: 120, cssClass: "cell-title", editor: Slick.Editors.Text},
{id: "admin_pass", name: "Admin Pass", field: "admin_pass", width: 120, cssClass: "cell-title", editor: Slick.Editors.Text},
{id: "admin_del", name: 'Delete', field: "admin_del", width: 250, formatter: buttonFormatter}
],
// Option de la grille
options = {
editable: true,
enableAddRow: true,
enableCellNavigation: true,
asyncEditorLoading: false,
autoEdit: false,
autoHeight: true
},
data = [],
grid = null,
// Sauvegarder l'ancien admin_id lorsqu'on modifie un admin
save = null;
// Lorsqu'on veut supprimer un admin
$('.del_admin').live('click', function () {
// Suppression dans la bdd
var me = $(this), id = me.attr('id'), data = grid.getData();
$.ajax({
type: "POST",
url: "include/ajax.php",
dataType: "json",
data: {del_admin_id: id},
error: function () {
alert("Erreur dans la suppression de la donnée...");
}
});
// Maj de la grille
data.splice(me.attr('data-row'), 1);
grid.setData(data);
grid.render();
});
for (i = 0; i < json.length; i += 1) {
data[i] = {
admin_id: json[i].admin_id,
admin_pass: json[i].admin_pass
};
}
grid = new Slick.Grid($("#grid_admin"), data, columns, options);
$("#grid_admin").on('click', function () {
var $active = grid.getActiveCell();
if ($active !== undefined && $active.cell === 0) {
save = $(grid.getActiveCellNode()).html();
} else {
save = null;
}
});
// Lorsqu'on modifie un admin
grid.onCellChange.subscribe(function (e, args) {
var item = args.item;
// On stocke l'ancien admin_id
if (save) {
item.set_admin = save;
} else {
item.set_admin = item.admin_id;
}
// Modification de la bdd
$.ajax({
type: "POST",
url: "include/ajax.php",
dataType: "json",
data: item,
error: function () {
alert("Erreur dans la modification des données...");
}
});
delete item.set_admin;
});
// Ajout d'un nouvel admin
grid.onAddNewRow.subscribe(function (e, args) {
var item = args.item;
// On peut ajouter un admin seulement en ajoutant un ID
if (!item.admin_id) {
return;
}
item.add_admin = true;
// Maj de la bdd
$.ajax({
type: "POST",
url: "include/ajax.php",
dataType: "json",
data: item,
error: function () {
alert("Erreur dans l'insertion des données...");
}
});
delete item.add_admin;
// Maj de la grille
grid.invalidateRow(data.length);
data.push(item);
grid.updateRowCount();
grid.render();
});
grid.autosizeColumns();
},
error: function () {
alert('Erreur dans la récupération des données...');
}
});
// Sélection des logs
var
i,
// Header des colonnes
columns = [
{id: "log_id", name: "Log ID", field: "log_id", width: 120, cssClass: "cell-title", editor: Slick.Editors.Text},
{id: "user_id", name: "User ID", field: "user_id", width: 120, cssClass: "cell-title", editor: Slick.Editors.Text},
{id: "log_trusted_ip", name: "Trusted IP", field: "log_trusted_ip", width: 120, cssClass: "cell-title", editor: Slick.Editors.Text},
{id: "log_trusted_port", name: "Trusted Port", field: "log_trusted_port", width: 120, cssClass: "cell-title", editor: Slick.Editors.Text},
{id: "log_remote_ip", name: "Remote IP", field: "log_remote_ip", width: 120, cssClass: "cell-title", editor: Slick.Editors.Text},
{id: "log_remote_port", name: "Remote Port", field: "log_remote_port", width: 120, cssClass: "cell-title", editor: Slick.Editors.Text},
{id: "log_start_time", name: "Start Time", field: "log_start_time", width: 120, cssClass: "cell-title", editor: Slick.Editors.Text},
{id: "log_end_time", name: "End Time", field: "log_end_time", width: 120, cssClass: "cell-title", editor: Slick.Editors.Text},
{id: "log_received", name: "Receveid", field: "log_received", width: 120, cssClass: "cell-title", editor: Slick.Editors.Text},
{id: "log_send", name: "Sent", field: "log_send", width: 120, cssClass: "cell-title", editor: Slick.Editors.Text}
],
// Options de la grille
options = {
editable: false,
enableAddRow: false,
enableCellNavigation: true,
asyncEditorLoading: false,
autoEdit: false,
autoHeight: true
},
data = [],
// Création de la grille
grid = new Slick.Grid($("#grid_log"), data, columns, options),
// Pagination
pager = new Slick.Controls.EnhancementPager({
container: $("#pagination"),
remoteUrl: "include/ajax.php",
params: {select: "log"},
datagrid: grid,
pagerType: ""
});
grid.autosizeColumns();
});