liquid_feedback_frontend

view app/main/registration/_action/register.lua @ 1376:2ff3ae341a6e

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

Impressum / About Us