-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated devise, User and Authorization models.
- Loading branch information
Mark Cottman-Fields
committed
Oct 31, 2012
1 parent
f255034
commit 1f83232
Showing
5 changed files
with
135 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController | ||
|
||
require 'uuidtools' | ||
|
||
def facebook | ||
oauthorize "Facebook" | ||
end | ||
|
||
def twitter | ||
oauthorize "Twitter" | ||
end | ||
|
||
def linked_in | ||
oauthorize "LinkedIn" | ||
end | ||
|
||
def passthru | ||
render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false | ||
end | ||
|
||
private | ||
|
||
def oauthorize(kind) | ||
@user = find_for_ouath(kind, env["omniauth.auth"], current_user) | ||
if @user | ||
flash[:notice] = I18n.t "devise.omniauth_callbacks.success", :kind => kind | ||
session["devise.#{kind.downcase}_data"] = env["omniauth.auth"] | ||
sign_in_and_redirect @user, :event => :authentication | ||
end | ||
end | ||
|
||
def find_for_ouath(provider, access_token, resource=nil) | ||
user, email, name, uid, auth_attr = nil, nil, nil, {} | ||
case provider | ||
when "Facebook" | ||
uid = access_token['uid'] | ||
email = access_token['extra']['user_hash']['email'] | ||
auth_attr = { :uid => uid, :token => access_token['credentials']['token'], :secret => nil, :name => access_token['extra']['user_hash']['name'], :link => access_token['extra']['user_hash']['link'] } | ||
when "Twitter" | ||
uid = access_token['extra']['user_hash']['id'] | ||
name = access_token['user_info']['name'] | ||
auth_attr = { :uid => uid, :token => access_token['credentials']['token'], :secret => access_token['credentials']['secret'], :name => name, :link => "http://twitter.com/#{name}" } | ||
when 'LinkedIn' | ||
uid = access_token['uid'] | ||
name = access_token['user_info']['name'] | ||
auth_attr = { :uid => uid, :token => access_token['credentials']['token'], :secret => access_token['credentials']['secret'], :name => name, :link => access_token['user_info']['public_profile_url'] } | ||
else | ||
raise 'Provider #{provider} not handled' | ||
end | ||
if resource.nil? | ||
if email | ||
user = find_for_oauth_by_email(email, resource) | ||
elsif uid && name | ||
user = find_for_oauth_by_uid(uid, resource) | ||
if user.nil? | ||
user = find_for_oauth_by_name(name, resource) | ||
end | ||
end | ||
else | ||
user = resource | ||
end | ||
|
||
auth = user.authorizations.find_by_provider(provider) | ||
if auth.nil? | ||
auth = user.authorizations.build(:provider => provider) | ||
user.authorizations << auth | ||
end | ||
auth.update_attributes auth_attr | ||
|
||
return user | ||
end | ||
|
||
def find_for_oauth_by_uid(uid, resource=nil) | ||
user = nil | ||
if auth = Authorization.find_by_uid(uid.to_s) | ||
user = auth.user | ||
end | ||
return user | ||
end | ||
|
||
def find_for_oauth_by_email(email, resource=nil) | ||
if user = User.find_by_email(email) | ||
user | ||
else | ||
user = User.new(:email => email, :password => Devise.friendly_token[0,20]) | ||
user.save | ||
end | ||
return user | ||
end | ||
|
||
def find_for_oauth_by_name(name, resource=nil) | ||
if user = User.find_by_name(name) | ||
user | ||
else | ||
user = User.new(:name => name, :password => Devise.friendly_token[0,20], :email => "#{UUIDTools::UUID.random_create}@host") | ||
user.save false | ||
end | ||
return user | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
class RegistrationsController < Devise::RegistrationsController | ||
|
||
def update | ||
if params[resource_name][:password].blank? | ||
params[resource_name].delete(:password) | ||
params[resource_name].delete(:password_confirmation) if params[resource_name][:password_confirmation].blank? | ||
end | ||
# Override Devise to use update_attributes instead of update_with_password. | ||
# This is the only change we make. | ||
if resource.update_attributes(params[resource_name]) | ||
set_flash_message :notice, :updated | ||
# Line below required if using Devise >= 1.2.0 | ||
sign_in resource_name, resource, :bypass => true | ||
redirect_to after_update_path_for(resource) | ||
else | ||
clean_up_passwords(resource) | ||
render_with_scope :edit | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class Authorization < ActiveRecord::Base | ||
# no attributes are publicly accessible, all are used only internally | ||
# attr_accessible :link, :name, :provider, :secret, :token, :uid, :user_id | ||
attr_accessible | ||
|
||
belongs_to :user | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters