| rev | 
   line source | 
| 
bsw/jbe@1309
 | 
     1 local verification = Verification:by_id(param.get_id())
 | 
| 
bsw/jbe@1309
 | 
     2 
 | 
| 
bsw/jbe@1309
 | 
     3 local function update_data()
 | 
| 
bsw@1356
 | 
     4   local old_verification_data = verification.verification_data or {}
 | 
| 
bsw/jbe@1309
 | 
     5   verification.verification_data = json.object()
 | 
| 
bsw/jbe@1309
 | 
     6   
 | 
| 
bsw/jbe@1309
 | 
     7   for i, field in ipairs(config.self_registration.fields) do
 | 
| 
bsw/jbe@1309
 | 
     8     local value = param.get(field.name)
 | 
| 
bsw/jbe@1309
 | 
     9     if field.name == "fiscal_code" then
 | 
| 
bsw/jbe@1309
 | 
    10       value = string.gsub(value, "[^A-Z0-9]", "")
 | 
| 
bsw/jbe@1309
 | 
    11     elseif field.name == "mobile_phone" then
 | 
| 
bsw/jbe@1309
 | 
    12       value = string.gsub(value, "[^0-9]", "")
 | 
| 
bsw@1352
 | 
    13     elseif field.name == "unit" then
 | 
| 
bsw@1352
 | 
    14       value = string.gsub(value, "[^0-9]", "")
 | 
| 
bsw@1690
 | 
    15       if old_verification_data.unit and old_verification_data.unit ~= "" and old_verification_data.unit ~= value then
 | 
| 
bsw@1366
 | 
    16         local old_unit_privilege = Privilege:by_pk(old_verification_data.unit, verification.requesting_member_id)
 | 
| 
bsw@1357
 | 
    17         if old_unit_privilege then
 | 
| 
bsw@1357
 | 
    18           old_unit_privilege:destroy()
 | 
| 
bsw@1357
 | 
    19         end
 | 
| 
bsw@1356
 | 
    20       end
 | 
| 
bsw@1356
 | 
    21       if value ~= old_verification_data.unit and value ~= "" then
 | 
| 
bsw@1353
 | 
    22         local unit_privilege = Privilege:new()
 | 
| 
bsw@1366
 | 
    23         unit_privilege.member_id = verification.requesting_member_id
 | 
| 
bsw@1355
 | 
    24         unit_privilege.unit_id = tonumber(value)
 | 
| 
bsw@1352
 | 
    25         unit_privilege.voting_right = true
 | 
| 
bsw@1352
 | 
    26         unit_privilege.initiative_right = true
 | 
| 
bsw@1354
 | 
    27         unit_privilege:save()
 | 
| 
bsw@1352
 | 
    28       end
 | 
| 
bsw@1359
 | 
    29     elseif field.name == "sequential_number" then
 | 
| 
bsw@1359
 | 
    30       value = old_verification_data.sequential_number 
 | 
| 
bsw@1362
 | 
    31       if not value then
 | 
| 
bsw@1362
 | 
    32         local last_sequential_number = 0
 | 
| 
bsw@1362
 | 
    33         db:query('LOCK TABLE "verification" IN SHARE ROW EXCLUSIVE MODE')
 | 
| 
bsw@1364
 | 
    34         local record = Verification:new_selector()
 | 
| 
bsw@1362
 | 
    35           :reset_fields()
 | 
| 
bsw@1364
 | 
    36           :add_field("max((verification_data->>'sequential_number')::int8)", "max_sequential_number")
 | 
| 
bsw@1362
 | 
    37           :optional_object_mode()
 | 
| 
bsw@1362
 | 
    38           :exec()
 | 
| 
bsw@1365
 | 
    39         if record and record.max_sequential_number then
 | 
| 
bsw@1364
 | 
    40           last_sequential_number = record.max_sequential_number
 | 
| 
bsw@1362
 | 
    41         end
 | 
| 
bsw@1362
 | 
    42         value = last_sequential_number + 1
 | 
| 
bsw@1362
 | 
    43       end
 | 
| 
bsw@1363
 | 
    44     elseif field.type ~= "image" then
 | 
| 
bsw@1363
 | 
    45       value = string.gsub(value, "^%s+", "")
 | 
| 
bsw@1363
 | 
    46       value = string.gsub(value, "%s+$", "")
 | 
| 
bsw@1363
 | 
    47       value = string.gsub(value, "%s+", " ")
 | 
| 
bsw/jbe@1309
 | 
    48     end
 | 
| 
bsw/jbe@1309
 | 
    49     verification.verification_data[field.name] = value
 | 
| 
bsw/jbe@1309
 | 
    50   end
 | 
| 
bsw/jbe@1309
 | 
    51 end
 | 
| 
bsw/jbe@1309
 | 
    52 
 | 
| 
bsw@1812
 | 
    53 local function check_db_error(db_error)
 | 
| 
bsw@1812
 | 
    54   if db_error then
 | 
| 
bsw@1814
 | 
    55     if db_error:is_kind_of("IntegrityConstraintViolation.UniqueViolation") then
 | 
| 
bsw@1812
 | 
    56       slot.select("error", function()
 | 
| 
bsw@1812
 | 
    57         ui.tag{ content = _"Identification unique violation: This identification is already in use for another member." }
 | 
| 
bsw@1812
 | 
    58       end )
 | 
| 
bsw@1812
 | 
    59       return false
 | 
| 
bsw@1812
 | 
    60     else
 | 
| 
bsw@1812
 | 
    61       error(db_error)
 | 
| 
bsw@1812
 | 
    62     end
 | 
| 
bsw@1812
 | 
    63   end
 | 
| 
bsw@1812
 | 
    64 end
 | 
| 
bsw@1812
 | 
    65 
 | 
| 
bsw/jbe@1309
 | 
    66 if verification.verified_member_id then
 | 
| 
bsw/jbe@1309
 | 
    67   
 | 
| 
bsw/jbe@1309
 | 
    68   local member = Member:by_id(verification.verified_member_id)
 | 
| 
bsw/jbe@1309
 | 
    69   
 | 
| 
bsw@1810
 | 
    70   local identification = param.get("identification")
 | 
| 
bsw@1809
 | 
    71   if identification and #identification == 0 then
 | 
| 
bsw@1809
 | 
    72     identification = nil
 | 
| 
bsw@1809
 | 
    73   end
 | 
| 
bsw@1809
 | 
    74   member.identification = identification
 | 
| 
bsw@1809
 | 
    75 
 | 
| 
bsw/jbe@1309
 | 
    76   member.notify_email = param.get("email")
 | 
| 
bsw@1812
 | 
    77 
 | 
| 
bsw@1815
 | 
    78   local success = check_db_error(member:try_save())
 | 
| 
bsw@1815
 | 
    79   if not success then
 | 
| 
bsw@1815
 | 
    80     return false
 | 
| 
bsw@1815
 | 
    81   end
 | 
| 
bsw@1812
 | 
    82 
 | 
| 
bsw/jbe@1309
 | 
    83   update_data()
 | 
| 
bsw@1812
 | 
    84 
 | 
| 
bsw/jbe@1309
 | 
    85   verification:save()
 | 
| 
bsw/jbe@1309
 | 
    86 
 | 
| 
bsw@1816
 | 
    87   if param.get("cancel") then
 | 
| 
bsw@1816
 | 
    88     db:query({ "SELECT delete_member(?)", member.id })
 | 
| 
bsw@1816
 | 
    89     return
 | 
| 
bsw@1816
 | 
    90   end
 | 
| 
bsw@1816
 | 
    91 
 | 
| 
bsw/jbe@1309
 | 
    92   if param.get("invite") then
 | 
| 
bsw/jbe@1309
 | 
    93     member:send_invitation()
 | 
| 
bsw/jbe@1309
 | 
    94   end
 | 
| 
bsw/jbe@1309
 | 
    95 
 | 
| 
bsw/jbe@1309
 | 
    96 elseif param.get("drop") then
 | 
| 
bsw/jbe@1309
 | 
    97   
 | 
| 
bsw/jbe@1309
 | 
    98   verification.denied = "now"
 | 
| 
bsw/jbe@1309
 | 
    99   verification:save()
 | 
| 
bsw/jbe@1309
 | 
   100   return
 | 
| 
bsw/jbe@1309
 | 
   101   
 | 
| 
bsw/jbe@1309
 | 
   102 elseif param.get("accredit") then
 | 
| 
bsw/jbe@1309
 | 
   103   
 | 
| 
bsw/jbe@1309
 | 
   104   local member = Member:by_id(verification.requesting_member_id)
 | 
| 
bsw@1813
 | 
   105 
 | 
| 
bsw@1813
 | 
   106   local identification = param.get("identification")
 | 
| 
bsw@1813
 | 
   107   if identification and #identification == 0 then
 | 
| 
bsw@1813
 | 
   108     identification = nil
 | 
| 
bsw@1813
 | 
   109   end
 | 
| 
bsw@1813
 | 
   110   member.identification = identification
 | 
| 
bsw@1813
 | 
   111 
 | 
| 
bsw/jbe@1309
 | 
   112   member.notify_email = param.get("email")
 | 
| 
bsw@1815
 | 
   113 
 | 
| 
bsw@1815
 | 
   114   local success = check_db_error(member:try_save())
 | 
| 
bsw@1815
 | 
   115   if not success then
 | 
| 
bsw@1815
 | 
   116     return false
 | 
| 
bsw@1815
 | 
   117   end
 | 
| 
bsw@1490
 | 
   118 
 | 
| 
bsw@1490
 | 
   119   if config.self_registration.manual_invitation then
 | 
| 
bsw@1490
 | 
   120     local function secret_token()
 | 
| 
bsw@1490
 | 
   121       local parts = {}
 | 
| 
bsw@1490
 | 
   122       for i = 1, 5 do
 | 
| 
bsw@1490
 | 
   123         parts[#parts+1] = multirand.string(5, "23456789bcdfghjkmnpqrstvwxyz")
 | 
| 
bsw@1490
 | 
   124       end
 | 
| 
bsw@1490
 | 
   125       return (table.concat(parts, "-"))
 | 
| 
bsw@1490
 | 
   126     end
 | 
| 
bsw@1490
 | 
   127     member.invite_code = secret_token()
 | 
| 
bsw@1490
 | 
   128     member:save()
 | 
| 
bsw@1490
 | 
   129   else
 | 
| 
bsw@1490
 | 
   130     member:send_invitation()
 | 
| 
bsw@1490
 | 
   131   end
 | 
| 
bsw/jbe@1309
 | 
   132 
 | 
| 
bsw/jbe@1309
 | 
   133   for i, unit_id in ipairs(config.self_registration.grant_privileges_for_unit_ids) do
 | 
| 
bsw/jbe@1309
 | 
   134     local privilege = Privilege:new()
 | 
| 
bsw/jbe@1309
 | 
   135     privilege.member_id = member.id
 | 
| 
bsw/jbe@1309
 | 
   136     privilege.unit_id = unit_id
 | 
| 
bsw/jbe@1309
 | 
   137     privilege.initiative_right = true
 | 
| 
bsw/jbe@1309
 | 
   138     privilege.voting_right = true
 | 
| 
bsw/jbe@1309
 | 
   139     privilege:save()
 | 
| 
bsw/jbe@1309
 | 
   140   end
 | 
| 
bsw/jbe@1309
 | 
   141 
 | 
| 
bsw/jbe@1309
 | 
   142   update_data()
 | 
| 
bsw/jbe@1309
 | 
   143   
 | 
| 
bsw/jbe@1309
 | 
   144   verification.verified_member_id = verification.requesting_member_id
 | 
| 
bsw/jbe@1309
 | 
   145   verification.verifying_member_id = app.session.member_id
 | 
| 
bsw/jbe@1309
 | 
   146   verification.verified = "now"
 | 
| 
bsw/jbe@1309
 | 
   147   
 | 
| 
bsw/jbe@1309
 | 
   148   verification:save()
 | 
| 
bsw/jbe@1309
 | 
   149   
 | 
| 
bsw/jbe@1309
 | 
   150   
 | 
| 
bsw/jbe@1309
 | 
   151 else
 | 
| 
bsw/jbe@1309
 | 
   152 
 | 
| 
bsw/jbe@1309
 | 
   153   update_data()
 | 
| 
bsw/jbe@1309
 | 
   154   verification:save()
 | 
| 
bsw/jbe@1309
 | 
   155 
 | 
| 
bsw/jbe@1309
 | 
   156 end
 |