Module: Ramaze::Helper::Auth

Defined in:
lib/cortex_reaver/helper/auth.rb

Overview

Provides authentication services

Instance Method Summary collapse

Instance Method Details

#admin?Boolean

Is the current user an admin?

Returns:

  • (Boolean)


6
7
8
# File 'lib/cortex_reaver/helper/auth.rb', line 6

def admin?
  u = session[:user] and u.admin?
end

#do_login(login, password) ⇒ Object

Tries to log in a user by login and password. If successful, sets session to the user and returns that user. Otherwise returns false.



27
28
29
30
31
32
33
34
# File 'lib/cortex_reaver/helper/auth.rb', line 27

def (, password)
  if user = CortexReaver::User.authenticate(, password)
    # Successful login
    session[:user] = user
  else
    false
  end
end

#do_logoutObject

Log out the current user, and returns the user object.



37
38
39
# File 'lib/cortex_reaver/helper/auth.rb', line 37

def do_logout
  session.delete :user
end

#error_403Object



54
55
56
# File 'lib/cortex_reaver/helper/auth.rb', line 54

def error_403
  respond 'Forbidden', 403
end

#for_auth(&block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/cortex_reaver/helper/auth.rb', line 10

def for_auth(&block)
  unless yield user
    # Failed block
    if session[:user]
      flash[:error] = "You don't have permission to do this."
      redirect CortexReaver::MainController.r
    else
      flash[:notice] = "Please log in first."
      session[:target_uri] = request.request_uri
      redirect CortexReaver::UserController.r(:login)
    end
  end
end

#require_roles(*roles) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/cortex_reaver/helper/auth.rb', line 41

def require_roles(*roles)
  for_auth do |u|
    roles.any? do |role|
      u.send role
    end
  end
end

#userObject

Shortcut for current user or an anonymous proxy



50
51
52
# File 'lib/cortex_reaver/helper/auth.rb', line 50

def user
  session[:user] || CortexReaver::User.anonymous
end