Guest User

Untitled

a guest
Dec 8th, 2017
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. class User < ApplicationRecord
  2. # Include default devise modules. Others available are:
  3. devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable, :confirmable,
  4. :omniauthable, :omniauth_providers => [:facebook,:twitter,:linkedin]
  5. has_many :organizations_users
  6. has_many :auth_providers
  7. has_many :organizations, through: :organizations_users
  8. accepts_nested_attributes_for :auth_providers
  9. def active_for_authentication?
  10. # Uncomment the below debug statement to view the properties of the returned self model values.
  11. super && self.active && self.exp_alert == false
  12. end
  13.  
  14. def self.from_omniauth(auth)
  15. exist = where(email: auth.info.email).first
  16. if exist
  17. existing_user = exist["id"]
  18. Auth_provider.where(provider: auth.provider, social_uid: auth.uid).first_or_create do |auth_provider|
  19. auth_provider.provider = auth.provider
  20. auth_provider.social_uid = auth.uid
  21. auth_provider.social_token = auth.credentials.token
  22. auth_provider.user_id = existing_user
  23. end
  24. else
  25. #@organization.users.create(first_name:params[:first_name])
  26. names = auth.info.name.strip.split(" ")
  27. first_name = names[0]
  28. last_name = names[1]
  29.  
  30. abc = User.new (
  31. email: 'joe@joe.com',password:'safdasfd23',
  32. auth_providers_attributes: [
  33. { provider: 'Kari' },
  34. { social_token: 'The egalitarian assumption of the modern citizen' },
  35. { social_uid: 'safsdfsf' } # this will be ignored
  36. ]
  37. )
  38.  
  39. abc.save
  40.  
  41.  
  42. #users.auth_providers.create(first_name:first_name,email,email:auth.info.email,provider:auth.provider)
  43. # user = where(provider: auth.provider, uid: auth.uid).first_or_create do |user|
  44. # user.first_name = first_name
  45. # user.last_name = last_name
  46. # user.email = auth.info.email
  47. # user.password = Devise.friendly_token[0,20]
  48. # user.active = 'true'
  49. # user.admin = 'false'
  50. # user.exp_alert = 'false'
  51. # user.skip_confirmation!
  52. #end
  53. #user
  54. end
  55. end
  56.  
  57. def self.new_with_session(params, session)
  58. super.tap do |user|
  59. if data = session["devise.facebook_data"] && session["devise.facebook_data"]["extra"]["raw_info"]
  60. user.email = data["email"] if user.email.blank?
  61. end
  62. end
  63. end
  64.  
  65. def self.find_or_create_from_auth_hash(auth_hash)
  66. user = where(provider: auth_hash.provider, uid: auth_hash.uid).first_or_create do |user|
  67. user.first_name = auth_hash.info.nickname
  68. user.active = 'true'
  69. user.admin=='false'
  70. user.exp_alert = 'false'
  71. user.password = Devise.friendly_token[0,20]
  72. user.token = auth_hash.credentials.token
  73. user.email = auth_hash.info.email
  74. user.secret = auth_hash.credentials.secret
  75. user.skip_confirmation!
  76. end
  77. user
  78. end
  79.  
  80. def self.linkedin_hash(auth_hash)
  81. user = where(provider: auth_hash.provider, uid: auth_hash.uid).first_or_create do |user|
  82. user.first_name = auth_hash.info.first_name
  83. user.last_name = auth_hash.info.last_name
  84. user.active = 'true'
  85. user.admin = 'false'
  86. user.exp_alert = 'false'
  87. user.password = Devise.friendly_token[0,20]
  88. user.token = auth_hash.credentials.token
  89. user.email = auth_hash.info.email
  90. user.skip_confirmation!
  91. end
  92. user
  93. end
  94.  
  95. def inactive_message
  96. "Your Account has not been active yet."
  97. end
  98.  
  99. def after_confirmation
  100. super
  101. self.update_attribute(:active, true)
  102. end
  103. end
  104.  
  105. class Auth_provider < ApplicationRecord
  106. devise :database_authenticatable
  107. belongs_to :user
  108.  
  109. accepts_nested_attributes_for :user
  110. end
Add Comment
Please, Sign In to add comment