Class: Janus::Manager

Inherits:
Object
  • Object
show all
Includes:
Hooks, Strategies
Defined in:
lib/janus/manager.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Strategies

#run_strategies, #run_strategy

Constructor Details

#initialize(request, cookies) ⇒ Manager

Returns a new instance of Manager.



8
9
10
# File 'lib/janus/manager.rb', line 8

def initialize(request, cookies)
  @request, @cookies = request, cookies
end

Instance Attribute Details

#cookiesObject (readonly)

Returns the value of attribute cookies.



6
7
8
# File 'lib/janus/manager.rb', line 6

def cookies
  @cookies
end

#requestObject (readonly)

Returns the value of attribute request.



6
7
8
# File 'lib/janus/manager.rb', line 6

def request
  @request
end

Instance Method Details

#authenticate(scope) ⇒ Object

Tries to authenticate the user using strategies, before returning the current user or nil.



14
15
16
17
# File 'lib/janus/manager.rb', line 14

def authenticate(scope)
  run_strategies(scope) unless authenticated?(scope)
  user(scope)
end

#authenticate!(scope) ⇒ Object

Raises a Janus::NotAuthenticated exception unless a user is authenticated.



20
21
22
# File 'lib/janus/manager.rb', line 20

def authenticate!(scope)
  raise Janus::NotAuthenticated.new(scope) unless authenticate?(scope)
end

#authenticate?(scope) ⇒ Boolean

Tries to authenticate the user before checking if it’s authenticated.

Returns:

  • (Boolean)


25
26
27
28
# File 'lib/janus/manager.rb', line 25

def authenticate?(scope)
  authenticate(scope)
  authenticated?(scope)
end

#authenticated?(scope) ⇒ Boolean

Returns true if a user is authenticated.

Returns:

  • (Boolean)


31
32
33
# File 'lib/janus/manager.rb', line 31

def authenticated?(scope) # :nodoc:
  !!session(scope)
end

#login(user, options = {}) ⇒ Object

Logs a user in.

FIXME: what should happen when a user signs in but a user is already signed in for the same scope?!



38
39
40
41
42
# File 'lib/janus/manager.rb', line 38

def (user, options = {})
  options[:scope] ||= Janus.scope_for(user)
  set_user(user, options)
  Janus::Manager.run_callbacks(:login, user, self, options)
end

#logout(*scopes) ⇒ Object

Logs a user out from the given scopes or from all scopes at once if no scope is defined. If no scope is left after logout, then the whole session will be resetted.



47
48
49
50
51
52
53
54
55
56
57
# File 'lib/janus/manager.rb', line 47

def logout(*scopes)
  scopes = janus_sessions.keys if scopes.empty?

  scopes.each do |scope|
    _user = user(scope)
    unset_user(scope)
    Janus::Manager.run_callbacks(:logout, _user, self, :scope => scope)
  end

  request.reset_session if janus_sessions.empty?
end

#session(scope) ⇒ Object

Returns the current session for user.



93
94
95
# File 'lib/janus/manager.rb', line 93

def session(scope)
  janus_sessions[scope.to_s]
end

#set_user(user, options = {}) ⇒ Object

Manually sets a user without going throught the whole login or authenticate process.



61
62
63
64
# File 'lib/janus/manager.rb', line 61

def set_user(user, options = {})
  scope = options[:scope] || Janus.scope_for(user)
  janus_sessions[scope.to_s] = { 'user_class' => user.class.name, 'user_id' => user.id }
end

#unset_user(scope) ⇒ Object

Manually removes the user without going throught the whole logout process.



67
68
69
70
# File 'lib/janus/manager.rb', line 67

def unset_user(scope)
  janus_sessions.delete(scope.to_s)
  @users.delete(scope.to_sym) unless @users.nil?
end

#user(scope) ⇒ Object

Returns the currently connected user.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/janus/manager.rb', line 73

def user(scope)
  scope = scope.to_sym
  @users ||= {}

  if authenticated?(scope)
    if @users[scope].nil?
      begin
      @users[scope] = user_class(scope).find(session(scope)['user_id'])
      rescue ActiveRecord::RecordNotFound
        unset_user(scope)
      else
        Janus::Manager.run_callbacks(:fetch, @users[scope], self, :scope => scope)
      end
    end

    @users[scope]
  end
end