Module: SimplestAuth::Controller

Defined in:
lib/simplest_auth/controller.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



67
68
69
# File 'lib/simplest_auth/controller.rb', line 67

def self.included(base)
  base.send :helper_method, :current_user, :logged_in?, :authorized? if base.respond_to? :helper_method
end

Instance Method Details

#access_deniedObject



17
18
19
20
21
# File 'lib/simplest_auth/controller.rb', line 17

def access_denied
  store_location
  flash[:error] = 
  redirect_to new_session_url
end

#authorized?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/simplest_auth/controller.rb', line 13

def authorized?
  logged_in?
end

#clear_sessionObject



63
64
65
# File 'lib/simplest_auth/controller.rb', line 63

def clear_session
  session[session_key] = nil
end

#current_userObject



44
45
46
47
48
49
50
51
52
# File 'lib/simplest_auth/controller.rb', line 44

def current_user
  @current_user ||= begin
    if user_class.respond_to?(:get)
      user_class.get(current_user_id)
    else
      current_user_id && user_class.where(:id => current_user_id).first
    end
  end || clear_session
end

#current_user=(user) ⇒ Object



54
55
56
57
# File 'lib/simplest_auth/controller.rb', line 54

def current_user=(user)
  session[session_key] = user ? user.id : nil
  @current_user = user || false
end

#current_user_idObject



59
60
61
# File 'lib/simplest_auth/controller.rb', line 59

def current_user_id
  session[session_key]
end

#logged_in?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/simplest_auth/controller.rb', line 40

def logged_in?
  !current_user_id.nil?
end

#login_messageObject



23
24
25
# File 'lib/simplest_auth/controller.rb', line 23

def 
  "Login or Registration Required"
end

#login_requiredObject



36
37
38
# File 'lib/simplest_auth/controller.rb', line 36

def 
  authorized? || access_denied
end

#redirect_back_or_default(default) ⇒ Object



31
32
33
34
# File 'lib/simplest_auth/controller.rb', line 31

def redirect_back_or_default(default)
  redirect_to(session[:return_to] || default)
  session[:return_to] = nil
end

#session_keyObject



9
10
11
# File 'lib/simplest_auth/controller.rb', line 9

def session_key
  user_class.session_key
end

#store_locationObject



27
28
29
# File 'lib/simplest_auth/controller.rb', line 27

def store_location
  session[:return_to] = (request.respond_to?(:fullpath) ? request.fullpath : request.request_uri)
end

#user_classObject



5
6
7
# File 'lib/simplest_auth/controller.rb', line 5

def user_class
  User
end