Class: Olelo::User

Inherits:
Object show all
Includes:
Util
Defined in:
lib/olelo/user.rb

Defined Under Namespace

Classes: Service

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util

#check, #decode64, #deep_copy, #encode64, #escape, #escape_html, #escape_javascript, included, #md5, #no_cache?, #sha256, #titlecase, #truncate, #unescape, #unescape_backslash, #unescape_html, #valid_xml_chars?

Constructor Details

#initialize(name, email, groups = nil) ⇒ User

Returns a new instance of User.



23
24
25
# File 'lib/olelo/user.rb', line 23

def initialize(name, email, groups = nil)
  @name, @email, @groups = name, email, Set.new(groups.to_a)
end

Instance Attribute Details

#emailObject

Returns the value of attribute email.



21
22
23
# File 'lib/olelo/user.rb', line 21

def email
  @email
end

#groupsObject (readonly)

Returns the value of attribute groups.



20
21
22
# File 'lib/olelo/user.rb', line 20

def groups
  @groups
end

#nameObject (readonly)

Returns the value of attribute name.



20
21
22
# File 'lib/olelo/user.rb', line 20

def name
  @name
end

Class Method Details

.anonymous(request) ⇒ Object



86
87
88
89
90
# File 'lib/olelo/user.rb', line 86

def anonymous(request)
  ip = request.ip || 'unknown-ip'
  host = request.ip && Socket.gethostbyaddr(request.ip.split('.').map(&:to_i).pack('C*')).first rescue nil
  new(host ? "#{host} (#{ip})" : ip, "anonymous@#{ip}")
end

.authenticate(name, password) ⇒ Object



100
101
102
# File 'lib/olelo/user.rb', line 100

def authenticate(name, password)
  service.authenticate(name, password).tap {|user| user.groups << 'user' }
end

.currentObject



63
64
65
# File 'lib/olelo/user.rb', line 63

def current
  Thread.current[:olelo_user]
end

.current=(user) ⇒ Object



67
68
69
# File 'lib/olelo/user.rb', line 67

def current=(user)
  Thread.current[:olelo_user] = user
end

.find(name) ⇒ Object



96
97
98
# File 'lib/olelo/user.rb', line 96

def find(name)
  find!(name) rescue nil
end

.find!(name) ⇒ Object



92
93
94
# File 'lib/olelo/user.rb', line 92

def find!(name)
  service.find(name)
end

.logged_in?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/olelo/user.rb', line 71

def logged_in?
  current && current.groups.include?('user')
end

.serviceObject



75
76
77
# File 'lib/olelo/user.rb', line 75

def service
  @service ||= Service[Config['authentication.service']].new(Config['authentication'][Config['authentication.service']])
end

.signup(name, password, confirm, email) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/olelo/user.rb', line 108

def (name, password, confirm, email)
  validate_password(password, confirm)
  user = new(name, email, %w(user))
  user.validate
  service.(user, password)
  user
end

.supports?(method) ⇒ Boolean

Returns:

  • (Boolean)


104
105
106
# File 'lib/olelo/user.rb', line 104

def supports?(method)
  service.respond_to?(method)
end

.validate_password(password, confirm) ⇒ Object



79
80
81
82
83
84
# File 'lib/olelo/user.rb', line 79

def validate_password(password, confirm)
  check do |errors|
    errors << :passwords_do_not_match.t if password != confirm
    errors << :empty_password.t if password.blank?
  end
end

Instance Method Details

#change_password(oldpassword, password, confirm) ⇒ Object



27
28
29
30
# File 'lib/olelo/user.rb', line 27

def change_password(oldpassword, password, confirm)
  User.validate_password(password, confirm)
  User.service.change_password(self, oldpassword, password)
end

#update(&block) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/olelo/user.rb', line 32

def update(&block)
  copy = dup
  block.call(copy)
  validate
  User.service.update(copy)
  instance_variables.each do |name|
    instance_variable_set(name, copy.instance_variable_get(name))
  end
end

#validateObject



42
43
44
45
46
47
# File 'lib/olelo/user.rb', line 42

def validate
  check do |errors|
    errors << :invalid_email.t if email !~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i
    errors << :invalid_name.t  if name !~ /[\w\.\-\+]+/
  end
end