Class: Chef::WebUIUser

Inherits:
Object show all
Includes:
IndexQueue::Indexable, Mixin::ParamsValidate
Defined in:
lib/chef/webui_user.rb

Constant Summary collapse

DESIGN_DOCUMENT =
{
  "version" => 3,
  "language" => "javascript",
  "views" => {
    "all" => {
      "map" => <<-EOJS
        function(doc) {
          if (doc.chef_type == "webui_user") {
            emit(doc.name, doc);
          }
        }
      EOJS
    },
    "all_id" => {
      "map" => <<-EOJS
      function(doc) {
        if (doc.chef_type == "webui_user") {
          emit(doc.name, doc.name);
        }
      }
      EOJS
    },
  },
}

Instance Attribute Summary collapse

Attributes included from IndexQueue::Indexable

#index_id

Class Method Summary collapse

Instance Method Summary collapse

Methods included from IndexQueue::Indexable

#add_to_index, #delete_from_index, included, #index_object_type, #with_indexer_metadata

Methods included from Mixin::ParamsValidate

#set_or_return, #validate

Constructor Details

#initialize(opts = {}) ⇒ WebUIUser

Create a new Chef::WebUIUser object.



63
64
65
66
67
68
# File 'lib/chef/webui_user.rb', line 63

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

Instance Attribute Details

#adminObject

Returns the value of attribute admin.



31
32
33
# File 'lib/chef/webui_user.rb', line 31

def admin
  @admin
end

#couchdbObject

Returns the value of attribute couchdb.



31
32
33
# File 'lib/chef/webui_user.rb', line 31

def couchdb
  @couchdb
end

#couchdb_idObject (readonly)

Returns the value of attribute couchdb_id.



32
33
34
# File 'lib/chef/webui_user.rb', line 32

def couchdb_id
  @couchdb_id
end

#couchdb_revObject (readonly)

Returns the value of attribute couchdb_rev.



32
33
34
# File 'lib/chef/webui_user.rb', line 32

def couchdb_rev
  @couchdb_rev
end

#nameObject

Returns the value of attribute name.



31
32
33
# File 'lib/chef/webui_user.rb', line 31

def name
  @name
end

#openidObject

Returns the value of attribute openid.



31
32
33
# File 'lib/chef/webui_user.rb', line 31

def openid
  @openid
end

#passwordObject (readonly)

Returns the value of attribute password.



32
33
34
# File 'lib/chef/webui_user.rb', line 32

def password
  @password
end

#saltObject (readonly)

Returns the value of attribute salt.



32
33
34
# File 'lib/chef/webui_user.rb', line 32

def salt
  @salt
end

#validatedObject

Returns the value of attribute validated.



31
32
33
# File 'lib/chef/webui_user.rb', line 31

def validated
  @validated
end

Class Method Details

.admin_existObject

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



207
208
209
210
211
212
213
214
215
216
# File 'lib/chef/webui_user.rb', line 207

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) ⇒ Object

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



122
123
124
125
126
127
128
129
# File 'lib/chef/webui_user.rb', line 122

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) ⇒ Object

Load an WebUIUser by name from CouchDB



145
146
147
# File 'lib/chef/webui_user.rb', line 145

def self.cdb_load(name)
  Chef::CouchDB.new.load("webui_user", name)
end

.create_design_document(couchdb = nil) ⇒ Object

Set up our CouchDB design document



201
202
203
204
# File 'lib/chef/webui_user.rb', line 201

def self.create_design_document(couchdb=nil)
  couchdb ||= Chef::CouchDB.new
  couchdb.create_design_document("users", DESIGN_DOCUMENT)
end

.has_key?(name) ⇒ Boolean

Whether or not there is an WebUIUser with this key.

Returns:

  • (Boolean)


157
158
159
# File 'lib/chef/webui_user.rb', line 157

def self.has_key?(name)
  Chef::CouchDB.new.has_key?("webui_user", name)
end

.json_create(o) ⇒ Object

Create a Chef::WebUIUser from JSON



114
115
116
117
118
# File 'lib/chef/webui_user.rb', line 114

def self.json_create(o)
  me = new(o)
  me.admin = o["admin"]
  me
end

.list(inflate = false) ⇒ Object



131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/chef/webui_user.rb', line 131

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) ⇒ Object

Load a User by name



150
151
152
153
# File 'lib/chef/webui_user.rb', line 150

def self.load(name)
  r = Chef::REST.new(Chef::Config[:chef_server_url])
  r.get_rest("users/#{name}")
end

Instance Method Details

#admin?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'lib/chef/webui_user.rb', line 74

def admin?
  admin
end

#cdb_destroyObject

Remove this WebUIUser from the CouchDB



162
163
164
# File 'lib/chef/webui_user.rb', line 162

def cdb_destroy
  couchdb.delete("webui_user", @name, @couchdb_rev)
end

#cdb_saveObject

Save this WebUIUser to the CouchDB



173
174
175
176
# File 'lib/chef/webui_user.rb', line 173

def cdb_save
  results = couchdb.store("webui_user", @name, self)
  @couchdb_rev = results["rev"]
end

#createObject

Create the WebUIUser via the REST API



194
195
196
197
198
# File 'lib/chef/webui_user.rb', line 194

def create
  r = Chef::REST.new(Chef::Config[:chef_server_url])
  r.post_rest("users", self)
  self
end

#destroyObject

Remove this WebUIUser via the REST API



167
168
169
170
# File 'lib/chef/webui_user.rb', line 167

def destroy
  r = Chef::REST.new(Chef::Config[:chef_server_url])
  r.delete_rest("users/#{@name}")
end

#saveObject

Save this WebUIUser via the REST API



179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/chef/webui_user.rb', line 179

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) ⇒ Object



87
88
89
# File 'lib/chef/webui_user.rb', line 87

def set_openid(given_openid)
  @openid = given_openid
end

#set_password(password, confirm_password = password) ⇒ Object

Set the password for this object.

Raises:

  • (ArgumentError)


79
80
81
82
83
84
85
# File 'lib/chef/webui_user.rb', line 79

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) ⇒ Object

Serialize this object as a hash



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/chef/webui_user.rb', line 96

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) ⇒ Object



91
92
93
# File 'lib/chef/webui_user.rb', line 91

def verify_password(given_password)
  encrypt_password(given_password) == @password
end