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



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

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 #{}")
	end
end

#additional_login_param_confirmed?Boolean

Returns:

  • (Boolean)


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

def 
	self. == 2 
end

#additional_login_param_confirmed_or_does_not_existObject

if the additional_login_param_status == 2



678
679
680
# File 'app/models/auth/concerns/user_concern.rb', line 678

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.



661
662
663
# File 'app/models/auth/concerns/user_concern.rb', line 661

def 
	
end

#additional_login_param_nameObject

returns the additional login param name.



778
779
780
# File 'app/models/auth/concerns/user_concern.rb', line 778

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)


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

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



609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
# File 'app/models/auth/concerns/user_concern.rb', line 609

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)


686
687
688
# File 'app/models/auth/concerns/user_concern.rb', line 686

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)


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

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)


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

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)


810
811
812
# File 'app/models/auth/concerns/user_concern.rb', line 810

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.



545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
# File 'app/models/auth/concerns/user_concern.rb', line 545

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



530
531
532
533
# File 'app/models/auth/concerns/user_concern.rb', line 530

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?



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

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 #{} 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)


648
649
650
# File 'app/models/auth/concerns/user_concern.rb', line 648

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.



708
709
710
711
712
713
# File 'app/models/auth/concerns/user_concern.rb', line 708

def 
	#puts "calling email changed"
	if email_changed? && ( == 1) && 
		errors.add(:email, "Please add or verify your #{} 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?



669
670
671
# File 'app/models/auth/concerns/user_concern.rb', line 669

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.



590
591
592
593
594
# File 'app/models/auth/concerns/user_concern.rb', line 590

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

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

#has_oauth_identity?Boolean

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

Returns:

  • (Boolean)


635
636
637
638
639
640
641
642
# File 'app/models/auth/concerns/user_concern.rb', line 635

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.



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

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)


802
803
804
# File 'app/models/auth/concerns/user_concern.rb', line 802

def is_admin?
	admin
end

#loginObject



404
405
406
# File 'app/models/auth/concerns/user_concern.rb', line 404

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

#login=(login) ⇒ Object

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



400
401
402
# File 'app/models/auth/concerns/user_concern.rb', line 400

def login=()
	@login = 
end

#refresh_endpointsObject

GCM - AMAZON ENDPOINT



823
824
825
826
827
828
829
830
831
832
# File 'app/models/auth/concerns/user_concern.rb', line 823

def refresh_endpoints
	puts "called refresh end points."
	puts self.android_token
	if self.android_token_changed?
		endpoint = Auth::Endpoint.new
		endpoint.android_token = self.android_token
		self.android_endpoint = endpoint.set_android_endpoint
	end
	## now have to make an endpoints controller.
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)


750
751
752
753
754
755
756
757
758
# File 'app/models/auth/concerns/user_concern.rb', line 750

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)


762
763
764
# File 'app/models/auth/concerns/user_concern.rb', line 762

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



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

def resource_first_name
	name
end

#resource_key_for_auth_configurationObject

> resource name converted to string with a capital

> first letter. eg : “User”



784
785
786
# File 'app/models/auth/concerns/user_concern.rb', line 784

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



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

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



451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'app/models/auth/concerns/user_concern.rb', line 451

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)


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

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)


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

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