Class: Otto::Security::Authentication::StrategyResult
- Inherits:
-
Data
- Object
- Data
- Otto::Security::Authentication::StrategyResult
- Defined in:
- lib/otto/security/authentication/strategy_result.rb
Instance Attribute Summary collapse
-
#auth_method ⇒ Object
readonly
Returns the value of attribute auth_method.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#session ⇒ Object
readonly
Returns the value of attribute session.
-
#strategy_name ⇒ Object
readonly
Returns the value of attribute strategy_name.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
Class Method Summary collapse
-
.anonymous(metadata: {}, strategy_name: 'anonymous') ⇒ StrategyResult
Create an anonymous (unauthenticated) result.
Instance Method Summary collapse
-
#anonymous? ⇒ Boolean
Check if the request is anonymous (no user in session).
-
#auth_attempt_succeeded? ⇒ Boolean
Check if authentication strategy just executed and succeeded.
-
#authenticated? ⇒ Boolean
Check if the request has an authenticated user in session.
-
#has_any_permission?(*permissions) ⇒ Boolean
Check if the user has any of the specified permissions.
-
#has_any_role?(*roles) ⇒ Boolean
Check if the user has any of the specified roles.
-
#has_permission?(permission) ⇒ Boolean
Check if the user has a specific permission.
-
#has_role?(role) ⇒ Boolean
Check if the user has a specific role.
-
#inspect ⇒ String
Create a string representation for debugging.
-
#permissions ⇒ Array<String>
Get all user permissions as an array.
-
#roles ⇒ Array<String>
Get all user roles as an array.
-
#session_id ⇒ String?
Get session ID from various possible locations.
-
#to_h ⇒ Hash
Create a hash representation.
-
#user_context ⇒ Hash
Get user context - a hash containing user-specific information and metadata.
-
#user_id ⇒ String, ...
Get user ID from various possible locations.
-
#user_name ⇒ String?
Get user name from various possible locations.
Instance Attribute Details
#auth_method ⇒ Object (readonly)
Returns the value of attribute auth_method
24 25 26 |
# File 'lib/otto/security/authentication/strategy_result.rb', line 24 def auth_method @auth_method end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata
24 25 26 |
# File 'lib/otto/security/authentication/strategy_result.rb', line 24 def end |
#session ⇒ Object (readonly)
Returns the value of attribute session
24 25 26 |
# File 'lib/otto/security/authentication/strategy_result.rb', line 24 def session @session end |
#strategy_name ⇒ Object (readonly)
Returns the value of attribute strategy_name
24 25 26 |
# File 'lib/otto/security/authentication/strategy_result.rb', line 24 def strategy_name @strategy_name end |
#user ⇒ Object (readonly)
Returns the value of attribute user
24 25 26 |
# File 'lib/otto/security/authentication/strategy_result.rb', line 24 def user @user end |
Class Method Details
.anonymous(metadata: {}, strategy_name: 'anonymous') ⇒ StrategyResult
Create an anonymous (unauthenticated) result
Used by middleware for routes without auth requirements and by PublicStrategy for publicly accessible routes.
113 114 115 116 117 118 119 120 121 |
# File 'lib/otto/security/authentication/strategy_result.rb', line 113 def self.anonymous(metadata: {}, strategy_name: 'anonymous') new( session: {}, user: nil, auth_method: 'anonymous', metadata: , strategy_name: strategy_name ) end |
Instance Method Details
#anonymous? ⇒ Boolean
Check if the request is anonymous (no user in session)
156 157 158 |
# File 'lib/otto/security/authentication/strategy_result.rb', line 156 def anonymous? user.nil? end |
#auth_attempt_succeeded? ⇒ Boolean
Check if authentication strategy just executed and succeeded
This checks AUTH ATTEMPT OUTCOME, not just session state. Returns true only when:
-
Route had an auth=… requirement (not anonymous/public)
-
Auth strategy executed
-
Authentication succeeded (user authenticated)
149 150 151 |
# File 'lib/otto/security/authentication/strategy_result.rb', line 149 def auth_attempt_succeeded? authenticated? && auth_method.to_s != 'anonymous' end |
#authenticated? ⇒ Boolean
Check if the request has an authenticated user in session
This checks REQUEST STATE, not auth attempt outcome. Returns true if session contains a user, regardless of whether authentication just occurred or was from a previous request.
133 134 135 |
# File 'lib/otto/security/authentication/strategy_result.rb', line 133 def authenticated? !user.nil? end |
#has_any_permission?(*permissions) ⇒ Boolean
Check if the user has any of the specified permissions
215 216 217 |
# File 'lib/otto/security/authentication/strategy_result.rb', line 215 def (*) .flatten.any? { || () } end |
#has_any_role?(*roles) ⇒ Boolean
Check if the user has any of the specified roles
207 208 209 |
# File 'lib/otto/security/authentication/strategy_result.rb', line 207 def has_any_role?(*roles) roles.flatten.any? { |role| has_role?(role) } end |
#has_permission?(permission) ⇒ Boolean
Check if the user has a specific permission
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/otto/security/authentication/strategy_result.rb', line 184 def () return false unless authenticated? # Try user model methods first, fall back to hash access for backward compatibility if user.respond_to?(:has_permission?) user.() elsif user.respond_to?(:permissions) = user. || [] = [] unless .is_a?(Array) .map(&:to_s).include?(.to_s) elsif user.is_a?(Hash) = user[:permissions] || user['permissions'] || [] = [] unless .is_a?(Array) .map(&:to_s).include?(.to_s) else false end end |
#has_role?(role) ⇒ Boolean
Check if the user has a specific role
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/otto/security/authentication/strategy_result.rb', line 164 def has_role?(role) return false unless authenticated? # Try user model methods first, fall back to hash access for backward compatibility if user.respond_to?(:role) user.role.to_s == role.to_s elsif user.respond_to?(:has_role?) user.has_role?(role) elsif user.is_a?(Hash) user_role = user[:role] || user['role'] user_role.to_s == role.to_s else false end end |
#inspect ⇒ String
Create a string representation for debugging
289 290 291 292 293 294 295 |
# File 'lib/otto/security/authentication/strategy_result.rb', line 289 def inspect if authenticated? "#<StrategyResult authenticated user=#{user_name || user_id} roles=#{roles} method=#{auth_method}>" else "#<StrategyResult anonymous method=#{auth_method}>" end end |
#permissions ⇒ Array<String>
Get all user permissions as an array
278 279 280 281 282 283 284 |
# File 'lib/otto/security/authentication/strategy_result.rb', line 278 def return [] unless authenticated? perms = user[:permissions] || user['permissions'] || [] perms = [perms] unless perms.is_a?(Array) perms.map(&:to_s) end |
#roles ⇒ Array<String>
Get all user roles as an array
261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/otto/security/authentication/strategy_result.rb', line 261 def roles return [] unless authenticated? roles_data = user[:roles] || user['roles'] if roles_data.is_a?(Array) roles_data.map(&:to_s) elsif roles_data [roles_data.to_s] else role = user[:role] || user['role'] role ? [role.to_s] : [] end end |
#session_id ⇒ String?
Get session ID from various possible locations
254 255 256 |
# File 'lib/otto/security/authentication/strategy_result.rb', line 254 def session_id session[:id] || session['id'] || session[:session_id] || session['session_id'] end |
#to_h ⇒ Hash
Create a hash representation
321 322 323 324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/otto/security/authentication/strategy_result.rb', line 321 def to_h { session: session, user: user, auth_method: auth_method, metadata: , authenticated: authenticated?, auth_attempt_succeeded: auth_attempt_succeeded?, user_id: user_id, user_name: user_name, roles: roles, permissions: , } end |
#user_context ⇒ Hash
Get user context - a hash containing user-specific information and metadata
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/otto/security/authentication/strategy_result.rb', line 300 def user_context if authenticated? case auth_method when 'session' { user_id: user_id, session: session } else end else case auth_method when 'anonymous' {} else end end end |
#user_id ⇒ String, ...
Get user ID from various possible locations
222 223 224 225 226 227 228 229 230 231 232 233 |
# File 'lib/otto/security/authentication/strategy_result.rb', line 222 def user_id return nil unless authenticated? # Try user model methods first, fall back to hash access and session if user.respond_to?(:id) user.id elsif user.respond_to?(:user_id) user.user_id elsif user.is_a?(Hash) user[:id] || user['id'] || user[:user_id] || user['user_id'] end || session[:user_id] || session['user_id'] end |
#user_name ⇒ String?
Get user name from various possible locations
238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/otto/security/authentication/strategy_result.rb', line 238 def user_name return nil unless authenticated? # Try user model methods first, fall back to hash access if user.respond_to?(:name) user.name elsif user.respond_to?(:username) user.username elsif user.is_a?(Hash) user[:name] || user['name'] || user[:username] || user['username'] end end |