@CommandHandler public ChargingStation(CreateAndAcceptChargingStationCommand command) { this(); apply(new ChargingStationCreatedEvent(command.getChargingStationId(), command.getUserIdentitiesWithAllPermissions(), command.getIdentityContext())); // created event sets authorization, check if this command is checkCommandAllowed(command.getIdentityContext(), command.getClass()); apply(new ChargingStationAcceptedEvent(command.getChargingStationId(), command.getIdentityContext())); }
/** * {@inheritDoc} */ @Override public void handle(String chargingStationId, JsonObject commandObject, IdentityContext identityContext) throws UserIdentityUnauthorizedException { ChargingStation chargingStation = repository.findOne(chargingStationId); ChargingStationId csId = new ChargingStationId(chargingStationId); if (chargingStation == null) { // charging station doesn't exist yet, check if the user identity is allowed to create/maintain charging stations if (!userIdentitiesWithAllPermissions.contains(identityContext.getUserIdentity())) { throw new UserIdentityUnauthorizedException(chargingStationId, identityContext.getUserIdentity(), CreateAndAcceptChargingStationCommand.class); } commandGateway.send(new CreateAndAcceptChargingStationCommand(csId, userIdentitiesWithAllPermissions, identityContext)); } else if (!chargingStation.isAccepted()) { if (!commandAuthorizationService.isAuthorized(csId, identityContext.getUserIdentity(), AcceptChargingStationCommand.class)) { throw new UserIdentityUnauthorizedException(chargingStationId, identityContext.getUserIdentity(), AcceptChargingStationCommand.class); } commandGateway.send(new AcceptChargingStationCommand(csId, identityContext)); } else { throw new IllegalStateException(String.format("Charging station { %s } is already in accepted state, you can't register this station", chargingStationId)); } }