123456789_123456789_123456789_123456789_123456789_

Class: Mongo::Auth::User::View

Relationships & Source Files
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Forwardable
Inherits: Object
Defined in: lib/mongo/auth/user/view.rb

Overview

Defines behavior for user related operation on databases.

Since:

  • 2.0.0

Class Method Summary

Instance Attribute Summary

Instance Method Summary

Constructor Details

.new(database) ⇒ View

Initialize the new user view.

Examples:

Initialize the user view.

View::User.new(database)

Parameters:

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/auth/user/view.rb', line 68

def initialize(database)
  @database = database
end

Instance Attribute Details

#databaseDatabase (readonly)

Returns:

  • (Database)

    database The view’s database.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/auth/user/view.rb', line 29

attr_reader :database

Instance Method Details

#create(user_or_name, options = {}) ⇒ Result

Create a new user in the database.

Examples:

Create a new read/write user.

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

Parameters:

  • user_or_name (Auth::User, String)

    The user object or user name.

  • options (Hash) (defaults to: {})

    The user options.

Options Hash (options):

  • :session (Session)

    The session to use for the operation.

  • :write_concern (Hash)

    The write concern options.

Returns:

  • (Result)

    The command response.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/auth/user/view.rb', line 48

def create(user_or_name, options = {})
  user = generate(user_or_name, options)
  execute_operation(options) do |session|
    Operation::CreateUser.new(
      user: user,
      db_name: database.name,
      session: session,
      write_concern: options[:write_concern] && WriteConcern.get(options[:write_concern]),
    )
  end
end

#execute_operation(options) (private)

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/auth/user/view.rb', line 156

def execute_operation(options)
  client.send(:with_session, options) do |session|
    op = yield session
    op.execute(next_primary(nil, session), context: Operation::Context.new(client: client, session: session))
  end
end

#generate(user, options) (private)

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/auth/user/view.rb', line 152

def generate(user, options)
  user.is_a?(String) ? Auth::User.new({ user: user }.merge(options)) : user
end

#info(name, options = {}) ⇒ Array

Get info for a particular user in the database.

Examples:

Get a particular user’s info.

view.info('emily')

Parameters:

  • name (String)

    The user name.

  • options (Hash) (defaults to: {})

    The options for the info operation.

Options Hash (options):

  • :session (Session)

    The session to use for the operation.

Returns:

  • (Array)

    An array wrapping a document containing information on a particular user.

Since:

  • 2.1.0

[ GitHub ]

  
# File 'lib/mongo/auth/user/view.rb', line 136

def info(name, options = {})
  user_query(name, options).documents
end

#remove(name, options = {}) ⇒ Result

Remove a user from the database.

Examples:

Remove the user from the database.

view.remove('user')

Parameters:

  • name (String)

    The user name.

  • options (Hash) (defaults to: {})

    The options for the remove operation.

Options Hash (options):

  • :session (Session)

    The session to use for the operation.

  • :write_concern (Hash)

    The write concern options.

Returns:

  • (Result)

    The command response.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/auth/user/view.rb', line 86

def remove(name, options = {})
  execute_operation(options) do |session|
    Operation::RemoveUser.new(
      user_name: name,
      db_name: database.name,
      session: session,
      write_concern: options[:write_concern] && WriteConcern.get(options[:write_concern]),
    )
  end
end

#update(user_or_name, options = {}) ⇒ Result

Update a user in the database.

Examples:

Update a user.

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

Parameters:

  • user_or_name (Auth::User, String)

    The user object or user name.

  • options (Hash) (defaults to: {})

    The user options.

Options Hash (options):

  • :session (Session)

    The session to use for the operation.

  • :write_concern (Hash)

    The write concern options.

Returns:

  • (Result)

    The response.

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/auth/user/view.rb', line 111

def update(user_or_name, options = {})
  user = generate(user_or_name, options)
  execute_operation(options) do |session|
    Operation::UpdateUser.new(
      user: user,
      db_name: database.name,
      session: session,
      write_concern: options[:write_concern] && WriteConcern.get(options[:write_concern]),
    )
  end
end

#user_query(name, options = {}) (private)

Since:

  • 2.0.0

[ GitHub ]

  
# File 'lib/mongo/auth/user/view.rb', line 142

def user_query(name, options = {})
  execute_operation(options) do |session|
    Operation::UsersInfo.new(
      user_name: name,
      db_name: database.name,
      session: session
    )
  end
end