kirgudu_base 0.0.23 → 0.0.25

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,414 +1,413 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
 
3
3
  module KirguduBase
4
- module Security
5
- class AuthenticationsController < ::KirguduBase::ApplicationController
4
+ module Security
5
+ class AuthenticationsController < ::KirguduBase::ApplicationController
6
6
 
7
7
 
8
- before_action :redirect_if_loggedin
8
+ before_action :redirect_if_loggedin
9
9
 
10
- before_action :init_authentications_variables
10
+ before_action :init_authentications_variables
11
11
 
12
- before_action :on_before_login, only: :login
13
- after_action :on_after_login, only: :login
12
+ before_action :on_before_login, only: :login
13
+ after_action :on_after_login, only: :login
14
14
 
15
- DEFAULT_FROM = "kirgudu.net@gmail.com"
15
+ DEFAULT_FROM = "kirgudu.net@gmail.com"
16
16
 
17
17
 
18
- def login
19
- params_to_local_data = [:email, :password, :token_id, :redirect_to]
20
- params_to_local_data.each do |param_name|
21
- @local_data[param_name] = params[param_name] if params[param_name]
22
- end
23
-
24
- session[:redirect_to] = params[:redirect_to] if params[:redirect_to]
25
-
26
- #logger.info("AUTH CONTROLLER TEMPLATE: #{@local_data[:template]}")
27
- #logger.info("LOGIN VIEW PATH: #{@local_data[:template]}/authentications/login")
28
-
29
- render "/#{@local_data[:template]}/authentications/login", locals: {data: @local_data}, layout: "/#{@local_data[:template]}/layouts/application"
30
- end
31
-
32
- def login_process
33
-
34
-
35
- @local_data[:user] = ::KirguduBase::Security::User.where(email: params[:email]).first
36
-
37
- params_to_local_data = [:email, :password, :token_id, :redirect_to]
38
- params_to_local_data.each do |param_name|
39
- @local_data[param_name] = params[param_name] if params[param_name]
40
- end
41
-
42
- process_result = false
43
-
44
-
45
- if @local_data[:user] && @local_data[:user].password == ::ChupakabraTools::Security.get_password_hash(params[:password])
46
- self.current_user_set_info(@local_data[:user], params[:remember_me])
47
- process_result = true
48
-
49
- else
50
- @local_data[:errors] << I18n.t("kirgudu_base.authentications.login.errors.generic_user_password")
51
- end
52
- unless verify_recaptcha
53
- @local_data[:errors] << I18n.t("kirgudu_base.authentications.login.errors.captcha_error")
54
- process_result = false
55
- end
56
-
57
-
58
- if process_result
59
- redirect_to session[:return_to] || ::KirguduBase.url_for_default_redirection_after_login || "/"
60
- else
61
- render "/#{@local_data[:template]}/authentications/login", locals: {data: @local_data}, layout: "#{@local_data[:template]}/layouts/application"
62
- end
63
- end
64
-
65
-
66
- def one_time_pass_login
67
- render "/#{@local_data[:template]}/authentications/token_login", locals: {data: @local_data}, layout: "#{@local_data[:template]}/layouts/application"
68
- end
69
-
70
- def one_time_pass_login_process
71
- end
72
-
73
- def direct_login
74
- @local_data[:app_id] = params[:app_id]
75
- @local_data[:email] = params[:email]
76
- @local_data[:password] = params[:password]
77
-
78
- end
79
-
80
- def logout
81
- security_logout(session)
82
-
83
- self.current_user_clear_info
84
- redirect_to url_for(controller: ::KirguduBase::Security::AuthenticationsController.to_route_path, action: 'login')
85
- end
86
-
87
- def create
88
-
89
- # omniauth = request.env["omniauth.auth"]
90
- # # Checking if External Authentication Services Allowed
91
- # external_authentication_allowed = Option.where(name: 'external_authentication_allowed').first
92
- #
93
- # if !external_authentication_allowed.nil? && external_authentication_allowed == false
94
- # flash[:error] = "External Authentication Services are prohibited. Pleas Login with Your Local Account"
95
- # redirect_to ::KirguduBase::Security::AuthenticationsController.to_url_for(action: :login)
96
- # return
97
- # end
98
- #
99
- # authentication = ::KirguduBase::Security::Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
100
- # if authentication
101
- # flash[:notice] = "Signed in successfully."
102
- # session[:user_id] = authentication.user.id
103
- # session[:user_provider] = authentication.provider
104
- # #sign_in_and_redirect(:user, authentication.user)
105
- # #redirect_to home_path
106
- # return
107
- # elsif @current_website
108
- # authentication = @current_website.authentications.create!(
109
- # provider: omniauth['provider'],
110
- # uid: omniauth['uid']
111
- # )
112
- # session[:user_provider] = authentication.provider
113
- # flash[:notice] = "Authentication successful."
114
- # #redirect_to home_path
115
- # return
116
- # else
117
- # user_params = {
118
- # first_name: omniauth.info.first_name || "",
119
- # last_name: omniauth.info.last_name || "",
120
- # email: omniauth.info.email,
121
- # #phone: "",
122
- # password: ::ChupakabraTools::Security.generate_secret(length: 10)
123
- # }
124
- #
125
- # @local_data[:user] = ::KirguduBase::Security::User.new(user_params)
126
- #
127
- # if @local_data[:user].save
128
- # authentication = @local_data[:user].authentications.create!(
129
- # provider: omniauth['provider'],
130
- # uid: omniauth['uid']
131
- # )
132
- # session[:user_provider] = authentication.provider
133
- # flash[:notice] = "Signed in successfully."
134
- # session[:user_id] = @local_data[:user].id
135
- # #redirect_to home_path
136
- # return
137
- # else
138
- # #session[:omniauth] = omniauth.except('extra')
139
- # redirect_to url_for(controller: ::KirguduBase::Security::AuthenticationsController.to_route_path, action: :register)
140
- # return
141
- # end
142
- # end
143
- end
144
-
145
- def register
146
- @local_data[:user] = ::KirguduBase::Security::User.new
147
-
148
- params_to_local_data = [:token_id, :redirect_to]
149
- params_to_local_data.each do |param_name|
150
- @local_data[param_name] = params[param_name] if params[param_name]
151
- end
152
- session[:token_id] = params[:token_id] if params[:token_id]
153
- session[:redirect_to] = params[:redirect_to] if params[:redirect_to]
154
-
155
-
156
- render "/#{@local_data[:template]}/authentications/register", locals: {data: @local_data}, layout: "#{@local_data[:template]}/layouts/application"
157
- end
158
-
159
- def register_process
160
-
161
- params_user = params[::KirguduBase::Security::User.for_form_params]
162
- @local_data[:user] = ::KirguduBase::Security::User.new(self.strong_params_on_create)
163
-
164
-
165
- params_to_local_data = [:token_id, :redirect_to]
166
- params_to_local_data.each do |param_name|
167
- @local_data[param_name] = params[param_name] if params[param_name]
168
- end
169
-
170
-
171
- @local_data[:user].email = params_user[:email]
172
- @local_data[:user].password = params_user[:password]
173
- @local_data[:user].password_confirmation = params_user[:password_confirmation]
174
- @local_data[:user].uin ||= ::SecureRandom.uuid
175
- @local_data[:user].created_at = DateTime.now
176
- @local_data[:user].updated_at = DateTime.now
177
-
178
-
179
- user_plain_text_password = @local_data[:user].password
180
- user_plain_text_password_confirmation = @local_data[:user].password_confirmation
181
-
182
- process_result = false
183
- captcha_is_ok = verify_recaptcha
184
-
185
- unless captcha_is_ok
186
- @local_data[:errors] << I18n.t("kirgudu_base.authentications.register.errors.captcha_error")
187
- process_result = false
188
- end
189
- if captcha_is_ok
190
- if @local_data[:user].valid?
191
- # starting transaction
192
- ActiveRecord::Base.transaction do
193
- @local_data[:user].password = ::ChupakabraTools::Security.get_password_hash(@local_data[:user].password)
194
- @local_data[:user].password_confirmation = @local_data[:user].password
195
-
196
- if @local_data[:user].save
197
- @local_data[:messages] << I18n.t("kirgudu_base.authentications.register.messages.user_created")
198
- process_result = true
199
- else
200
- @local_data[:errors] << I18n.t("kirgudu_base.authentications.register.errors.failed_to_save_user")
201
- end
202
- end
203
-
204
- end
205
- end
206
-
207
-
208
- if process_result
209
- render "/#{@local_data[:template]}/authentications/register_successful", locals: {data: @local_data}
210
- else
211
- @local_data[:user].password = user_plain_text_password
212
- @local_data[:user].password_confirmation = user_plain_text_password_confirmation
213
- render "/#{@local_data[:template]}/authentications/register", locals: {data: @local_data}, layout: "#{@local_data[:template]}/layouts/application"
214
- end
215
- end
216
-
217
- def restore_password
218
-
219
- params_to_local_data = [:token_id, :redirect_to, :email]
220
- params_to_local_data.each do |param_name|
221
- @local_data[param_name] = params[param_name] if params[param_name]
222
- end
223
- render "#{@local_data[:template]}/authentications/restore_password", locals: {data: @local_data}, layout: "#{@local_data[:template]}/layouts/application"
224
- end
225
-
226
- def restore_password_send
227
- params_to_local_data = [:token_id, :redirect_to, :email]
228
- params_to_local_data.each do |param_name|
229
- @local_data[param_name] = params[param_name] if params[param_name]
230
- end
231
-
232
- process_result = false
233
-
234
- if verify_recaptcha
235
- if @local_data[:email] && !@local_data[:email].blank?
236
- if @local_data[:email]
237
- user = ::KirguduBase::Security::User.get_by_email(@local_data[:email])
238
- if user
239
- restore = ::KirguduBase::Security::RestorePasswordCode.where { (expire_at > Time.now) & (is_used == false) }.order(::KirguduBase::Security::RestorePasswordCode.for_sql_order_by(:created_at, :desc)).first
240
- unless restore
241
- restore = ::KirguduBase::Security::RestorePasswordCode.new(expire_at: 30.minute.from_now, code: 100000 + SecureRandom.random_number(899999), user_id: user.id)
242
- unless restore.save
243
- restore = nil
244
- @local_data[:errors] << I18n.t("kirgudu_base.authentications.restore_password.errors.cant_create_restore_code")
245
- end
246
- end
247
- if restore
248
- begin
249
- ::KirguduBase::AuthenticationsMailer.restore_password_code(DEFAULT_FROM, user.email, user, restore).deliver!
250
- process_result = true
251
- restore.sent_at = Time.now
252
- restore.save
253
- rescue Exception => e
254
- @local_data[:errors] << I18n.t("kirgudu_base.authentications.restore_password.errors.mail_delivery_error")
255
- end
256
-
257
- end
258
- else
259
- @local_data[:errors] << I18n.t("kirgudu_base.authentications.restore_password.errors.user_not_found")
260
- end
261
- else
262
- @local_data[:errors] << I18n.t("kirgudu_base.authentications.restore_password.errors.email_wrong_format")
263
- end
264
- else
265
- @local_data[:errors] << I18n.t("kirgudu_base.authentications.restore_password.errors.email_cannot_be_empty")
266
- end
267
- else
268
- @local_data[:errors] << I18n.t("kirgudu_base.authentications.restore_password.errors.captcha_error")
269
- end
270
-
271
-
272
- if process_result
273
- redirect_to ::KirguduBase.authentications_controller.to_url_for(:restore_password_code, email: user.email)
274
- else
275
- render "/#{@local_data[:template]}/authentications/restore_password", locals: {data: @local_data}, layout: "#{@local_data[:template]}/layouts/application"
276
- end
277
-
278
- end
279
-
280
- def restore_password_code
281
-
282
- params_to_local_data = [:token_id, :redirect_to, :email, :id, :code]
283
- params_to_local_data.each do |param_name|
284
- @local_data[param_name] = params[param_name] if params[param_name]
285
- end
286
- render "/#{@local_data[:template]}/authentications/restore_password_code", locals: {data: @local_data}, layout: "#{@local_data[:template]}/layouts/application"
287
- end
288
-
289
- def restore_password_code_process
290
-
291
- params_to_local_data = [:token_id, :redirect_to, :email, :id, :code]
292
- params_to_local_data.each do |param_name|
293
- @local_data[param_name] = params[param_name] if params[param_name]
294
- end
295
-
296
- restore_code = nil
297
- restore_code = ::KirguduBase::Security::RestorePasswordCode.find(@local_data[:id]) if @local_data[:id]
298
-
299
- process_result = false
300
- if restore_code && restore_code.code == @local_data[:code]
301
- unless restore_code.is_used?
302
- password = ::ChupakabraTools::Security.generate_secret(length: 8)
303
- restore_code.user.password = ::ChupakabraTools::Security.get_password_hash(password)
304
- if restore_code.user.save
305
- begin
306
- ::KirguduBase::AuthenticationsMailer.restore_password_success(DEFAULT_FROM, restore_code.user.email, restore_code.user, password).deliver!
307
- @local_data[:email] = restore_code.user.email
308
- process_result = true
309
- restore_code.is_used = true
310
- restore_code.save
311
- rescue Exception => e
312
- @local_data[:errors] << I18n.t("kirgudu_base.authentications.restore_password_code.errors.failed_to_send_password")
313
- end
314
- else
315
- @local_data[:errors] << I18n.t("kirgudu_base.authentications.restore_password_code.errors.failed_to_save_password")
316
- end
317
- else
318
- @local_data[:errors] << I18n.t("kirgudu_base.authentications.restore_password_code.errors.used_code")
319
- end
320
- else
321
- @local_data[:errors] << I18n.t("kirgudu_base.authentications.restore_password_code.errors.wrong_code")
322
- process_result = false
323
- end
324
-
325
-
326
- if process_result
327
- redirect_to ::KirguduBase.authentications_controller.to_url_for(:restore_password_success, email: @local_data[:email])
328
- else
329
- render "/#{@local_data[:template]}/authentications/restore_password_code", locals: {data: @local_data}
330
- end
331
- end
332
-
333
- def restore_password_success
334
-
335
- params_to_local_data = [:token_id, :redirect_to, :email, :id, :code]
336
- params_to_local_data.each do |param_name|
337
- @local_data[param_name] = params[param_name] if params[param_name]
338
- end
339
- render "/#{@local_data[:template]}/authentications/restore_password_success", locals: {data: @local_data}
340
-
341
- end
342
-
343
-
344
- def confirm_email
345
-
346
- end
347
-
348
- def confirm_email_process
349
-
350
- end
351
-
352
- def destroy
353
-
354
- end
355
-
356
- def destroy_process
357
-
358
- @local_data[:user] = ::KirguduBase::Security::User.where(id: session[self.current_user_symbol])
359
-
360
- if @local_data[:user]
361
- if @local_data[:user].destroy
362
- flash[:notice] = "Successfully destroyed authentication."
363
- redirect_to authentications_url
364
- else
365
- # failed to delete user
366
- end
367
- else
18
+ def login
19
+ params_to_local_data = [:email, :password, :token_id, :redirect_to]
20
+ params_to_local_data.each do |param_name|
21
+ @local_data[param_name] = params[param_name] if params[param_name]
22
+ end
23
+
24
+ session[:redirect_to] = params[:redirect_to] if params[:redirect_to]
25
+
26
+ #logger.info("AUTH CONTROLLER TEMPLATE: #{@local_data[:template]}")
27
+ #logger.info("LOGIN VIEW PATH: #{@local_data[:template]}/authentications/login")
28
+
29
+ render "/#{@local_data[:template]}/authentications/login", locals: {data: @local_data}, layout: "/#{@local_data[:template]}/layouts/application"
30
+ end
31
+
32
+ def login_process
33
+
34
+ @local_data[:user] = ::KirguduBase::Security::User.where(email: params[:email]).first
35
+
36
+ params_to_local_data = [:email, :password, :token_id, :redirect_to]
37
+ params_to_local_data.each do |param_name|
38
+ @local_data[param_name] = params[param_name] if params[param_name]
39
+ end
40
+
41
+ process_result = false
42
+
43
+
44
+ if @local_data[:user] && @local_data[:user].password == ::ChupakabraTools::Security.get_password_hash(params[:password])
45
+ self.current_user_set_info(@local_data[:user], params[:remember_me])
46
+ process_result = true
47
+
48
+ else
49
+ @local_data[:errors] << I18n.t("kirgudu_base.authentications.login.errors.generic_user_password")
50
+ end
51
+ unless verify_recaptcha
52
+ @local_data[:errors] << I18n.t("kirgudu_base.authentications.login.errors.captcha_error")
53
+ process_result = false
54
+ end
55
+
56
+
57
+ if process_result
58
+ redirect_to session[:return_to] || ::KirguduBase.url_for_default_redirection_after_login || "/"
59
+ else
60
+ render "/#{@local_data[:template]}/authentications/login", locals: {data: @local_data}, layout: "#{@local_data[:template]}/layouts/application"
61
+ end
62
+ end
63
+
64
+
65
+ def one_time_pass_login
66
+ render "/#{@local_data[:template]}/authentications/token_login", locals: {data: @local_data}, layout: "#{@local_data[:template]}/layouts/application"
67
+ end
68
+
69
+ def one_time_pass_login_process
70
+ end
71
+
72
+ def direct_login
73
+ @local_data[:app_id] = params[:app_id]
74
+ @local_data[:email] = params[:email]
75
+ @local_data[:password] = params[:password]
76
+
77
+ end
78
+
79
+ def logout
80
+ security_logout(session)
81
+
82
+ self.current_user_clear_info
83
+ redirect_to self.get_url_for_login
84
+ end
85
+
86
+ def create
87
+
88
+ # omniauth = request.env["omniauth.auth"]
89
+ # # Checking if External Authentication Services Allowed
90
+ # external_authentication_allowed = Option.where(name: 'external_authentication_allowed').first
91
+ #
92
+ # if !external_authentication_allowed.nil? && external_authentication_allowed == false
93
+ # flash[:error] = "External Authentication Services are prohibited. Pleas Login with Your Local Account"
94
+ # redirect_to ::KirguduBase::Security::AuthenticationsController.to_url_for(action: :login)
95
+ # return
96
+ # end
97
+ #
98
+ # authentication = ::KirguduBase::Security::Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
99
+ # if authentication
100
+ # flash[:notice] = "Signed in successfully."
101
+ # session[:user_id] = authentication.user.id
102
+ # session[:user_provider] = authentication.provider
103
+ # #sign_in_and_redirect(:user, authentication.user)
104
+ # #redirect_to home_path
105
+ # return
106
+ # elsif @current_website
107
+ # authentication = @current_website.authentications.create!(
108
+ # provider: omniauth['provider'],
109
+ # uid: omniauth['uid']
110
+ # )
111
+ # session[:user_provider] = authentication.provider
112
+ # flash[:notice] = "Authentication successful."
113
+ # #redirect_to home_path
114
+ # return
115
+ # else
116
+ # user_params = {
117
+ # first_name: omniauth.info.first_name || "",
118
+ # last_name: omniauth.info.last_name || "",
119
+ # email: omniauth.info.email,
120
+ # #phone: "",
121
+ # password: ::ChupakabraTools::Security.generate_secret(length: 10)
122
+ # }
123
+ #
124
+ # @local_data[:user] = ::KirguduBase::Security::User.new(user_params)
125
+ #
126
+ # if @local_data[:user].save
127
+ # authentication = @local_data[:user].authentications.create!(
128
+ # provider: omniauth['provider'],
129
+ # uid: omniauth['uid']
130
+ # )
131
+ # session[:user_provider] = authentication.provider
132
+ # flash[:notice] = "Signed in successfully."
133
+ # session[:user_id] = @local_data[:user].id
134
+ # #redirect_to home_path
135
+ # return
136
+ # else
137
+ # #session[:omniauth] = omniauth.except('extra')
138
+ # redirect_to url_for(controller: ::KirguduBase::Security::AuthenticationsController.to_route_path, action: :register)
139
+ # return
140
+ # end
141
+ # end
142
+ end
143
+
144
+ def register
145
+ @local_data[:user] = ::KirguduBase::Security::User.new
146
+
147
+ params_to_local_data = [:token_id, :redirect_to]
148
+ params_to_local_data.each do |param_name|
149
+ @local_data[param_name] = params[param_name] if params[param_name]
150
+ end
151
+ session[:token_id] = params[:token_id] if params[:token_id]
152
+ session[:redirect_to] = params[:redirect_to] if params[:redirect_to]
153
+
154
+
155
+ render "/#{@local_data[:template]}/authentications/register", locals: {data: @local_data}, layout: "#{@local_data[:template]}/layouts/application"
156
+ end
157
+
158
+ def register_process
159
+
160
+ params_user = params[::KirguduBase::Security::User.for_form_params]
161
+ @local_data[:user] = ::KirguduBase::Security::User.new(self.strong_params_on_create)
162
+
163
+
164
+ params_to_local_data = [:token_id, :redirect_to]
165
+ params_to_local_data.each do |param_name|
166
+ @local_data[param_name] = params[param_name] if params[param_name]
167
+ end
168
+
169
+
170
+ @local_data[:user].email = params_user[:email]
171
+ @local_data[:user].password = params_user[:password]
172
+ @local_data[:user].password_confirmation = params_user[:password_confirmation]
173
+ @local_data[:user].uin ||= ::SecureRandom.uuid
174
+ @local_data[:user].created_at = DateTime.now
175
+ @local_data[:user].updated_at = DateTime.now
176
+
177
+
178
+ user_plain_text_password = @local_data[:user].password
179
+ user_plain_text_password_confirmation = @local_data[:user].password_confirmation
180
+
181
+ process_result = false
182
+ captcha_is_ok = verify_recaptcha
183
+
184
+ unless captcha_is_ok
185
+ @local_data[:errors] << I18n.t("kirgudu_base.authentications.register.errors.captcha_error")
186
+ process_result = false
187
+ end
188
+ if captcha_is_ok
189
+ if @local_data[:user].valid?
190
+ # starting transaction
191
+ ActiveRecord::Base.transaction do
192
+ @local_data[:user].password = ::ChupakabraTools::Security.get_password_hash(@local_data[:user].password)
193
+ @local_data[:user].password_confirmation = @local_data[:user].password
194
+
195
+ if @local_data[:user].save
196
+ @local_data[:messages] << I18n.t("kirgudu_base.authentications.register.messages.user_created")
197
+ process_result = true
198
+ else
199
+ @local_data[:errors] << I18n.t("kirgudu_base.authentications.register.errors.failed_to_save_user")
200
+ end
201
+ end
202
+
203
+ end
204
+ end
205
+
206
+
207
+ if process_result
208
+ render "/#{@local_data[:template]}/authentications/register_successful", locals: {data: @local_data}
209
+ else
210
+ @local_data[:user].password = user_plain_text_password
211
+ @local_data[:user].password_confirmation = user_plain_text_password_confirmation
212
+ render "/#{@local_data[:template]}/authentications/register", locals: {data: @local_data}, layout: "#{@local_data[:template]}/layouts/application"
213
+ end
214
+ end
215
+
216
+ def restore_password
217
+
218
+ params_to_local_data = [:token_id, :redirect_to, :email]
219
+ params_to_local_data.each do |param_name|
220
+ @local_data[param_name] = params[param_name] if params[param_name]
221
+ end
222
+ render "#{@local_data[:template]}/authentications/restore_password", locals: {data: @local_data}, layout: "#{@local_data[:template]}/layouts/application"
223
+ end
224
+
225
+ def restore_password_send
226
+ params_to_local_data = [:token_id, :redirect_to, :email]
227
+ params_to_local_data.each do |param_name|
228
+ @local_data[param_name] = params[param_name] if params[param_name]
229
+ end
230
+
231
+ process_result = false
232
+
233
+ if verify_recaptcha
234
+ if @local_data[:email] && !@local_data[:email].blank?
235
+ if @local_data[:email]
236
+ user = ::KirguduBase::Security::User.get_by_email(@local_data[:email])
237
+ if user
238
+ restore = ::KirguduBase::Security::RestorePasswordCode.where { (expire_at > Time.now) & (is_used == false) }.order(::KirguduBase::Security::RestorePasswordCode.for_sql_order_by(:created_at, :desc)).first
239
+ unless restore
240
+ restore = ::KirguduBase::Security::RestorePasswordCode.new(expire_at: 30.minute.from_now, code: 100000 + SecureRandom.random_number(899999), user_id: user.id)
241
+ unless restore.save
242
+ restore = nil
243
+ @local_data[:errors] << I18n.t("kirgudu_base.authentications.restore_password.errors.cant_create_restore_code")
244
+ end
245
+ end
246
+ if restore
247
+ begin
248
+ ::KirguduBase::AuthenticationsMailer.restore_password_code(DEFAULT_FROM, user.email, user, restore).deliver!
249
+ process_result = true
250
+ restore.sent_at = Time.now
251
+ restore.save
252
+ rescue Exception => e
253
+ @local_data[:errors] << I18n.t("kirgudu_base.authentications.restore_password.errors.mail_delivery_error")
254
+ end
255
+
256
+ end
257
+ else
258
+ @local_data[:errors] << I18n.t("kirgudu_base.authentications.restore_password.errors.user_not_found")
259
+ end
260
+ else
261
+ @local_data[:errors] << I18n.t("kirgudu_base.authentications.restore_password.errors.email_wrong_format")
262
+ end
263
+ else
264
+ @local_data[:errors] << I18n.t("kirgudu_base.authentications.restore_password.errors.email_cannot_be_empty")
265
+ end
266
+ else
267
+ @local_data[:errors] << I18n.t("kirgudu_base.authentications.restore_password.errors.captcha_error")
268
+ end
269
+
270
+
271
+ if process_result
272
+ redirect_to self.get_url_for_restore_password_code(email: user.email)
273
+ else
274
+ render "/#{@local_data[:template]}/authentications/restore_password", locals: {data: @local_data}, layout: "#{@local_data[:template]}/layouts/application"
275
+ end
276
+
277
+ end
278
+
279
+ def restore_password_code
280
+
281
+ params_to_local_data = [:token_id, :redirect_to, :email, :id, :code]
282
+ params_to_local_data.each do |param_name|
283
+ @local_data[param_name] = params[param_name] if params[param_name]
284
+ end
285
+ render "/#{@local_data[:template]}/authentications/restore_password_code", locals: {data: @local_data}, layout: "#{@local_data[:template]}/layouts/application"
286
+ end
287
+
288
+ def restore_password_code_process
289
+
290
+ params_to_local_data = [:token_id, :redirect_to, :email, :id, :code]
291
+ params_to_local_data.each do |param_name|
292
+ @local_data[param_name] = params[param_name] if params[param_name]
293
+ end
294
+
295
+ restore_code = nil
296
+ restore_code = ::KirguduBase::Security::RestorePasswordCode.find(@local_data[:id]) if @local_data[:id]
297
+
298
+ process_result = false
299
+ if restore_code && restore_code.code == @local_data[:code]
300
+ unless restore_code.is_used?
301
+ password = ::ChupakabraTools::Security.generate_secret(length: 8)
302
+ restore_code.user.password = ::ChupakabraTools::Security.get_password_hash(password)
303
+ if restore_code.user.save
304
+ begin
305
+ ::KirguduBase::AuthenticationsMailer.restore_password_success(DEFAULT_FROM, restore_code.user.email, restore_code.user, password).deliver!
306
+ @local_data[:email] = restore_code.user.email
307
+ process_result = true
308
+ restore_code.is_used = true
309
+ restore_code.save
310
+ rescue Exception => e
311
+ @local_data[:errors] << I18n.t("kirgudu_base.authentications.restore_password_code.errors.failed_to_send_password")
312
+ end
313
+ else
314
+ @local_data[:errors] << I18n.t("kirgudu_base.authentications.restore_password_code.errors.failed_to_save_password")
315
+ end
316
+ else
317
+ @local_data[:errors] << I18n.t("kirgudu_base.authentications.restore_password_code.errors.used_code")
318
+ end
319
+ else
320
+ @local_data[:errors] << I18n.t("kirgudu_base.authentications.restore_password_code.errors.wrong_code")
321
+ process_result = false
322
+ end
323
+
324
+
325
+ if process_result
326
+ redirect_to ::KirguduBase.authentications_controller.to_url_for(:restore_password_success, email: @local_data[:email])
327
+ else
328
+ render "/#{@local_data[:template]}/authentications/restore_password_code", locals: {data: @local_data}
329
+ end
330
+ end
331
+
332
+ def restore_password_success
333
+
334
+ params_to_local_data = [:token_id, :redirect_to, :email, :id, :code]
335
+ params_to_local_data.each do |param_name|
336
+ @local_data[param_name] = params[param_name] if params[param_name]
337
+ end
338
+ render "/#{@local_data[:template]}/authentications/restore_password_success", locals: {data: @local_data}
339
+
340
+ end
341
+
342
+
343
+ def confirm_email
344
+
345
+ end
346
+
347
+ def confirm_email_process
348
+
349
+ end
350
+
351
+ def destroy
352
+
353
+ end
354
+
355
+ def destroy_process
356
+
357
+ @local_data[:user] = ::KirguduBase::Security::User.where(id: session[self.current_user_symbol])
358
+
359
+ if @local_data[:user]
360
+ if @local_data[:user].destroy
361
+ flash[:notice] = "Successfully destroyed authentication."
362
+ redirect_to authentications_url
363
+ else
364
+ # failed to delete user
365
+ end
366
+ else
368
367
 
369
- end
370
- end
368
+ end
369
+ end
371
370
 
372
371
 
373
- protected
374
- def on_before_login
372
+ protected
373
+ def on_before_login
375
374
 
376
- end
375
+ end
377
376
 
378
- def on_after_login
377
+ def on_after_login
379
378
 
380
- end
379
+ end
381
380
 
382
- def strong_params_on_create
383
- params.require(::KirguduBase::Security::User.for_form_params).permit(:email, :first_name, :last_name, :password, :password_confirmation)
384
- end
381
+ def strong_params_on_create
382
+ params.require(::KirguduBase::Security::User.for_form_params).permit(:email, :first_name, :last_name, :password, :password_confirmation)
383
+ end
385
384
 
386
- private
387
- def redirect_if_loggedin
388
- if self.current_user
389
- if session[:token_id]
385
+ private
386
+ def redirect_if_loggedin
387
+ if self.current_user
388
+ if session[:token_id]
390
389
 
391
- else
392
- # redirect_to params[:return_to] || session[:return_to] || self.url_for_default_redirection_after_login || ::KirguduBase.url_for_default_redirection_after_login || "/"
393
- raise "redirect if logged in error"
394
- redirect_to params[:return_to] || session[:return_to] || ::KirguduBase.url_for_default_redirection_after_login
395
- end
396
- end
397
- end
390
+ else
391
+ # redirect_to params[:return_to] || session[:return_to] || self.url_for_default_redirection_after_login || ::KirguduBase.url_for_default_redirection_after_login || "/"
392
+ raise "redirect if logged in error"
393
+ redirect_to params[:return_to] || session[:return_to] || ::KirguduBase.url_for_default_redirection_after_login
394
+ end
395
+ end
396
+ end
398
397
 
399
398
 
400
- def init_authentications_variables
401
- @local_data = {
402
- template: self.class.get_method_latest_result(:get_kb_template),
403
- messages: [],
404
- errors: []
405
- }
406
- end
399
+ def init_authentications_variables
400
+ @local_data = {
401
+ template: self.class.get_method_latest_result(:get_kb_template),
402
+ messages: [],
403
+ errors: []
404
+ }
405
+ end
407
406
 
408
407
 
409
- def url_for_default_redirection_after_login
410
- nil
411
- end
412
- end
413
- end
408
+ def url_for_default_redirection_after_login
409
+ nil
410
+ end
411
+ end
412
+ end
414
413
  end