Done method to get dimmable light if owner or guest

This commit is contained in:
Claudio Maggioni (maggicl) 2020-04-21 14:06:56 +02:00
parent db102f75e4
commit cfe3848d7e
2 changed files with 36 additions and 28 deletions

View File

@ -8,7 +8,6 @@ import ch.usi.inf.sa4.sanmarinoes.smarthut.error.NotFoundException;
import ch.usi.inf.sa4.sanmarinoes.smarthut.models.*;
import java.security.Principal;
import java.util.List;
import java.util.Optional;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@ -20,7 +19,6 @@ import org.springframework.web.bind.annotation.*;
public class DimmableLightController {
@Autowired private UserRepository userRepository;
@Autowired private RoomRepository roomRepository;
@Autowired private DimmableLightRepository dimmableLightService;
@Autowired private SceneRepository sceneRepository;
@Autowired private StateRepository<State<?>> stateRepository;
@ -48,35 +46,35 @@ public class DimmableLightController {
return save(new DimmableLight(), dl);
}
private DimmableLight fetchIfOwnerOrGuest(final Principal principal, Long id, Long hostId)
throws NotFoundException {
if (hostId == null) {
return dimmableLightService
.findByIdAndUsername(id, principal.getName())
.orElseThrow(NotFoundException::new);
} else {
/*
* Slightly less extremely verbose check through various repositories to control user/guest authorization.
*/
DimmableLight dl =
dimmableLightService
.findByIdAndUserId(id, hostId)
.orElseThrow(NotFoundException::new);
User host = userRepository.findById(hostId).orElseThrow(IllegalStateException::new);
User guest = userRepository.findByUsername(principal.getName());
if (!host.getGuests().contains(guest)) {
throw new NotFoundException();
} else {
return dl;
}
}
}
@PutMapping
public DimmableLight update(
@Valid @RequestBody DimmableSaveRequest sp,
final Principal principal,
Optional<Long> guestId)
@Valid @RequestBody DimmableSaveRequest sp, final Principal principal, Long hostId)
throws NotFoundException {
/**
* Extremely verbose check through various repositories to control user/guest authorization.
*/
if (guestId.isPresent()
&& userRepository
.findById(
roomRepository
.findById(sp.getRoomId().longValue())
.get()
.getUserId()
.longValue())
.get()
.getGuests()
.contains(userRepository.findById(guestId.get().longValue()))) {
return save(
dimmableLightService
.findByIdAndUsername(sp.getId(), principal.getName())
.orElseThrow(NotFoundException::new),
sp);
} else {
throw new Error("401: Unauthorized user. Not a guest.");
}
return save(fetchIfOwnerOrGuest(principal, sp.getId(), hostId), sp);
}
@DeleteMapping("/{id}")

View File

@ -25,6 +25,16 @@ public interface DeviceRepository<T extends Device> extends CrudRepository<T, Lo
@Query("SELECT d FROM Device d JOIN d.room r JOIN r.user u WHERE d.id = ?1 AND u.username = ?2")
Optional<T> findByIdAndUsername(Long id, String username);
/**
* Finds devices by their id and a user id
*
* @param id the device id
* @param userId a User's id
* @return an optional device, empty if none found
*/
@Query("SELECT d FROM Device d JOIN d.room r JOIN r.user u WHERE d.id = ?1 AND u.id = ?2")
Optional<T> findByIdAndUserId(Long id, Long userId);
/**
* Finds all devices belonging to a user
*