| 
									
										
										
										
											2015-09-08 15:45:34 +03:00
										 |  |  | require "mongo" | 
					
						
							| 
									
										
										
										
											2015-09-08 16:20:27 +03:00
										 |  |  | require "../core/devops-config" | 
					
						
							| 
									
										
										
										
											2015-09-08 15:45:34 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | class UsersPermissionsUpdater | 
					
						
							|  |  |  |   attr_reader :users_collection | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-08 16:20:27 +03:00
										 |  |  |   def initialize | 
					
						
							|  |  |  |     DevopsConfig.read | 
					
						
							|  |  |  |     config = DevopsConfig.config | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     db = config[:mongo_db] || "devops" | 
					
						
							|  |  |  |     host = config[:mongo_host] || "localhost" | 
					
						
							|  |  |  |     port = config[:mongo_port] || 27017
 | 
					
						
							|  |  |  |     user = config[:mongo_user] | 
					
						
							|  |  |  |     password = config[:mongo_password] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     puts | 
					
						
							|  |  |  |     puts "Initialized updater for db '#{db}' located on host '#{host}'" | 
					
						
							| 
									
										
										
										
											2015-09-08 15:45:34 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     @db = Mongo::MongoClient.new(host, port).db(db) | 
					
						
							|  |  |  |     @db.authenticate(user, password) | 
					
						
							|  |  |  |     @users_collection = @db.collection('users') | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def admins | 
					
						
							|  |  |  |     users_collection.find('privileges.server' => 'rwx') | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def readers | 
					
						
							|  |  |  |     users_collection.find('privileges.server' => 'r') | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-08 16:20:27 +03:00
										 |  |  |   def set_priveleges_to_users(users, privilege_value) | 
					
						
							| 
									
										
										
										
											2015-09-08 15:45:34 +03:00
										 |  |  |     ids = users.to_a.map {|u| u['_id']} | 
					
						
							| 
									
										
										
										
											2015-09-08 16:20:27 +03:00
										 |  |  |     puts "users: #{ids.join(', ')}" | 
					
						
							| 
									
										
										
										
											2015-09-08 15:45:34 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-08 16:57:19 +03:00
										 |  |  |     ids.each do |id| | 
					
						
							|  |  |  |       @users_collection.update( | 
					
						
							|  |  |  |         {"_id" => id}, | 
					
						
							|  |  |  |         { | 
					
						
							|  |  |  |           "$set" => { | 
					
						
							|  |  |  |             "privileges.stack" => privilege_value, | 
					
						
							|  |  |  |             "privileges.stack_template" => privilege_value | 
					
						
							|  |  |  |           } | 
					
						
							| 
									
										
										
										
											2015-09-08 15:45:34 +03:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2015-09-08 16:57:19 +03:00
										 |  |  |       ) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-08 15:45:34 +03:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-08 16:20:27 +03:00
										 |  |  | updater = UsersPermissionsUpdater.new | 
					
						
							|  |  |  | updater.set_priveleges_to_users(updater.admins, 'rwx') | 
					
						
							|  |  |  | puts "Admin privileges updated" | 
					
						
							| 
									
										
										
										
											2015-09-08 15:45:34 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-09-08 16:20:27 +03:00
										 |  |  | updater.set_priveleges_to_users(updater.readers, 'r') | 
					
						
							|  |  |  | puts "Readers privileges updated" |