1
1
class CommentsController < ApplicationController
2
2
skip_before_filter :verify_authenticity_token , :only => :create
3
- before_filter :verify_authenticity_token_unless_openid , :only => :create
3
+ before_filter :verify_authenticity_token_unless_using_openid , :only => :create
4
4
5
5
include UrlHelper
6
- OPEN_ID_ERRORS = {
7
- :missing => "Sorry, the OpenID server couldn't be found" ,
8
- :canceled => "OpenID verification was canceled" ,
9
- :failed => "Sorry, the OpenID verification failed" }
10
6
11
7
before_filter :find_post , :except => [ :new ]
12
8
@@ -26,53 +22,44 @@ def new
26
22
27
23
# TODO: Spec OpenID with cucumber and rack-my-id
28
24
def create
29
- @comment = Comment . new ( ( session [ :pending_comment ] || comment_params || { } ) .
30
- reject { |key , value | !Comment . protected_attribute? ( key ) } )
25
+ @comment = Comment . new ( ( session [ :pending_comment ] || comment_params || { } ) .
26
+ reject { |key , value | !Comment . protected_attribute? ( key ) } )
27
+
31
28
@comment . post = @post
32
29
33
- session [ :pending_comment ] = nil
34
-
35
- if @comment . requires_openid_authentication?
36
- session [ :pending_comment ] = comment_params
37
- authenticate_with_open_id ( @comment . author ,
38
- :optional => [ :nickname , :fullname , :email ]
39
- ) do |result , identity_url , registration |
40
- if result . status == :successful
41
- @comment . post = @post
42
-
43
- @comment . author_url = @comment . author
44
- @comment . author = (
45
- registration [ "fullname" ] ||
46
- registration [ "nickname" ] ||
47
- @comment . author_url
48
- ) . to_s
49
- @comment . author_email = (
50
- registration [ "email" ] ||
51
- @comment . author_url
52
- ) . to_s
53
-
54
- @comment . openid_error = ""
55
- session [ :pending_comment ] = nil
56
- else
57
- @comment . openid_error = OPEN_ID_ERRORS [ result . status ]
58
- end
59
- end
60
- else
30
+ if !@comment . requires_openid_authentication?
61
31
@comment . blank_openid_fields
62
- end
63
-
64
- # #authenticate_with_open_id may have already provided a response
65
- unless response . headers [ Rack ::OpenID ::AUTHENTICATE_HEADER ]
66
- if @comment . save
67
- redirect_to post_path ( @post )
68
- else
69
- render :template => 'posts/show'
32
+ save_comment_or_show_error
33
+ else
34
+ if request . env [ 'omniauth.auth' ] . nil? && params [ :message ] . blank? # Begin auth.
35
+ session [ :pending_comment ] = comment_params
36
+ session [ :post_id ] = @post . id
37
+ redirect_to auth_path ( :open_id_comment , "openid_url=#{ @comment . author } " )
38
+ elsif !request . env [ 'omniauth.auth' ] . nil? # Process success response.
39
+ @comment . author_url = request . env [ 'omniauth.auth' ] [ :uid ]
40
+ @comment . author = request . env [ 'omniauth.auth' ] [ :info ] [ :name ]
41
+ @comment . author_email = request . env [ 'omniauth.auth' ] [ :info ] [ :email ] || ''
42
+ @comment . openid_error = ''
43
+ save_comment_or_show_error
44
+ else # Process error response.
45
+ @comment . openid_error = params [ :message ]
46
+ save_comment_or_show_error
70
47
end
71
48
end
72
49
end
73
50
74
51
private
75
52
53
+ def save_comment_or_show_error
54
+ if @comment . save
55
+ session [ :pending_comment ] = nil
56
+ session [ :post_id ] = nil
57
+ redirect_to post_path ( @post )
58
+ else
59
+ render :template => 'posts/show'
60
+ end
61
+ end
62
+
76
63
def comment_params
77
64
params . require ( :comment ) . permit ( :author , :body )
78
65
end
@@ -83,9 +70,21 @@ def find_post
83
70
@post = Post . find_by_permalink ( *[ :year , :month , :day , :slug ] . map { |x |
84
71
params [ x ]
85
72
} )
73
+
74
+ rescue ActiveRecord ::RecordNotFound
75
+ @post = Post . find ( session [ :post_id ] )
86
76
end
87
77
88
- def verify_authenticity_token_unless_openid
78
+ def verify_authenticity_token_unless_using_openid
89
79
verify_authenticity_token unless using_open_id?
90
80
end
81
+
82
+ def using_open_id?
83
+ if !request . env [ 'omniauth.auth' ] . nil? &&
84
+ request . env [ 'omniauth.auth' ] [ :provider ] == OMNIAUTH_OPEN_ID_COMMENT_STRATEGY
85
+ return true
86
+ end
87
+
88
+ return false
89
+ end
91
90
end
0 commit comments