def get_updated_by_fieldname(self, field_chef, policy, obj, computer): updated_path = '.'.join(field_chef.split('.')[:3]) if is_user_policy(field_chef): if obj['type'] != 'user': user = computer['user'] else: user = obj updated_path += '.users.' + get_username_chef_format(user) updated_path += '.updated_by' return updated_path
def get_policy_attr_to_check(self, policy, user): return '%s.users.%s.can_share' % (policy['path'], get_username_chef_format(user))
def check_user_data(self, user): if user['type'] != 'user': raise ValueError('user must be an user') if ((not 'email' in user or user['email'] == '') and (not 'first_name' in user or user['first_name'] == '') and (not 'last_name' in user or user['last_name'] == '')): # Nothing to do return computers = self.db.nodes.find_one({"_id": ObjectId(user['_id']) })['computers'] for computer_id in computers: computer = self.db.nodes.find_one({"_id": ObjectId(computer_id)}) if "node_chef_id" in computer: # Check Chef node node = ChefNode(computer['node_chef_id'], self.api) logger.info("Computer: %s Chef ID: %s" % (computer['name'], computer['node_chef_id'])) if not node.exists: logger.error("No Chef node with ID %s!" % (computer['node_chef_id'])) else: if not node.normal.has_dotted('gecos_info'): node.normal.set_dotted('gecos_info', {}) if not node.normal.has_dotted('gecos_info.users'): node.normal.set_dotted('gecos_info.users', {}) username = get_username_chef_format(user) if not node.normal.has_dotted('gecos_info.users.%s' % (username)): node.normal.set_dotted( 'gecos_info.users.%s' % (username), {}) updated = False if (not node.normal.has_dotted( 'gecos_info.users.%s.email' % (username)) or node.normal.get_dotted( 'gecos_info.users.%s.email' % (username)) != user['email']): node.normal.set_dotted( 'gecos_info.users.%s.email' % (username), user['email']) updated = True if (not node.normal.has_dotted( 'gecos_info.users.%s.firstName' % (username)) or node.normal.get_dotted( 'gecos_info.users.%s.firstName' % (username)) != user['first_name']): node.normal.set_dotted( 'gecos_info.users.%s.firstName' % (username), user['first_name']) updated = True if (not node.normal.has_dotted( 'gecos_info.users.%s.lastName' % (username)) or node.normal.get_dotted( 'gecos_info.users.%s.lastName' % (username)) != user['last_name']): node.normal.set_dotted( 'gecos_info.users.%s.lastName' % (username), user['last_name']) updated = True if updated: logger.info( "Updating user %s data in computer: %s Chef ID: %s" % (user['name'], computer['name'], computer['node_chef_id'])) node.save() else: logger.error("No Chef ID in '%s' computer!" % (computer['name']))