24
25
26
27
28
29
30
31
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
58
59
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
# File 'app/controllers/panda/core/admin/sessions_controller.rb', line 24
def create
auth = request.env["omniauth.auth"]
provider_path = params[:provider]&.to_sym
provider = find_provider_by_path(provider_path)
resolved = Core.config.resolved_authentication_providers(request)
unless provider && resolved.key?(provider)
redirect_to admin_login_path, flash: {error: "Authentication provider not enabled"}
return
end
user = User.find_or_create_from_auth_hash(auth)
if user.persisted?
if Core.config.authentication_validator
validation_error = Core.config.authentication_validator.call(user, auth, request)
if validation_error
redirect_to admin_login_path, flash: {error: validation_error}
return
end
end
policy = Panda::Core.config.authorization_policy
unless user.admin? || policy.call(user, :access_admin, nil)
flash[:error] = "You do not have permission to access the admin area"
flash.keep(:error) if Rails.env.test?
redirect_to admin_login_path
return
end
session[Panda::Core::ADMIN_SESSION_KEY] = user.id
Panda::Core::Current.user = user
user.track_login!(request) if user.respond_to?(:track_login!)
Panda::Core::UserActivity.log!(user: user, action: "login", request: request) if defined?(Panda::Core::UserActivity)
ActiveSupport::Notifications.instrument("panda.core.user_login",
user: user,
provider: provider)
post_auth_path = session.delete(:post_auth_redirect_path)
if (redirect_proc = Panda::Core.config.post_authentication_redirect)
begin
redirect_url = redirect_proc.call(user, request)
if redirect_url.present?
redirect_to append_admin_path_suffix(redirect_url, post_auth_path), allow_other_host: true, flash: {success: "Successfully logged in as #{user.name}"}
return
end
rescue => e
Rails.logger.error "post_authentication_redirect hook failed: #{e.class}: #{e.message}"
end
end
origin = session.delete(:origin_subdomain)
if origin.present? && request.host.split(".").first != origin
scheme = request.scheme
base_domain = request.host.sub(/\A[^.]+\./, "")
port = request.port
port_suffix = [80, 443].include?(port) ? "" : ":#{port}"
base_url = "#{scheme}://#{origin}.#{base_domain}#{port_suffix}#{Core.config.admin_path}"
redirect_to append_admin_path_suffix(base_url, post_auth_path), allow_other_host: true, flash: {success: "Successfully logged in as #{user.name}"}
return
end
redirect_path = Panda::Core.config.dashboard_redirect_path || admin_root_path
redirect_path = redirect_path.call if redirect_path.respond_to?(:call)
redirect_to redirect_path, flash: {success: "Successfully logged in as #{user.name}"}
else
redirect_to admin_login_path, flash: {error: "Unable to create account: #{user.errors.full_messages.join(", ")}"}
end
rescue => e
Rails.logger.error "Authentication error: #{e.message}"
redirect_to admin_login_path, flash: {error: "Authentication failed: #{e.message}"}
end
|