liquid_feedback_frontend
view app/main/api/member.lua @ 1538:25ea15b4bd5e
Reworked cookie session control, exceptions for API
| author | bsw | 
|---|---|
| date | Tue Oct 20 17:48:49 2020 +0200 (2020-10-20) | 
| parents | 1e5c1edf7388 | 
| children | be96623e575a | 
 line source
     1 slot.set_layout(nil, "application/json")
     3 local r = json.object{
     4   result = json.array()
     5 }
     7 local selector = Member:new_selector()
     8   :add_where("activated NOTNULL")
     9   :add_order_by("id")
    11 if param.get("id") then
    12   selector:add_where{ "id = ?", param.get("id") }
    13 end
    15 local role = param.get("role")
    16 if role then
    17   local units = Unit:new_selector()
    18     :add_where{ "attr->>'role' = ?", role }
    19     :exec()
    20   if #units ~= 1 then
    21     request.set_status("400 Bad Request")
    22     slot.put_into("data", json.export{ 
    23       error = "invalid_role",
    24       error_description = "role not available"
    25     })
    26     return
    27   end
    28   local unit = units[1]
    29   if unit.attr.only_visible_for_role 
    30     and (
    31       not app.access_token 
    32       or not app.access_token.member:has_role(unit.attr.only_visible_for_role)
    33     )
    34   then
    35     request.set_status("400 Bad Request")
    36     slot.put_into("data", json.export{ 
    37       error = "no_priv",
    38       error_description = "no privileges to access this role"
    39     })
    40     return
    41   end
    42   selector:join("privilege", nil, "privilege.member_id = member.id")
    43   selector:join("unit", nil, { "unit.id = privilege.unit_id AND unit.attr->>'role' = ?", role })
    44 end
    46 local search = param.get("q")
    47 if app.scopes.read_identities and search then
    48   search = "%" .. search .. "%"
    49   selector:add_where{ "name ILIKE ? OR identification ILIKE ?", search, search }
    50 end
    52 local members = selector:exec()
    53 local r = json.object()
    54 r.result = execute.chunk{ module = "api", chunk = "_member", params = { 
    55   members = members,
    56   include_unit_ids = param.get("include_unit_ids") and true or false,
    57   include_units = param.get("include_units") and true or false,
    58   include_roles = param.get("include_roles") and true or false
    59 } } 
    62 slot.put_into("data", json.export(r))
    63 slot.put_into("data", "\n")
