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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
|
if not modules then modules = { } end modules ['util-sql-users'] = {
version = 1.001,
comment = "companion to lmx-*",
author = "Hans Hagen, PRAGMA-ADE, Hasselt NL",
copyright = "PRAGMA ADE / ConTeXt Development Team",
license = "see context related readme files"
}
-- This is experimental code and currently part of the base installation simply
-- because it's easier to dirtribute this way. Eventually it will be documented
-- and the related scripts will show up as well.
local sql = utilities.sql
local find, topattern = string.find, string.topattern
local sumHEXA = md5.sumHEXA
local toboolean = string.toboolean
local lpegmatch = lpeg.match
local sql = require("util-sql") -- utilities.sql
local users = { }
sql.users = users
local trace_sql = false trackers.register("sql.users.trace", function(v) trace_sql = v end)
local report = logs.reporter("sql","users")
local split = lpeg.splitat(":")
local valid = nil
local hash = function(s) return "MD5:" .. sumHEXA(s) end
if LUAVERSION >= 5.3 then
local sha2 = require("util-sha")
local HASH224 = sha2.HASH224
local HASH256 = sha2.HASH256
local HASH384 = sha2.HASH384
local HASH512 = sha2.HASH512
valid = {
MD5 = hash,
SHA224 = function(s) return "SHA224:" .. HASH224(s) end,
SHA256 = function(s) return "SHA256:" .. HASH256(s) end,
SHA384 = function(s) return "SHA384:" .. HASH384(s) end,
SHA512 = function(s) return "SHA512:" .. HASH512(s) end,
}
else
valid = {
MD5 = hash,
SHA224 = hash,
SHA256 = hash,
SHA384 = hash,
SHA512 = hash,
}
end
local function encryptpassword(str,how)
if not str or str == "" then
return ""
end
local prefix, rest = lpegmatch(split,str)
if prefix and rest and valid[prefix] then
return str
end
return (how and valid[how] or valid.MD5)(str)
end
local function cleanuppassword(str)
local prefix, rest = lpegmatch(split,str)
if prefix and rest and valid[prefix] then
return rest
end
return str
end
local function samepasswords(one,two)
if not one or not two then
return false
end
return encryptpassword(one) == encryptpassword(two)
end
local function validaddress(address,addresses)
if address and addresses and address ~= "" and addresses ~= "" then
if find(address,topattern(addresses,true,true)) then
return true, "valid remote address"
end
return false, "invalid remote address"
else
return true, "no remote address check"
end
end
users.encryptpassword = encryptpassword
users.cleanuppassword = cleanuppassword
users.samepasswords = samepasswords
users.validaddress = validaddress
-- print(users.encryptpassword("test")) -- MD5:098F6BCD4621D373CADE4E832627B4F6
local function checkeddb(presets,datatable)
return sql.usedatabase(presets,datatable or presets.datatable or "users")
end
users.usedb = checkeddb
local groupnames = { }
local groupnumbers = { }
local function registergroup(name)
local n = #groupnames + 1
groupnames [n] = name
groupnames [tostring(n)] = name
groupnames [name] = name
groupnumbers[n] = n
groupnumbers[tostring(n)] = n
groupnumbers[name] = n
return n
end
registergroup("superuser")
registergroup("administrator")
registergroup("user")
registergroup("guest")
users.groupnames = groupnames
users.groupnumbers = groupnumbers
-- password 'test':
--
-- INSERT insert into users (`name`,`password`,`group`,`enabled`) values ('...','MD5:098F6BCD4621D373CADE4E832627B4F6',1,1) ;
--
-- MD5:098F6BCD4621D373CADE4E832627B4F6
-- SHA224:90A3ED9E32B2AAF4C61C410EB925426119E1A9DC53D4286ADE99A809
-- SHA256:9F86D081884C7D659A2FEAA0C55AD015A3BF4F1B2B0B822CD15D6C15B0F00A08
-- SHA384:768412320F7B0AA5812FCE428DC4706B3CAE50E02A64CAA16A782249BFE8EFC4B7EF1CCB126255D196047DFEDF17A0A9
-- SHA512:EE26B0DD4AF7E749AA1A8EE3C10AE9923F618980772E473F8819A5D4940E0DB27AC185F8A0E1D5F84F88BC887FD67B143732C304CC5FA9AD8E6F57F50028A8FF
-- old values (a name can have utf and a password a long hash):
--
-- name 80, fullname 80, password 50
local template = [[
CREATE TABLE `users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(100) NOT NULL,
`fullname` varchar(100) NOT NULL,
`password` varchar(200) DEFAULT NULL,
`group` int(11) NOT NULL,
`enabled` int(11) DEFAULT '1',
`email` varchar(80) DEFAULT NULL,
`address` varchar(256) DEFAULT NULL,
`theme` varchar(50) DEFAULT NULL,
`data` longtext,
PRIMARY KEY (`id`),
UNIQUE KEY `name_unique` (`name`)
) DEFAULT CHARSET = utf8 ;
]]
local sqlite_template = [[
CREATE TABLE `users` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`name` TEXT NOT NULL,
`fullname` TEXT NOT NULL,
`password` TEXT DEFAULT NULL,
`group` INTEGER NOT NULL,
`enabled` INTEGER DEFAULT '1',
`email` TEXT DEFAULT NULL,
`address` TEXT DEFAULT NULL,
`theme` TEXT DEFAULT NULL,
`data` TEXT DEFAULT NULL
) ;
]]
local converter, fields = sql.makeconverter {
{ name = "id", type = "number" },
{ name = "name", type = "string" },
{ name = "fullname", type = "string" },
{ name = "password", type = "string" },
{ name = "group", type = groupnames },
{ name = "enabled", type = "boolean" },
{ name = "email", type = "string" },
{ name = "address", type = "string" },
{ name = "theme", type = "string" },
{ name = "data", type = "deserialize" },
}
function users.createdb(presets,datatable)
local db = checkeddb(presets,datatable)
db.execute {
template = db.usedmethod == "sqlite" and sqlite_template or template,
variables = {
basename = db.basename,
},
}
report("datatable %a created in %a",db.name,db.base)
return db
end
local template =[[
SELECT
%fields%
FROM
%basename%
WHERE
`name` = '%[name]%'
AND
`password` = '%[password]%'
;
]]
local template =[[
SELECT
%fields%
FROM
%basename%
WHERE
`name` = '%[name]%'
;
]]
function users.valid(db,username,password,address)
local data = db.execute {
template = template,
converter = converter,
variables = {
basename = db.basename,
fields = fields,
name = username,
},
}
local data = data and data[1]
if not data then
return false, "unknown user"
elseif not data.enabled then
return false, "disabled user"
elseif data.password ~= encryptpassword(password) then
return false, "wrong password"
elseif not validaddress(address,data.address) then
return false, "invalid address"
else
data.password = nil
return data, "okay"
end
end
local template =[[
INSERT INTO %basename% (
`name`,
`fullname`,
`password`,
`group`,
`enabled`,
`email`,
`address`,
`theme`,
`data`
) VALUES (
'%[name]%',
'%[fullname]%',
'%[password]%',
'%[group]%',
'%[enabled]%',
'%[email]%',
'%[address]%',
'%[theme]%',
'%[data]%'
) ;
]]
function users.add(db,specification)
local name = specification.username or specification.name
if not name or name == "" then
return
end
local data = specification.data
db.execute {
template = template,
variables = {
basename = db.basename,
name = name,
fullname = name or fullname,
password = encryptpassword(specification.password or ""),
group = groupnumbers[specification.group] or groupnumbers.guest,
enabled = toboolean(specification.enabled) and "1" or "0",
email = specification.email,
address = specification.address,
theme = specification.theme,
data = type(data) == "table" and db.serialize(data,"return") or "",
},
}
end
local template =[[
SELECT
%fields%
FROM
%basename%
WHERE
`name` = '%[name]%' ;
]]
function users.getbyname(db,name)
local data = db.execute {
template = template,
converter = converter,
variables = {
basename = db.basename,
fields = fields,
name = name,
},
}
return data and data[1] or nil
end
local template =[[
SELECT
%fields%
FROM
%basename%
WHERE
`id` = '%id%' ;
]]
local function getbyid(db,id)
local data = db.execute {
template = template,
converter = converter,
variables = {
basename = db.basename,
fields = fields,
id = id,
},
}
return data and data[1] or nil
end
users.getbyid = getbyid
local template =[[
UPDATE
%basename%
SET
`fullname` = '%[fullname]%',
`password` = '%[password]%',
`group` = '%[group]%',
`enabled` = '%[enabled]%',
`email` = '%[email]%',
`address` = '%[address]%',
`theme` = '%[theme]%',
`data` = '%[data]%'
WHERE
`id` = '%id%'
;
]]
function users.save(db,id,specification)
id = tonumber(id)
if not id then
return
end
local user = getbyid(db,id)
if tonumber(user.id) ~= id then
return
end
local fullname = specification.fullname == nil and user.fulname or specification.fullname
local password = specification.password == nil and user.password or specification.password
local group = specification.group == nil and user.group or specification.group
local enabled = specification.enabled == nil and user.enabled or specification.enabled
local email = specification.email == nil and user.email or specification.email
local address = specification.address == nil and user.address or specification.address
local theme = specification.theme == nil and user.theme or specification.theme
local data = specification.data == nil and user.data or specification.data
db.execute {
template = template,
variables = {
basename = db.basename,
id = id,
fullname = fullname,
password = encryptpassword(password),
group = groupnumbers[group],
enabled = toboolean(enabled) and "1" or "0",
email = email,
address = address,
theme = theme,
data = type(data) == "table" and db.serialize(data,"return") or "",
},
}
return getbyid(db,id)
end
local template =[[
DELETE FROM
%basename%
WHERE
`id` = '%id%' ;
]]
function users.remove(db,id)
db.execute {
template = template,
variables = {
basename = db.basename,
id = id,
},
}
end
local template =[[
SELECT
%fields%
FROM
%basename%
ORDER BY
`name` ;
]]
function users.collect(db) -- maybe also an id/name only variant
local records, keys = db.execute {
template = template,
converter = converter,
variables = {
basename = db.basename,
fields = fields,
},
}
return records, keys
end
|