Guest User

Untitled

a guest
Jun 18th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.46 KB | None | 0 0
  1. class UserAccountsController < ApplicationController
  2. def signin
  3. auth = request.env['rack.auth']
  4.  
  5. user_for_provider_and_uid = UserAccount.find_by_provider_and_uid(auth['provider'], auth['uid']).first()
  6. new_auth = Authorization.new(:user_id => auth['user_id'], :provider => auth['provider'], :uid => auth['uid'], :user_info => auth['user_info'], :credentials => auth['credentials'])
  7.  
  8. if user_for_provider_and_uid.nil?
  9. user = signed_in? ? self.current_user : UserAccount.new(:name => auth['user_info']['name'])
  10. user.authorizations << new_auth
  11. user.save!
  12. self.current_user = user
  13. else
  14. self.current_user = user_for_provider_and_uid
  15. end
  16.  
  17. redirect_to :root
  18. end
  19. end
  20.  
  21.  
  22.  
  23. class UserAccount
  24. include Mongoid::Document
  25. field :name, :type => String
  26. field :admin, :type => Boolean, :default => false
  27.  
  28. references_many :lists, :stored_as => :array, :inverse_of => :users
  29. embeds_many :authorizations, :class_name => "Authorization"
  30.  
  31. def self.find_by_provider_and_uid(provider, uid)
  32. UserAccount.where("authorizations.provider" => provider).and("authorizations.uid" => uid)
  33. end
  34. end
  35.  
  36. class Authorization
  37. include Mongoid::Document
  38. field :provider, :type => String
  39. field :uid, :type => String
  40. field :user_id, :type => Integer
  41. field :user_info, :type => Hash
  42. field :user_credentials, :type => Hash
  43.  
  44. embedded_in :useraccount, :inverse_of => :authorizations # Remove this and it starts behaving badly!
  45. end
Add Comment
Please, Sign In to add comment