secobarbital-authlogic_facebook_connect 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc ADDED
@@ -0,0 +1,79 @@
1
+ == Install and use
2
+
3
+ === 1. Install the facebooker gem and make Rails use it
4
+
5
+ $ sudo gem install facebooker
6
+
7
+ Make sure that you have setup you config/facebooker.yml to match your facebook application, make you application depended on the facebooker gem
8
+
9
+ config.gem "facebooker"
10
+
11
+ and run the xd_receiver generator to create the cross domain scripting bridge to make it possible for your application to communicate with facebook
12
+
13
+ $ script/generate xd_receiver
14
+
15
+ For more information on the facebooker gem checkout it's readme http://github.com/mmangino/facebooker/tree/master
16
+
17
+ === 2. Install the Authlogic Facebook Connect plugin
18
+
19
+ $ script/plugin install git://github.com/kalasjocke/authlogic_facebook_connect.git
20
+
21
+ This plugin should soon be packed inside a gem.
22
+
23
+ === 3. Make some changes to your database
24
+
25
+ class AddFacebookConnectFieldsToUser < ActiveRecord::Migration
26
+ def self.up
27
+ add_column :users, :name, :string
28
+ add_column :users, :facebook_uid, :integer, :limit => 8
29
+ end
30
+
31
+ def self.down
32
+ remove_column :users, :facebook_uid
33
+ remove_column :users, :name
34
+ end
35
+ end
36
+
37
+ === 4. Make your layout look something like this
38
+
39
+ The important parts are the facebook html namespace, facebook javascript include and the facebook helper calls to init the facebooker gem.
40
+
41
+ <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
42
+ "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
43
+
44
+ <html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">
45
+ <head>
46
+ <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
47
+ <title>Your awsome facebook connected app</title>
48
+
49
+ <%= stylesheet_link_tag 'application' %>
50
+ <%= javascript_include_tag :defaults %>
51
+
52
+ <script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php" type="text/javascript"></script>
53
+ </head>
54
+ <body>
55
+
56
+ <p style="color: green"><%= flash[:notice] %></p>
57
+
58
+ <%= fb_connect_javascript_tag %>
59
+ <%= init_fb_connect "XFBML" %>
60
+
61
+ <%= yield %>
62
+
63
+ </body>
64
+ </html>
65
+
66
+ === 5. Add the Facebook Connect button to your login form
67
+
68
+ <%= authlogic_facebook_login_button %>
69
+
70
+ === Notes
71
+
72
+ If you want to save some user data when connecting to facebook you can use the before_connect hook in your user model.
73
+
74
+ def before_connect(facebook_session)
75
+ self.name = facebook_session.user.name
76
+ end
77
+
78
+ For more information about what you can get form the facebook_session checkout the Facebooker gem rdoc.
79
+
data/Rakefile ADDED
@@ -0,0 +1,21 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "secobarbital-authlogic_facebook_connect"
8
+ gem.summary = %Q{Extension of the Authlogic library to add Facebook Connect support built upon the excellent facebooker gem}
9
+ gem.email = ["jocke.ekberg@gmail.com", "sumboh@thingbuzz.com"]
10
+ gem.homepage = "http://github.com/kalasjocke/authlogic_facebook_connect"
11
+ gem.authors = ["Joakim Ekberg", "Seggy Umboh"]
12
+
13
+ gem.add_dependency "authlogic"
14
+ gem.add_dependency "facebooker"
15
+
16
+ gem.files.exclude ".gitignore"
17
+ gem.files.exclude "*.gemspec"
18
+ end
19
+ rescue LoadError
20
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
21
+ end
data/Version ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'authlogic_facebook_connect'
@@ -0,0 +1,10 @@
1
+ # require "authlogic_facebook_connect/version"
2
+ require "authlogic_facebook_connect/acts_as_authentic"
3
+ require "authlogic_facebook_connect/session"
4
+ require "authlogic_facebook_connect/helper"
5
+
6
+ if ActiveRecord::Base.respond_to?(:add_acts_as_authentic_module)
7
+ ActiveRecord::Base.send(:include, AuthlogicFacebookConnect::ActsAsAuthentic)
8
+ Authlogic::Session::Base.send(:include, AuthlogicFacebookConnect::Session)
9
+ ActionController::Base.helper AuthlogicFacebookConnect::Helper
10
+ end
@@ -0,0 +1,20 @@
1
+ module AuthlogicFacebookConnect
2
+ module ActsAsAuthentic
3
+ def self.included(klass)
4
+ klass.class_eval do
5
+ extend Config
6
+ add_acts_as_authentic_module(Methods, :prepend)
7
+ end
8
+ end
9
+
10
+ module Config
11
+ end
12
+
13
+ module Methods
14
+ def self.included(klass)
15
+ klass.class_eval do
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,29 @@
1
+ module AuthlogicFacebookConnect
2
+ module Helper
3
+ def authlogic_facebook_login_button(options = {})
4
+ # TODO: Make this with correct helpers istead of this uggly hack.
5
+
6
+ options[:controller] ||= "user_session"
7
+ options[:js] ||= :prototype
8
+
9
+ case options[:js]
10
+ when :prototype
11
+ js_selector = "$('connect_to_facebook_form')"
12
+ when :jquery
13
+ js_selector = "jQuery('#connect_to_facebook_form')"
14
+ end
15
+
16
+ output = "<form id='connect_to_facebook_form' method='post' action='/#{options[:controller]}'>\n"
17
+ output << "<input type='hidden' name='authenticity_token' value='#{form_authenticity_token}'/>\n"
18
+ output << "</form>\n"
19
+ output << "<script type='text/javascript' charset='utf-8'>\n"
20
+ output << " function connect_to_facebook() {\n"
21
+ output << " #{js_selector}.submit();\n"
22
+ output << " }\n"
23
+ output << "</script>\n"
24
+ options.delete(:controller)
25
+ output << fb_login_button("connect_to_facebook()", options)
26
+ output
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,156 @@
1
+ module AuthlogicFacebookConnect
2
+ module Session
3
+ def self.included(klass)
4
+ klass.class_eval do
5
+ extend Config
6
+ include Methods
7
+ end
8
+ end
9
+
10
+ module Config
11
+ # Should the user be saved with our without validations?
12
+ #
13
+ # The default behavior is to save the user without validations and then
14
+ # in an application specific interface ask for the additional user
15
+ # details to make the user valid as facebook just provides a facebook id.
16
+ #
17
+ # This is useful if you do want to turn on user validations, maybe if you
18
+ # just have facebook connect as an additional authentication solution and
19
+ # you already have valid users.
20
+ #
21
+ # * <tt>Default:</tt> true
22
+ # * <tt>Accepts:</tt> Boolean
23
+ def facebook_valid_user(value = nil)
24
+ rw_config(:facebook_valid_user, value, false)
25
+ end
26
+ alias_method :facebook_valid_user=, :facebook_valid_user
27
+
28
+ # What user field should be used for the facebook UID?
29
+ #
30
+ # This is useful if you want to use a single field for multiple types of
31
+ # alternate user IDs, e.g. one that handles both OpenID identifiers and
32
+ # facebook ids.
33
+ #
34
+ # * <tt>Default:</tt> :facebook_uid
35
+ # * <tt>Accepts:</tt> Symbol
36
+ def facebook_uid_field(value = nil)
37
+ rw_config(:facebook_uid_field, value, :facebook_uid)
38
+ end
39
+ alias_method :facebook_uid_field=, :facebook_uid_field
40
+
41
+ # What session key field should be used for the facebook session key
42
+ #
43
+ #
44
+ # * <tt>Default:</tt> :facebook_session_key
45
+ # * <tt>Accepts:</tt> Symbol
46
+ def facebook_session_key_field(value = nil)
47
+ rw_config(:facebook_session_key_field, value, :facebook_session_key)
48
+ end
49
+ alias_method :facebook_session_key_field=, :facebook_session_key_field
50
+
51
+ # Class representing facebook users we want to authenticate against
52
+ #
53
+ # * <tt>Default:</tt> klass
54
+ # * <tt>Accepts:</tt> Class
55
+ def facebook_user_class(value = nil)
56
+ rw_config(:facebook_user_class, value, klass)
57
+ end
58
+ alias_method :facebook_user_class=, :facebook_user_class
59
+
60
+ # Should a new user creation be skipped if there is no user with given facebook uid?
61
+ #
62
+ # The default behavior is not to skip (hence create new user). You may want to turn it on
63
+ # if you want to try with different model.
64
+ #
65
+ # * <tt>Default:</tt> false
66
+ # * <tt>Accepts:</tt> Boolean
67
+ def facebook_skip_new_user_creation(value = nil)
68
+ rw_config(:facebook_skip_new_user_creation, value, false)
69
+ end
70
+ alias_method :facebook_skip_new_user_creation=, :facebook_skip_new_user_creation
71
+ end
72
+
73
+ module Methods
74
+ def self.included(klass)
75
+ klass.class_eval do
76
+ validate :validate_by_facebook_connect, :if => :authenticating_with_facebook_connect?
77
+ end
78
+
79
+ def credentials=(value)
80
+ # TODO: Is there a nicer way to tell Authlogic that we don't have any credentials than this?
81
+ values = [:facebook_connect]
82
+ super
83
+ end
84
+ end
85
+
86
+ def validate_by_facebook_connect
87
+ facebook_session = controller.facebook_session
88
+ self.attempted_record = facebook_user_class.find(:first, :conditions => { facebook_uid_field => facebook_session.user.uid })
89
+
90
+ if self.attempted_record
91
+ self.attempted_record.send(:"#{facebook_session_key_field}=", facebook_session.session_key)
92
+ self.attempted_record.save
93
+ end
94
+
95
+ unless self.attempted_record || facebook_skip_new_user_creation
96
+ begin
97
+ # Get the user from facebook and create a local user.
98
+ #
99
+ # We assign it after the call to new in case the attribute is protected.
100
+
101
+ if klass == facebook_user_class
102
+ new_user.send(:"#{facebook_uid_field}=", facebook_session.user.uid)
103
+ new_user.send(:"#{facebook_session_key_field}=", facebook_session.session_key)
104
+ else
105
+ new_user.send(:"build_#{facebook_user_class.to_s.underscore}", :"#{facebook_uid_field}" => facebook_session.user.uid, :"#{facebook_session_key_field}" => facebook_session.session_key)
106
+ end
107
+
108
+ new_user.before_connect(facebook_session) if new_user.respond_to?(:before_connect)
109
+
110
+ self.attempted_record = new_user
111
+
112
+ if facebook_valid_user
113
+ errors.add_to_base(
114
+ I18n.t('error_messages.facebook_user_creation_failed',
115
+ :default => 'There was a problem creating a new user ' +
116
+ 'for your Facebook account')) unless self.attempted_record.valid?
117
+
118
+ self.attempted_record = nil
119
+ else
120
+ self.attempted_record.save_with_validation(false)
121
+ end
122
+ rescue Facebooker::Session::SessionExpired
123
+ errors.add_to_base(I18n.t('error_messages.facebooker_session_expired',
124
+ :default => "Your Facebook Connect session has expired, please reconnect."))
125
+ end
126
+ end
127
+ end
128
+
129
+ def authenticating_with_facebook_connect?
130
+ controller.set_facebook_session
131
+ attempted_record.nil? && errors.empty? && controller.facebook_session
132
+ end
133
+
134
+ private
135
+ def facebook_valid_user
136
+ self.class.facebook_valid_user
137
+ end
138
+
139
+ def facebook_uid_field
140
+ self.class.facebook_uid_field
141
+ end
142
+
143
+ def facebook_session_key_field
144
+ self.class.facebook_session_key_field
145
+ end
146
+
147
+ def facebook_user_class
148
+ self.class.facebook_user_class
149
+ end
150
+
151
+ def facebook_skip_new_user_creation
152
+ self.class.facebook_skip_new_user_creation
153
+ end
154
+ end
155
+ end
156
+ end
File without changes
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: secobarbital-authlogic_facebook_connect
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 1
7
+ - 0
8
+ - 0
9
+ version: 1.0.0
10
+ platform: ruby
11
+ authors:
12
+ - Joakim Ekberg
13
+ - Seggy Umboh
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-03-19 00:00:00 -07:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: authlogic
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: facebooker
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ segments:
41
+ - 0
42
+ version: "0"
43
+ type: :runtime
44
+ version_requirements: *id002
45
+ description:
46
+ email:
47
+ - jocke.ekberg@gmail.com
48
+ - sumboh@thingbuzz.com
49
+ executables: []
50
+
51
+ extensions: []
52
+
53
+ extra_rdoc_files:
54
+ - README.rdoc
55
+ files:
56
+ - README.rdoc
57
+ - Rakefile
58
+ - Version
59
+ - init.rb
60
+ - lib/authlogic_facebook_connect.rb
61
+ - lib/authlogic_facebook_connect/acts_as_authentic.rb
62
+ - lib/authlogic_facebook_connect/helper.rb
63
+ - lib/authlogic_facebook_connect/session.rb
64
+ - lib/authlogic_facebook_connect/version.rb
65
+ has_rdoc: true
66
+ homepage: http://github.com/kalasjocke/authlogic_facebook_connect
67
+ licenses: []
68
+
69
+ post_install_message:
70
+ rdoc_options:
71
+ - --charset=UTF-8
72
+ require_paths:
73
+ - lib
74
+ required_ruby_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ segments:
79
+ - 0
80
+ version: "0"
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ segments:
86
+ - 0
87
+ version: "0"
88
+ requirements: []
89
+
90
+ rubyforge_project:
91
+ rubygems_version: 1.3.6
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: Extension of the Authlogic library to add Facebook Connect support built upon the excellent facebooker gem
95
+ test_files: []
96
+