liquid_feedback_frontend

diff app/main/registration/_action/register.lua @ 1309:32cc544d5a5b

Cumulative patch for upcoming frontend version 4
author bsw/jbe
date Sun Jul 15 14:07:29 2018 +0200 (2018-07-15)
parents
children 74ce9970605b
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/app/main/registration/_action/register.lua	Sun Jul 15 14:07:29 2018 +0200
     1.3 @@ -0,0 +1,296 @@
     1.4 +local function check_italian_mobile_phone_number(value)
     1.5 +
     1.6 +  if not value then
     1.7 +    return false
     1.8 +  end
     1.9 +
    1.10 +  value = string.gsub(value, "[^0-9]*", "")
    1.11 +
    1.12 +  if #(value) < 9 or #(value) > 10 then
    1.13 +    return false
    1.14 +  end
    1.15 +
    1.16 +  local mobile_phone_prefixes = {
    1.17 +    { min = 320,  max = 329, comment = "Wind Tre" },
    1.18 +    { min = 330,  max = 339, comment = "Telecom Italia (TIM)" },
    1.19 +    { min = 340,  max = 349, comment = "Vodafone Omnitel" },
    1.20 +    { min = 350,  max = 359, comment = "" },
    1.21 +    { min = 360,  max = 369, comment = "Telecom Italia (TIM)" },
    1.22 +    { min = 370,  max = 379, comment = "" },
    1.23 +    { min = 380,  max = 389, comment = "Wind Tre" },
    1.24 +    { min = 390,  max = 393, comment = "Wind Tre" },
    1.25 +    { min = 394,  max = 399, comment = "Wind Tre" }
    1.26 +  }
    1.27 +
    1.28 +  local value_prefix = tonumber(string.match(value, "^(...)"))
    1.29 +
    1.30 +  local valid_prefix = false
    1.31 +
    1.32 +  for i, prefix in ipairs(mobile_phone_prefixes) do
    1.33 +    trace.debug(value_prefix, prefix.min)
    1.34 +    if value_prefix >= prefix.min and value_prefix <= prefix.max then
    1.35 +      valid_prefix = true
    1.36 +    end
    1.37 +  end
    1.38 +
    1.39 +  if valid_prefix then
    1.40 +    return true
    1.41 +  else
    1.42 +    return false
    1.43 +  end
    1.44 +end
    1.45 +
    1.46 +local function check_uk_mobile_phone_number(value)
    1.47 +
    1.48 +  if not value then
    1.49 +    return false
    1.50 +  end
    1.51 +
    1.52 +  value = string.gsub(value, "[^0-9]*", "")
    1.53 +
    1.54 +  if #(value) < 11 or #(value) > 11 then
    1.55 +    return false
    1.56 +  end
    1.57 +
    1.58 +  local mobile_phone_prefixes = {
    1.59 +    { min = 071,  max = 079, comment = "UK phone" },
    1.60 +  }
    1.61 +
    1.62 +  local value_prefix = tonumber(string.match(value, "^(...)"))
    1.63 +
    1.64 +  local valid_prefix = false
    1.65 +
    1.66 +  for i, prefix in ipairs(mobile_phone_prefixes) do
    1.67 +    trace.debug(value_prefix, prefix.min)
    1.68 +    if value_prefix >= prefix.min and value_prefix <= prefix.max then
    1.69 +      valid_prefix = true
    1.70 +    end
    1.71 +  end
    1.72 +
    1.73 +  if valid_prefix then
    1.74 +    return true
    1.75 +  else
    1.76 +    return false
    1.77 +  end
    1.78 +end
    1.79 +
    1.80 +local errors = 0
    1.81 +
    1.82 +local manual_verification
    1.83 +
    1.84 +if config.self_registration.allow_bypass_checks and param.get("manual_verification") then
    1.85 +  manual_verification = true
    1.86 +end
    1.87 +
    1.88 +for i, checkbox in ipairs(config.use_terms_checkboxes) do
    1.89 +  local accepted = param.get("use_terms_checkbox_" .. checkbox.name, atom.boolean)
    1.90 +  if not accepted then
    1.91 +    slot.put_into("error", checkbox.not_accepted_error)
    1.92 +    errors = errors + 1
    1.93 +  end
    1.94 +end
    1.95 +
    1.96 +local email = param.get("email")
    1.97 +
    1.98 +local members = Member:new_selector()
    1.99 +  :add_where{ "notify_email = ? OR notify_email_unconfirmed = ?", email }
   1.100 +  :exec()
   1.101 +  
   1.102 +if #members > 0 then
   1.103 +  slot.select("error", function()
   1.104 +    slot.put_into("registration_register_email_invalid", "already_used")
   1.105 +    ui.tag{ content = _"This email address already been used. Please check your inbox for an invitation or contact us." }
   1.106 +  end)
   1.107 +  errors = errors + 1
   1.108 +end
   1.109 +
   1.110 +local verification = Verification:new()
   1.111 +verification.requested = "now"
   1.112 +verification.request_origin = json.object{
   1.113 +  ip = request.get_header("X-Forwarded-For"),
   1.114 +  hostname = request.get_header("X-Forwarded-Host")
   1.115 +}
   1.116 +verification.request_data = json.object()
   1.117 +
   1.118 +for i, field in ipairs(config.self_registration.fields) do
   1.119 +  if field.name == "date_of_birth" then
   1.120 +    local day = tonumber(param.get("verification_data_" .. field.name .. "_day"))
   1.121 +    local month = tonumber(param.get("verification_data_" .. field.name .. "_month"))
   1.122 +    local year = tonumber(param.get("verification_data_" .. field.name .. "_year"))
   1.123 +    local date = atom.date:new{ year = year, month = month, day = day }
   1.124 +    if date.invalid then
   1.125 +      slot.select("error", function()
   1.126 +        ui.container{ content = _"Please check date of birth" }
   1.127 +        slot.put_into("self_registration__invalid_" .. field.name, "invalid")
   1.128 +      end)
   1.129 +      errors = errors + 1
   1.130 +    end
   1.131 +    local today = atom.date:get_current()
   1.132 +    local date_16y_ago = atom.date:new{ year = today.year - 16, month = today.month, day = today.day }
   1.133 +    if date_16y_ago.invalid and today.month == 2 and today.day == 29 then
   1.134 +      date_16y_ago = atom.date:new{ year = today.year - 16, month = 2, day = 28 }
   1.135 +    end
   1.136 +    if date > date_16y_ago then
   1.137 +      request.redirect{ external = encode.url { module = "registration", view = "register_rejected_age" } }      
   1.138 +      return
   1.139 +    end
   1.140 +    verification.request_data[field.name] = string.format("%04i-%02i-%02i", year, month, day)
   1.141 +    
   1.142 +  else
   1.143 +    local value = param.get("verification_data_" .. field.name)
   1.144 +    if not value or (#value < 1 and (not manual_verification or field.name ~= "mobile_phone")) then
   1.145 +      slot.put_into("self_registration__invalid_" .. field.name, "to_short")
   1.146 +      slot.select("error", function()
   1.147 +        ui.container{ content = _("Please enter: #{field_name}", { field_name = field.label }) }
   1.148 +      end)
   1.149 +      errors = errors + 1
   1.150 +    end
   1.151 +    if field.name == "fiscal_code" then
   1.152 +      value = string.upper(value)
   1.153 +      value = string.gsub(value, "[^A-Z0-9]", "")
   1.154 +    elseif field.name == "mobile_phone" then
   1.155 +      value = string.gsub(value, "[^0-9]", "")
   1.156 +    else
   1.157 +      value = string.gsub(value, "^%s+", "")
   1.158 +      value = string.gsub(value, "%s+$", "")
   1.159 +      value = string.gsub(value, "%s+", " ")
   1.160 +    end
   1.161 +    verification.request_data[field.name] = value
   1.162 +  end
   1.163 +end
   1.164 +
   1.165 +local automatic_verification_possible = true
   1.166 +
   1.167 +local mobile_phone = verification.request_data.mobile_phone
   1.168 +
   1.169 +if not manual_verification then
   1.170 +  if config.self_registration.check_for_italien_mobile_phone then
   1.171 +    if not check_italian_mobile_phone_number(mobile_phone) then
   1.172 +      slot.select("error", function()
   1.173 +        ui.container{ content = _"Please check the mobile phone number (invalid format)" }
   1.174 +      end)
   1.175 +      errors = errors + 1
   1.176 +    end
   1.177 +  end
   1.178 +
   1.179 +  if config.self_registration.check_for_uk_mobile_phone then
   1.180 +    if not check_uk_mobile_phone_number(mobile_phone) then
   1.181 +      slot.select("error", function()
   1.182 +        ui.container{ content = _"Please check the mobile phone number (invalid format)" }
   1.183 +      end)
   1.184 +      errors = errors + 1
   1.185 +    end
   1.186 +  end
   1.187 +end
   1.188 +
   1.189 +if config.self_registration.check_for_italian_fiscal_code then
   1.190 +  local check_fiscal_code = execute.chunk{ module = "registration", chunk = "_check_fiscal_code" }
   1.191 +
   1.192 +  local fiscal_code_valid, fiscal_code_error = check_fiscal_code(
   1.193 +    verification.request_data.fiscal_code,
   1.194 +    {
   1.195 +      first_name = verification.request_data.first_name,
   1.196 +      last_name = verification.request_data.name,
   1.197 +      year = tonumber(string.match(verification.request_data.date_of_birth, "^(....)-..-..$")),
   1.198 +      month = tonumber(string.match(verification.request_data.date_of_birth, "^....-(..)-..$")),
   1.199 +      day = tonumber(string.match(verification.request_data.date_of_birth, "^....-..-(..)$")),
   1.200 +    }
   1.201 +  )
   1.202 +
   1.203 +  if fiscal_code_valid then
   1.204 +    verification.comment = (verification.comment or "").. " /// Fiscal code matched"
   1.205 +  else
   1.206 +    slot.select("error", function()
   1.207 +      ui.container{ content = _"Please check the fiscal code (invalid format or does not match name, first name and/or date of birth)" }
   1.208 +    end)
   1.209 +    errors = errors + 1
   1.210 +    --table.insert(manual_check_reasons, "fiscal code does not match (" .. fiscal_code_error .. ")")
   1.211 +  end
   1.212 +end
   1.213 +
   1.214 +if errors > 0 then
   1.215 +  return false
   1.216 +end
   1.217 +
   1.218 +local member = Member:new()
   1.219 +member.notify_email = email
   1.220 +member:save()
   1.221 +
   1.222 +for i, checkbox in ipairs(config.use_terms_checkboxes) do
   1.223 +  local accepted = param.get("use_terms_checkbox_" .. checkbox.name, atom.boolean)
   1.224 +  local member_useterms = MemberUseterms:new()
   1.225 +  member_useterms.member_id = member.id
   1.226 +  member_useterms.contract_identifier = checkbox.name
   1.227 +  member_useterms:save()
   1.228 +end
   1.229 +
   1.230 +verification.requesting_member_id = member.id
   1.231 +
   1.232 +local manual_check_reasons = {}
   1.233 +
   1.234 +if manual_verification then
   1.235 +  table.insert(manual_check_reasons, "User requested manual verification (during step 1)")
   1.236 +end
   1.237 +
   1.238 +local existing_verifications = Verification:new_selector()
   1.239 +  :add_where{ "request_data->>'mobile_phone' = ?", mobile_phone }
   1.240 +  :add_where("comment ilike '%SMS code%'")
   1.241 +  :exec()
   1.242 +
   1.243 +if #existing_verifications > 0 then
   1.244 +  table.insert(manual_check_reasons, "mobile phone number already used before")
   1.245 +end
   1.246 +
   1.247 +if #manual_check_reasons > 0 then
   1.248 +  local reasons = table.concat(manual_check_reasons, ", ")
   1.249 +  verification.comment = (verification.comment or "").. " /// Manual verification needed: " .. reasons
   1.250 +  verification:save()
   1.251 +  request.redirect{ external = encode.url { module = "registration", view = "register_manual_check_needed" } } 
   1.252 +
   1.253 +else
   1.254 +  local pin = multirand.string(6, "0123456789")
   1.255 +  verification.request_data.sms_code = pin
   1.256 +  verification.request_data.sms_code_tries = 3
   1.257 +  local sms_text = config.self_registration.sms_text
   1.258 +  local sms_text = string.gsub(sms_text, "{PIN}", pin)
   1.259 +  print("SMS Code: " .. sms_text)
   1.260 +  local phone_number
   1.261 +  if config.self_registration.sms_strip_leading_zero then
   1.262 +    phone_number = string.match(verification.request_data.mobile_phone, "0(.+)")
   1.263 +  else
   1.264 +    phone_number = verification.request_data.mobile_phone
   1.265 +  end
   1.266 +  phone_number = config.self_registration.sms_prefix .. phone_number
   1.267 +  local params = {
   1.268 +    id = config.self_registration.sms_id,
   1.269 +    pass = config.self_registration.sms_pass,
   1.270 +    gateway = config.self_registration.sms_gateway,
   1.271 +    absender = config.self_registration.sms_from,
   1.272 +    text = sms_text,
   1.273 +    nummer = phone_number,
   1.274 +    test = config.self_registration.test and "1" or nil
   1.275 +  }
   1.276 +  local params_list = {}
   1.277 +  for k, v in pairs(params) do
   1.278 +    table.insert(params_list, encode.url_part(k) .. "=" .. encode.url_part(v))
   1.279 +  end
   1.280 +  
   1.281 +  local params_string = table.concat(params_list, "&")
   1.282 +  local url = "http://gateway.any-sms.biz/send_sms.php?" .. params_string
   1.283 +  print("curl " .. url)
   1.284 +  local output, err, status = extos.pfilter(nil, "curl", url)
   1.285 +  print(output)
   1.286 +  verification.request_data.sms_code_sent_status = output
   1.287 +  if not string.match(output, "^err:0") then
   1.288 +    verification.comment = (verification.comment or "").. " /// Manual verification needed: sending SMS failed (" .. output .. ")"
   1.289 +    verification:save()
   1.290 +    request.redirect{ external = encode.url { module = "registration", view = "register_manual_check_needed" } } 
   1.291 +    return
   1.292 +  end
   1.293 +  verification.comment = (verification.comment or "") .. " /// SMS code " .. pin .. " sent"
   1.294 +  verification:save()
   1.295 +  request.redirect{ external = encode.url { module = "registration", view = "register_enter_pin", id = verification.id } }
   1.296 +end
   1.297 +
   1.298 +
   1.299 +

Impressum / About Us