liquid_feedback_frontend

view app/main/registration/_action/register.lua @ 1314:74ce9970605b

Allow empty optional registration fields
author bsw
date Wed Aug 01 17:26:16 2018 +0200 (2018-08-01)
parents 32cc544d5a5b
children 186a172c8b9e
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 field.name == "date_of_birth" then
117 local day = tonumber(param.get("verification_data_" .. field.name .. "_day"))
118 local month = tonumber(param.get("verification_data_" .. field.name .. "_month"))
119 local year = tonumber(param.get("verification_data_" .. field.name .. "_year"))
120 local date = atom.date:new{ year = year, month = month, day = day }
121 if date.invalid then
122 slot.select("error", function()
123 ui.container{ content = _"Please check date of birth" }
124 slot.put_into("self_registration__invalid_" .. field.name, "invalid")
125 end)
126 errors = errors + 1
127 end
128 local today = atom.date:get_current()
129 local date_16y_ago = atom.date:new{ year = today.year - 16, month = today.month, day = today.day }
130 if date_16y_ago.invalid and today.month == 2 and today.day == 29 then
131 date_16y_ago = atom.date:new{ year = today.year - 16, month = 2, day = 28 }
132 end
133 if date > date_16y_ago then
134 request.redirect{ external = encode.url { module = "registration", view = "register_rejected_age" } }
135 return
136 end
137 verification.request_data[field.name] = string.format("%04i-%02i-%02i", year, month, day)
139 else
140 local value = param.get("verification_data_" .. field.name)
141 if not field.optional and (not value or (#value < 1 and (not manual_verification or field.name ~= "mobile_phone"))) then
142 slot.put_into("self_registration__invalid_" .. field.name, "to_short")
143 slot.select("error", function()
144 ui.container{ content = _("Please enter: #{field_name}", { field_name = field.label }) }
145 end)
146 errors = errors + 1
147 end
148 if field.name == "fiscal_code" then
149 value = string.upper(value)
150 value = string.gsub(value, "[^A-Z0-9]", "")
151 elseif field.name == "mobile_phone" then
152 value = string.gsub(value, "[^0-9]", "")
153 else
154 value = string.gsub(value, "^%s+", "")
155 value = string.gsub(value, "%s+$", "")
156 value = string.gsub(value, "%s+", " ")
157 end
158 verification.request_data[field.name] = value
159 end
160 end
162 local automatic_verification_possible = true
164 local mobile_phone = verification.request_data.mobile_phone
166 if not manual_verification then
167 if config.self_registration.check_for_italien_mobile_phone then
168 if not check_italian_mobile_phone_number(mobile_phone) then
169 slot.select("error", function()
170 ui.container{ content = _"Please check the mobile phone number (invalid format)" }
171 end)
172 errors = errors + 1
173 end
174 end
176 if config.self_registration.check_for_uk_mobile_phone then
177 if not check_uk_mobile_phone_number(mobile_phone) then
178 slot.select("error", function()
179 ui.container{ content = _"Please check the mobile phone number (invalid format)" }
180 end)
181 errors = errors + 1
182 end
183 end
184 end
186 if config.self_registration.check_for_italian_fiscal_code then
187 local check_fiscal_code = execute.chunk{ module = "registration", chunk = "_check_fiscal_code" }
189 local fiscal_code_valid, fiscal_code_error = check_fiscal_code(
190 verification.request_data.fiscal_code,
191 {
192 first_name = verification.request_data.first_name,
193 last_name = verification.request_data.name,
194 year = tonumber(string.match(verification.request_data.date_of_birth, "^(....)-..-..$")),
195 month = tonumber(string.match(verification.request_data.date_of_birth, "^....-(..)-..$")),
196 day = tonumber(string.match(verification.request_data.date_of_birth, "^....-..-(..)$")),
197 }
198 )
200 if fiscal_code_valid then
201 verification.comment = (verification.comment or "").. " /// Fiscal code matched"
202 else
203 slot.select("error", function()
204 ui.container{ content = _"Please check the fiscal code (invalid format or does not match name, first name and/or date of birth)" }
205 end)
206 errors = errors + 1
207 --table.insert(manual_check_reasons, "fiscal code does not match (" .. fiscal_code_error .. ")")
208 end
209 end
211 if errors > 0 then
212 return false
213 end
215 local member = Member:new()
216 member.notify_email = email
217 member:save()
219 for i, checkbox in ipairs(config.use_terms_checkboxes) do
220 local accepted = param.get("use_terms_checkbox_" .. checkbox.name, atom.boolean)
221 local member_useterms = MemberUseterms:new()
222 member_useterms.member_id = member.id
223 member_useterms.contract_identifier = checkbox.name
224 member_useterms:save()
225 end
227 verification.requesting_member_id = member.id
229 local manual_check_reasons = {}
231 if manual_verification then
232 table.insert(manual_check_reasons, "User requested manual verification (during step 1)")
233 end
235 local existing_verifications = Verification:new_selector()
236 :add_where{ "request_data->>'mobile_phone' = ?", mobile_phone }
237 :add_where("comment ilike '%SMS code%'")
238 :exec()
240 if #existing_verifications > 0 then
241 table.insert(manual_check_reasons, "mobile phone number already used before")
242 end
244 if #manual_check_reasons > 0 then
245 local reasons = table.concat(manual_check_reasons, ", ")
246 verification.comment = (verification.comment or "").. " /// Manual verification needed: " .. reasons
247 verification:save()
248 request.redirect{ external = encode.url { module = "registration", view = "register_manual_check_needed" } }
250 else
251 local pin = multirand.string(6, "0123456789")
252 verification.request_data.sms_code = pin
253 verification.request_data.sms_code_tries = 3
254 local sms_text = config.self_registration.sms_text
255 local sms_text = string.gsub(sms_text, "{PIN}", pin)
256 print("SMS Code: " .. sms_text)
257 local phone_number
258 if config.self_registration.sms_strip_leading_zero then
259 phone_number = string.match(verification.request_data.mobile_phone, "0(.+)")
260 else
261 phone_number = verification.request_data.mobile_phone
262 end
263 phone_number = config.self_registration.sms_prefix .. phone_number
264 local params = {
265 id = config.self_registration.sms_id,
266 pass = config.self_registration.sms_pass,
267 gateway = config.self_registration.sms_gateway,
268 absender = config.self_registration.sms_from,
269 text = sms_text,
270 nummer = phone_number,
271 test = config.self_registration.test and "1" or nil
272 }
273 local params_list = {}
274 for k, v in pairs(params) do
275 table.insert(params_list, encode.url_part(k) .. "=" .. encode.url_part(v))
276 end
278 local params_string = table.concat(params_list, "&")
279 local url = "http://gateway.any-sms.biz/send_sms.php?" .. params_string
280 print("curl " .. url)
281 local output, err, status = extos.pfilter(nil, "curl", url)
282 print(output)
283 verification.request_data.sms_code_sent_status = output
284 if not string.match(output, "^err:0") then
285 verification.comment = (verification.comment or "").. " /// Manual verification needed: sending SMS failed (" .. output .. ")"
286 verification:save()
287 request.redirect{ external = encode.url { module = "registration", view = "register_manual_check_needed" } }
288 return
289 end
290 verification.comment = (verification.comment or "") .. " /// SMS code " .. pin .. " sent"
291 verification:save()
292 request.redirect{ external = encode.url { module = "registration", view = "register_enter_pin", id = verification.id } }
293 end

Impressum / About Us