liquid_feedback_frontend

view env/util/add_support.lua @ 1516:9d99a4f262a2

Fixed privilege check for support via API
author bsw
date Thu Aug 20 14:03:09 2020 +0200 (2020-08-20)
parents 71f54c43d7cb
children 87e2a58bef9e
line source
1 function util.add_support(initiative_id, draft_id)
3 local initiative = Initiative:new_selector():add_where{ "id = ?", initiative_id }:single_object_mode():exec()
5 -- TODO important m1 selectors returning result _SET_!
6 local issue = initiative:get_reference_selector("issue"):for_share():single_object_mode():exec()
8 local member = app.access_token.member or app.session.member
10 if not member:has_voting_right_for_unit_id(issue.area.unit_id) then
11 slot.put_into("error", _"No voting rights.")
12 return false
13 end
15 if issue.closed then
16 slot.put_into("error", _"This issue is already closed.")
17 return false
18 elseif issue.fully_frozen then
19 slot.put_into("error", _"Voting for this issue has already begun.")
20 return false
21 elseif
22 (issue.half_frozen and issue.phase_finished) or
23 (not issue.accepted and issue.phase_finished)
24 then
25 slot.put_into("error", _"Current phase is already closed.")
26 return false
27 end
29 if initiative.revoked then
30 slot.put_into("error", _"This initiative is revoked")
31 return false
32 end
34 local member = app.session.member
36 local supporter = Supporter:by_pk(initiative.id, member.id)
38 local last_draft = Draft:new_selector()
39 :add_where{ "initiative_id = ?", initiative.id }
40 :add_order_by("id DESC")
41 :limit(1)
42 :single_object_mode()
43 :exec()
45 if draft_id and draft_id ~= last_draft.id then
46 slot.select("error", function()
47 ui.tag{ content = _"The initiative draft has been updated again in the meanwhile, support not updated!" }
48 end)
49 return false
50 end
52 if not supporter then
53 supporter = Supporter:new()
54 supporter.member_id = member.id
55 supporter.initiative_id = initiative.id
56 supporter.draft_id = last_draft.id
57 supporter:save()
58 elseif supporter.draft_id ~= last_draft.id then
59 supporter.draft_id = last_draft.id
60 supporter:save()
61 slot.put_into("notice", _"Your support has been updated to the latest draft")
62 else
63 slot.put_into("notice", _"You are already supporting the latest draft")
64 end
66 return true
68 end

Impressum / About Us