Class: Arango::User

Inherits:
Object
  • Object
show all
Includes:
Helper::Satisfaction
Defined in:
lib/arango/user.rb

Overview

Arango User

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helper::Satisfaction

#satisfy_category?, #satisfy_class?, #satisfy_class_or_string?, #satisfy_module?, #satisfy_module_or_nil?, #satisfy_module_or_string?, #warning_deprecated

Constructor Details

#initialize(server:, password: "", name:, extra: {}, active: nil) ⇒ User

Create new user



93
94
95
96
97
98
99
# File 'lib/arango/user.rb', line 93

def initialize(server:, password: "", name:, extra: {}, active: nil)
  @server = server
  @password = password
  @name     = name
  @extra    = extra
  @active   = active
end

Instance Attribute Details

#activeObject

Returns the value of attribute active.



63
64
65
# File 'lib/arango/user.rb', line 63

def active
  @active
end

#extraObject

Returns the value of attribute extra.



63
64
65
# File 'lib/arango/user.rb', line 63

def extra
  @extra
end

#nameObject

Returns the value of attribute name.



63
64
65
# File 'lib/arango/user.rb', line 63

def name
  @name
end

#password=(value) ⇒ Object (writeonly)

Sets the attribute password

Parameters:

  • value

    the value to set the attribute password to.



64
65
66
# File 'lib/arango/user.rb', line 64

def password=(value)
  @password = value
end

#serverObject (readonly)

Returns the value of attribute server.



65
66
67
# File 'lib/arango/user.rb', line 65

def server
  @server
end

Class Method Details

.all(server: Arango.current_server) ⇒ Array<Arango::User>

Retrieves all users.

Parameters:

Returns:



12
13
14
15
# File 'lib/arango/user.rb', line 12

def all(server: Arango.current_server)
  result = Arango::Requests::User::List.execute(server: server)
  result.map { |u| self.new(server: server, **u) }
end

.create(name:, password: "", extra: {}, active: nil, server: Arango.current_server) ⇒ Arango::User

Create new user

Parameters:

  • name (String)
  • password (String) (defaults to: "")
  • extra (defaults to: {})
  • server (Arango::Server) (defaults to: Arango.current_server)

    , defaults to Arango.current_server

Returns:



23
24
25
# File 'lib/arango/user.rb', line 23

def create(name:, password: "", extra: {}, active: nil, server: Arango.current_server)
  self.new.create(server: server, password: password, extra: extra, active: active)
end

.drop(name:, server: Arango.current_server) ⇒ Arango::Result

Delete user by name

Parameters:

Returns:



48
49
50
# File 'lib/arango/user.rb', line 48

def drop(name:, server: Arango.current_server)
  Arango::Requests::User::Delete.execute(server: server, args: { user: name })
end

.exists?(name:, server: Arango.current_server) ⇒ Boolean

Check if user exists

Parameters:

Returns:

  • (Boolean)


56
57
58
59
60
# File 'lib/arango/user.rb', line 56

def exists?(name:, server: Arango.current_server)
  !!Arango::Requests::User::Get.execute(server: server, args: { user: name })
rescue
  false
end

.get(name:, server: Arango.current_server) ⇒ Arango::User

Get user by name

Parameters:

Returns:



31
32
33
34
# File 'lib/arango/user.rb', line 31

def get(name:, server: Arango.current_server)
  result = Arango::Requests::User::Get.execute(server: server, args: { user: name })
  self.new(server: server, **result)
end

.list(server: Arango.current_server) ⇒ Array

List users # @param server [Arango::Server], defaults to Arango.current_server

Returns:

  • (Array)

    of user names



39
40
41
42
# File 'lib/arango/user.rb', line 39

def list(server: Arango.current_server)
  result = Arango::Requests::User::List.execute(server: server)
  result.map { |u| u.name }
end

Instance Method Details

#add_collection_access(grant:, database:, collection:) ⇒ Object

Grant access to database collection

Parameters:

  • grant (String)

    one of ‘rw`, `ro`, `none`

  • database (String)

    or [Arango::Database]

  • database (String)

    or [Arango::DocumentCollection]

Returns:



136
137
138
139
140
141
142
143
144
145
# File 'lib/arango/user.rb', line 136

def add_collection_access(grant:, database:, collection:)
  satisfy_category?(grant, %w[rw ro none])
  satisfy_class_or_string?(database, Arango::Database)
  satisfy_module_or_string?(collection, Arango::DocumentCollection::Mixin)
  database = database.name     if database.is_a?(Arango::Database)
  collection = collection.name if collection.is_a?(Arango::DocumentCollection)
  body = { grant: grant }
  result = Arango::Requests::User::SetCollectionAccessLevel.execute(user: self.name, database: database, collection: collection, body: body)
  return return_directly?(result) ? result : result[:"#{database}/#{collection}"]
end

#add_database_access(grant:, database:) ⇒ Object

Grant access to database

Parameters:

  • grant (String)

    one of ‘rw`, `ro`, `none`

  • database (String)

    or [Arango::Database]

Returns:



116
117
118
119
120
121
122
123
# File 'lib/arango/user.rb', line 116

def add_database_access(grant:, database:)
  satisfy_category?(grant, %w[rw ro none])
  satisfy_class_or_string?(database, Arango::Database)
  database = database.name if database.is_a?(Arango::Database)
  body = { grant: grant }
  result = Arango::Requests::User::SetDatabaseAccessLevel.execute(user: self.name, database: database, body: body)
  return return_directly?(result) ? result : result[database.to_sym]
end

#collection_access(database:, collection:) ⇒ Object

Get user access level for database collection

Parameters:

  • database (String)

    or [Arango::Database]

  • database (String)

    or [Arango::DocumentCollection]



190
191
192
193
194
195
196
197
# File 'lib/arango/user.rb', line 190

def collection_access(database:, collection:)
  satisfy_class_or_string?(database, Arango::Database)
  satisfy_module_or_string?(collection, Arango::DocumentCollection::Mixin)
  database = database.name     if database.is_a?(Arango::Database)
  collection = collection.name if collection.is_a?(Arango::DocumentCollection)
  result = Arango::Requests::User::GetCollectionAccessLevel.execute(name: self.name, database: database, collection: collection)
  return return_directly?(result) ? result : result[:result]
end

#createObject



101
102
103
104
105
106
107
108
109
110
# File 'lib/arango/user.rb', line 101

def create
  body = {
    user: name,
    passwd: password,
    extra: extra,
    active: active
  }
  Arango::Requests::User::Create.new(server: server, body: body).execute
  self
end

#database_access(database:) ⇒ Object

Get user access level for database

Parameters:

  • database (String)

    or [Arango::Database]



180
181
182
183
184
185
# File 'lib/arango/user.rb', line 180

def database_access(database:)
  satisfy_class_or_string?(database, Arango::Database)
  database = database.name if database.is_a?(Arango::Database)
  result = Arango::Requests::User::GetDatabaseAccessLevel.execute(name: self.name, database: database)
  return return_directly?(result) ? result : result[:result]
end

#grant(database:) ⇒ Object

Grant read-write access to database

Parameters:

  • database (String)

    or [Arango::Database]



127
128
129
# File 'lib/arango/user.rb', line 127

def grant(database:)
  add_database_access(grant: "rw", database: database)
end

#list_access(full: nil) ⇒ Object Also known as: databases

List databases a user has access to @param: full



171
172
173
174
175
# File 'lib/arango/user.rb', line 171

def list_access(full: nil)
  query = { full: full }
  result = Arango::Requests::User::ListDatabases.execute(body: query)
  return return_directly?(result) ? result : result[:result]
end

#replace(password: @password, active: @active, extra: @extra) ⇒ Object

Replace user record



70
71
72
73
74
75
76
77
78
79
# File 'lib/arango/user.rb', line 70

def replace(password: @password, active: @active, extra: @extra)
  body = {
    passwd: password,
    active: active,
    extra: extra
  }
  result = Arango::Requests::User::Replace.execute(body: body)
  @password = password
  return_element(result)
end

#revoke_collection_access(database:, collection:) ⇒ Object

Revoke user access to database collection

Parameters:

  • database (String)

    or [Arango::Database]

  • database (String)

    or [Arango::DocumentCollection]



160
161
162
163
164
165
166
167
# File 'lib/arango/user.rb', line 160

def revoke_collection_access(database:, collection:)
  satisfy_class_or_string?(database, Arango::Database)
  satisfy_module_or_string?(collection, Arango::DocumentCollection)
  database = database.name     if database.is_a?(Arango::Database)
  collection = collection.name if collection.is_a?(Arango::DocumentCollection)
  result = Arango::Requests::User::ClearCollectionAccessLevel.execute(user: self.name, database: database, collection: collection)
  return return_directly?(result) ? result : true
end

#revoke_database_access(database:) ⇒ Object Also known as: revoke

Revoke user access to database

Parameters:

  • database (String)

    or [Arango::Database]



149
150
151
152
153
154
# File 'lib/arango/user.rb', line 149

def revoke_database_access(database:)
  satisfy_class_or_string?(database, Arango::Database)
  database = database.name if database.is_a?(Arango::Database)
  result = Arango::Requests::User::ClearDatabaseAccessLevel.execute(user: self.name, database: database)
  return return_directly?(result) ? result : true
end

#update(password: @password, active: @active, extra: @extra) ⇒ Object



81
82
83
84
85
86
87
88
89
90
# File 'lib/arango/user.rb', line 81

def update(password: @password, active: @active, extra: @extra)
  body = {
    passwd: password,
    active: active,
    extra: extra
  }
  result = Arango::Requests::User::Update.execute(body: body)
  @password = password
  return_element(result)
end