I'm looking for a part-time remote job.

Hire me


I'm the author of:

Mastering Redmine is a comprehensive guide with tips, tricks and best practices, and an easy-to-learn structure.

Check the book's project or

Buy the book

Social pages of the book:

By buying this book you also donate to Redmine (see this page).


Follow me:

openid_account_controller_patch.rb

Leonid Titov, 11 Jul 2013 09:04

Download (6.73 KB)

 
1
require_dependency 'account_controller'
2

    
3
module OpenidAccountControllerPatch
4

    
5
    def self.included(base)
6
        base.extend(ClassMethods)
7
        base.send(:include, InstanceMethods)
8
        base.class_eval do
9
            unloadable
10

    
11
            alias_method :open_id_authenticate, :fixed_open_id_authenticate
12
        end
13
    end
14

    
15
    module ClassMethods
16
    end
17

    
18
    module InstanceMethods
19

    
20
        OPENID_AX_EMAIL     = 'http://axschema.org/contact/email'
21
        OPENID_AX_FIRSTNAME = 'http://axschema.org/namePerson/first'
22
        OPENID_AX_LASTNAME  = 'http://axschema.org/namePerson/last'
23
        OPENID_AX_LANGUAGE  = 'http://axschema.org/pref/language'
24

    
25
        # Original function
26
        #def open_id_authenticate(openid_url)
27
        #    authenticate_with_open_id(openid_url, :required => [ :nickname, :fullname, :email ], :return_to => signin_url) do |result, identity_url, registration|
28
        #        if result.successful?
29
        #            user = User.find_or_initialize_by_identity_url(identity_url)
30
        #            if user.new_record?
31
        #                redirect_to(home_url) && return unless Setting.self_registration?
32
        #
33
        #                user.login = registration['nickname'] unless registration['nickname'].nil?
34
        #                user.mail = registration['email'] unless registration['email'].nil?
35
        #                user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil?
36
        #                user.random_password
37
        #                user.register
38
        #
39
        #                case Setting.self_registration
40
        #                when '1'
41
        #                    register_by_email_activation(user) do
42
        #                        onthefly_creation_failed(user)
43
        #                    end
44
        #                when '3'
45
        #                    register_automatically(user) do
46
        #                        onthefly_creation_failed(user)
47
        #                    end
48
        #                else
49
        #                    register_manually_by_administrator(user) do
50
        #                        onthefly_creation_failed(user)
51
        #                    end
52
        #                end
53
        #            else
54
        #                if user.active?
55
        #                    successful_authentication(user)
56
        #                else
57
        #                    account_pending
58
        #                end
59
        #            end
60
        #        end
61
        #    end
62
        #end
63

    
64
        def fixed_open_id_authenticate(openid_url)
65
            options = [ :nickname, :fullname, :email ]
66

    
67
            # Google OpenID
68
            options << OPENID_AX_EMAIL     # mail
69
            options << OPENID_AX_FIRSTNAME # firstname
70
            options << OPENID_AX_LASTNAME  # lastname
71
            options << OPENID_AX_LANGUAGE  # language
72

    
73
            authenticate_with_open_id(openid_url, :required => options, :return_to => signin_url, :method => :post) do |result, identity_url, registration|
74
                if result.successful?
75
                    user = User.find_or_initialize_by_identity_url(identity_url)
76
                    
77
                    if user.new_record? 
78
                        redirect_to(home_url) && return unless Setting.self_registration?
79

    
80
                        if Rails::VERSION::MAJOR >= 3
81
                            ax_registration = OpenID::AX::FetchResponse.from_success_response(request.env[Rack::OpenID::RESPONSE])
82
                        else
83
                            ax_registration = registration
84
                        end
85

    
86
                        if ax_registration[OPENID_AX_EMAIL]
87
                            userByEmail = User.find_by_mail(registration[OPENID_AX_EMAIL].first)                                    
88
                            if userByEmail
89
                              user = userByEmail
90
                              user.identity_url = identity_url
91
                              user.save()
92
                            else
93
                              user.login = ax_registration[OPENID_AX_EMAIL].first
94
                              user.mail = ax_registration[OPENID_AX_EMAIL].first
95
                            end
96
                        else
97
                            user.login = registration['nickname'] unless registration['nickname'].nil?
98
                            user.mail = registration['email'] unless registration['email'].nil?
99
                        end
100
                        user.firstname, user.lastname = registration['fullname'].split(' ') unless registration['fullname'].nil?
101
                        if ax_registration[OPENID_AX_FIRSTNAME]
102
                            user.firstname = ax_registration[OPENID_AX_FIRSTNAME].first
103
                        end
104
                        if ax_registration[OPENID_AX_LASTNAME]
105
                            user.lastname = ax_registration[OPENID_AX_LASTNAME].first
106
                        end
107
                        if ax_registration[OPENID_AX_LANGUAGE]
108
                            lang = ax_registration[OPENID_AX_LANGUAGE].first
109
                            if lang
110
                                language = find_language(lang) || find_language(lang.split('-').first)
111
                                if language
112
                                    user.language = language.to_s
113
                                    set_language_if_valid(language)
114
                                end
115
                            end
116
                        end
117
                        user.random_password
118
                        user.register
119

    
120
                        settings = Setting.plugin_openid_fix
121

    
122
                        if settings[:self_registration].nil?
123
                            self_registration = Setting.self_registration == '2' ? '2' : '3'
124
                        elsif settings[:self_registration].empty?
125
                            self_registration = Setting.self_registration
126
                        else
127
                            self_registration = settings[:self_registration]
128
                        end
129

    
130
                        case self_registration
131
                        when '1'
132
                            register_by_email_activation(user) do
133
                                onthefly_creation_failed(user)
134
                            end
135
                        when '3'
136
                            register_automatically(user) do
137
                                onthefly_creation_failed(user)
138
                            end
139
                        else
140
                            register_manually_by_administrator(user) do
141
                                onthefly_creation_failed(user)
142
                            end
143
                        end
144
                    else
145
                        if user.active?
146
                            successful_authentication(user)
147
                        else
148
                            account_pending
149
                        end
150
                    end
151
                end
152
            end
153
        end
154

    
155
    end
156

    
157
end
Terms of use | Privacy policy