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

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/mongo/auth/user/view.rb

Overview

Defines behaviour for user related operation on databases.

Since:

  • 2.0.0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(database) ⇒ View

Initialize the new user view.

Examples:

Initialize the user view.

View::User.new(database)

Parameters:

Since:

  • 2.0.0



63
64
65
# File 'lib/mongo/auth/user/view.rb', line 63

def initialize(database)
  @database = database
end

Instance Attribute Details

#databaseDatabase (readonly)

Returns database The view's database.

Returns:

  • (Database)

    database The view's database.

Since:

  • 2.0.0



26
27
28
# File 'lib/mongo/auth/user/view.rb', line 26

def database
  @database
end

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.

Returns:

  • (Result)

    The command response.

Since:

  • 2.0.0



44
45
46
47
48
49
50
51
52
53
# File 'lib/mongo/auth/user/view.rb', line 44

def create(user_or_name, options = {})
  user = generate(user_or_name, options)
  client.send(:with_session, options) do |session|
    Operation::Write::Command::CreateUser.new(
      user: user,
      db_name: database.name,
      session: session
    ).execute(next_primary)
  end
end

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

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:

  • (Hash)

    A document containing information on a particular user.

Since:

  • 2.1.0



127
128
129
# File 'lib/mongo/auth/user/view.rb', line 127

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.

Returns:

  • (Result)

    The command response.

Since:

  • 2.0.0



80
81
82
83
84
85
86
87
88
# File 'lib/mongo/auth/user/view.rb', line 80

def remove(name, options = {})
  client.send(:with_session, options) do |session|
    Operation::Write::Command::RemoveUser.new(
      user_name: name,
      db_name: database.name,
      session: session
    ).execute(next_primary)
  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.

Returns:

  • (Result)

    The response.

Since:

  • 2.0.0



103
104
105
106
107
108
109
110
111
112
# File 'lib/mongo/auth/user/view.rb', line 103

def update(user_or_name, options = {})
  client.send(:with_session, options) do |session|
    user = generate(user_or_name, options)
    Operation::Write::Command::UpdateUser.new(
      user: user,
      db_name: database.name,
      session: session
    ).execute(next_primary)
  end
end