Class: Visor::Auth::Backends::MongoDB
- 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
-
.connect(opts = {}) ⇒ Object
Initializes a MongoDB Backend instance.
Instance Method Summary collapse
-
#connection ⇒ Mongo::Collection
Establishes and returns a MongoDB database connection.
-
#delete_all! ⇒ Object
Delete all registered users.
-
#delete_user(access_key) ⇒ BSON::OrderedHash
Delete a registered user.
-
#get_user(access_key) ⇒ BSON::OrderedHash
Returns an user information.
-
#get_users(filters = {}) ⇒ Array
Returns an array with the registered users.
-
#initialize(opts) ⇒ MongoDB
constructor
A new instance of MongoDB.
-
#post_user(user) ⇒ BSON::OrderedHash
Create a new user record for the given information.
-
#put_user(access_key, update) ⇒ BSON::OrderedHash
Update an user information.
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.
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
#connection ⇒ Mongo::Collection
Establishes and returns a MongoDB database connection.
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.
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.
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.
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.
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.
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 |