def update_event(self, user, event, mkrelations=True, commit=True): self.logger.debug('User {0} updates a event {1}'.format( user.username, event.identifier)) try: # the the creator user = self.user_broker.get_by_id(user.identifier) self.set_extended_logging(event, user, user.group, False) # TODO relations on update self.event_broker.update(event, False) # generate relations if needed! """ attributes = get_all_attributes_from_event(event) if (mkrelations == 'True' or mkrelations is True) and attributes: self.relation_broker.generate_bulk_attributes_relations(event, attributes, False) """ self.event_broker.do_commit(commit) return event except ValidationException: message = ObjectValidator.getFirstValidationError(event) raise ControllerException( u'Could not update object definition due to: {0}'.format( message)) except BrokerException as error: raise ControllerException(error)
def insert(self, instance, commit=True, validate=True): """ Insert a <<get_broker_class()>> :param instance: The get_broker_class() to be inserted :type instance: extension of Base Note: handles the commit and the identifier of the user is taken into account if set """ if validate: errors = not instance.validate() if errors: raise ValidationException( 'Instance to be inserted is invalid.{0}'.format( ObjectValidator.getFirstValidationError(instance))) try: self.session.add(instance) self.do_commit(commit) except sqlalchemy.exc.IntegrityError as error: raise IntegrityException(error) except sqlalchemy.exc.DatabaseError as error: self.session.rollback() raise BrokerException(error) except sqlalchemy.exc.SQLAlchemyError as error: self.session.rollback() raise BrokerException(error)
def update(self, instance, commit=True, validate=True): """ updates an <<get_broker_class()>> :param instance: The get_broker_class() to be updated :type instance: extension of Base """ if validate: errors = not instance.validate() if errors: raise ValidationException( 'Instance to be inserted is invalid.{0}'.format( ObjectValidator.getFirstValidationError(instance))) # an elo den update try: self.session.merge(instance) self.do_commit(commit) except sqlalchemy.exc.IntegrityError as error: raise IntegrityException(error) except sqlalchemy.exc.DatabaseError as error: self.session.rollback() raise BrokerException(error) except sqlalchemy.exc.SQLAlchemyError as error: self.session.rollback() raise BrokerException(error) self.do_commit(commit)
def update_condition(self, condition): try: self.condition_broker.update(condition, True) except ValidationException as error: message = ObjectValidator.getFirstValidationError(condition) raise ControllerException( u'Could not add condition due to: {0}'.format(message)) except (BrokerException) as error: raise ControllerException(error)
def insert_condition(self, condition, commit=True): try: self.condition_broker.insert(condition, False) self.condition_broker.do_commit(commit) except ValidationException as error: message = ObjectValidator.getFirstValidationError(condition) raise ControllerException( u'Could not add condition due to: {0}'.format(message)) except (BrokerException) as error: raise ControllerException(error)
def update_group(self, group): try: self.group_broker.update(group) return group except ValidationException as error: message = ObjectValidator.getFirstValidationError(group) raise ControllerException( u'Could not update group due to: {0}'.format(message)) except BrokerException as error: raise ControllerException(error)
def add_attribute(self, attribute): """ Add an attribute to this event :param attribute: Attribute to be added :type attribute: Attribute """ errors = not attribute.validate() if errors: raise ValidationException(ValidationException(ObjectValidator.getFirstValidationError(attribute))) function = getattr(self.attributes, 'append') function(attribute)
def add_group(self, group): """ Add a group to this event :param group: Group to be added :type group: Group """ errors = not group.validate() if errors: raise ValidationException(u'Invalid Group:' + ValidationException(ObjectValidator.getFirstValidationError(group))) function = getattr(self.maingroups, 'append') function(group)
def add_dbject(self, obj): """ Add an object to this event :param obj: Obejct to be added :type obj: Obejct """ errors = not obj.validate() if errors: raise ValidationException(u'Invalid Object:' + ValidationException(ObjectValidator.getFirstValidationError(obj))) function = getattr(self.objects, 'append') function(obj)
def update_mail(self, mail_template, user): try: user = self.user_broker.get_by_id(user.identifier) self.set_simple_logging(mail_template, user, insert=False) mail_template = self.mail_broker.update(mail_template) return mail_template except ValidationException as error: message = ObjectValidator.getFirstValidationError(mail_template) raise ControllerException( u'Could not update mail due to: {0}'.format(message)) except BrokerException as error: raise ControllerException(error)
def insert_group(self, group, validate=True, commit=True): try: self.group_broker.insert(group, commit, validate) except ValidationException as error: message = ObjectValidator.getFirstValidationError(group) raise ControllerException( u'Could not add group due to: {0}'.format(message)) except IntegrityException as error: raise ControllerIntegrityException(error) except (BrokerException) as error: raise ControllerException(error)
def update_reference_definition(self, reference_definition, user): try: user = self.user_broker.get_by_id(user.identifier) self.set_simple_logging(reference_definition, user, insert=False) reference_definition = self.reference_definition_broker.update( reference_definition) return reference_definition except ValidationException as error: message = ObjectValidator.getFirstValidationError( reference_definition) raise ControllerException( u'Could not update reference due to: {0}'.format(message)) except BrokerException as error: raise ControllerException(error)
def insert_object_definition(self, obj, user, commit=True): try: obj.chksum = gen_obj_chksum(obj) user = self.user_broker.get_by_id(user.identifier) self.set_simple_logging(obj, user, insert=True) self.obj_def_broker.insert(obj, False) self.obj_def_broker.do_commit(commit) return obj except ValidationException as error: message = ObjectValidator.getFirstValidationError(obj) raise ControllerException( u'Could not insert object definition due to: {0}'.format( message)) except BrokerException as error: raise ControllerException(error)
def update_user(self, user): try: if user.gpg_key: self.mail_controller.import_gpg_key(user.gpg_key) self.user_broker.update(user) # add it again in case of changes # TODO import gpg key # if user.gpg_key: # self.mail_handler.import_gpg_key(user.gpg_key) return user except ValidationException as error: message = ObjectValidator.getFirstValidationError(user) raise ControllerException(u'Could not update user due to: {0}'.format(message)) except BrokerException as error: raise ControllerException(error)
def update_object_definition(self, obj, user, commit=True): if obj.cybox_std: raise ControllerException( u'Could not update object definition as the object is part of the cybox standard' ) try: obj.chksum = gen_obj_chksum(obj) user = self.user_broker.get_by_id(user.identifier) self.set_simple_logging(obj, user, insert=False) self.obj_def_broker.update(obj, commit) return obj except ValidationException as error: message = ObjectValidator.getFirstValidationError(obj) raise ControllerException( u'Could not update object definition due to: {0}'.format( message)) except BrokerException as error: raise ControllerException(error)
def update_attribute_definition(self, attribute, user, commit=True): if attribute.cybox_std: raise ControllerException( u'Could not update attribute definition as the attribute is part of the cybox standard' ) try: attribute.chksum = gen_attr_chksum(attribute) user = self.user_broker.get_by_id(user.identifier) self.set_simple_logging(attribute, user, insert=False) attribute = self.attr_def_broker.update(attribute, commit) return attribute except ValidationException as error: message = ObjectValidator.getFirstValidationError(attribute) raise ControllerException( u'Could not update attribute definition due to: {0}'.format( message)) except BrokerException as error: raise ControllerException(error)
def insert_attribute_definition(self, attribute, user, commit=True): try: attribute.chksum = gen_attr_chksum(attribute) user = self.user_broker.get_by_id(user.identifier) # check if handler is associated if not attribute.attribute_handler: handler = self.handler_broker.get_by_id( attribute.attributehandler_id) attribute.attribute_handler = handler self.set_simple_logging(attribute, user, insert=True) attribute = self.attr_def_broker.insert(attribute, False) self.attr_def_broker.do_commit(commit) return attribute except ValidationException as error: message = ObjectValidator.getFirstValidationError(attribute) raise ControllerException( u'Could not update object definition due to: {0}'.format( message)) except BrokerException as error: raise ControllerException(error)
def insert_user(self, user, validate=True, commit=True, send_mail=True): try: # Add unset elements self.set_activation_str(user) if user.plain_password: user.password = hashSHA1(user.plain_password + self.salt) # TODO: add api key and mail sending self.user_broker.insert(user, validate=validate, commit=commit) if user.gpg_key: self.mail_controller.import_gpg_key(user.gpg_key) if send_mail: self.mail_controller.send_activation_mail(user) except IntegrityException as error: raise ControllerIntegrityException(error) except ValidationException as error: message = ObjectValidator.getFirstValidationError(user) raise ControllerException(u'Could not add user due to: {0}'.format(message)) except (BrokerException) as error: raise ControllerException(error)
def insert_reference_definition(self, reference_definition, user, commit=True): try: reference_definition.chksum = gen_reference_chksum( reference_definition) user = self.user_broker.get_by_id(user.identifier) if not reference_definition.reference_handler: handler = self.reference_broker.get_handler_by_id( reference_definition.referencehandler_id) reference_definition.reference_handler = handler self.set_simple_logging(reference_definition, user, insert=True) reference_definition = self.reference_definition_broker.insert( reference_definition, False) self.reference_definition_broker.do_commit(commit) return reference_definition except ValidationException as error: message = ObjectValidator.getFirstValidationError( reference_definition) raise ControllerException( u'Could not update reference due to: {0}'.format(message)) except BrokerException as error: raise ControllerException(error)
def insert_event(self, user, event, mkrelations=True, commit=True): """ inserts an event If it is invalid the event is returned :param event: :type event: Event :returns: Event, Boolean """ self.logger.debug('User {0} inserts a new event'.format(user.username)) try: # the the creator user = self.user_broker.get_by_id(user.identifier) self.set_extended_logging(event, user, user.group, True) if commit: # do this only if there is a commit # set the own user group to the groups of the event and fill permissions permissions = self.get_event_user_permissions(event, user) group = self.group_broker.get_by_id(user.group.identifier) event_permission = EventGroupPermission() event_permission.permissions = permissions event_permission.group = group self.set_extended_logging(event_permission, user, user.group, True) found = False for group in event.groups: if event_permission.group.equals(group.group): found = True group.permissions = event_permission.permissions break if not found: event.groups.append(event_permission) # generate relations if needed! flat_attribtues = self.relations_controller.get_flat_attributes_for_event( event) if (mkrelations == 'True' or mkrelations is True) and flat_attribtues: self.relations_controller.generate_bulk_attributes_relations( event, flat_attribtues, False) self.event_broker.insert(event, False) self.event_broker.do_commit(commit) return event except ValidationException: message = ObjectValidator.getFirstValidationError(event) raise ControllerException( u'Could not update object definition due to: {0}'.format( message)) except IntegrityException as error: self.logger.debug(error) self.logger.info( u'User {0} tried to insert an event with uuid "{1}" but the uuid already exists' .format(user.username, event.uuid)) raise ControllerIntegrityException(error) except BrokerException as error: raise ControllerException(error)