Class: Rhoconnect::User

Inherits:
StoreOrm show all
Defined in:
lib/rhoconnect/user.rb

Overview

Inspired by sinatra-authentication Password uses simple sha1 digest for hashing

Instance Attribute Summary

Attributes inherited from StoreOrm

#id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from StoreOrm

_field_key, _prefix, class_prefix, #decrement!, field, #field_key, fields, #increment!, #initialize, is_exist?, list, load, marshal_class_name, #next_id, populate_attributes, set, #store, store, #to_array, validates_presence_of

Constructor Details

This class inherits a constructor from Rhoconnect::StoreOrm

Class Method Details

.allObject

Rails like methods



38
39
40
# File 'lib/rhoconnect/user.rb', line 38

def all
  App.load(APP_NAME).users.members
end

.authenticate(login, password) ⇒ Object



30
31
32
33
34
35
# File 'lib/rhoconnect/user.rb', line 30

def authenticate(,password)
  return unless is_exist?()
  current_user = load()
  return if current_user.nil?
  return current_user if User.encrypt(password, current_user.salt) == current_user.hashed_password
end

.create(fields = {}) ⇒ Object

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rhoconnect/user.rb', line 17

def create(fields={})
  raise ArgumentError.new("Empty login") if (fields[:login].nil? or fields[:login].empty?)
  raise ArgumentError.new("Reserved user id #{fields[:login]}") if fields[:login] && fields[:login] == '__shared__'
  fields[:id] = fields[:login]
  user = super(fields)
  if Rhoconnect.stats
    Rhoconnect::Stats::Record.set('users') { Store.incr('user:count') }
  else
    Store.incr('user:count')
  end
  user
end

.ping(params) ⇒ Object



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

def ping(params)
  if params['async']
    PingJob.enqueue(params)
  else
    PingJob.perform(params)
  end
end

Instance Method Details

#create_tokenObject



74
75
76
77
78
79
80
81
82
83
# File 'lib/rhoconnect/user.rb', line 74

def create_token
  if self.token_id && ApiToken.is_exist?(self.token_id)
    self.token.delete
  end
  fields = {:user_id => self.}
  if self. == 'rhoadmin'
    fields[:value] = Rhoconnect.api_token
  end
  self.token_id = ApiToken.create(fields).id
end

#deleteObject



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/rhoconnect/user.rb', line 61

def delete
  clients.members.each do |client_id|
    Client.load(client_id,{:source_name => '*'}).delete
  end
  self.token.delete if self.token
  if Rhoconnect.stats
    Rhoconnect::Stats::Record.set('users') { Store.decr('user:count') }
  else
    Store.decr('user:count')
  end
  super
end

#new_password=(pass) ⇒ Object



51
52
53
# File 'lib/rhoconnect/user.rb', line 51

def new_password=(pass)
  self.password=(pass)
end

#password=(pass) ⇒ Object



55
56
57
58
59
# File 'lib/rhoconnect/user.rb', line 55

def password=(pass)
  @password = pass
  self.salt = User.random_string(10) if !self.salt
  self.hashed_password = User.encrypt(@password, self.salt)
end

#to_hashObject



102
103
104
105
106
107
108
# File 'lib/rhoconnect/user.rb', line 102

def to_hash
   res = {}
   self.class.fields.each do |field|
     res[field[:name].to_sym] = send(field[:name].to_sym) if field[:name] == 'login'
   end
   res
end

#tokenObject



85
86
87
# File 'lib/rhoconnect/user.rb', line 85

def token
  ApiToken.load(self.token_id)
end

#token=(value) ⇒ Object



89
90
91
92
93
94
# File 'lib/rhoconnect/user.rb', line 89

def token=(value)
  if self.token_id && ApiToken.is_exist?(self.token_id)
    self.token.delete
  end
  self.token_id = ApiToken.create(:user_id => self., :value => value).id
end

#update(fields) ⇒ Object



96
97
98
99
100
# File 'lib/rhoconnect/user.rb', line 96

def update(fields)
  fields.each do |key,value|
    self.send("#{key.to_sym}=", value) unless key == 'login'
  end
end