Skip to content

Commit

Permalink
[SERVER] implement getParkingLotIdByAuthorizationHeader()
Browse files Browse the repository at this point in the history
  • Loading branch information
Huynh Thanh Binh committed Jul 20, 2020
1 parent e7b073f commit f38dadb
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 8 deletions.
8 changes: 7 additions & 1 deletion api/parkinglot-api/src/main/proto/ParkingLot.proto
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,13 @@ message MapToParkingLotNameMapResponse {

service ParkingLotService {

/** PUBLIC
/** PUBLIC - PARKING-LOT EMPLOYEE ONLY
* use-case: get parking-lot id by request's authorization header (parse employee id from Json Web Token - JWT)
* + request: empty
* + response: parking-lot's ID */
rpc getParkingLotIdByAuthorizationHeader (google.protobuf.Empty) returns (google.protobuf.Int64Value);

/** PUBLIC - ADMIN ONLY
* use-case: send message to parking-lot employee, known parking-lot id
* + request: parking-lot employee's ID
* + response: parking-lot's ID */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@ public SaigonParkingServerInterceptor() {
this(Collections.emptyMap());
}


public SaigonParkingServerInterceptor(Map<Class<? extends Throwable>, String> errorCodeMap) {
authentication = new SaigonParkingAuthenticationImpl();
this.errorCodeMap = errorCodeMap;
}


@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> serverCall,
Metadata metadata,
Expand Down Expand Up @@ -150,18 +152,26 @@ public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, Re
serverCallHandler);
}


public void validateAdmin() {
if (!roleContext.get().equals("ADMIN")) {
throw new PermissionDeniedException();
}
validateUserRole("ADMIN");
}


public void validateUser(@NotNull Long userEntityId) {
if (!userIdContext.get().equals(userEntityId)) {
throw new UsernameNotMatchException();
}
}


public void validateUserRole(@NotEmpty String acceptedRole) {
if (!acceptedRole.equals(roleContext.get())) {
throw new PermissionDeniedException();
}
}


public void validateUserRole(@NotEmpty List<String> acceptedRoles) {
if (!acceptedRoles.contains(roleContext.get())) {
throw new PermissionDeniedException();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,19 +69,42 @@ public final class ParkingLotServiceGrpcImpl extends ParkingLotServiceImplBase {
private final EnumMapper enumMapper;
private final SaigonParkingServerInterceptor serverInterceptor;

@Override
public void getParkingLotIdByAuthorizationHeader(Empty request, StreamObserver<Int64Value> responseObserver) {
try {
serverInterceptor.validateUserRole("PARKING_LOT_EMPLOYEE");

long employeeId = serverInterceptor.getUserIdContext().get();
long parkingLotId = parkingLotService.getParkingLotIdByParkingLotEmployeeId(employeeId);

responseObserver.onNext(Int64Value.of(parkingLotId));
responseObserver.onCompleted();

LoggingUtil.log(Level.INFO, "SERVICE", "Success",
String.format("getParkingLotIdByAuthorizationHeader(%d): %d", employeeId, parkingLotId));

} catch (Exception exception) {

responseObserver.onError(exception);

LoggingUtil.log(Level.ERROR, "SERVICE", "Exception", exception.getClass().getSimpleName());
LoggingUtil.log(Level.WARN, "SERVICE", "Session FAIL", "getParkingLotIdByAuthorizationHeader()");
}
}

@Override
public void getParkingLotIdByParkingLotEmployeeId(Int64Value request, StreamObserver<Int64Value> responseObserver) {
try {
serverInterceptor.validateAdmin();

Int64Value parkingLotIdResponse = Int64Value.of(parkingLotService
.getParkingLotIdByParkingLotEmployeeId(request.getValue()));
long employeeId = request.getValue();
long parkingLotId = parkingLotService.getParkingLotIdByParkingLotEmployeeId(employeeId);

responseObserver.onNext(parkingLotIdResponse);
responseObserver.onNext(Int64Value.of(parkingLotId));
responseObserver.onCompleted();

LoggingUtil.log(Level.INFO, "SERVICE", "Success",
String.format("getParkingLotIdByParkingLotEmployeeId(%d)", request.getValue()));
String.format("getParkingLotIdByParkingLotEmployeeId(%d): %d", employeeId, parkingLotId));

} catch (Exception exception) {

Expand Down

0 comments on commit f38dadb

Please sign in to comment.