Class: DBAuth

Inherits:
Object
  • Object
show all
Extended by:
JSONModel
Includes:
BCrypt
Defined in:
backend/app/model/dbauth.rb

Constant Summary

Constants included from JSONModel

JSONModel::REFERENCE_KEY_REGEX

Class Method Summary collapse

Methods included from JSONModel

JSONModel, JSONModel, add_error_handler, all, allow_unmapped_enum_value, backend_url, check_valid_refs, client_mode?, custom_validations, destroy_model, enum_default_value, enum_values, handle_error, init, load_schema, models, models, parse_jsonmodel_ref, parse_reference, repository, repository_for, schema_src, set_publish_flags!, set_repository, strict_mode, strict_mode?, validate_schema, with_repository

Class Method Details

.authenticate(username, password) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'backend/app/model/dbauth.rb', line 29

def self.authenticate(username, password)
  username = username.downcase

  DB.open do |db|
  pwhash = db[:auth_db].filter(:username => username).get(:pwhash)

  if pwhash and (Password.new(pwhash) == password)
    user = User.find(:username => username)
    JSONModel(:user).from_hash(
     :username => username,
     :name => user.name,
     :email => user.email,
     :first_name => user.first_name,
     :last_name => user.last_name,
     :telephone => user.telephone,
     :title => user.title,
     :department => user.department,
     :additional_contact => user.additional_contact
    )
  else
    nil
  end
end
end

.delete_user(username) ⇒ Object



69
70
71
72
73
# File 'backend/app/model/dbauth.rb', line 69

def self.delete_user(username)
  DB.open do |db|
    db[:auth_db].filter(:username => username).delete
  end
end

.matching_usernames(query) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
# File 'backend/app/model/dbauth.rb', line 55

def self.matching_usernames(query)
  DB.open do |db|
    query = query.gsub(/[%]/, '').downcase
    db[:auth_db].left_outer_join(:user, :username => :username).
                 filter(Sequel.~(:is_system_user => 1)).
                 filter(Sequel.like(Sequel.function(:lower, :auth_db__username),
                                    "#{query}%")).
      select(:auth_db__username).
      limit(AppConfig[:max_usernames_per_source].to_i).
      map {|row| row[:username]}
  end
end

.set_password(username, password) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'backend/app/model/dbauth.rb', line 8

def self.set_password(username, password)
  pwhash = Password.create(password)
  username = username.downcase

  DB.open do |db|
    DB.attempt {
      db[:auth_db].insert(:username => username,
                          :pwhash => pwhash,
                          :create_time => Time.now,
                          :system_mtime => Time.now)
    }.and_if_constraint_fails {
      db[:auth_db].
      filter(:username => username).
      update(:username => username,
             :pwhash => pwhash,
             :system_mtime => Time.now)
    }
  end
end