46 lines
1.0 KiB
Ruby
46 lines
1.0 KiB
Ruby
require "db/mongo/models/user"
|
|
module Connectors
|
|
class User < Base
|
|
include Helpers::InsertCommand,
|
|
Helpers::ShowCommand,
|
|
Helpers::ListCommand,
|
|
Helpers::DeleteCommand,
|
|
Helpers::UpdateCommand
|
|
|
|
def initialize(db)
|
|
self.collection = db.collection('users')
|
|
end
|
|
|
|
def user_auth user, password
|
|
u = collection.find('_id' => user, 'password' => password).to_a.first
|
|
raise RecordNotFound.new('Invalid username or password') if u.nil?
|
|
model_from_bson(u)
|
|
end
|
|
|
|
def users(ids=nil, options={})
|
|
query = {}
|
|
query['_id'] = {'$in' => ids} if ids.is_a?(Array)
|
|
list(query, options)
|
|
end
|
|
|
|
def users_names(ids=nil)
|
|
users = self.users(ids)
|
|
users.map(&:id)
|
|
end
|
|
|
|
def create_root_user
|
|
u = user('root')
|
|
rescue RecordNotFound => e
|
|
root = Devops::Model::User.create_root
|
|
collection.insert(root.to_mongo_hash)
|
|
end
|
|
|
|
private
|
|
|
|
def model_from_bson(bson)
|
|
Devops::Model::User.build_from_bson(bson)
|
|
end
|
|
end
|
|
|
|
end
|