Class: Visor::Auth::Backends::MongoDB

Inherits:
Base
  • Object
show all
Includes:
Common::Exception
Defined in:
lib/auth/backends/mongo_db.rb

Overview

The MongoDB Backend for the VISoR Auth.

Constant Summary collapse

DEFAULT_DB =

Connection constants

Default MongoDB database

'visor'
DEFAULT_HOST =

Default MongoDB host address

'127.0.0.1'
DEFAULT_PORT =

Default MongoDB host port

27017
DEFAULT_USER =

Default MongoDB user

nil
DEFAULT_PASSWORD =

Default MongoDB password

nil

Constants inherited from Base

Base::ALL, Base::MANDATORY, Base::READONLY

Instance Attribute Summary

Attributes inherited from Base

#conn, #db, #host, #password, #port, #user

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#set_protected_post, #set_protected_put, #to_sql_insert, #to_sql_update, #to_sql_where, #validate_data_post, #validate_data_put, #validate_query_filters

Constructor Details

#initialize(opts) ⇒ MongoDB

Returns a new instance of MongoDB.



51
52
53
54
# File 'lib/auth/backends/mongo_db.rb', line 51

def initialize(opts)
  super opts
  @conn = connection
end

Class Method Details

.connect(opts = {}) ⇒ Object

Initializes a MongoDB Backend instance.

Parameters:

  • [Hash] (Hash)

    a customizable set of options

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

    a customizable set of options

Options Hash (opts):

  • :uri (String)

    The connection uri, if provided, no other option needs to be setted.

  • :db (String) — default: DEFAULT_DB

    The wanted database.

  • :host (String) — default: DEFAULT_HOST

    The host address.

  • :port (Integer) — default: DEFAULT_PORT

    The port to be used.

  • :user (String) — default: DEFAULT_USER

    The user to be used.

  • :password (String) — default: DEFAULT_PASSWORD

    The password to be used.

  • :conn (Object)

    The connection pool to access database.



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/auth/backends/mongo_db.rb', line 39

def self.connect(opts = {})
  opts[:uri]      ||= ''
  uri             = URI.parse(opts[:uri])
  opts[:db]       = uri.path ? uri.path.gsub('/', '') : DEFAULT_DB
  opts[:host]     = uri.host || DEFAULT_HOST
  opts[:port]     = uri.port || DEFAULT_PORT
  opts[:user]     = uri.user || DEFAULT_USER
  opts[:password] = uri.password || DEFAULT_PASSWORD

  self.new opts
end

Instance Method Details

#connectionMongo::Collection

Establishes and returns a MongoDB database connection.

Returns:

  • (Mongo::Collection)

    A MongoDB collection object.



60
61
62
63
64
# File 'lib/auth/backends/mongo_db.rb', line 60

def connection
  db = Mongo::Connection.new(@host, @port, :pool_size => 10, :pool_timeout => 5).db(@db)
  db.authenticate(@user, @password) unless @user.empty? && @password.empty?
  db.collection('users')
end

#delete_all!Object

Delete all registered users.



114
115
116
# File 'lib/auth/backends/mongo_db.rb', line 114

def delete_all!
  @conn.remove
end

#delete_user(access_key) ⇒ BSON::OrderedHash

Delete a registered user.

Parameters:

  • access_key (String)

    The user access_key.

Returns:

  • (BSON::OrderedHash)

    The deleted image metadata.

Raises:

  • (NotFound)

    If user was not found.



105
106
107
108
109
110
# File 'lib/auth/backends/mongo_db.rb', line 105

def delete_user(access_key)
  user = @conn.find_one(access_key: access_key)
  raise NotFound, "No user found with Access Key '#{access_key}'." unless user
  @conn.remove(access_key: access_key)
  user
end

#get_user(access_key) ⇒ BSON::OrderedHash

Returns an user information.

Parameters:

  • access_key (String)

    The user access_key.

Returns:

  • (BSON::OrderedHash)

    The requested user information.

Raises:

  • (NotFound)

    If user was not found.



91
92
93
94
95
# File 'lib/auth/backends/mongo_db.rb', line 91

def get_user(access_key)
  user = @conn.find_one(access_key: access_key)
  raise NotFound, "No user found with Access Key '#{access_key}'." unless user
  user
end

#get_users(filters = {}) ⇒ Array

Returns an array with the registered users.

Parameters:

  • [Hash] (Hash)

    a customizable set of options

Returns:

  • (Array)

    The users information.

Raises:

  • (NotFound)

    If there are no registered users.



75
76
77
78
79
80
81
# File 'lib/auth/backends/mongo_db.rb', line 75

def get_users(filters = {})
  validate_query_filters filters unless filters.empty?
  users = @conn.find(filters).to_a
  raise NotFound, "No users found." if users.empty? && filters.empty?
  raise NotFound, "No users found with given parameters." if users.empty?
  users
end

#post_user(user) ⇒ BSON::OrderedHash

Create a new user record for the given information.

Parameters:

  • user (Hash)

    The user information.

Returns:

  • (BSON::OrderedHash)

    The already added user information.

Raises:

  • (Invalid)

    If user information validation fails.

  • (ConflictError)

    If an access_key was already taken.



127
128
129
130
131
132
133
134
# File 'lib/auth/backends/mongo_db.rb', line 127

def post_user(user)
  validate_data_post user
  exists = @conn.find_one(access_key: user[:access_key])
  raise ConflictError, "The Access Key '#{user[:access_key]}' was already taken." if exists
  set_protected_post user
  @conn.insert(user)
  self.get_user(user[:access_key])
end

#put_user(access_key, update) ⇒ BSON::OrderedHash

Update an user information.

Parameters:

  • access_key (String)

    The user access_key.

  • update (Hash)

    The user information update.

Returns:

  • (BSON::OrderedHash)

    The updated user information.

Raises:

  • (Invalid)

    If user information validation fails.

  • (ConflictError)

    If an access_key was already taken.

  • (NotFound)

    If user was not found.



147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/auth/backends/mongo_db.rb', line 147

def put_user(access_key, update)
  validate_data_put update
  user = @conn.find_one(access_key: access_key)
  raise NotFound, "No user found with Access Key '#{access_key}'." unless user
  if update[:access_key]
    exists = @conn.find_one(access_key: update[:access_key])
    raise ConflictError, "The Access Key '#{update[:access_key]}' was already taken." if exists
  end
  set_protected_put update
  @conn.update({access_key: access_key}, :$set => update)
  self.get_user(update[:access_key] || access_key)
end