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.



131
132
133
134
# File 'lib/yiffspace/auth/helper.rb', line 131

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



136
137
138
# File 'lib/yiffspace/auth/helper.rb', line 136

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

#client_name=(value) ⇒ Object



140
141
142
# File 'lib/yiffspace/auth/helper.rb', line 140

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

#full_reset!Object



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

def full_reset!
  reset_state!
  reset_auth!
  reset_user!
  reset_return_path!
end

#generate_state!Object



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

def generate_state!
  generator  = auth_client_config.state_generator
  self.state = generator.call(*(generator.arity.zero? ? [] : [self]))
end

#has_permission?(name) ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
120
121
# File 'lib/yiffspace/auth/helper.rb', line 117

def has_permission?(name)
  return false unless auth?

  auth.permissions.has?(name)
end

#require_auth(path) ⇒ Object



113
114
115
# File 'lib/yiffspace/auth/helper.rb', line 113

def require_auth(path)
  redirect_to(path) unless auth?
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_return_path!Object



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

def reset_return_path!
  session.delete(auth_client_config.return_path_session_key)
end

#reset_state!Object



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

def reset_state!
  session.delete(auth_client_config.state_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

#return_pathObject



92
93
94
# File 'lib/yiffspace/auth/helper.rb', line 92

def return_path
  session[auth_client_config.return_path_session_key]
end

#return_path=(value) ⇒ Object



96
97
98
99
100
# File 'lib/yiffspace/auth/helper.rb', line 96

def return_path=(value)
  return if value && (!value.start_with?("/") || value.start_with?("//"))

  session[auth_client_config.return_path_session_key] = value
end

#stateObject



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

def state
  session[auth_client_config.state_session_key]
end

#state=(value) ⇒ Object



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

def state=(value)
  session[auth_client_config.state_session_key] = value
end

#url_helpersObject



123
124
125
# File 'lib/yiffspace/auth/helper.rb', line 123

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