/
auth_controller.rb
526 lines (413 loc) · 19.1 KB
/
auth_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
# Copyright (C) 2009 Pascal Rettig.
class Editor::AuthController < ParagraphController #:nodoc:all
permit 'editor_editor'
user_actions [:add_feature, :add_login_feature]
# Editor for authorization paragraphs
editor_header "Member Paragraphs", :paragraph_member
editor_for :login, :name => 'User Login', :features => ['login']
editor_for :enter_vip, :name => 'Enter VIP #', :features => ['enter_vip'], :triggers => [['Failed VIP Login','failure'],['Successful VIP Login','success' ],['Repeat Successful VIP Login','repeat']]
editor_for :user_register, :name => 'User Registration', :feature => 'user_register', :triggers => [ ['Successful Registration','action'] ]
editor_for :user_activation, :name => 'User Activation', :feature => 'user_activation', :triggers => [ ['Successful Activation','action'] ]
editor_for :user_edit_account, :name => 'User Edit Account', :feature => 'user_edit_account', :triggers => [ ['Edit Account','action' ]]
editor_for :edit_account, :name => 'Edit Account', :triggers => [ ['Edit Profile','action' ]]
editor_for :missing_password, :name => 'Missing Password', :triggers => [ ['Sent Email','action']], :features => ['missing_password']
editor_for :email_list, :name => 'Email List Signup', :triggers => [ ['Signed Up','action']], :features => ['email_list']
editor_for :splash, :name => 'Splash Page'
editor_for :view_account, :name => 'View Account', :no_options => true
class UserRegisterOptions < HashModel
# For feature stuff
include HandlerActions
attributes :registration_type => 'account',
:required_fields => [ ], :optional_fields => [ 'first_name','last_name'],
:success_page_id => nil, :already_registered_page_id => nil,
:user_class_id => nil, :modify_profile => 'modify', :registration_template_id => nil,
:include_subscriptions => [], :country => 'United States', :add_tags => '',
:work_address_required_fields => [],
:address_required_fields => [],
:content_publication_id => nil, :source => nil, :lockout_redirect => false,
:require_activation => false, :activation_page_id => nil,
:features => []
boolean_options :lockout_redirect, :require_activation
page_options :success_page_id, :activation_page_id
validates_presence_of :success_page_id, :user_class_id
def validate
if !self.features.is_a?(Array)
self.features = self.features.to_a.sort { |a,b| a[0] <=> b[0] }.map { |elm| obj = Handlers::ParagraphFeature.new(elm[1]); obj.to_hash }
self.register_features.each do |feature|
if !feature.options.valid?
self.errors.add_to_base('Feature Error')
end
end
end
end
def available_field_list
{ :email => [ 'Email'.t,:text_field, :email ],
:password => [ 'Password'.t, :password_field, :password ],
:password_confirmation => [ 'Confirm Password'.t, :password_field, :password_confirmation ],
:first_name => ['First Name'.t,:text_field,:first_name],
:middle_name => ['Middle Name'.t,:text_field,:middle_name],
:last_name => ['Last Name'.t,:text_field,:last_name],
:gender => ['Gender'.t, :radio_buttons, :gender, { :options => [ ['Male'.t,'m'],['Female'.t,'f' ] ] } ],
:introduction => ['Introduction'.t, :radio_buttons, :introduction, { :options => [ ['Mr.'.t,'Mr.'],['Mrs.'.t,'Mrs.' ], ['Ms.'.t, 'Ms.'] ] } ],
:username => [ 'Username'.t,:text_field, :username ],
:salutation => [ 'Salutation'.t,:text_field, :salutation ]
}
end
def available_optional_field_options
opts = available_field_list
opts.delete(:email)
opts.delete(:password)
opts.delete(:password_confirmation)
opts.map { |elm| [ elm[1][0],elm[0].to_s ] }.sort
end
def available_field_options
available_field_list.to_a.map { |elm| [ elm[1][0],elm[0].to_s ] }.sort
end
def all_field_list
available_field_list.to_a
end
def any_field_list
flds = available_field_list
fields = (self.required_fields + self.optional_fields).uniq
['password_confirmation','password','email'].each do |fld|
fields.unshift(fld) if !fields.include?(fld)
end
fields.map { |elm| flds[elm.to_sym] ? [ elm.to_sym, flds[elm.to_sym] ] : nil }.compact
end
def always_required_fields
flds = [ 'email']
flds += [ 'password','password_confirmation'] if self.registration_type == 'account'
flds
end
def required_field_list
flds = available_field_list
fields = (self.required_fields).uniq
['password_confirmation','password','email'].each do |fld|
fields.unshift(fld) if !fields.include?(fld)
end
fields.map { |elm| flds[elm.to_sym] ? [ elm.to_sym, flds[elm.to_sym] ] : nil }.compact
end
def optional_field_list
flds = available_field_list
fields = (self.optional_fields).uniq
fields.map { |elm| flds[elm.to_sym] ? [ elm.to_sym, flds[elm.to_sym] ] : nil }.compact
end
def available_address_field_list
{
:company => [ 'Company'.t, :text_field,:company],
:phone => [ 'Phone'.t, :text_field,:phone],
:fax => [ 'Fax'.t, :text_field,:fax],
:address => [ 'Address'.t, :text_field,:address],
:address_2 => [ 'Address (Line 2)'.t, :text_field,:address_2],
:city => [ 'City'.t, :text_field,:city],
:state => [ 'State'.t, :select,:state,{ :options => ContentModel.state_select_options } ],
:zip => [ 'Zip Code'.t, :text_field,:zip],
:country => [ 'Country'.t, :country_select, :country]
}
end
def available_address_field_options(business = false)
flds = available_address_field_list
if(!business)
flds.delete(:company)
end
flds.to_a.map { |elm| [ elm[1][0],elm[0].to_s ] }.sort
end
def address_field_list
hsh = available_address_field_list
hsh.delete(:company)
hsh.to_a
end
def business_address_field_list
available_address_field_list.to_a
end
def content_model
if publication
@content_mode ||= @pub.conten_model
else
nil
end
end
def publication
@pub ||= ContentPublication.find_by_id(self.content_publication_id)
end
def available_features
[['--Select a feature to add--','']] + get_handler_options(:editor,:auth_user_register_feature)
end
def register_features
@register_features ||= self.features.map do |feature|
Handlers::ParagraphFeature.new(feature.merge({ :feature_type => 'editor_auth_user_register_feature'}))
end
end
end
class UserActivationOptions < HashModel
attributes :require_acceptance => false, :redirect_page_id => nil,
:already_activated_redirect_page_id => nil, :login_after_activation => false
page_options :redirect_page_id, :already_activated_redirect_page_id
boolean_options :require_acceptance, :login_after_activation
end
class UserEditAccountOptions < HashModel
# For feature stuff
include HandlerActions
attributes :required_fields => [ ],
:user_class_id => nil, :modify_profile => 'keep', :mail_template_id => nil,
:optional_fields => [ 'first_name','last_name'],
:success_page_id => nil,
:include_subscriptions => [], :country => 'United States', :add_tags => '',
:work_address_required_fields => [],
:address_required_fields => [],
:content_publication_id => nil, :content_publication_user_field => nil,
:access_token_id => nil
page_options :success_page_id
validates_presence_of :user_class_id
def validate
if self.content_publication_id
if self.content_publication_user_field
errors.add(:content_publication_user_field) unless self.publication_field_options.rassoc self.content_publication_user_field
else
errors.add(:content_publication_user_field) unless self.content_publication_user_field
end
end
end
def available_field_list
{ :email => [ 'Email'.t,:text_field, :email ],
:password => [ 'Reset Password'.t, :password_field, :password ],
:password_confirmation => [ 'Confirm Password'.t, :password_field, :password_confirmation ],
:first_name => ['First Name'.t,:text_field,:first_name],
:middle_name => ['Middle Name'.t,:text_field,:middle_name],
:last_name => ['Last Name'.t,:text_field,:last_name],
:gender => ['Gender'.t, :radio_buttons, :gender, { :options => [ ['Male'.t,'m'],['Female'.t,'f' ] ] } ],
:introduction => ['Introduction'.t, :radio_buttons, :introduction, { :options => [ ['Mr.'.t,'Mr.'],['Mrs.'.t,'Mrs.' ], ['Ms.'.t, 'Ms.'] ] } ],
:username => [ 'Username'.t,:text_field, :username ],
:salutation => [ 'Salutation'.t,:text_field, :salutation ]
}
end
def available_optional_field_options
opts = available_field_list
opts.delete(:email)
opts.delete(:password)
opts.delete(:password_confirmation)
opts.map { |elm| [ elm[1][0],elm[0].to_s ] }.sort
end
def available_field_options
available_field_list.to_a.map { |elm| [ elm[1][0],elm[0].to_s ] }.sort
end
def all_field_list
available_field_list.to_a
end
def any_field_list
flds = available_field_list
fields = (self.required_fields + self.optional_fields).uniq
['password_confirmation','password','email'].each do |fld|
fields.unshift(fld) if !fields.include?(fld)
end
fields.map { |elm| flds[elm.to_sym] ? [ elm.to_sym, flds[elm.to_sym] ] : nil }.compact
end
def always_required_fields
flds = [ 'email', 'password', 'password_confirmation' ]
end
def required_field_list
flds = available_field_list
fields = (self.required_fields).uniq
['password_confirmation','password','email'].each do |fld|
fields.unshift(fld) if !fields.include?(fld)
end
fields.map { |elm| flds[elm.to_sym] ? [ elm.to_sym, flds[elm.to_sym] ] : nil }.compact
end
def optional_field_list
flds = available_field_list
fields = (self.optional_fields).uniq
fields.map { |elm| flds[elm.to_sym] ? [ elm.to_sym, flds[elm.to_sym] ] : nil }.compact
end
def available_address_field_list
{
:company => [ 'Company'.t, :text_field,:company],
:phone => [ 'Phone'.t, :text_field,:phone],
:fax => [ 'Fax'.t, :text_field,:fax],
:address => [ 'Address'.t, :text_field,:address],
:address_2 => [ 'Address (Line 2)'.t, :text_field,:address_2],
:city => [ 'City'.t, :text_field,:city],
:state => [ 'State'.t, :select,:state,{ :options => ContentModel.state_select_options } ],
:zip => [ 'Zip Code'.t, :text_field,:zip],
:country => [ 'Country'.t, :country_select, :country]
}
end
def available_address_field_options(business = false)
flds = available_address_field_list
if(!business)
flds.delete(:company)
end
flds.to_a.map { |elm| [ elm[1][0],elm[0].to_s ] }.sort
end
def address_field_list
hsh = available_address_field_list
hsh.delete(:company)
hsh.to_a
end
def business_address_field_list
available_address_field_list.to_a
end
def content_model
if publication
@content_mode ||= @pub.conten_model
else
nil
end
end
def publication
@pub ||= ContentPublication.find_by_id(self.content_publication_id)
end
def publication_options
content_model_ids = ContentModelField.find(:all, :conditions => "field_type = 'belongs_to' AND field_module = 'content/core_field'").collect { |fld| fld.content_model_id if fld.relation_class == EndUser }.uniq.compact
if content_model_ids.empty?
[]
else
ContentPublication.select_options_with_nil('Publication',:conditions => { :publication_type => 'create', :publication_module => 'content/core_publication', :content_model_id => content_model_ids }) unless content_model_ids.empty?
end
end
def publication_field_options
self.publication.content_model.content_model_fields.find(:all, :conditions => "field_type = 'belongs_to' AND field_module = 'content/core_field'").collect { |elm| [elm.name, elm.field] } if self.publication
end
end
class LoginOptions < HashModel
# For feature stuff
include HandlerActions
default_options :login_type => 'email',:success_page => nil, :forward_login => 'no',:failure_page => nil, :features => []
integer_options :success_page, :failure_page
validates_presence_of :forward_login
options_form(fld(:login_type, :select, :options => :login_type_options),
fld(:success_page, :select, :options => :page_options, :label => 'Destination Page'),
fld(:failure_page, :select, :options => :page_options, :label => 'Failure Page'),
fld(:forward_login, :radio_buttons, :options => :forward_login_options, :description => 'If users were locked out of a previous, forward them back to that page.')
)
def validate
if !self.features.is_a?(Array)
self.features = self.features.to_a.sort { |a,b| a[0] <=> b[0] }.map { |elm| obj = Handlers::ParagraphFeature.new(elm[1]); obj.to_hash }
self.login_features.each do |feature|
if !feature.options.valid?
self.errors.add_to_base('Feature Error')
end
end
end
end
def options_partial
nil
end
def self.login_type_options
[['By Email','email'], ['By Username','username'], ['Either','both']]
end
def self.page_options
[[ '--Stay on Same Page--'.t, nil ]] + SiteNode.page_options()
end
def self.forward_login_options
[['Yes','yes'], ['No','no']]
end
def available_features
[['--Select a feature to add--','']] + get_handler_options(:editor,:auth_login_feature)
end
def login_features
@login_features ||= self.features.map do |feature|
Handlers::ParagraphFeature.new(feature.merge({ :feature_type => 'editor_auth_login_feature'}))
end
end
end
def edit_account
@options = EditAccountOptions.new(params[:edit_profile] || @paragraph.data || {})
if request.post? && @options.valid?
if @options.include_subscriptions.is_a?(Array)
@options.include_subscriptions = @options.include_subscriptions.find_all { |elem| !elem.blank? }.collect { |elem| elem.to_i }
else
@options.include_subscriptions = []
end
@options.success_page = @options.success_page.to_i
@paragraph.data = @options.to_h
@paragraph.save
render_paragraph_update
return
end
@content_publications = [ ['No Publication', 0 ] ] + ContentPublication.find(:all,:conditions => 'publication_type = "create"',:order => 'content_models.name, content_publications.name',
:include => :content_model ).collect { |pub| [ pub.content_model.name + ' - ' + pub.name, pub.id ] }
@pages = [[ '--Select Page--'.t, nil ]] + SiteNode.page_options()
@fields = %w{username gender first_name last_name dob address work_address}
@field_options = [ [ 'Required', 'required' ], [ 'Optional','optional' ], ['Do not Display','off' ] ]
@subscriptions = UserSubscription.find_select_options(:all,:order => 'name')
end
class EditAccountOptions < HashModel
default_options :success_page => nil, :form_display => 'normal', :first_name => 'required', :last_name => 'required',
:gender => 'required', :username => 'off', :dob => 'off', :address => 'off', :work_address => 'off', :add_tags => '',
:include_subscriptions => [], :country => 'United States', :reset_password => 'show',
:address_type => 'us', :edit_button => nil
validates_presence_of :success_page
integer_options :success_page
end
def enter_vip
@options = EnterVipOptions.new(params[:enter_vip] || @paragraph.data || {})
if request.post? && params[:enter_vip] && @options.valid?
@paragraph.data = @options.to_h
@paragraph.save
render_paragraph_update
return
end
@pages = [['--Select Page--'.t, nil ]] + SiteNode.page_options()
end
class EnterVipOptions < HashModel
default_options :success_page => nil, :already_registered_page => nil, :login_even_if_registered => false, :add_tags => ''
validates_presence_of :success_page, :login_even_if_registered
integer_options :success_page
boolean_options :login_even_if_registered
end
def missing_password
@options = MissingPasswordOptions.new(params[:missing_password] || @paragraph.data || {})
return if handle_paragraph_update(@options)
@mail_templates = [['--Select Mail Template--'.t, nil ]] + MailTemplate.find_select_options(:all)
end
class MissingPasswordOptions < HashModel
default_options :email_template => nil, :reset_password_page => nil
integer_options :email_template, :reset_password_page
validates_presence_of :email_template, :reset_password_page
end
def email_list
@options = EmailListOptions.new(params[:email_list] || @paragraph.data || {})
return if handle_paragraph_update(@options)
@on_off = [ ['Required','required'],['Optional','optional'],['Off','off']]
@subscriptions = [['--Do not use a subscription--','']] + UserSubscription.find_select_options(:all,:order => 'name')
@pages = [['--Stay on same page--'.t, nil ]] + SiteNode.page_options()
end
class EmailListOptions < HashModel
default_options :user_subscription_id => nil, :tags => nil, :zip => 'optional', :first_name => 'off', :last_name => 'off',:destination_page_id => nil, :user_source => nil,
:success_message => 'Thank you, your email has been added to our list', :partial_post => 'yes'
integer_options :user_subscription_id, :destination_page_id
end
class SplashOptions < HashModel
default_options :splash_page_id => nil, :cookie_name => 'splash'
validates_presence_of :splash_page_id, :cookie_name
integer_options :splash_page_id
options_form(fld(:splash_page_id, :select, :options => :page_options),
fld(:cookie_name, :text_field, :description => 'Name of the splash page cooke (should be different for each splash page')
)
def self.page_options
[['--Select Splash Page--'.t,nil]] + SiteNode.page_options()
end
end
def add_feature
@info = get_handler_info(:editor,:auth_user_register_feature,params[:feature_handler])
if @info && myself.editor?
@feature = Handlers::ParagraphFeature.new({ })
@feature.feature_handler = @info[:identifier]
@feature.feature_type = 'editor_auth_user_register_feature'
render :partial => 'user_register_feature', :locals => { :feature => @feature, :idx => params[:index] }
else
render :nothing => true
end
end
def add_login_feature
@info = get_handler_info(:editor,:auth_login_feature,params[:feature_handler])
if @info && myself.editor?
@feature = Handlers::ParagraphFeature.new({ })
@feature.feature_handler = @info[:identifier]
@feature.feature_type = 'editor_auth_login_feature'
render :partial => 'login_feature', :locals => { :feature => @feature, :idx => params[:index] }
else
render :nothing => true
end
end
end