Class: Bosh::Director::Api::UserManager

Inherits:
Object
  • Object
show all
Defined in:
lib/bosh/director/api/user_manager.rb

Instance Method Summary collapse

Instance Method Details

#authenticate(username, password) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/bosh/director/api/user_manager.rb', line 17

def authenticate(username, password)
  # This is a dev-mode shortcut
  if Models::User.count == 0
    return username == "admin" && password == "admin"
  end

  user = find_by_name(username)
  BCrypt::Password.new(user.password) == password
rescue UserNotFound
  false
end

#create_user(new_user) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/bosh/director/api/user_manager.rb', line 33

def create_user(new_user)
  user = Models::User.new
  user.username = new_user.username
  if new_user.password
    user.password = BCrypt::Password.create(new_user.password).to_s
  end
  save_user(user)
  user
end

#delete_user(username) ⇒ Object



29
30
31
# File 'lib/bosh/director/api/user_manager.rb', line 29

def delete_user(username)
  find_by_name(username).destroy
end

#find_by_name(name) ⇒ Models::User

Returns User.

Parameters:

  • name (String)

    User name

Returns:



9
10
11
12
13
14
15
# File 'lib/bosh/director/api/user_manager.rb', line 9

def find_by_name(name)
  user = Models::User[:username => name]
  if user.nil?
    raise UserNotFound, "User `#{name}' doesn't exist"
  end
  user
end

#get_user_from_request(request) ⇒ Object



50
51
52
53
54
# File 'lib/bosh/director/api/user_manager.rb', line 50

def get_user_from_request(request)
  hash = Yajl::Parser.new.parse(request.body)
  Models::User.new(:username => hash["username"],
                   :password => hash["password"])
end

#update_user(updated_user) ⇒ Object



43
44
45
46
47
48
# File 'lib/bosh/director/api/user_manager.rb', line 43

def update_user(updated_user)
  user = find_by_name(updated_user.username)
  user.password = BCrypt::Password.create(updated_user.password).to_s
  save_user(user)
  user
end