Module: YiffSpace::Auth::Helper

Extended by:
ActiveSupport::Concern
Included in:
Scoped
Defined in:
lib/yiffspace/auth/helper.rb

Defined Under Namespace

Modules: ClassMethods, Scoped

Instance Method Summary collapse

Instance Method Details

#authObject



33
34
35
36
37
# File 'lib/yiffspace/auth/helper.rb', line 33

def auth
  return AuthInfo::Anonymous.instance if auth_raw.blank?

  AuthInfo.from_session(auth_raw)
end

#auth=(value) ⇒ Object



43
44
45
46
# File 'lib/yiffspace/auth/helper.rb', line 43

def auth=(value)
  value                                        = nil if value.is_a?(AuthInfo::Anonymous)
  session[auth_client_config.auth_session_key] = value&.to_session
end

#auth?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/yiffspace/auth/helper.rb', line 39

def auth?
  auth_raw.present? && !auth.anonymous?
end

#auth_client_configObject

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.



102
103
104
105
# File 'lib/yiffspace/auth/helper.rb', line 102

def auth_client_config
  client_name = self.client_name
  client_name.present? ? YiffSpace::Auth[client_name.to_sym] : YiffSpace::Auth.default
end

#auth_rawObject



29
30
31
# File 'lib/yiffspace/auth/helper.rb', line 29

def auth_raw
  session[auth_client_config.auth_session_key]
end

#client_nameObject



107
108
109
# File 'lib/yiffspace/auth/helper.rb', line 107

def client_name
  respond_to?(:request, true) && request.env[CLIENT_NAME_ENV]
end

#client_name=(value) ⇒ Object



111
112
113
# File 'lib/yiffspace/auth/helper.rb', line 111

def client_name=(value)
  request.env[CLIENT_NAME_ENV] = value.to_sym
end

#full_reset!Object



75
76
77
78
# File 'lib/yiffspace/auth/helper.rb', line 75

def full_reset!
  reset_auth!
  reset_user!
end

#has_permission?(name) ⇒ Boolean

Returns:

  • (Boolean)


88
89
90
91
92
# File 'lib/yiffspace/auth/helper.rb', line 88

def has_permission?(name)
  return false unless user?

  auth.permissions.has?(name)
end

#logged_in?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/yiffspace/auth/helper.rb', line 84

def logged_in?
  user?
end

#require_auth(path) ⇒ Object



80
81
82
# File 'lib/yiffspace/auth/helper.rb', line 80

def require_auth(path)
  redirect_to(path) unless user?
end

#reset_auth!Object



48
49
50
# File 'lib/yiffspace/auth/helper.rb', line 48

def reset_auth!
  session.delete(auth_client_config.auth_session_key)
end

#reset_user!Object



71
72
73
# File 'lib/yiffspace/auth/helper.rb', line 71

def reset_user!
  session.delete(auth_client_config.user_session_key)
end

#url_helpersObject



94
95
96
# File 'lib/yiffspace/auth/helper.rb', line 94

def url_helpers
  YiffSpace::Auth::Engine.for(client_name).routes.url_helpers
end

#userObject



56
57
58
59
60
# File 'lib/yiffspace/auth/helper.rb', line 56

def user
  return UserInfo::Anonymous.instance if user_raw.blank?

  UserInfo.from_session(user_raw)
end

#user=(value) ⇒ Object



66
67
68
69
# File 'lib/yiffspace/auth/helper.rb', line 66

def user=(value)
  value                                        = nil if value.is_a?(UserInfo::Anonymous)
  session[auth_client_config.user_session_key] = value&.to_session
end

#user?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/yiffspace/auth/helper.rb', line 62

def user?
  user_raw.present? && !user.anonymous?
end

#user_rawObject



52
53
54
# File 'lib/yiffspace/auth/helper.rb', line 52

def user_raw
  session[auth_client_config.user_session_key]
end