| 
									
										
										
										
											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) | 
					
						
							|  |  |  |       self.collection = db.collection('users') | 
					
						
							|  |  |  |     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? | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def users(ids=nil) | 
					
						
							|  |  |  |       query = {} | 
					
						
							|  |  |  |       query['_id'] = {'$in' => ids} if ids.is_a?(Array) | 
					
						
							|  |  |  |       list(query) | 
					
						
							|  |  |  |     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 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def check_user_privileges(id, cmd, required_privelege) | 
					
						
							|  |  |  |       user = show(id) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-15 16:52:50 +03:00
										 |  |  |       unless Devops::Model::User::PRIVILEGES.include?(required_privelege) | 
					
						
							| 
									
										
										
										
											2015-02-12 13:01:05 +03:00
										 |  |  |         raise InvalidPrivileges.new("Access internal problem with privilege '#{required_privelege}'") | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |       unless user.can?(cmd, required_privelege) | 
					
						
							|  |  |  |         raise InvalidPrivileges.new("Access denied for '#{user.id}'") | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       true | 
					
						
							|  |  |  |     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 |