Module: AxisNetcam::Camera::Users

Included in:
AxisNetcam::Camera
Defined in:
lib/axis-netcam/camera.rb

Overview

Functionality related to managing the camera’s user and group lists.

Instance Method Summary collapse

Instance Method Details

#add_or_update_user(user) ⇒ Object

Same as add_user, but updates the user account instead of creating it if it already exists.



257
258
259
260
261
262
263
# File 'lib/axis-netcam/camera.rb', line 257

def add_or_update_user(user)
  if user_exists?(user[:username], user)
    update_user(user[:username], user)
  else
    add_user(user)
  end
end

#add_user(user) ⇒ Object

Adds a new user based on the given hash. The hash must have :username, :password, and :comment elements.



228
229
230
231
232
233
234
235
236
237
238
# File 'lib/axis-netcam/camera.rb', line 228

def add_user(user)
  params = {
    'action'  => 'add',
    'user'    => user[:username],
    'pwd'     => user[:password],
    'comment' => user[:comment],
    'grp'     => 'users',
    'sgrp'    => 'axview'
  }
  axis_action("admin/pwdgrp.cgi", params)
end

#remove_user(username) ⇒ Object

Deletes the user with the given username.



266
267
268
269
270
271
272
# File 'lib/axis-netcam/camera.rb', line 266

def remove_user(username)
  params = {
    'action'  => 'remove',
    'user'    => username,
  }
  axis_action("admin/pwdgrp.cgi", params)
end

#update_user(username, attributes) ⇒ Object

Updates a user based on the given hash.

username

specifies the username of the user to update.

attributes

must be a hash with new values for :password and :comment.



243
244
245
246
247
248
249
250
251
252
253
# File 'lib/axis-netcam/camera.rb', line 243

def update_user(username, attributes)
  params = {
    'action'  => 'update',
    'user'    => username,
    'pwd'     => user[:password],
    'comment' => user[:comment],
    'grp'     => 'users',
    'sgrp'    => 'axview'
  }
  axis_action("admin/pwdgrp.cgi", params)
end

#user_exists?(username) ⇒ Boolean

Checks if the user with the given username exists.

Returns:

  • (Boolean)


291
292
293
# File 'lib/axis-netcam/camera.rb', line 291

def user_exists?(username)
  users.include? username
end

#usersObject

Returns an array with the usernames of the users on the camera.



275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/axis-netcam/camera.rb', line 275

def users
  str = axis_action("admin/pwdgrp.cgi", {'action' => 'get'})
  return false unless str
  usernames = []
  str.split.collect do |u| 
    u =~ /.*?="(.*)"/
    if $~
      usernames += $~[1].split(',')
    else
      usernames += []
    end
  end
  usernames.uniq!
end