class Mongo::Auth::User::View

Defines behaviour for user related operation on databases.

@since 2.0.0

Attributes

database[R]

@return [ Database ] database The view's database.

Public Class Methods

new(database) click to toggle source

Initialize the new user view.

@example Initialize the user view.

View::User.new(database)

@param [ Mongo::Database ] database The database the view is for.

@since 2.0.0

# File lib/mongo/auth/user/view.rb, line 58
def initialize(database)
  @database = database
end

Public Instance Methods

create(user_or_name, options = {}) click to toggle source

Create a new user in the database.

@example Create a new read/write user.

view.create('user', password: 'password', roles: [ 'readWrite' ])

@param [ Auth::User, String ] user_or_name The user object or user name. @param [ Hash ] options The user options.

@return [ Result ] The command response.

@since 2.0.0

# File lib/mongo/auth/user/view.rb, line 42
def create(user_or_name, options = {})
  user = generate(user_or_name, options)
  Operation::Write::CreateUser.new(
    user: user,
    db_name: database.name
  ).execute(next_primary)
end
info(name) click to toggle source

Get info for a particular user in the database.

@example Get a particular user's info.

view.info('emily')

@param [ String ] name The user name.

@return [ Hash ] A document containing information on a particular user.

@since 2.1.0

# File lib/mongo/auth/user/view.rb, line 108
def info(name)
  user_query(name).documents
end
remove(name) click to toggle source

Remove a user from the database.

@example Remove the user from the database.

view.remove('user')

@param [ String ] name The user name.

@return [ Result ] The command response.

@since 2.0.0

# File lib/mongo/auth/user/view.rb, line 72
def remove(name)
  Operation::Write::RemoveUser.new(
    user_name: name,
    db_name: database.name
  ).execute(next_primary)
end
update(user_or_name, options = {}) click to toggle source

Update a user in the database.

@example Update a user.

view.update('name', password: 'testpwd')

@param [ Auth::User, String ] user_or_name The user object or user name. @param [ Hash ] options The user options.

@return [ Result ] The response.

@since 2.0.0

# File lib/mongo/auth/user/view.rb, line 90
def update(user_or_name, options = {})
  user = generate(user_or_name, options)
  Operation::Write::UpdateUser.new(
    user: user,
    db_name: database.name
  ).execute(next_primary)
end

Private Instance Methods

generate(user, options) click to toggle source
# File lib/mongo/auth/user/view.rb, line 121
def generate(user, options)
  user.is_a?(String) ? Auth::User.new({ user: user }.merge(options)) : user
end
user_query(name) click to toggle source
# File lib/mongo/auth/user/view.rb, line 114
def user_query(name)
  Operation::Commands::UserQuery.new(
    user_name: name,
    db_name: database.name
  ).execute(next_primary)
end