| 
									
										
										
										
											2015-07-16 17:18:55 +03:00
										 |  |  | require "db/mongo/models/user" | 
					
						
							| 
									
										
										
										
											2015-02-12 13:01:05 +03:00
										 |  |  | module Connectors | 
					
						
							|  |  |  |   class User < Base | 
					
						
							|  |  |  |     include Helpers::InsertCommand, | 
					
						
							|  |  |  |       Helpers::ShowCommand, | 
					
						
							|  |  |  |       Helpers::ListCommand, | 
					
						
							|  |  |  |       Helpers::DeleteCommand, | 
					
						
							|  |  |  |       Helpers::UpdateCommand | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def initialize(db) | 
					
						
							| 
									
										
										
										
											2015-09-18 12:28:16 +03:00
										 |  |  |       super(db) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def collection_name | 
					
						
							|  |  |  |       'users' | 
					
						
							| 
									
										
										
										
											2015-02-12 13:01:05 +03:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def user_auth user, password | 
					
						
							|  |  |  |       u = collection.find('_id' => user, 'password' => password).to_a.first | 
					
						
							|  |  |  |       raise RecordNotFound.new('Invalid username or password') if u.nil? | 
					
						
							| 
									
										
										
										
											2015-07-16 17:18:55 +03:00
										 |  |  |       model_from_bson(u) | 
					
						
							| 
									
										
										
										
											2015-02-12 13:01:05 +03:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-08-03 15:09:04 +03:00
										 |  |  |     def users(ids=nil, options={}) | 
					
						
							| 
									
										
										
										
											2015-02-12 13:01:05 +03:00
										 |  |  |       query = {} | 
					
						
							|  |  |  |       query['_id'] = {'$in' => ids} if ids.is_a?(Array) | 
					
						
							| 
									
										
										
										
											2015-08-03 15:09:04 +03:00
										 |  |  |       list(query, options) | 
					
						
							| 
									
										
										
										
											2015-02-12 13:01:05 +03:00
										 |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def users_names(ids=nil) | 
					
						
							|  |  |  |       users = self.users(ids) | 
					
						
							|  |  |  |       users.map(&:id) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def create_root_user | 
					
						
							|  |  |  |       u = user('root') | 
					
						
							|  |  |  |     rescue RecordNotFound => e | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       root = Devops::Model::User.create_root | 
					
						
							| 
									
										
										
										
											2015-02-12 13:01:05 +03:00
										 |  |  |       collection.insert(root.to_mongo_hash) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     private | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def model_from_bson(bson) | 
					
						
							| 
									
										
										
										
											2015-03-06 12:20:30 +03:00
										 |  |  |       Devops::Model::User.build_from_bson(bson) | 
					
						
							| 
									
										
										
										
											2015-02-12 13:01:05 +03:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | end |