forked from Movimento5StelleLazio/ParlamentoElettronicoM5S
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmember.lua
executable file
·630 lines (553 loc) · 18.2 KB
/
member.lua
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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
Member = mondelefant.new_class()
Member.table = 'member'
Member:add_reference{
mode = "1m",
to = "MemberHistory",
this_key = 'id',
that_key = 'member_id',
ref = 'history_entries',
back_ref = 'member'
}
Member:add_reference{
mode = '1m',
to = "MemberImage",
this_key = 'id',
that_key = 'member_id',
ref = 'images',
back_ref = 'member'
}
Member:add_reference{
mode = '1m',
to = "Contact",
this_key = 'id',
that_key = 'member_id',
ref = 'contacts',
back_ref = 'member',
default_order = '"other_member_id"'
}
Member:add_reference{
mode = '1m',
to = "MemberLogin",
this_key = 'id',
that_key = 'member_id',
ref = 'member_login',
back_ref = 'member'
}
Member:add_reference{
mode = '1m',
to = "Contact",
this_key = 'id',
that_key = 'member_id',
ref = 'foreign_contacts',
back_ref = 'other_member',
default_order = '"member_id"'
}
Member:add_reference{
mode = '1m',
to = "Session",
this_key = 'id',
that_key = 'member_id',
ref = 'sessions',
back_ref = 'member',
default_order = '"ident"'
}
Member:add_reference{
mode = '1m',
to = "Draft",
this_key = 'id',
that_key = 'author_id',
ref = 'drafts',
back_ref = 'author',
default_order = '"id"'
}
Member:add_reference{
mode = '1m',
to = "Suggestion",
this_key = 'id',
that_key = 'author_id',
ref = 'suggestions',
back_ref = 'author',
default_order = '"id"'
}
Member:add_reference{
mode = '1m',
to = "Membership",
this_key = 'id',
that_key = 'member_id',
ref = 'memberships',
back_ref = 'member',
default_order = '"area_id"'
}
Member:add_reference{
mode = '1m',
to = "Interest",
this_key = 'id',
that_key = 'member_id',
ref = 'interests',
back_ref = 'member',
default_order = '"id"'
}
Member:add_reference{
mode = '1m',
to = "Initiator",
this_key = 'id',
that_key = 'member_id',
ref = 'initiators',
back_ref = 'member'
}
Member:add_reference{
mode = '1m',
to = "Supporter",
this_key = 'id',
that_key = 'member_id',
ref = 'supporters',
back_ref = 'member'
}
Member:add_reference{
mode = '1m',
to = "Opinion",
this_key = 'id',
that_key = 'member_id',
ref = 'opinions',
back_ref = 'member',
default_order = '"id"'
}
Member:add_reference{
mode = '1m',
to = "Delegation",
this_key = 'id',
that_key = 'truster_id',
ref = 'outgoing_delegations',
back_ref = 'truster',
default_order = '"id"'
}
Member:add_reference{
mode = '1m',
to = "Delegation",
this_key = 'id',
that_key = 'trustee_id',
ref = 'incoming_delegations',
back_ref = 'trustee',
default_order = '"id"'
}
Member:add_reference{
mode = '1m',
to = "DirectVoter",
this_key = 'id',
that_key = 'member_id',
ref = 'direct_voter',
back_ref = 'member',
default_order = '"issue_id"'
}
Member:add_reference{
mode = '1m',
to = "Vote",
this_key = 'id',
that_key = 'member_id',
ref = 'vote',
back_ref = 'member',
default_order = '"issue_id", "initiative_id"'
}
Member:add_reference{
mode = 'mm',
to = "Member",
this_key = 'id',
that_key = 'id',
connected_by_table = 'contact',
connected_by_this_key = 'member_id',
connected_by_that_key = 'other_member_id',
ref = 'saved_members',
}
Member:add_reference{
mode = 'mm',
to = "Member",
this_key = 'id',
that_key = 'id',
connected_by_table = 'contact',
connected_by_this_key = 'other_member_id',
connected_by_that_key = 'member_id',
ref = 'saved_by_members',
}
Member:add_reference{
mode = 'mm',
to = "Unit",
this_key = 'id',
that_key = 'id',
connected_by_table = 'privilege',
connected_by_this_key = 'member_id',
connected_by_that_key = 'unit_id',
ref = 'units'
}
Member:add_reference{
mode = 'mm',
to = "Area",
this_key = 'id',
that_key = 'id',
connected_by_table = 'membership',
connected_by_this_key = 'member_id',
connected_by_that_key = 'area_id',
ref = 'areas'
}
Member:add_reference{
mode = 'mm',
to = "Issue",
this_key = 'id',
that_key = 'id',
connected_by_table = 'interest',
connected_by_this_key = 'member_id',
connected_by_that_key = 'issue_id',
ref = 'issues'
}
Member:add_reference{
mode = 'mm',
to = "Initiative",
this_key = 'id',
that_key = 'id',
connected_by_table = 'initiator',
connected_by_this_key = 'member_id',
connected_by_that_key = 'initiative_id',
ref = 'initiated_initiatives'
}
Member:add_reference{
mode = 'mm',
to = "Initiative",
this_key = 'id',
that_key = 'id',
connected_by_table = 'supporter',
connected_by_this_key = 'member_id',
connected_by_that_key = 'initiative_id',
ref = 'supported_initiatives'
}
Member:add_reference{
mode = 'mm',
to = "Event",
this_key = 'id',
that_key = 'id',
connected_by_table = 'checked_event',
connected_by_this_key = 'member_id',
connected_by_that_key = 'event_id',
ref = 'events'
}
model.has_rendered_content(Member, RenderedMemberStatement, "statement")
function Member:build_selector(args)
local selector = self:new_selector()
if args.active ~= nil then
selector:add_where{ "member.active = ?", args.active }
end
if args.locked ~= nil then
selector:add_where{ "member.locked = ?", args.locked }
end
if args.is_contact_of_member_id then
selector:join("contact", "__model_member__contact", "member.id = __model_member__contact.other_member_id")
selector:add_where{ "__model_member__contact.member_id = ?", args.is_contact_of_member_id }
end
if args.voting_right_for_unit_id then
selector:join("privilege", "__model_member__privilege", { "member.id = __model_member__privilege.member_id AND __model_member__privilege.voting_right AND __model_member__privilege.unit_id = ?", args.voting_right_for_unit_id })
end
if args.admin_search then
local search_string = "%" .. args.admin_search .. "%"
selector:add_where{ "member.identification ILIKE ? OR member.name ILIKE ?", search_string, search_string }
end
if args.order then
if args.order == "id" then
selector:add_order_by("id")
elseif args.order == "identification" then
selector:add_order_by("identification")
elseif args.order == "name" then
selector:add_order_by("name")
else
error("invalid order")
end
end
return selector
end
function Member:lockForReference()
self.get_db_conn().query("LOCK TABLE " .. self:get_qualified_table() .. " IN ROW SHARE MODE")
end
function Member.object:set_password(password)
trace.disable()
local hash_prefix
local salt_length
local function rounds()
return multirand.integer(
config.password_hash_min_rounds,
config.password_hash_max_rounds
)
end
if config.password_hash_algorithm == "crypt_md5" then
hash_prefix = "$1$"
salt_length = 8
elseif config.password_hash_algorithm == "crypt_sha256" then
hash_prefix = "$5$rounds=" .. rounds() .. "$"
salt_length = 16
elseif config.password_hash_algorithm == "crypt_sha512" then
hash_prefix = "$6$rounds=" .. rounds() .. "$"
salt_length = 16
else
error("Unknown hash algorithm selected in configuration")
end
hash_prefix = hash_prefix .. multirand.string(
salt_length,
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz./"
)
local hash = extos.crypt(password, hash_prefix)
if not hash or hash:sub(1, #hash_prefix) ~= hash_prefix then
error("Password hashing algorithm failed")
end
self.password = hash
end
function Member.object:check_password(password)
if type(password) == "string" and type(self.password) == "string" then
return extos.crypt(password, self.password) == self.password
else
return false
end
end
function Member.object_get:password_hash_needs_update()
if self.password == nil then
return nil
end
local function check_rounds(rounds)
if rounds then
rounds = tonumber(rounds)
if
rounds >= config.password_hash_min_rounds and
rounds <= config.password_hash_max_rounds
then
return false
end
end
return true
end
if config.password_hash_algorithm == "crypt_md5" then
return self.password:sub(1,3) ~= "$1$"
elseif config.password_hash_algorithm == "crypt_sha256" then
return check_rounds(self.password:match("^%$5%$rounds=([1-9][0-9]*)%$"))
elseif config.password_hash_algorithm == "crypt_sha512" then
return check_rounds(self.password:match("^%$6%$rounds=([1-9][0-9]*)%$"))
else
error("Unknown hash algorithm selected in configuration")
end
end
function Member.object_get:published_contacts()
return Member:new_selector()
:join('"contact"', nil, '"contact"."other_member_id" = "member"."id"')
:add_where{ '"contact"."member_id" = ?', self.id }
:add_where("public")
:exec()
end
function Member:by_login_and_password(login, password)
local selector = self:new_selector()
selector:add_where{'"login" = ?', login }
selector:add_where('NOT "locked"')
selector:optional_object_mode()
local member = selector:exec()
if member and member:check_password(password) then
return member
else
return nil
end
end
function Member:by_login(login)
local selector = self:new_selector()
selector:add_where{'"login" = ?', login }
selector:optional_object_mode()
return selector:exec()
end
function Member:by_name(name)
local selector = self:new_selector()
selector:add_where{'"name" = ?', name }
selector:optional_object_mode()
return selector:exec()
end
function Member:get_search_selector(search_string)
return self:new_selector()
:add_field( {'"highlight"("member"."name", ?)', search_string }, "name_highlighted")
:add_where{ '"member"."text_search_data" @@ "text_search_query"(?)', search_string }
:add_where("activated NOTNULL AND active")
end
function Member.object:send_invitation(template_file, subject)
trace.disable()
self.invite_code = multirand.string( 24, "23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" )
self:save()
local subject = subject
local content
if template_file then
local fh = io.open(template_file, "r")
content = fh:read("*a")
content = (content:gsub("#{invite_code}", self.invite_code))
else
subject = config.mail_subject_prefix .. _"Invitation to LiquidFeedback"
content = slot.use_temporary(function()
slot.put(_"Hello\n\n")
slot.put(_"You are invited to LiquidFeedback. To register please click the following link:\n\n")
slot.put(request.get_absolute_baseurl() .. "index/register.html?invite=" .. self.invite_code .. "\n\n")
slot.put(_"If this link is not working, please open following url in your web browser:\n\n")
slot.put(request.get_absolute_baseurl() .. "index/register.html\n\n")
slot.put(_"On that page please enter the invite key:\n\n")
slot.put(self.invite_code .. "\n\n")
end)
end
local success = net.send_mail{
envelope_from = config.mail_envelope_from,
from = config.mail_from,
reply_to = config.mail_reply_to,
to = self.notify_email_unconfirmed or self.notify_email,
subject = subject,
content_type = "text/plain; charset=UTF-8",
content = content
}
return success
end
function Member.object:set_notify_email(notify_email)
trace.disable()
local expiry = db:query("SELECT now() + '7 days'::interval as expiry", "object").expiry
self.notify_email_unconfirmed = notify_email
self.notify_email_secret = multirand.string( 24, "23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz" )
self.notify_email_secret_expiry = expiry
local content = slot.use_temporary(function()
slot.put(_"Hello " .. self.name .. ",\n\n")
slot.put(_"Please confirm your email address by clicking the following link:\n\n")
slot.put(request.get_absolute_baseurl() .. "index/confirm_notify_email.html?secret=" .. self.notify_email_secret .. "\n\n")
slot.put(_"If this link is not working, please open following url in your web browser:\n\n")
slot.put(request.get_absolute_baseurl() .. "index/confirm_notify_email.html\n\n")
slot.put(_"On that page please enter the confirmation code:\n\n")
slot.put(self.notify_email_secret .. "\n\n")
end)
local success = net.send_mail{
envelope_from = config.mail_envelope_from,
from = config.mail_from,
reply_to = config.mail_reply_to,
to = self.notify_email_unconfirmed,
subject = config.mail_subject_prefix .. _"Email confirmation request",
content_type = "text/plain; charset=UTF-8",
content = content
}
if success then
local lock_expiry = db:query("SELECT now() + '1 hour'::interval AS lock_expiry", "object").lock_expiry
self.notify_email_lock_expiry = lock_expiry
end
self:save()
return success
end
function Member.object:get_setting(key)
return Setting:by_pk(self.id, key)
end
function Member.object:get_setting_value(key)
local setting = Setting:by_pk(self.id, key)
if setting then
return setting.value
end
end
function Member.object:set_setting(key, value)
local setting = self:get_setting(key)
if not setting then
setting = Setting:new()
setting.member_id = self.id
setting.key = key
end
setting.value = value
setting:save()
end
function Member.object:get_setting_maps_by_key(key)
return SettingMap:new_selector()
:add_where{ "member_id = ?", self.id }
:add_where{ "key = ?", key }
:add_order_by("subkey")
:exec()
end
function Member.object:get_setting_map_by_key_and_subkey(key, subkey)
return SettingMap:new_selector()
:add_where{ "member_id = ?", self.id }
:add_where{ "key = ?", key }
:add_where{ "subkey = ?", subkey }
:add_order_by("subkey")
:optional_object_mode()
:exec()
end
function Member.object:set_setting_map(key, subkey, value)
setting_map = self:get_setting_map_by_key_and_subkey(key, subkey)
if not setting_map then
setting_map = SettingMap:new()
setting_map.member_id = self.id
setting_map.key = key
setting_map.subkey = subkey
end
setting_map.value = value
setting_map:save()
end
function Member.object_get:notify_email_locked()
return(
Member:new_selector()
:add_where{ "id = ?", app.session.member.id }
:add_where("notify_email_lock_expiry > now()")
:count() == 1
)
end
function Member.object_get:units_with_voting_right()
return(Unit:new_selector()
:join("privilege", nil, { "privilege.unit_id = unit.id AND privilege.member_id = ? AND privilege.voting_right", self.id })
:exec()
)
end
function Member.object:ui_field_text(args)
args = args or {}
if app.session:has_access("authors_pseudonymous") then
-- ugly workaround for getting html into a replaced string and to the user
ui.container{label = args.label, label_attr={class="ui_field_label"}, content = function()
slot.put(string.format('<span><a href="%s">%s</a></span>',
encode.url{
module = "member",
view = "show",
id = self.id,
},
encode.html(self.name)))
end
}
else
ui.field.text{ label = args.label, value = _"[not displayed public]" }
end
end
function Member.object:has_voting_right_for_unit_id(unit_id)
if not self.__units_with_voting_right_hash then
local privileges = Privilege:new_selector()
:add_where{ "member_id = ?", self.id }
:add_where("voting_right")
:exec()
self.__units_with_voting_right_hash = {}
for i, privilege in ipairs(privileges) do
self.__units_with_voting_right_hash[privilege.unit_id] = true
end
end
return self.__units_with_voting_right_hash[unit_id] and true or false
end
function Member.object:has_polling_right_for_unit_id(unit_id)
if not self.__units_with_polling_right_hash then
local privileges = Privilege:new_selector()
:add_where{ "member_id = ?", self.id }
:add_where("polling_right")
:exec()
self.__units_with_polling_right_hash = {}
for i, privilege in ipairs(privileges) do
self.__units_with_polling_right_hash[privilege.unit_id] = true
end
end
return self.__units_with_polling_right_hash[unit_id] and true or false
end
function Member.object:get_login_data(mode)
local selector = MemberLogin:new_selector()
selector:add_where{ "member_id = ? AND geolat IS NOT NULL AND geolng IS NOT NULL", app.session.member.id }
-- :add_where{ "login_time < ?", self.last_login }
selector:add_order_by('"login_time" DESC')
selector:limit(1)
selector:optional_object_mode()
return selector:exec()
end
function Member.object:get_delegatee_member(unit_id, area_id, issue_id)
local selector = Member:new_selector()
if unit_id then
selector:join("delegation", nil, { "delegation.trustee_id = member.id AND delegation.scope = 'unit' AND delegation.unit_id = ? AND delegation.truster_id = ?", unit_id, self.id })
end
selector:optional_object_mode()
return selector:exec()
end