liquid_feedback_frontend

view app/main/registration/_action/register.lua @ 1359:7532831a7618

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

Impressum / About Us