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

Dmitry Chernov, 12 Jul 2013 09:54

Download (4.87 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
        def fixed_open_id_authenticate(openid_url)
26
            options = [ :nickname, :fullname, :email ]
27

    
28
            # Google OpenID
29
            options << OPENID_AX_EMAIL     # mail
30
            options << OPENID_AX_FIRSTNAME # firstname
31
            options << OPENID_AX_LASTNAME  # lastname
32
            options << OPENID_AX_LANGUAGE  # language
33

    
34
            authenticate_with_open_id(openid_url, :required => options, :return_to => signin_url, :method => :post) do |result, identity_url, registration|
35
                if result.successful?
36
                    user = User.find_or_initialize_by_identity_url(identity_url)
37

    
38
                    if user.new_record? 
39
                        redirect_to(home_url) && return unless Setting.self_registration?
40

    
41
                       if Rails::VERSION::MAJOR >= 3
42
                           ax_registration = OpenID::AX::FetchResponse.from_success_response(request.env[Rack::OpenID::RESPONSE])
43
                       else
44
                           ax_registration = registration
45
                       end
46

    
47
                        if ax_registration[OPENID_AX_EMAIL]
48
                            userByEmail = User.find_by_mail(ax_registration[OPENID_AX_EMAIL].first)                                    
49
                            if userByEmail
50
                              user = userByEmail
51
                              user.identity_url = identity_url
52
                              user.save()
53
                            else
54
                              user.login = ax_registration[OPENID_AX_EMAIL].first
55
                              user.mail = ax_registration[OPENID_AX_EMAIL].first
56
                            end
57
                        else
58
                            user.login = ax_registration['nickname'] unless ax_registration['nickname'].nil?
59
                            user.mail = ax_registration['email'] unless ax_registration['email'].nil?
60
                        end
61
                        user.firstname, user.lastname = ax_registration['fullname'].split(' ') unless ax_registration['fullname'].nil?
62
                        if ax_registration[OPENID_AX_FIRSTNAME]
63
                            user.firstname = ax_registration[OPENID_AX_FIRSTNAME].first
64
                        end
65
                        if ax_registration[OPENID_AX_LASTNAME]
66
                            user.lastname = ax_registration[OPENID_AX_LASTNAME].first
67
                        end
68
                        if ax_registration[OPENID_AX_LANGUAGE]
69
                            lang = ax_registration[OPENID_AX_LANGUAGE].first
70
                            if lang
71
                                language = find_language(lang) || find_language(lang.split('-').first)
72
                                if language
73
                                    user.language = language.to_s
74
                                    set_language_if_valid(language)
75
                                end
76
                            end
77
                        end
78
                        user.random_password
79
                        user.register
80

    
81
                        settings = Setting.plugin_openid_fix
82

    
83
                        if settings[:self_registration].nil?
84
                            self_registration = Setting.self_registration == '2' ? '2' : '3'
85
                        elsif settings[:self_registration].empty?
86
                            self_registration = Setting.self_registration
87
                        else
88
                            self_registration = settings[:self_registration]
89
                        end
90

    
91
                        case self_registration
92
                        when '1'
93
                            register_by_email_activation(user) do
94
                                onthefly_creation_failed(user)
95
                            end
96
                        when '3'
97
                            register_automatically(user) do
98
                                onthefly_creation_failed(user)
99
                            end
100
                        else
101
                            register_manually_by_administrator(user) do
102
                                onthefly_creation_failed(user)
103
                            end
104
                        end
105
                    else
106
                        if user.active?
107
                            successful_authentication(user)
108
                        else
109
                            account_pending
110
                        end
111
                    end
112
                end
113
            end
114
        end
115

    
116
    end
117

    
118
end
Terms of use | Privacy policy