Parent

Included Modules

Class/Module Index [+]

Quicksearch

Chef::WebUIUser

Constants

DESIGN_DOCUMENT

Attributes

admin[RW]
couchdb[RW]
couchdb_id[R]
couchdb_rev[R]
name[RW]
openid[RW]
password[R]
salt[R]
validated[RW]

Public Class Methods

admin_exist() click to toggle source

return true if an admin user exists. this is pretty expensive (O(n)), should think of a better way (nuo)

# File lib/chef/webui_user.rb, line 206
def self.admin_exist
  users = self.cdb_list
  users.each do |u|
    user = self.cdb_load(u)
    if user.admin
      return user.name
    end
  end
  nil
end
cdb_list(inflate=false) click to toggle source

List all the Chef::WebUIUser objects in the CouchDB. If inflate is set to true, you will get the full list of all registration objects. Otherwise, you'll just get the IDs

# File lib/chef/webui_user.rb, line 121
def self.cdb_list(inflate=false)
  rs = Chef::CouchDB.new.list("users", inflate)
  if inflate
    rs["rows"].collect { |r| r["value"] }
  else
    rs["rows"].collect { |r| r["key"] }
  end
end
cdb_load(name) click to toggle source

Load an WebUIUser by name from CouchDB

# File lib/chef/webui_user.rb, line 144
def self.cdb_load(name)
  Chef::CouchDB.new.load("webui_user", name)
end
create_design_document(couchdb=nil) click to toggle source

Set up our CouchDB design document

# File lib/chef/webui_user.rb, line 200
def self.create_design_document(couchdb=nil)
  couchdb ||= Chef::CouchDB.new
  couchdb.create_design_document("users", DESIGN_DOCUMENT)
end
has_key?(name) click to toggle source

Whether or not there is an WebUIUser with this key.

# File lib/chef/webui_user.rb, line 156
def self.has_key?(name)
  Chef::CouchDB.new.has_key?("webui_user", name)
end
json_create(o) click to toggle source

Create a Chef::WebUIUser from JSON

# File lib/chef/webui_user.rb, line 113
def self.json_create(o)
  me = new(o)
  me.admin = o["admin"]
  me
end
list(inflate=false) click to toggle source
# File lib/chef/webui_user.rb, line 130
def self.list(inflate=false)
  r = Chef::REST.new(Chef::Config[:chef_server_url])
  if inflate
    response = Hash.new
    Chef::Search::Query.new.search(:user) do |n|
      response[n.name] = n unless n.nil?
    end
    response
  else
    r.get_rest("users")
  end
end
load(name) click to toggle source

Load a User by name

# File lib/chef/webui_user.rb, line 149
def self.load(name)
  r = Chef::REST.new(Chef::Config[:chef_server_url])
  r.get_rest("users/#{name}")
end
new(opts={}) click to toggle source

Create a new Chef::WebUIUser object.

# File lib/chef/webui_user.rb, line 62
def initialize(opts={})
  @name, @salt, @password = opts['name'], opts['salt'], opts['password']
  @openid, @couchdb_rev, @couchdb_id = opts['openid'], opts['_rev'], opts['_id']
  @admin = false
  @couchdb = Chef::CouchDB.new
end

Public Instance Methods

admin?() click to toggle source
# File lib/chef/webui_user.rb, line 73
def admin?
  admin
end
cdb_destroy() click to toggle source

Remove this WebUIUser from the CouchDB

# File lib/chef/webui_user.rb, line 161
def cdb_destroy
  couchdb.delete("webui_user", @name, @couchdb_rev)
end
cdb_save() click to toggle source

Save this WebUIUser to the CouchDB

# File lib/chef/webui_user.rb, line 172
def cdb_save
  results = couchdb.store("webui_user", @name, self)
  @couchdb_rev = results["rev"]
end
create() click to toggle source

Create the WebUIUser via the REST API

# File lib/chef/webui_user.rb, line 193
def create
  r = Chef::REST.new(Chef::Config[:chef_server_url])
  r.post_rest("users", self)
  self
end
destroy() click to toggle source

Remove this WebUIUser via the REST API

# File lib/chef/webui_user.rb, line 166
def destroy
  r = Chef::REST.new(Chef::Config[:chef_server_url])
  r.delete_rest("users/#{@name}")
end
name=(n) click to toggle source
# File lib/chef/webui_user.rb, line 69
def name=(n)
  @name = n.gsub(/\./, '_')
end
save() click to toggle source

Save this WebUIUser via the REST API

# File lib/chef/webui_user.rb, line 178
def save
  r = Chef::REST.new(Chef::Config[:chef_server_url])
  begin
    r.put_rest("users/#{@name}", self)
  rescue Net::HTTPServerException => e
    if e.response.code == "404"
      r.post_rest("users", self)
    else
      raise e
    end
  end
  self
end
set_openid(given_openid) click to toggle source
# File lib/chef/webui_user.rb, line 86
def set_openid(given_openid)
  @openid = given_openid
end
set_password(password, confirm_password=password) click to toggle source

Set the password for this object.

# File lib/chef/webui_user.rb, line 78
def set_password(password, confirm_password=password) 
  raise ArgumentError, "Passwords do not match" unless password == confirm_password
  raise ArgumentError, "Password cannot be blank" if (password.nil? || password.length==0)
  raise ArgumentError, "Password must be a minimum of 6 characters" if password.length < 6
  generate_salt
  @password = encrypt_password(password)      
end
to_json(*a) click to toggle source

Serialize this object as a hash

# File lib/chef/webui_user.rb, line 95
def to_json(*a)
  attributes = Hash.new
  recipes = Array.new
  result = {
    'name' => name,
    'json_class' => self.class.name,
    'salt' => salt,
    'password' => password,
    'openid' => openid,
    'admin' => admin,
    'chef_type' => 'webui_user',
  }
  result["_id"]  = @couchdb_id if @couchdb_id  
  result["_rev"] = @couchdb_rev if @couchdb_rev
  result.to_json(*a)
end
verify_password(given_password) click to toggle source
# File lib/chef/webui_user.rb, line 90
def verify_password(given_password)
  encrypt_password(given_password) == @password
end

Protected Instance Methods

encrypt_password(password) click to toggle source
# File lib/chef/webui_user.rb, line 226
def encrypt_password(password)
  Digest::SHA1.hexdigest("--#{salt}--#{password}--")
end
generate_salt() click to toggle source
# File lib/chef/webui_user.rb, line 219
def generate_salt
  @salt = Time.now.to_s
  chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
  1.upto(30) { |i| @salt << chars[rand(chars.size-1)] }
  @salt
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.