Class: Rack::Casual::UserFactory

Inherits:
Object
  • Object
show all
Defined in:
lib/rack/casual/user_factory.rb

Class Method Summary collapse

Class Method Details

.authenticate_with_cas_ticket(ticket, service_url, ip) ⇒ Object

Authenticate user by CAS.

If ticket is valid it will search for user with the given username. If no user is found it will be automatically created if Rack::Casual.create_user is set.

Returns the user or nil if user not found



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/rack/casual/user_factory.rb', line 15

def self.authenticate_with_cas_ticket(ticket, service_url, ip)
  user   = nil
  client = Client.new(service_url, ticket)
  
  if client.validate_ticket
    # find or create the user
    user = find(client.username)
    if user.nil? && Rack::Casual.create_user
      user = make(client.username)
    end

    return nil if user.nil? || user_not_active(user)

    # Set extra attributes if supported by user
    if user.respond_to?(:cas_extra_attributes=)
      user.cas_extra_attributes = client.extra_attributes 
    end

    update_tracking(user, ip)
  end

  user
end

.authenticate_with_token(token, ip) ⇒ Object

Find user by authentication token. If user exists and tracking is enabled, tracking info is updated. Returns user or nil if user not found.



44
45
46
47
48
49
# File 'lib/rack/casual/user_factory.rb', line 44

def self.authenticate_with_token(token, ip)
  user = resource.where(Rack::Casual.auth_token => token).first
  return nil if user_not_active(user)
  update_tracking(user, ip) if user
  user
end

.find(username) ⇒ Object

Find user by username



67
68
69
# File 'lib/rack/casual/user_factory.rb', line 67

def self.find(username)
  resource.where(Rack::Casual.username => username).first
end

.make(username) ⇒ Object

Initializes a new user



72
73
74
# File 'lib/rack/casual/user_factory.rb', line 72

def self.make(username)
  resource.new(Rack::Casual.username => username)
end

.resourceObject

Returns the user class



77
78
79
80
# File 'lib/rack/casual/user_factory.rb', line 77

def self.resource
  # Rack::Casual.user_class.constantize
  Kernel.const_get(Rack::Casual.user_class)
end

.update_tracking(user, ip) ⇒ Object

Update tracking info (last logged in at / ip) if tracking_enabled is set. Saves the user regardless of whether tracking was updated or not.



57
58
59
60
61
62
63
64
# File 'lib/rack/casual/user_factory.rb', line 57

def self.update_tracking(user, ip)
  if Rack::Casual.tracking_enabled
    user. = Time.now   if user.respond_to?(:last_login_at)
    user. = ip         if user.respond_to?(:last_login_ip)
    user. += 1           if user.respond_to?(:login_count)
  end
  user.save
end

.user_not_active(user) ⇒ Object



51
52
53
# File 'lib/rack/casual/user_factory.rb', line 51

def self.user_not_active(user)
  user.respond_to?(:active?) && user.send(:active?) == false
end