Class: Vines::Services::CouchModels::User

Inherits:
CouchRest::Model::Base
  • Object
show all
Extended by:
Storage::CouchDB::ClassMethods
Defined in:
lib/vines/services/storage/couchdb/user.rb

Constant Summary collapse

KEYS =
%w[_id name permissions system created_at updated_at].freeze

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Storage::CouchDB::ClassMethods

build_from_database, to_s

Class Method Details

.find_allObject



102
103
104
105
106
# File 'lib/vines/services/storage/couchdb/user.rb', line 102

def self.find_all
  by_jid.map do |doc|
    {jid: doc.jid, name: doc.name, system: doc.system}
  end
end

.find_by_jid(jid) ⇒ Object



108
109
110
# File 'lib/vines/services/storage/couchdb/user.rb', line 108

def self.find_by_jid(jid)
  first_from_view('by_jid', jid.to_s.downcase)
end

Instance Method Details

#admin!Object

Grant this user all possible permissions.



55
56
57
58
59
60
61
62
# File 'lib/vines/services/storage/couchdb/user.rb', line 55

def admin!
  write_attribute('permissions', {
    'systems'  => true,
    'services' => true,
    'files'    => true,
    'users'    => true
  })
end

#change_password(previous, desired) ⇒ Object



78
79
80
81
82
# File 'lib/vines/services/storage/couchdb/user.rb', line 78

def change_password(previous, desired)
  hash = BCrypt::Password.new(password) rescue nil
  raise 'password failure' unless hash && hash == previous
  self.plain_password = desired
end

#jidObject



84
85
86
# File 'lib/vines/services/storage/couchdb/user.rb', line 84

def jid
  id ? id.sub('user:', '') : nil
end

#permissions=(perms) ⇒ Object



64
65
66
67
68
69
70
# File 'lib/vines/services/storage/couchdb/user.rb', line 64

def permissions=(perms)
  perms ||= {}
  self.manage_systems = perms['systems']
  self.manage_services = perms['services']
  self.manage_files = perms['files']
  self.manage_users = perms['users']
end

#plain_password=(desired) ⇒ Object



72
73
74
75
76
# File 'lib/vines/services/storage/couchdb/user.rb', line 72

def plain_password=(desired)
  desired = (desired || '').strip
  raise 'password too short' if desired.size < (system ? 128 : 8)
  write_attribute('password', BCrypt::Password.create(desired))
end

#servicesObject

Query the Service/by_user view and return the Service objects to which this User has access. The services are cached so subsequent calls to this method do not query the view.



91
92
93
# File 'lib/vines/services/storage/couchdb/user.rb', line 91

def services
  @services ||= Service.find_by_user(jid)
end

#to_resultObject



95
96
97
98
99
100
# File 'lib/vines/services/storage/couchdb/user.rb', line 95

def to_result
  to_hash.clone.keep_if {|k, v| KEYS.include?(k) }.tap do |h|
    h['jid'] = h.delete('_id').sub('user:', '')
    h['services'] = h['system'] ? []: services.map {|s| s.id }
  end
end