def ensure_valid_user_type(self, user_type): """Ensure that a given user_type is valid.""" for user_type_itx in self.get_user_types(): if user_type is user_type_itx or user_type == user_type_itx.__name__: return user_type_itx raise InvalidUserTypeException('An invalid user type has been passed')
def get_user_by_username(self, username): """Obtain a user object for the given username.""" generic_object = UserBase(username=username) for user_class in UserBase.__subclasses__(): if str(user_class.__name__) == str(generic_object.get_user_type()): user_object = user_class(username=username) self._register_object(user_object) return user_object raise InvalidUserTypeException('Failed to determine user type for %s' % generic_object.get_username())
def create(self, username, password, user_type=LocalUser): """Create a user.""" self._get_registered_object('auth').assert_permission( PERMISSIONS.MANAGE_USERS ) if not user_type.CAN_CREATE: raise InvalidUserTypeException('Cannot create this type of user') if not password: raise BlankPasswordException('Password cannot be blank') # Ensure that username is not part of a reserved namespace for user_class in self.get_user_types(): if (user_class is not user_type and user_class.USER_PREFIX is not None and username.startswith(user_class.USER_PREFIX)): raise InvalidUsernameException( 'Username is within a reserved namespace' ) # Ensure that there is not a duplicate user if user_type._check_exists(username): raise UserAlreadyExistsException('There is a user with the same username \'%s\'' % username) # Ensure valid user type user_type = self.ensure_valid_user_type(user_type) # Generate password salt for user and hash password salt = user_type._generate_salt() hashed_password = user_type._hash_string(password, salt) # Create config for user and update MCVirt config user_config = user_type.get_default_config() user_config['password'] = hashed_password user_config['salt'] = salt user_config['user_type'] = user_type.__name__ def update_config(config): config['users'][username] = user_config MCVirtConfig().update_config(update_config, 'Create user \'%s\'' % username) if user_type.DISTRIBUTED and self._is_cluster_master: # Create the user on the other nodes in the cluster def remote_command(node_connection): remote_user_factory = node_connection.get_connection('user_factory') remote_user_factory.create(username, password) cluster = self._get_registered_object('cluster') cluster.run_remote_command(remote_command)
def generate_user(self, user_type): """Remove any existing connection user and generates credentials for a new connection user. """ # Ensure valid user type user_type = self.ensure_valid_user_type(user_type) # Ensure that users can be generated if not user_type.CAN_GENERATE: raise InvalidUserTypeException('Users of type \'%s\' cannot be generated' % user_type.__name__) if user_type.UNIQUE: # Delete any old connection users for old_user_object in self.get_all_user_objects(user_classes=[user_type]): old_user_object.delete() username = user_type.USER_PREFIX + user_type.generate_password(32, numeric_only=True) password = user_type.generate_password(32) self.create(username=username, password=password, user_type=user_type) return username, password
def ensure_valid_user_type(self, user_type): """Ensure that a given user_type is valid.""" if user_type not in self.get_user_types(): raise InvalidUserTypeException( 'An invalid user type has been passed')
def set_password(self, new_password): """Default functionality for password change is to throw an exception""" raise InvalidUserTypeException( 'Cannot change password for this type of user')