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

- (Manager) initialize(request, cookies)

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

- (Object) cookies (readonly)

Returns the value of attribute cookies



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

def cookies
  @cookies
end

- (Object) request (readonly)

Returns the value of attribute request



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

def request
  @request
end

Instance Method Details

- (Object) authenticate(scope)

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

- (Object) authenticate!(scope)

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

- (Boolean) authenticate?(scope)

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

- (Boolean) authenticated?(scope)

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

- (Object) login(user, options = {})

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

- (Object) logout(*scopes)

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

- (Object) session(scope)

Returns the current session for user.



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

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

- (Object) set_user(user, options = {})

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_sym] = { :user_class => user.class, :user_id => user.id }
end

- (Object) unset_user(scope)

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_sym)
  @users.delete(scope.to_sym) unless @users.nil?
end

- (Object) user(scope)

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] = session(scope)[:user_class].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