Skip to content
This repository has been archived by the owner on Jan 6, 2018. It is now read-only.

Commit

Permalink
code for posting to twitter & facebook
Browse files Browse the repository at this point in the history
  • Loading branch information
peter committed Oct 17, 2011
1 parent b276b21 commit 3db025b
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 28 deletions.
10 changes: 10 additions & 0 deletions README
Expand Up @@ -69,8 +69,18 @@ http://localhost:8080/user/admin (to access, "update user set role="admin" wher

A user table with email (string), password (string), salt (string), verified (boolean/tinyint) fields.

== To post to Twitter / Facebook

for a in current_user.authentications
case a.provider
when 'facebook' then current_user.facebook(a).feed!(:message => 'Hello, Testing Facebook', :name => 'Lynk' )
when 'twitter' then current_user.twitter(a).update("Hello, Testing Twitter")
end
end

== Other Notes

Add your OAuth keys to config/initializers/omniauth.rb
There is intentionally nearly zero styling--you'll want to wrap your own stylesheets and CSS around everything.

Tests are up to you for now after modifying the code however you want.
Expand Down
35 changes: 14 additions & 21 deletions app/controllers/user_controller.rb
Expand Up @@ -31,15 +31,13 @@ def signup
## TODO: timeout in email verify link
def verify
@user = User.find(params[:id])
logger.info "VERIFYING USER #{@user.id}"
logger.info "User: #{@user.email} -- #{@user.email_sig(@user.email)} -- #{params[:sig]}"
if @user &&
@user.email_sig(params[:email] || @user.email) == params[:sig] && ## check sig
(@user.email = params[:email] || @user.email) && ## chg email ?
(params[:email] || @user.verified.blank?) && ## ensure unverified/new if not addr chg
(@user.verified = true) && ## set to verified
@user.save
logger.info "VERIFIED USER #{@user.verified} / #{@user.id}. Email = #{params[:email]} || #{@user.email}"

session[:user] = @user.to_session
flash[:message] = "Email verified"
if params[:email]
Expand Down Expand Up @@ -105,13 +103,11 @@ def change_email
def delete
@user = current_user
if @user
@user.deactivated = 'deleted'
@user.email = nil
@user.save!
@user.deactivate
flash[:message] = 'Your account has been deleted'
end
session[:user] = nil
redirect_to '/'
redirect_to '/user/signup'
end

def forgot_password
Expand Down Expand Up @@ -171,13 +167,10 @@ def admin
params[:user].delete(:password_confirmation)
end

logger.info " USER ADMIN SAVING CHANGES"
if @user.update_attributes(params[:user])
flash[:message] = "Changes saved"
logger.info " USER ADMIN SAVED"
else
flash[:warning] = "There were errors"
logger.info " USER ADMIN FAILED"
end
end

Expand All @@ -190,48 +183,48 @@ def admin
## via facebook, twitter, linkedin, etc.
def connect
omniauth = request.env["omniauth.auth"]
logger.debug "OMNIAUTH HASH:\n" + omniauth.to_yaml;

authentication = Authentication.find_by_provider_and_uid(omniauth['provider'], omniauth['uid'])
logger.debug "=== have authentication? #{authentication || 'no'}"

## authentication row matches?
if authentication
flash[:notice] = "Signed in successfully."
#sign_in_and_redirect(:user, authentication.user)
## log me in
session[:user] = User.active.find_by_id(authentication.user_id).to_session
redirect_to '/user/account'

## logged in?
elsif current_user
logger.debug "=== have current_user? #{current_user.id}"
logger.debug "=== provider: #{omniauth['provider']}, uid: #{omniauth['uid']}"
#current_user.authentications.create!(:provider => omniauth['provider'], :uid => omniauth['uid'])

authentication = Authentication.new(:user_id => current_user.id,
:provider => omniauth['provider'],
:uid => omniauth['uid'])
:uid => omniauth['uid'],
:token => omniauth['credentials']['token'],
:secret => omniauth['credentials']['secret']
)

if authentication.save!
logger.debug "=== authentication created"
flash[:notice] = "Authentication successful."
else
flash[:warning] = "There was a problem connecting your account"
end
redirect_to '/user/account'


## new user
else
logger.debug "=== create new user.."

user = User.new
user.apply_omniauth(omniauth)
user.password = User.random_string(10) if user.password.blank?
user.verified = true;

if user.save
flash[:notice] = "Signed in successfully."
session[:user] = user.to_session
redirect_to '/user/account'
else
## don't have all we need? send to signup.
session[:omniauth] = omniauth.except('extra')
session[:omniauth] = omniauth ## .except('extra')
redirect_to '/user/signup'
end
end
Expand Down
39 changes: 32 additions & 7 deletions app/models/user.rb
Expand Up @@ -9,9 +9,9 @@ class User < ActiveRecord::Base
validates_presence_of :password, :password_confirmation, :if => Proc.new{ |u| u['validate_password'] }
validates_confirmation_of :password, :if => Proc.new{ |u| u['validate_password'] }

validates_presence_of :email, :unless => :deactivated
validates_uniqueness_of :email, :unless => :deactivated
validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :message => "Invalid email", :unless => :deactivated
# validates_presence_of :email, :unless => :deactivated ## OPTIONAL
validates_uniqueness_of :email, :unless => :deactivated, :allow_nil => true
validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :message => "Invalid email", :unless => :deactivated, :allow_nil => true

attr_protected :id, :salt
attr_accessor :password, :password_confirmation
Expand Down Expand Up @@ -101,9 +101,11 @@ def to_session

## TODO: delete authentication rows
def deactivate
Authentication.delete_all(:user_id => self.id)
email = nil
name = 'Deactivated'
deactivated = 'deactivated'
user.save!
save!
end

def block
Expand All @@ -120,11 +122,34 @@ def apply_omniauth(omniauth)
end
self.name = omniauth['user_info']['name'] if name.blank?
self.timezone = 'Eastern Time (US & Canada)' if timezone.blank?
authentications.build(:provider => omniauth['provider'], :uid => omniauth['uid'])

authentications.build(:provider => omniauth['provider'], :uid => omniauth['uid'],
:token =>(omniauth['credentials']['token']),
:secret =>(omniauth['credentials']['secret'])
)

end


def facebook(authentication = nil)
unless @fb_user
authentication = authentication || authentications.find_by_provider('facebook')
@fb_user ||= FbGraph::User.me(authentication.token)
end
@fb_user
end

def password_required?
(authentications.empty? || !password.blank?) && super
def twitter(authentication = nil)
unless @twitter_user
authentication = authentication || authentications.find_by_provider('twitter')
@twitter_user = Twitter::Client.new(
:consumer_key => Settings::TwitterConsumerKey,
:consumer_secret => Settings::TwitterConsumerSecret,
:oauth_token => authentication.token,
:oauth_token_secret => authentication.secret
) rescue nil
end
@twitter_user
end

end
10 changes: 10 additions & 0 deletions db/migrate/20111014194054_add_token_to_authentication.rb
@@ -0,0 +1,10 @@
class AddTokenToAuthentication < ActiveRecord::Migration
def self.up
add_column :authentication, :token, :string
add_column :authentication, :secret, :string
end

def self.down
remove_column :authentication, :token
end
end
4 changes: 4 additions & 0 deletions lib/settings.rb
Expand Up @@ -12,6 +12,10 @@ module Settings
SiteName = 'Rawreg Example'
SiteEmail = 'Rawreg Example <info@localhost>'
Secret = "Change this random string... #428u3 ksadf 9823fokjh a kajshd fjh92hf3h";

## TODO: remove redundancy of these keys both here and config/initializers/omniauth
TwitterConsumerKey = 'XXXXXXXXXXXX'
TwitterConsumerSecret = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
end

end

0 comments on commit 3db025b

Please sign in to comment.