Module: Auth::Concerns::UserConcern

Extended by:
ActiveSupport::Concern
Includes:
ChiefModelConcern, EsConcern
Defined in:
app/models/auth/concerns/user_concern.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#additional_login_param_changed_on_unconfirmed_emailObject

if you change the additional login param while the email is not confirmed, you will get a validation error on additional_login_param



678
679
680
681
682
683
684
685
686
# File 'app/models/auth/concerns/user_concern.rb', line 678

def 
  #puts "calling additional login param changed"
  #puts "pending reconfirmation?"
  #puts self.pending_reconfirmation?   

  if   && (self.pending_reconfirmation?)
    errors.add(:additional_login_param,"Please verify your email or add an email id before changing your #{additional_login_param_name}")
  end
end

#additional_login_param_confirmed?Boolean

Returns:

  • (Boolean)


654
655
656
# File 'app/models/auth/concerns/user_concern.rb', line 654

def 
  self. == 2 
end

#additional_login_param_confirmed_or_does_not_existObject

if the additional_login_param_status == 2



659
660
661
# File 'app/models/auth/concerns/user_concern.rb', line 659

def 
   || self. == 0
end

#additional_login_param_formatObject

this method will validate the format of the additional_login_param. it can be overridden by the user to do his own custom validation. default behaviour is not to add any errors in the validation process.



642
643
644
# File 'app/models/auth/concerns/user_concern.rb', line 642

def 
  
end

#additional_login_param_nameObject

returns the additional login param name.



759
760
761
# File 'app/models/auth/concerns/user_concern.rb', line 759

def 
  Auth.configuration.auth_resources[self.class.name.to_s.underscore.capitalize][:additional_login_param_name]
end

#additional_login_param_required?Boolean

it is required only if the email is missing.

Returns:

  • (Boolean)


635
636
637
# File 'app/models/auth/concerns/user_concern.rb', line 635

def 
  email.nil?
end

#as_json(options = {}) ⇒ Object

for the api responses. if there is a current_app_id, then it will respond with the authentication-token and es if there is none, then it will return nil. it should return the errors irrespective of these settings. if otp_verification key is present in the options, then the auth_token and es will not be returned. this is needed in



590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
# File 'app/models/auth/concerns/user_concern.rb', line 590

def as_json(options={})
  
  
  json = {:nothing => true}
  
  if (!self.destroyed? && options[:otp_verification].nil?)
    
    if self.m_client.current_app_id && at_least_one_authentication_key_confirmed? && self.errors.empty?
        
        json = {}
           json[:es] = self.client_authentication[self.m_client.current_app_id]
           json[:authentication_token] = self.authentication_token
           unless options[:show_id].nil?
             json[:id] = self.id.to_s
             json[:admin] = self.admin.to_s
           end
         
     end
     if self.errors.full_messages.size > 0
        json[:errors] = self.errors.full_messages
     end
  end
  json
end

#at_least_one_authentication_key_confirmed?Boolean

at least one authentication_key should be confirmed. so even if we change the other one, we still return the remote authentication options even when that one is still unconfirmed. used in lib/devise to decide whether to return the auth token and es and redirect. used in self.as_json, to see whether to return the auth_token and es.

Returns:

  • (Boolean)


667
668
669
# File 'app/models/auth/concerns/user_concern.rb', line 667

def at_least_one_authentication_key_confirmed?
  (self.confirmed? && !self.pending_reconfirmation?) || self. == 2
end

#attr_blank_to_blank?(attr) ⇒ Boolean

has the attribute gone from blank to blank? what happens is that if submit the update form, it submits empty strings for input fields which we dont fill. so suppose you change the adiditonal_login_param , it will submit email as “”, in that case , earlier the email was nil, and now it becomes “”, so that is detected as an email change and it feels like both email and additional param have changed and triggers the validation #email_and_additional_login_param_both_changed, so we dont want that to happen, so we check if the param has gone from being blank to blank in the below validation.

Parameters:

  • attr (String)

    : the param name.

Returns:

  • (Boolean)


699
700
701
702
703
704
705
706
707
# File 'app/models/auth/concerns/user_concern.rb', line 699

def attr_blank_to_blank?(attr)
  #puts "calling blank to blank."
  if self.respond_to?(attr)
    if (self.send("#{attr}_was").blank? && self.send("#{attr}").blank?)
      
      true
    end
  end
end

#authentication_keys_confirmed?Boolean

used in auth/registrations/update.js.erb use it to chekc if the resource is fully confirmed, otherwise we redirect in the erb to whichever of the two needs to be confirmed.

Returns:

  • (Boolean)


673
674
675
# File 'app/models/auth/concerns/user_concern.rb', line 673

def authentication_keys_confirmed?  
  return email_confirmed_or_does_not_exist && 
end

#can_create_discount_coupons?Boolean

@return true/false : override to decide how the user decides if it can create discount coupons for its contents or not. the current implementation returns true by default

Returns:

  • (Boolean)


796
797
798
# File 'app/models/auth/concerns/user_concern.rb', line 796

def can_create_discount_coupons?
  true
end

#create_clientObject

tries to create a client with a unique api_key, and user id. tries 10 attempts initially tries a versioned_create if the op is successfull then it breaks. if the op_count becomes zero it breaks. if there is no client with this user id, then and only then will it change the api_key and again try to create a client with this resource_id and this api_key. at the end it will exit, and there may or may not be a client with this resource_id. so this method basically fails silently, and so when you look at a user profiel and if you don’t see an api_key, it means that there is no client for him, that is the true sign that it failed. api key checking includes whether the user for that key is confirmed or not. client is created irrespective of whether the user is confirmed or not.



526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
# File 'app/models/auth/concerns/user_concern.rb', line 526

def create_client
  

  ##we want to create a new client, provided that there is no client for this user id.
  ##if a client already exists, then we dont want to do anything.
  ##when we create the client we want to be sure that 
  ##provided that there is no client with this user id.
  #puts "called create client."

  ##first find out if there is already a client for this user id.
  c = Auth::Client.new(:api_key => SecureRandom.hex(32), :resource_id => self.id)

  puts "Came to create a client."

  c.versioned_create({:resource_id => self.id})
  op_count = 10

  puts "-------CREATED A CLIENT AS FOLLOWS:-----------"
  puts c.attributes.to_s

  while(true)
    
    if c.op_success?
      #puts "the op was a success"
      break
    elsif op_count == 0
      #puts "op count was 0"
      break
    elsif (Auth::Client.where(:resource_id => self.id).count == 0)
      #puts "tried to create here."
      c.api_key = SecureRandom.hex(32)
      c.versioned_create({:resource_id => self.id})
      op_count-=1
    else
      #puts "finally broke."
      break
    end


  end

end

#destroy_clientObject



511
512
513
514
# File 'app/models/auth/concerns/user_concern.rb', line 511

def destroy_client
  @client = Auth::Client.find(self.id)
  @client.delete
end

#email_and_additional_login_param_both_changedObject

now what if both have changed?



710
711
712
713
714
715
716
717
718
# File 'app/models/auth/concerns/user_concern.rb', line 710

def 
  #puts "calling email and additional login param both changed"
  ##add error saying you cannot change both at the same time.
  ##additional login param can change as long as neither goes from nil to blank or blank to nil.

  if email_changed? && !attr_blank_to_blank?("email") &&  && !attr_blank_to_blank?("additional_login_param")
    errors.add(:email,"you cannot update your email and #{additional_login_param_name} at the same time")
  end
end

#email_changed?Boolean

skip_email_unique_validation is set to true in omni_concern in the situation: 1.there is no user with the given identity. however it is possible that a user with this email exists. in that case, if we try to do versioned_create, then the prepare_insert block in mongoid_versioned_atomic, runs validations. these include, checking if the email is unique, and in this case, if a user with this email already exists, then the versioned_create doesnt happen at all. We don’t want to first check if there is already an account with this email, and in another step then try to do a versioned_update, because in the time in between another user could be created. So instead we simply just set #skip_email_unique_validation to true, and as a result the unique validation is skipped.

Returns:

  • (Boolean)


629
630
631
# File 'app/models/auth/concerns/user_concern.rb', line 629

def email_changed?
    super && skip_email_unique_validation.nil?
end

#email_changed_on_unconfirmed_additional_login_paramObject

if you change the email while the additional login param not confirmed, then you will get validation errors on the email, as long as you have enabled an additional_login_param in the configuration.



689
690
691
692
693
694
# File 'app/models/auth/concerns/user_concern.rb', line 689

def 
  #puts "calling email changed"
  if email_changed? && ( == 1) && 
    errors.add(:email, "Please add or verify your #{additional_login_param_name} before changing your email id")
  end
end

#email_confirmed_or_does_not_existObject

confirmed? OR both email and unconfirmed email are nil AND additional_login_param has been confirmed already. currently used in this file in #authentication_keys_confirmed?



650
651
652
# File 'app/models/auth/concerns/user_concern.rb', line 650

def email_confirmed_or_does_not_exist
  (self.confirmed? && !self.pending_reconfirmation?)  ||  (self.email.nil? && self.unconfirmed_email.nil?)
end

#get_user_info(keys) ⇒ Object

@param : array of field names that you want the values for. @return : hash of key , value pairs containing the values that you asked for.



571
572
573
574
575
# File 'app/models/auth/concerns/user_concern.rb', line 571

def (keys)
  keys = keys.keep_if{ |c| (.include? c) && (self.respond_to(c.to_sym)) }

  return Hash[keys.map{|c| [c,self.send("#{c}")]}]
end

#has_gcm_tokenObject



781
782
783
# File 'app/models/auth/concerns/user_concern.rb', line 781

def has_gcm_token
  false
end

#has_oauth_identity?Boolean

returns true if there is at least one non empty oauth identity

Returns:

  • (Boolean)


616
617
618
619
620
621
622
623
# File 'app/models/auth/concerns/user_concern.rb', line 616

def has_oauth_identity?
  return false unless self.respond_to? :identities
  self.identities.keep_if{|c| 

    Auth::Identity.new(c).has_provider?

    }.size > 0
end

#has_phoneObject

THIS DEF CAN BE OVERRIDDEN IN YOUR MODEL TO SUIT YOUR NEEDS.



770
771
772
# File 'app/models/auth/concerns/user_concern.rb', line 770

def has_phone
  Auth.configuration.auth_resources[resource_key_for_auth_configuration][:additional_login_param_name] && Auth.configuration.auth_resources[resource_key_for_auth_configuration][:additional_login_param_name] == "mobile"  
end

#is_admin?Boolean

this method is to be overridden, it returns the value of the admin_variable. it can be used to decide if the user is an admin.

Returns:

  • (Boolean)


788
789
790
# File 'app/models/auth/concerns/user_concern.rb', line 788

def is_admin?
  admin
end

#loginObject



385
386
387
# File 'app/models/auth/concerns/user_concern.rb', line 385

def 
   @login || self.email || self.
end

#login=(login) ⇒ Object

FOR THE LOGIN AUTHENTICATION KEY PARAMETER, WE DEFINE GETTERS AND SETTERS



381
382
383
# File 'app/models/auth/concerns/user_concern.rb', line 381

def login=()
  @login = 
end

#reply_with_auth_token_es?(client, curr_user) ⇒ Boolean

this def is used to determine if the auth_token and es should be sent back.

Returns:

  • (Boolean)


731
732
733
734
735
736
737
738
739
# File 'app/models/auth/concerns/user_concern.rb', line 731

def reply_with_auth_token_es?(client,curr_user)

   ##we have a client authentication for the client.
        ##we have an authentication token
        ##we are signed_in
        ##we have at least one authentication_key confirmed.
        return false if !curr_user
        client && client_authentication[client.current_app_id] && authentication_token && (id.to_s == curr_user.id.to_s) && at_least_one_authentication_key_confirmed?
end

#reply_with_redirect_url_and_auth_token_and_es?(redirect_url, client, curr_user) ⇒ Boolean

just a combination of having the redirect_url and the above method, and whether to redirect or not.

Returns:

  • (Boolean)


743
744
745
# File 'app/models/auth/concerns/user_concern.rb', line 743

def reply_with_redirect_url_and_auth_token_and_es?(redirect_url,client,curr_user)
  Auth.configuration.do_redirect && redirect_url && reply_with_auth_token_es?(client,curr_user)
end

#resource_first_nameObject

override as needed. currently used in _gateway.html.erb



776
777
778
# File 'app/models/auth/concerns/user_concern.rb', line 776

def resource_first_name
  name
end

#resource_key_for_auth_configurationObject

> resource name converted to string with a capital

> first letter. eg : “User”



765
766
767
# File 'app/models/auth/concerns/user_concern.rb', line 765

def resource_key_for_auth_configuration
  self.class.name.to_s.underscore.capitalize
end

if the resource was created by an administrator, and the attr_accessor request_send_reset_password_link is true, then it will check if the email is confirmed, and then send the reset_password_instructions to the email. otherwise will check if the mobile is confirmed, and will just generate that reset_password_link



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
# File 'app/models/auth/concerns/user_concern.rb', line 458

def send_reset_password_link
  
  reset_password_link = nil

  #puts "came to send reset password link, and this is the attr accessor."
  #puts self.request_send_reset_password_link.to_s

  ## if there was an unconfirmed_email present.

  if self.created_by_admin

    #puts "the request send is true."
    #puts self.attributes.to_s
    #puts "is the additional login param confirmed"
    #puts self.additional_login_param_confirmed?
    ## this case is exceptional because the user will have gone to 
    if self.confirmed?
      #puts "self is confirmed."
      begin
        self.class.skip_callback(:save, :after, :send_reset_password_link)
        
        self.class.send_reset_password_instructions(self.attributes)
      rescue
      ensure
        self.created_by_admin = false
        self.save
        self.class.set_callback(:save, :after, :send_reset_password_link)
      end

    elsif self.
      #puts "additiona login param is confirmed."
      begin
        #self.created_by_admin = false
        self.class.skip_callback(:save, :after, :send_reset_password_link)
        reset_password_link = Rails.application.routes.url_helpers.send("edit_#{self.class.name.downcase}_password_path",{:reset_password_token => self.set_reset_password_token})
          
      rescue => e
        puts e.to_s
      ensure
        self.created_by_admin = false
        self.save
        self.class.set_callback(:save, :after, :send_reset_password_link)
      end
      
    end

  end    
  
  reset_password_link

end

#set_client_authenticationObject

setting these as nil, forces a new auth_token and es to be generated because in the before_save hooks they are set if they are blank. def set_es

if !email.nil?
  salt = SecureRandom.hex(32)
  pre_es = salt + email
  self.es = Digest::SHA256.hexdigest(pre_es)
end

end



432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
# File 'app/models/auth/concerns/user_concern.rb', line 432

def set_client_authentication
  

  if !self.m_client.nil?
    #puts "the client is not nil"
    #puts "is self client authentication nil"
    #puts self.client_authentication[self.m_client.current_app_id].nil?
    #puts "is self valie"
    #puts self.valid?
    if self.client_authentication[self.m_client.current_app_id].nil? && self.valid?
      self.client_authentication[self.m_client.current_app_id] = SecureRandom.hex(32)
      
      self.save
    end

  else
    
  end
  #
  
end

#set_client_authentication?(act_name, cont_name, client) ⇒ Boolean

no longer used.

Returns:

  • (Boolean)


721
722
723
724
725
726
# File 'app/models/auth/concerns/user_concern.rb', line 721

def set_client_authentication?(act_name,cont_name,client)
  
  client && act_name != "destroy" && !(["passwords","confirmations","unlocks"].include? cont_name)
  
  
end

#token_expired?Boolean

Returns:

  • (Boolean)


748
749
750
751
752
753
754
# File 'app/models/auth/concerns/user_concern.rb', line 748

def token_expired?
  if authentication_token_expires_at < Time.now.to_i
    ## the before_save callback in omniauth.rb, will automatically regenerate the authentication token
    #save
    true
  end
end