Module: YiffSpace::Auth::Helper
Defined Under Namespace
Modules: ClassMethods, Scoped
Instance Method Summary
collapse
Instance Method Details
#auth ⇒ Object
52
53
54
55
56
57
|
# File 'lib/yiffspace/auth/helper.rb', line 52
def auth
return spoofed_auth if spoofing?
return AuthInfo::Anonymous.instance if auth_raw.blank?
AuthInfo.from_session(auth_raw)
end
|
#auth=(value) ⇒ Object
63
64
65
66
|
# File 'lib/yiffspace/auth/helper.rb', line 63
def auth=(value)
value = nil if value.is_a?(AuthInfo::Anonymous)
write_session_cache(auth_client_config.auth_session_key, value&.to_session)
end
|
#auth? ⇒ Boolean
59
60
61
|
# File 'lib/yiffspace/auth/helper.rb', line 59
def auth?
spoofing? || (auth_raw.present? && !auth.anonymous?)
end
|
#auth_client_config ⇒ Object
Returns the Auth::Client for the current request. In auth engine controllers this is
resolved from the routing default set by Engine.for; in host app controllers it falls
back to the default registered client. Override in your controller to choose a specific
client when multiple are registered.
158
159
160
161
|
# File 'lib/yiffspace/auth/helper.rb', line 158
def auth_client_config
client_name = self.client_name
client_name.present? ? YiffSpace::Auth[client_name.to_sym] : YiffSpace::Auth.default
end
|
#auth_raw ⇒ Object
48
49
50
|
# File 'lib/yiffspace/auth/helper.rb', line 48
def auth_raw
read_session_cache(auth_client_config.auth_session_key)
end
|
#client_name ⇒ Object
163
164
165
|
# File 'lib/yiffspace/auth/helper.rb', line 163
def client_name
respond_to?(:request, true) && request.env[CLIENT_NAME_ENV]
end
|
#client_name=(value) ⇒ Object
167
168
169
|
# File 'lib/yiffspace/auth/helper.rb', line 167
def client_name=(value)
request.env[CLIENT_NAME_ENV] = value.to_sym
end
|
#full_reset! ⇒ Object
96
97
98
99
|
# File 'lib/yiffspace/auth/helper.rb', line 96
def full_reset!
reset_auth!
reset_user!
end
|
#has_permission?(name) ⇒ Boolean
109
110
111
112
113
|
# File 'lib/yiffspace/auth/helper.rb', line 109
def has_permission?(name)
return false unless logged_in?
auth.permissions.has?(name)
end
|
#logged_in? ⇒ Boolean
105
106
107
|
# File 'lib/yiffspace/auth/helper.rb', line 105
def logged_in?
auth? && user?
end
|
#require_auth(path) ⇒ Object
101
102
103
|
# File 'lib/yiffspace/auth/helper.rb', line 101
def require_auth(path)
redirect_to(path) unless logged_in?
end
|
#reset_auth! ⇒ Object
68
69
70
|
# File 'lib/yiffspace/auth/helper.rb', line 68
def reset_auth!
write_session_cache(auth_client_config.auth_session_key, nil)
end
|
#reset_user! ⇒ Object
92
93
94
|
# File 'lib/yiffspace/auth/helper.rb', line 92
def reset_user!
write_session_cache(auth_client_config.user_session_key, nil)
end
|
#spoof_override ⇒ Object
Per-request override set via the disable_spoof_auth/override_spoof_auth class macros, or
by calling this setter directly (e.g. from a custom before_action, for values that can
only be computed per-request). false disables spoofing; a Hash overrides individual
:spoof_user_id/:spoof_permissions/:spoof_roles values; nil (the default) defers entirely
to the client's configuration.
176
177
178
|
# File 'lib/yiffspace/auth/helper.rb', line 176
def spoof_override
respond_to?(:request, true) && request.env[SPOOF_OVERRIDE_ENV]
end
|
#spoof_override=(value) ⇒ Object
180
181
182
|
# File 'lib/yiffspace/auth/helper.rb', line 180
def spoof_override=(value)
request.env[SPOOF_OVERRIDE_ENV] = value
end
|
#sync_auth_if_dirty! ⇒ Object
Checks the dirty flag written by the Logto webhook handler. If set, re-fetches
the user's current roles and permissions from the Logto Management API and
rewrites the session — without waiting for the access token to expire.
Call this as a before_action in any controller that needs instant revocation.
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
# File 'lib/yiffspace/auth/helper.rb', line 119
def sync_auth_if_dirty!
return if spoofing?
return unless auth?
flag_key = format(DIRTY_FLAG_KEY, auth.id)
return unless Rails.cache.exist?(flag_key)
Rails.cache.delete(flag_key)
management = auth_client_config.logto_management
api_user = management.get_user_by_id(auth.id)
if api_user.nil? || api_user.data["isSuspended"]
full_reset!
return
end
roles = management.get_user_roles(auth.id)
permissions = roles.flat_map { |role| management.get_role_scopes(role["id"]) }
.pluck("name")
.uniq
self.auth = AuthInfo.new(
id: auth.id,
token: auth.token,
roles: roles.pluck("name"),
permissions: permissions,
client_id: auth.client_id,
)
end
|
#url_helpers ⇒ Object
150
151
152
|
# File 'lib/yiffspace/auth/helper.rb', line 150
def url_helpers
YiffSpace::Auth::Engine.for(client_name).routes.url_helpers
end
|
#user ⇒ Object
76
77
78
79
80
81
|
# File 'lib/yiffspace/auth/helper.rb', line 76
def user
return spoofed_user if spoofing?
return UserInfo::Anonymous.instance if user_raw.blank?
UserInfo.from_session(user_raw)
end
|
#user=(value) ⇒ Object
87
88
89
90
|
# File 'lib/yiffspace/auth/helper.rb', line 87
def user=(value)
value = nil if value.is_a?(UserInfo::Anonymous)
write_session_cache(auth_client_config.user_session_key, value&.to_session)
end
|
#user? ⇒ Boolean
83
84
85
|
# File 'lib/yiffspace/auth/helper.rb', line 83
def user?
spoofing? || (user_raw.present? && !user.anonymous?)
end
|
#user_raw ⇒ Object
72
73
74
|
# File 'lib/yiffspace/auth/helper.rb', line 72
def user_raw
read_session_cache(auth_client_config.user_session_key)
end
|