GitHub Sale: sign up for any paid plan this week and pay nothing until January 1, 2009!  [ hide ]

public
Description: Open Source Ruby on Rails Project Gallery
Homepage: http://www.opensourcerails.com
Clone URL: git://github.com/jcnetdev/opensourcerails.git
Adding OpenID
jcnetdev (author)
Sat Aug 30 20:00:30 -0700 2008
commit  026e51778e63e215be1bbcb5333df67e842a58e6
tree    de09c5ab72e83b03aee19456d3bcc696deb9cc59
parent  7779c6c75fee5c510b46c4528e5e32e51bf4d783
...
60
61
62
63
 
64
65
66
67
68
 
 
 
 
 
 
69
70
71
72
73
 
 
 
 
74
 
 
 
 
75
76
77
...
60
61
62
 
63
64
 
 
 
 
65
66
67
68
69
70
71
 
 
 
72
73
74
75
76
77
78
79
80
81
82
83
84
0
@@ -60,18 +60,25 @@ class SessionsController < ApplicationController
0
   end
0
 
0
   def open_id_authentication(openid_url)
0
- authenticate_with_open_id(openid_url, :required => [:email]) do |result, identity_url, registration|
0
+ authenticate_with_open_id(openid_url, :required => [:nickname, :email]) do |result, identity_url, registration|
0
       if result.successful?
0
- @openid_user = User.find_or_initialize_by_identity_url(identity_url)
0
- if @openid_user.new_record?
0
- @openid_user.login = registration['nickname']
0
- @openid_user.login = "anon_" + rand(99999).to_s if @openid_user.login.blank?
0
+ @openid_user = User.find_by_identity_url(identity_url)
0
+ unless @openid_user
0
+ @openid_user = current_or_anon_user
0
+ @openid_user.identity_url = identity_url
0
+ @openid_user.login = registration['nickname'] if @openid_user[:login].blank?
0
+ @openid_user.login = "user_" + rand(99999).to_s if @openid_user[:login].blank?
0
           @openid_user.email = registration['email']
0
- passwd = rand_passwd
0
- @openid_user.password = passwd
0
- @openid_user.password_confirmation = passwd
0
           @openid_user.ip_address = request.remote_ip
0
+ if @openid_user.password.blank?
0
+ @openid_user.password = @openid_user.password_confirmation = rand_passwd
0
+ end
0
+ @openid_user.signed_up = true
0
           @openid_user.save!
0
+
0
+ @openid_user.register!
0
+ @openid_user.activate!
0
+
0
         end
0
         self.current_user = @openid_user
0
         successful_login
...
12
13
14
 
15
16
17
...
23
24
25
26
 
27
28
29
...
34
35
36
37
 
38
39
40
...
58
59
60
61
 
62
63
64
...
192
193
194
195
196
197
 
198
199
 
200
201
202
203
204
 
 
 
 
 
 
205
206
207
...
12
13
14
15
16
17
18
...
24
25
26
 
27
28
29
30
...
35
36
37
 
38
39
40
41
...
59
60
61
 
62
63
64
65
...
193
194
195
 
 
 
196
197
 
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
0
@@ -12,6 +12,7 @@ class UsersController < ApplicationController
0
       @user = anon_user
0
     end
0
   end
0
+
0
   def create
0
     cookies.delete :auth_token
0
     # protects against session fixation attacks, wreaks havoc with
0
@@ -23,7 +24,7 @@ class UsersController < ApplicationController
0
     @user.signed_up = true
0
     @user.save!
0
     
0
- # register user
0
+ # register
0
     @user.register!
0
         
0
     self.current_user = @user
0
@@ -34,7 +35,7 @@ class UsersController < ApplicationController
0
   end
0
 
0
   def show
0
- @user = find_user
0
+ @user = find_user
0
     @bookmarked_projects = @user.projects.paginate(:page => params[:page], :per_page => AppConfig.bookmarks_per_page)
0
     @submitted_projects = @user.submitted
0
     @activities = @user.activities.all(:limit => 101, :order => "created_at DESC")
0
@@ -58,7 +59,7 @@ class UsersController < ApplicationController
0
   def edit
0
     @user = find_user
0
     return unless verify_owner(@user)
0
-
0
+
0
     respond_to do |format|
0
       format.html
0
       format.js do
0
@@ -192,16 +193,20 @@ class UsersController < ApplicationController
0
     end
0
   end
0
   
0
-
0
-
0
-protected
0
+ protected
0
   def find_user
0
- if(params[:id].to_s.include? "anon_")
0
+ if params[:id].to_s.include? "anon_"
0
       user_id = params[:id].gsub("anon_","").to_i
0
       @user = User.find_by_id(user_id)
0
     else
0
       @user = User.find_by_login(params[:id])
0
     end
0
+
0
+ if @user
0
+ return @user
0
+ else
0
+ raise ActiveRecord::RecordNotFound
0
+ end
0
   end
0
   
0
   # verify that the current user can edit this profile
...
32
33
34
 
35
36
37
38
39
40
41
42
43
44
 
 
 
 
 
 
 
 
 
 
 
45
46
47
48
49
 
 
 
 
 
50
51
52
...
184
185
186
 
187
188
189
190
191
...
268
269
270
 
 
 
 
 
 
 
 
 
 
 
 
271
272
273
...
296
297
298
299
 
300
301
302
...
313
314
315
316
 
317
318
319
...
32
33
34
35
36
37
 
 
 
 
 
 
 
 
38
39
40
41
42
43
44
45
46
47
48
49
 
 
 
 
50
51
52
53
54
55
56
57
...
189
190
191
192
193
 
194
195
196
...
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
...
313
314
315
 
316
317
318
319
...
330
331
332
 
333
334
335
336
0
@@ -32,21 +32,26 @@ class User < ActiveRecord::Base
0
   
0
   # Virtual attribute for the unencrypted password
0
   attr_accessor :password
0
+ attr_accessor :skip_email
0
 
0
   # basic info validations
0
- validates_presence_of :ip_address, :unless => :signed_up?
0
- validates_presence_of :login, :if => :signed_up?
0
- validates_presence_of :email, :if => :signed_up?
0
- validates_length_of :login, :within => 3..40, :if => :signed_up?
0
- validates_length_of :email, :within => 3..100, :if => :signed_up?
0
- validates_uniqueness_of :login, :email, :case_sensitive => false, :if => :signed_up?
0
- validates_as_email_address :email, :if => :signed_up?
0
-
0
+ validates_presence_of :login, :if => :signed_up?
0
+ validates_length_of :login, :within => 3..40, :if => :signed_up?, :allow_blank => true
0
+ validates_uniqueness_of :login, :case_sensitive => false, :if => :signed_up?, :allow_blank => true
0
+ validates_format_of :login, :with => /^\w+$/i, :message => "must only contain letters and numbers", :allow_blank => true
0
+
0
+ # set up email
0
+ validates_presence_of :email, :if => :email_required?
0
+ validates_length_of :email, :within => 3..100, :if => :email_required?
0
+ validates_uniqueness_of :email, :case_sensitive => false, :if => :email_required?
0
+ validates_as_email_address :email, :if => :email_required?
0
+
0
   # password validations
0
- validates_presence_of :password, :if => :password_required?
0
- validates_presence_of :password_confirmation, :if => :password_required?
0
- validates_length_of :password, :within => 4..40, :if => :password_required?
0
- validates_confirmation_of :password, :if => :password_required?
0
+ validates_presence_of :password, :if => :password_required?
0
+ validates_presence_of :password_confirmation, :if => :password_required?
0
+ validates_length_of :password, :within => 4..40, :if => :password_required?
0
+ validates_confirmation_of :password, :if => :password_required?
0
+
0
   before_save :encrypt_password
0
 
0
   # prevents a user from submitting a crafted form that bypasses activation
0
@@ -184,8 +189,8 @@ class User < ActiveRecord::Base
0
   end
0
   
0
   def to_s
0
+ return self.login unless self[:login].blank?
0
     return self.name unless self.name.blank?
0
- return self.login unless self.login.blank?
0
     return "Anonymous"
0
   end
0
   
0
@@ -268,6 +273,18 @@ class User < ActiveRecord::Base
0
     self.activities.each{|r| r.destroy}
0
   end
0
   
0
+ def open_id?
0
+ !self.identity_url.blank?
0
+ end
0
+
0
+ def login_editable?
0
+ self.open_id? and self.login.include?("user_")
0
+ end
0
+
0
+ def email_required?
0
+ signed_up? and !open_id?
0
+ end
0
+
0
   def self.clear_spam
0
     User.find(:all, :conditions => {:spammer => true}).each do |user|
0
       user.is_spammer!
0
@@ -296,7 +313,7 @@ class User < ActiveRecord::Base
0
     def do_register
0
       logger.debug("REGISTERING!")
0
 
0
- if AppConfig.require_email_activation
0
+ if AppConfig.require_email_activation and !self.skip_email
0
         send_activation_code
0
       else
0
         self.activate!
0
@@ -313,7 +330,7 @@ class User < ActiveRecord::Base
0
       self.activated_at = Time.now.utc
0
       self.deleted_at = self.activation_code = nil
0
       
0
- if AppConfig.require_email_activation
0
+ if AppConfig.require_email_activation and !self.skip_email
0
         UserMailer.deliver_activation_success(self)
0
       else
0
         UserMailer.deliver_signup_notification(self)
...
23
24
25
26
 
27
28
29
...
23
24
25
 
26
27
28
29
0
@@ -23,7 +23,7 @@ class UserMailer < ActionMailer::Base
0
   
0
   def tell_friend(user, friend)
0
     setup_email(friend.to)
0
- reply_to friend.from
0
+ reply_to friend.from unless friend.from.blank?
0
     
0
     subject AppConfig.tellafriend_subject
0
     body :tell_friend => friend, :current_user => user
...
8
9
10
 
 
 
11
12
13
...
8
9
10
11
12
13
14
15
16
0
@@ -8,6 +8,9 @@
0
 
0
 - build_form_for user, :html => html_options do |f|
0
   = f.error_messages
0
+ - if user.login_editable?
0
+ = f.text_field :login, :label => "Set your username"
0
+
0
   = f.text_field :name
0
   = f.text_field :email, :label => "Email (not displayed)"
0
   = f.text_field :homepage, :label => "Homepage"
...
10
11
12
13
 
 
14
15
16
...
26
27
28
 
 
 
 
29
30
31
...
10
11
12
 
13
14
15
16
17
...
27
28
29
30
31
32
33
34
35
36
0
@@ -10,7 +10,8 @@
0
         %p
0
           %strong Name:
0
           =h default(user.name, "[Not Entered]")
0
-
0
+
0
+
0
           = br
0
           %strong Email:
0
           =h default(user.email, "[Not Entered]")
0
@@ -26,6 +27,10 @@
0
             =h(user.profile)
0
 
0
           = br*2
0
+ - if user.login_editable?
0
+ %strong Set your username....
0
+ = br
0
+
0
           %strong
0
             = link_to "Edit Profile", edit_user_url(user), :class => "edit"
0
             |
...
9
10
11
12
 
13
14
15
...
183
184
185
 
186
187
188
...
9
10
11
 
12
13
14
15
...
183
184
185
186
187
188
189
0
@@ -9,7 +9,7 @@
0
 #
0
 # It's strongly recommended to check this file into your version control system.
0
 
0
-ActiveRecord::Schema.define(:version => 20080831003704) do
0
+ActiveRecord::Schema.define(:version => 20080831011932) do
0
 
0
   create_table "activities", :force => true do |t|
0
     t.string "user_name"
0
@@ -183,6 +183,7 @@ ActiveRecord::Schema.define(:version => 20080831003704) do
0
     t.string "homepage"
0
     t.integer "tell_friend_count", :limit => 11, :default => 0
0
     t.datetime "tell_friend_last_sent"
0
+ t.string "identity_url"
0
   end
0
 
0
   add_index "users", ["ip_address"], :name => "index_users_on_ip_address"
...
47
48
49
 
 
 
 
 
 
 
...
47
48
49
50
51
52
53
54
55
56
0
@@ -47,3 +47,10 @@
0
       color: #000000
0
       font-weight: bold
0
       background: url(/images/template/dotted_black.gif) repeat-x left bottom
0
+
0
+ #about_user
0
+ h3
0
+ padding-bottom: 15px
0
+
0
+ .clear
0
+ height: 0
...
150
151
152
153
154
155
156
157
158
...
150
151
152
 
 
 
153
154
155
0
@@ -150,9 +150,6 @@ module OpenIdAuthentication
0
     def open_id_redirect_url(open_id_request, return_to = nil, method = nil)
0
       open_id_request.return_to_args['_method'] = (method || request.method).to_s
0
       open_id_request.return_to_args['open_id_complete'] = '1'
0
- if (method || request.method).to_s != 'get'
0
- open_id_request.return_to_args[request_forgery_protection_token.to_s] = form_authenticity_token
0
- end
0
       open_id_request.redirect_url(root_url, return_to || requested_url)
0
     end
0
 

Comments

    No one has commented yet.