liquid_feedback_frontend

view app/main/registration/_action/register.lua @ 1685:d09c3a0d17e6

Added multiselect field
author bsw
date Mon Sep 20 13:03:37 2021 +0200 (2021-09-20)
parents b1cec2dcc035
children c4821ac9e71b
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 elseif field.type == "multiselect" then
142 local values = {}
143 for i_options, option in ipairs(field.options) do
144 if not option.id then
145 option.id = option.name
146 end
147 local value = param.get("verification_data_" .. field.name .. "__" .. option.id)
148 if value == "1" then
149 if option.other then
150 table.insert(values, param.get("verification_data_" .. field.name .. "_other"))
151 else
152 table.insert(values, option.name)
153 end
154 end
155 end
156 verification.request_data[field.name] = table.concat(values, ", ")
157 else
158 local value = param.get("verification_data_" .. field.name)
159 if field.type == "dropdown" then
160 local other_option_id
161 for i_options, option in ipairs(field.options) do
162 if not option.id then
163 option.id = option.name
164 end
165 if option.other then
166 other_option_id = option.id
167 end
168 end
169 if other_option_id and other_option_id == value then
170 value = param.get("verification_data_" .. field.name .. "_other")
171 end
172 end
174 local optional = false
175 if field.optional then
176 optional = true
177 end
178 if field.optional_checkbox and param.get("verification_data_" .. field.name .. "_optout", atom.boolean) then
179 optional = true
180 end
181 if not optional and (not value or (#value < 1 and (not manual_verification or field.name ~= "mobile_phone"))) then
182 slot.put_into("self_registration__invalid_" .. field.name, "to_short")
183 slot.select("error", function()
184 ui.container{ content = _("Please enter: #{field_name}", { field_name = field.label }) }
185 end)
186 errors = errors + 1
187 end
188 if field.name == "fiscal_code" then
189 value = string.upper(value)
190 value = string.gsub(value, "[^A-Z0-9]", "")
191 elseif field.name == "mobile_phone" then
192 value = string.gsub(value, "[^0-9]", "")
193 elseif field.type == "image" then
194 if field.save_func then
195 value = field.save_func(value)
196 end
197 else
198 value = string.gsub(value, "^%s+", "")
199 value = string.gsub(value, "%s+$", "")
200 value = string.gsub(value, "%s+", " ")
201 end
202 verification.request_data[field.name] = value
203 end
204 end
206 local mobile_phone = verification.request_data.mobile_phone
208 if not manual_verification then
209 if config.self_registration.check_for_italien_mobile_phone then
210 if not check_italian_mobile_phone_number(mobile_phone) then
211 slot.select("error", function()
212 ui.container{ content = _"Please check the mobile phone number (invalid format)" }
213 end)
214 errors = errors + 1
215 end
216 end
218 if config.self_registration.check_for_uk_mobile_phone then
219 if not check_uk_mobile_phone_number(mobile_phone) then
220 slot.select("error", function()
221 ui.container{ content = _"Please check the mobile phone number (invalid format)" }
222 end)
223 errors = errors + 1
224 end
225 end
226 end
227 end
229 if config.self_registration.check_for_italian_fiscal_code then
230 local check_fiscal_code = execute.chunk{ module = "registration", chunk = "_check_fiscal_code" }
232 local fiscal_code_valid, fiscal_code_error = check_fiscal_code(
233 verification.request_data.fiscal_code,
234 {
235 first_name = verification.request_data.first_name,
236 last_name = verification.request_data.name,
237 year = tonumber(string.match(verification.request_data.date_of_birth, "^(....)-..-..$")),
238 month = tonumber(string.match(verification.request_data.date_of_birth, "^....-(..)-..$")),
239 day = tonumber(string.match(verification.request_data.date_of_birth, "^....-..-(..)$")),
240 }
241 )
243 if fiscal_code_valid then
244 verification.comment = (verification.comment or "").. " /// Fiscal code matched"
245 else
246 slot.select("error", function()
247 ui.container{ content = _"Please check the fiscal code (invalid format or does not match name, first name and/or date of birth)" }
248 end)
249 errors = errors + 1
250 --table.insert(manual_check_reasons, "fiscal code does not match (" .. fiscal_code_error .. ")")
251 end
252 end
254 if errors > 0 then
255 return false
256 end
258 local member = Member:new()
259 member.notify_email = email
260 member:save()
262 for i, checkbox in ipairs(config.use_terms_checkboxes) do
263 local accepted = param.get("use_terms_checkbox_" .. checkbox.name, atom.boolean)
264 local member_useterms = MemberUseterms:new()
265 member_useterms.member_id = member.id
266 member_useterms.contract_identifier = checkbox.name
267 member_useterms:save()
268 end
270 verification.requesting_member_id = member.id
272 local manual_check_reasons = {}
274 if manual_verification then
275 table.insert(manual_check_reasons, "User requested manual verification (during step 1)")
276 end
278 if not config.self_registration.sms_id then
279 table.insert(manual_check_reasons, "User requested manual verification (during step 1)")
280 end
282 local existing_verifications = Verification:new_selector()
283 :add_where{ "request_data->>'mobile_phone' = ?", mobile_phone }
284 :add_where("comment ilike '%SMS code%'")
285 :exec()
287 if #existing_verifications > 0 then
288 table.insert(manual_check_reasons, "mobile phone number already used before")
289 end
291 if #manual_check_reasons > 0 then
292 local reasons = table.concat(manual_check_reasons, ", ")
293 verification.comment = (verification.comment or "").. " /// Manual verification needed: " .. reasons
294 verification:save()
295 request.redirect{ external = encode.url { module = "registration", view = "register_manual_check_needed" } }
297 else
298 local pin = multirand.string(6, "0123456789")
299 verification.request_data.sms_code = pin
300 verification.request_data.sms_code_tries = 3
301 local sms_text = config.self_registration.sms_text
302 local sms_text = string.gsub(sms_text, "{PIN}", pin)
303 print("SMS Code: " .. sms_text)
304 local phone_number
305 if config.self_registration.sms_strip_leading_zero then
306 phone_number = string.match(verification.request_data.mobile_phone, "0(.+)")
307 else
308 phone_number = verification.request_data.mobile_phone
309 end
310 phone_number = config.self_registration.sms_prefix .. phone_number
311 local params = {
312 id = config.self_registration.sms_id,
313 pass = config.self_registration.sms_pass,
314 gateway = config.self_registration.sms_gateway,
315 absender = config.self_registration.sms_from,
316 text = sms_text,
317 nummer = phone_number,
318 test = config.self_registration.test and "1" or nil
319 }
320 local params_list = {}
321 for k, v in pairs(params) do
322 table.insert(params_list, encode.url_part(k) .. "=" .. encode.url_part(v))
323 end
325 local params_string = table.concat(params_list, "&")
326 local url = "http://gateway.any-sms.biz/send_sms.php?" .. params_string
327 print("curl " .. url)
328 local output, err, status = extos.pfilter(nil, "curl", url)
329 print(output)
330 verification.request_data.sms_code_sent_status = output
331 if not string.match(output, "^err:0") then
332 verification.comment = (verification.comment or "").. " /// Manual verification needed: sending SMS failed (" .. output .. ")"
333 verification:save()
334 request.redirect{ external = encode.url { module = "registration", view = "register_manual_check_needed" } }
335 return
336 end
337 verification.comment = (verification.comment or "") .. " /// SMS code " .. pin .. " sent"
338 verification:save()
339 request.redirect{ external = encode.url { module = "registration", view = "register_enter_pin", id = verification.id } }
340 end

Impressum / About Us