liquid_feedback_frontend

view app/main/registration/_action/register.lua @ 1684:b1cec2dcc035

Added support for other option in dropdown fields
author bsw
date Mon Sep 20 12:32:40 2021 +0200 (2021-09-20)
parents 4232b30dfb11
children d09c3a0d17e6
line source
1 local function check_italian_mobile_phone_number(value)
3 if not value then
4 return false
5 end
7 value = string.gsub(value, "[^0-9]*", "")
9 if #(value) < 9 or #(value) > 10 then
10 return false
11 end
13 local mobile_phone_prefixes = {
14 { min = 320, max = 329, comment = "Wind Tre" },
15 { min = 330, max = 339, comment = "Telecom Italia (TIM)" },
16 { min = 340, max = 349, comment = "Vodafone Omnitel" },
17 { min = 350, max = 359, comment = "" },
18 { min = 360, max = 369, comment = "Telecom Italia (TIM)" },
19 { min = 370, max = 379, comment = "" },
20 { min = 380, max = 389, comment = "Wind Tre" },
21 { min = 390, max = 393, comment = "Wind Tre" },
22 { min = 394, max = 399, comment = "Wind Tre" }
23 }
25 local value_prefix = tonumber(string.match(value, "^(...)"))
27 local valid_prefix = false
29 for i, prefix in ipairs(mobile_phone_prefixes) do
30 trace.debug(value_prefix, prefix.min)
31 if value_prefix >= prefix.min and value_prefix <= prefix.max then
32 valid_prefix = true
33 end
34 end
36 if valid_prefix then
37 return true
38 else
39 return false
40 end
41 end
43 local function check_uk_mobile_phone_number(value)
45 if not value then
46 return false
47 end
49 value = string.gsub(value, "[^0-9]*", "")
51 if #(value) < 11 or #(value) > 11 then
52 return false
53 end
55 local mobile_phone_prefixes = {
56 { min = 071, max = 079, comment = "UK phone" },
57 }
59 local value_prefix = tonumber(string.match(value, "^(...)"))
61 local valid_prefix = false
63 for i, prefix in ipairs(mobile_phone_prefixes) do
64 trace.debug(value_prefix, prefix.min)
65 if value_prefix >= prefix.min and value_prefix <= prefix.max then
66 valid_prefix = true
67 end
68 end
70 if valid_prefix then
71 return true
72 else
73 return false
74 end
75 end
77 local errors = 0
79 local manual_verification
81 if config.self_registration.allow_bypass_checks and param.get("manual_verification") then
82 manual_verification = true
83 end
85 for i, checkbox in ipairs(config.use_terms_checkboxes) do
86 local accepted = param.get("use_terms_checkbox_" .. checkbox.name, atom.boolean)
87 if not accepted then
88 slot.put_into("error", checkbox.not_accepted_error)
89 errors = errors + 1
90 end
91 end
93 local email = param.get("email")
95 local members = Member:new_selector()
96 :add_where{ "notify_email = ? OR notify_email_unconfirmed = ?", email }
97 :exec()
99 if #members > 0 then
100 slot.select("error", function()
101 slot.put_into("registration_register_email_invalid", "already_used")
102 ui.tag{ content = _"This email address already been used. Please check your inbox for an invitation or contact us." }
103 end)
104 errors = errors + 1
105 end
107 local verification = Verification:new()
108 verification.requested = "now"
109 verification.request_origin = json.object{
110 ip = request.get_header("X-Forwarded-For"),
111 hostname = request.get_header("X-Forwarded-Host")
112 }
113 verification.request_data = json.object()
115 for i, field in ipairs(config.self_registration.fields) do
116 if not field.internal and field.type ~= "comment" then
117 if field.name == "date_of_birth" then
118 local day = tonumber(param.get("verification_data_" .. field.name .. "_day"))
119 local month = tonumber(param.get("verification_data_" .. field.name .. "_month"))
120 local year = tonumber(param.get("verification_data_" .. field.name .. "_year"))
121 local date = atom.date:new{ year = year, month = month, day = day }
122 if date.invalid then
123 slot.select("error", function()
124 ui.container{ content = _"Please check date of birth" }
125 slot.put_into("self_registration__invalid_" .. field.name, "invalid")
126 end)
127 errors = errors + 1
128 end
129 local today = atom.date:get_current()
130 local min_age = config.self_registration.min_age or 16
131 local date_nyears_ago = atom.date:new{ year = today.year - min_age, month = today.month, day = today.day }
132 if date_nyears_ago.invalid and today.month == 2 and today.day == 29 then
133 date_nyears_ago = atom.date:new{ year = today.year - min_age, month = 2, day = 28 }
134 end
135 if date > date_nyears_ago then
136 request.redirect{ external = encode.url { module = "registration", view = "register_rejected_age" } }
137 return
138 end
139 verification.request_data[field.name] = string.format("%04i-%02i-%02i", year, month, day)
141 else
142 local value = param.get("verification_data_" .. field.name)
143 if field.type == "dropdown" then
144 local other_option_id
145 for i_options, option in ipairs(field.options) do
146 if not option.id then
147 option.id = option.name
148 end
149 if option.other then
150 other_option_id = option.id
151 end
152 end
153 if other_option_id and other_option_id == value then
154 value = param.get("verification_data_" .. field.name .. "_other")
155 end
156 end
158 local optional = false
159 if field.optional then
160 optional = true
161 end
162 if field.optional_checkbox and param.get("verification_data_" .. field.name .. "_optout", atom.boolean) then
163 optional = true
164 end
165 if not optional and (not value or (#value < 1 and (not manual_verification or field.name ~= "mobile_phone"))) then
166 slot.put_into("self_registration__invalid_" .. field.name, "to_short")
167 slot.select("error", function()
168 ui.container{ content = _("Please enter: #{field_name}", { field_name = field.label }) }
169 end)
170 errors = errors + 1
171 end
172 if field.name == "fiscal_code" then
173 value = string.upper(value)
174 value = string.gsub(value, "[^A-Z0-9]", "")
175 elseif field.name == "mobile_phone" then
176 value = string.gsub(value, "[^0-9]", "")
177 elseif field.type == "image" then
178 if field.save_func then
179 value = field.save_func(value)
180 end
181 else
182 value = string.gsub(value, "^%s+", "")
183 value = string.gsub(value, "%s+$", "")
184 value = string.gsub(value, "%s+", " ")
185 end
186 verification.request_data[field.name] = value
187 end
188 end
190 local mobile_phone = verification.request_data.mobile_phone
192 if not manual_verification then
193 if config.self_registration.check_for_italien_mobile_phone then
194 if not check_italian_mobile_phone_number(mobile_phone) then
195 slot.select("error", function()
196 ui.container{ content = _"Please check the mobile phone number (invalid format)" }
197 end)
198 errors = errors + 1
199 end
200 end
202 if config.self_registration.check_for_uk_mobile_phone then
203 if not check_uk_mobile_phone_number(mobile_phone) then
204 slot.select("error", function()
205 ui.container{ content = _"Please check the mobile phone number (invalid format)" }
206 end)
207 errors = errors + 1
208 end
209 end
210 end
211 end
213 if config.self_registration.check_for_italian_fiscal_code then
214 local check_fiscal_code = execute.chunk{ module = "registration", chunk = "_check_fiscal_code" }
216 local fiscal_code_valid, fiscal_code_error = check_fiscal_code(
217 verification.request_data.fiscal_code,
218 {
219 first_name = verification.request_data.first_name,
220 last_name = verification.request_data.name,
221 year = tonumber(string.match(verification.request_data.date_of_birth, "^(....)-..-..$")),
222 month = tonumber(string.match(verification.request_data.date_of_birth, "^....-(..)-..$")),
223 day = tonumber(string.match(verification.request_data.date_of_birth, "^....-..-(..)$")),
224 }
225 )
227 if fiscal_code_valid then
228 verification.comment = (verification.comment or "").. " /// Fiscal code matched"
229 else
230 slot.select("error", function()
231 ui.container{ content = _"Please check the fiscal code (invalid format or does not match name, first name and/or date of birth)" }
232 end)
233 errors = errors + 1
234 --table.insert(manual_check_reasons, "fiscal code does not match (" .. fiscal_code_error .. ")")
235 end
236 end
238 if errors > 0 then
239 return false
240 end
242 local member = Member:new()
243 member.notify_email = email
244 member:save()
246 for i, checkbox in ipairs(config.use_terms_checkboxes) do
247 local accepted = param.get("use_terms_checkbox_" .. checkbox.name, atom.boolean)
248 local member_useterms = MemberUseterms:new()
249 member_useterms.member_id = member.id
250 member_useterms.contract_identifier = checkbox.name
251 member_useterms:save()
252 end
254 verification.requesting_member_id = member.id
256 local manual_check_reasons = {}
258 if manual_verification then
259 table.insert(manual_check_reasons, "User requested manual verification (during step 1)")
260 end
262 if not config.self_registration.sms_id then
263 table.insert(manual_check_reasons, "User requested manual verification (during step 1)")
264 end
266 local existing_verifications = Verification:new_selector()
267 :add_where{ "request_data->>'mobile_phone' = ?", mobile_phone }
268 :add_where("comment ilike '%SMS code%'")
269 :exec()
271 if #existing_verifications > 0 then
272 table.insert(manual_check_reasons, "mobile phone number already used before")
273 end
275 if #manual_check_reasons > 0 then
276 local reasons = table.concat(manual_check_reasons, ", ")
277 verification.comment = (verification.comment or "").. " /// Manual verification needed: " .. reasons
278 verification:save()
279 request.redirect{ external = encode.url { module = "registration", view = "register_manual_check_needed" } }
281 else
282 local pin = multirand.string(6, "0123456789")
283 verification.request_data.sms_code = pin
284 verification.request_data.sms_code_tries = 3
285 local sms_text = config.self_registration.sms_text
286 local sms_text = string.gsub(sms_text, "{PIN}", pin)
287 print("SMS Code: " .. sms_text)
288 local phone_number
289 if config.self_registration.sms_strip_leading_zero then
290 phone_number = string.match(verification.request_data.mobile_phone, "0(.+)")
291 else
292 phone_number = verification.request_data.mobile_phone
293 end
294 phone_number = config.self_registration.sms_prefix .. phone_number
295 local params = {
296 id = config.self_registration.sms_id,
297 pass = config.self_registration.sms_pass,
298 gateway = config.self_registration.sms_gateway,
299 absender = config.self_registration.sms_from,
300 text = sms_text,
301 nummer = phone_number,
302 test = config.self_registration.test and "1" or nil
303 }
304 local params_list = {}
305 for k, v in pairs(params) do
306 table.insert(params_list, encode.url_part(k) .. "=" .. encode.url_part(v))
307 end
309 local params_string = table.concat(params_list, "&")
310 local url = "http://gateway.any-sms.biz/send_sms.php?" .. params_string
311 print("curl " .. url)
312 local output, err, status = extos.pfilter(nil, "curl", url)
313 print(output)
314 verification.request_data.sms_code_sent_status = output
315 if not string.match(output, "^err:0") then
316 verification.comment = (verification.comment or "").. " /// Manual verification needed: sending SMS failed (" .. output .. ")"
317 verification:save()
318 request.redirect{ external = encode.url { module = "registration", view = "register_manual_check_needed" } }
319 return
320 end
321 verification.comment = (verification.comment or "") .. " /// SMS code " .. pin .. " sent"
322 verification:save()
323 request.redirect{ external = encode.url { module = "registration", view = "register_enter_pin", id = verification.id } }
324 end

Impressum / About Us