package fr.plil.sio.persistence.jpa; import fr.plil.sio.persistence.api.Group; import fr.plil.sio.persistence.api.Right; import fr.plil.sio.persistence.api.User; import fr.plil.sio.persistence.api.UserService; import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class UserServiceJpa implements UserService { @Autowired private UserRepository userRepository; @Autowired private GroupRepository groupRepository; @Autowired private RightRepository rightRepository; @Override public User create(String name, String groupName) { if(name==null || groupName==null){ throw new IllegalArgumentException("name cannot be null"); } Group group = new Group(); group = groupRepository.findByName(groupName); User user = userRepository.findByName(name); if(group==null){ throw new IllegalArgumentException("group trouve avec le find by name null"); } if(user!=null){ throw new IllegalStateException("a user with the same name already exists"); } user = new User(); user.setName(name); if(group!=null) user.setGroup(group); group.getUsers().add(user); userRepository.save(user); return user; } @Override public boolean delete(String name) { if(name == null){ throw new IllegalArgumentException("name cannot be null"); } User user = findByName(name); if(user==null){ return false; } userRepository.delete(user.getId()); return true; } @Override public User findByName(String name) { if(name==null){ throw new IllegalArgumentException("name cannot be null"); } return userRepository.findByName(name); } @Override public boolean isUserHasRight(String userName, Right right) { if(userName==null){ throw new IllegalArgumentException("userName cannot be null"); } if(right==null){ throw new IllegalArgumentException("right cannot be null"); } if(rightRepository.findByName(right.getName()).size() == 0){ throw new IllegalArgumentException("right does not exists"); } User user = userRepository.findByName(userName); Group group = user.getGroup(); List list = group.getRights(); if(list.contains(right) || list.contains(right.getParent())){ return true; } return false; } }