DeleteHandler#

com.palmyralabs.palmyra.handlers.DeleteHandler

Delete lifecycle. Composes PreProcessor and a rollback failure hook.

The default preProcess(Tuple, HandlerContext) copies ctx.getParams() into the tuple attributes before the ACL check fires.

Methods#

Method Signature
validate void validate(Tuple tuple, HandlerContext ctx)
preProcessRaw Tuple preProcessRaw(Tuple tuple, HandlerContext ctx)
preProcess void preProcess(Tuple tuple, HandlerContext ctx) — default copies ctx.getParams() into the tuple
aclCheck int aclCheck(Tuple tuple, HandlerContext ctx)
getAcl int getAcl(Tuple tuple, HandlerContext ctx)
preDelete Tuple preDelete(Tuple tuple, HandlerContext ctx)
onDelete Tuple onDelete(Tuple tuple, HandlerContext ctx)
postDelete Tuple postDelete(Tuple tuple, HandlerContext ctx)
rollback default Tuple rollback(Tuple tuple, HandlerContext ctx)

Use preDelete / filter patterns to implement soft-delete; use onDelete for hard-delete side effects.

Example#

@Component
@CrudMapping(value = "/v1/admin/user", type = User.class)
public class UserDeleteHandler implements DeleteHandler {

    @Autowired
    private UserProvider userProvider;
    @Autowired
    private OutboxService outbox;

    @Override
    public int aclCheck(Tuple tuple, HandlerContext ctx) {
        return userProvider.hasRole("USER_DELETE") ? 0 : -1;
    }

    @Override
    public void preProcess(Tuple tuple, HandlerContext ctx) {
        DeleteHandler.super.preProcess(tuple, ctx);
        tuple.set("deletedBy", userProvider.getUserId());
    }

    @Override
    public Tuple preDelete(Tuple tuple, HandlerContext ctx) {
        // soft-delete: flip status rather than remove the row
        tuple.set("status", "DELETED");
        tuple.set("deletedAt", Instant.now());
        return tuple;
    }

    @Override
    public Tuple onDelete(Tuple tuple, HandlerContext ctx) {
        // revoke active sessions in-band with the delete
        outbox.publish("user.sessions.revoke", tuple.get("id"));
        return tuple;
    }

    @Override
    public Tuple postDelete(Tuple tuple, HandlerContext ctx) {
        outbox.publish("user.deleted", tuple);
        return tuple;
    }

    @Override
    public Tuple rollback(Tuple tuple, HandlerContext ctx) {
        outbox.discardPending(tuple.get("id"));
        return tuple;
    }
}