Module: SimpleAuth::Authentication::InstanceMethods

Defined in:
lib/simple_auth/authentication.rb

Instance Method Summary collapse

Instance Method Details

#authenticateObject

Deny the user access if they are signed out.

Examples:

before_filter :authenticate


76
77
78
# File 'lib/simple_auth/authentication.rb', line 76

def authenticate
  deny_access if (!signed_in? && !permission_exist?)
end

#current_permission(invoice = nil) ⇒ Object



29
30
31
# File 'lib/simple_auth/authentication.rb', line 29

def current_permission(invoice = nil)
  @_current_permission ||= permission_from_token(invoice) 
end

#current_userUser?

User in the current cookie

Returns:

  • (User, nil)


24
25
26
# File 'lib/simple_auth/authentication.rb', line 24

def current_user
  @_current_user ||= user_from_cookie
end

#current_user=(user) ⇒ Object

Set the current user

Parameters:

  • (User)


40
41
42
# File 'lib/simple_auth/authentication.rb', line 40

def current_user=(user)
  @_current_user = user
end

#deny_access(flash_message = nil) ⇒ Object

Store the current location and redirect to sign in. Display a failure flash message if included.

Parameters:

  • optional (String)

    flash message to display to denied user



110
111
112
113
114
# File 'lib/simple_auth/authentication.rb', line 110

def deny_access(flash_message = nil)
  store_location
  flash[:failure] = flash_message if flash_message
  redirect_to()
end

#permission_exist?true, false

Is exist permission and invoice

Returns:

  • (true, false)


54
55
56
57
58
59
60
61
# File 'lib/simple_auth/authentication.rb', line 54

def permission_exist?
  if params[:token] && _permission_token =  params[:token][32..-1]
    _invoice_token = params[:token][0..31]
    _invoice = Invoice.find(_invoice_token)
    _permission = Permission.find_by_token(_permission_token)
    _invoice && _permission
  end
end

#sign_in(user) ⇒ Object

Sign user in to cookie.

Examples:

(@user)

Parameters:

  • (User)


86
87
88
89
90
91
92
93
94
# File 'lib/simple_auth/authentication.rb', line 86

def (user)
  if user
    cookies[:remember_token] = {
      :value   => user.remember_token,
      :expires => 1.year.from_now.utc
    }
    current_user = user
  end
end

#sign_outObject

Sign user out of cookie.

Examples:

sign_out


100
101
102
103
104
# File 'lib/simple_auth/authentication.rb', line 100

def sign_out
  current_user.reset_remember_token! if current_user
  cookies.delete(:remember_token)
  current_user = nil
end

#signed_in?true, false

Is the current user signed in?

Returns:

  • (true, false)


47
48
49
# File 'lib/simple_auth/authentication.rb', line 47

def signed_in?
  ! current_user.nil?
end

#signed_out?true, false

Is the current user signed out?

Returns:

  • (true, false)


68
69
70
# File 'lib/simple_auth/authentication.rb', line 68

def signed_out?
  current_user.nil?
end