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



58
59
60
# File 'lib/mongo/auth/user/view.rb', line 58

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.

Returns:

  • (Result)

    The command response.

Since:

  • 2.0.0



42
43
44
45
46
47
48
# 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) ⇒ 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.

Returns:

  • (Hash)

    A document containing information on a particular user.

Since:

  • 2.1.0



108
109
110
# File 'lib/mongo/auth/user/view.rb', line 108

def info(name)
  user_query(name).documents
end

#remove(name) ⇒ Result

Remove a user from the database.

Examples:

Remove the user from the database.

view.remove('user')

Parameters:

  • name (String)

    The user name.

Returns:

  • (Result)

    The command response.

Since:

  • 2.0.0



72
73
74
75
76
77
# 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 = {}) ⇒ 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.

Returns:

  • (Result)

    The response.

Since:

  • 2.0.0



90
91
92
93
94
95
96
# 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