Checkpoint getting authority works + added new user

This commit is contained in:
Filip Izydorczyk 2020-09-29 19:06:29 +02:00
parent 3ad4d3a84b
commit b4d1c87461
3 changed files with 44 additions and 12 deletions

View File

@ -28,11 +28,25 @@ public class App {
@EventListener(ApplicationReadyEvent.class) @EventListener(ApplicationReadyEvent.class)
public void importData() { public void importData() {
User testUser = new User(); User filip = new User();
testUser.setEmail("filizy@st.amu.edu.pl"); filip.setEmail("filizy@st.amu.edu.pl");
testUser.setName("Filip"); filip.setName("Filip");
testUser.setSurname("Izydorczyk"); filip.setSurname("Izydorczyk");
testUser.setRole(UserRoles.STUDENT); filip.setRole(UserRoles.STUDENT);
this.userService.save(testUser); this.userService.save(filip);
User hub = new User();
hub.setEmail("hubwrz1@st.amu.edu.pl");
hub.setName("Hubert");
hub.setSurname("Wrzesiński");
hub.setRole(UserRoles.STUDENT);
this.userService.save(hub);
User mac = new User();
mac.setEmail("macglo2@st.amu.edu.pl");
mac.setName("Maciej");
mac.setSurname("Głowacki");
mac.setRole(UserRoles.STUDENT);
this.userService.save(mac);
} }
} }

View File

@ -10,18 +10,26 @@ import com.plannaplan.Controller;
import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Assignment;
import com.plannaplan.entities.Commision; import com.plannaplan.entities.Commision;
import com.plannaplan.entities.Groups; import com.plannaplan.entities.Groups;
import com.plannaplan.entities.User;
import com.plannaplan.models.ConfigData; import com.plannaplan.models.ConfigData;
import com.plannaplan.services.AssignmentService; import com.plannaplan.services.AssignmentService;
import com.plannaplan.services.CommisionService; import com.plannaplan.services.CommisionService;
import com.plannaplan.services.GroupService; import com.plannaplan.services.GroupService;
import com.plannaplan.services.UserService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
import org.springframework.security.authentication.AnonymousAuthenticationToken;
import org.springframework.security.authentication.AuthenticationProvider;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
@RestController @RestController
@CrossOrigin @CrossOrigin
@ -37,22 +45,32 @@ public class CommisionController {
@Autowired @Autowired
private AssignmentService assignmentService; private AssignmentService assignmentService;
@Autowired
private UserService userService;
public CommisionController() { public CommisionController() {
} }
@PostMapping("/add") @PostMapping("/add")
public String addCommision(@RequestParam("id") Long id) { public ResponseEntity<String> addCommision(@RequestParam("id") Long id) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
User user;
if (!(authentication instanceof AnonymousAuthenticationToken)) {
user = this.userService.getUserByEmail(authentication.getName());
} else {
return new ResponseEntity<>("Succes", HttpStatus.UNAUTHORIZED);
}
Groups group = this.groupServcicxe.getGroupById(id).orElseThrow(() -> new NullPointerException()); Groups group = this.groupServcicxe.getGroupById(id).orElseThrow(() -> new NullPointerException());
Commision com = new Commision(null); Commision com = new Commision(user);
// Object principal =
// SecurityContextHolder.getContext().getAuthentication().getPrincipal();
Assignment a = new Assignment(group, com); Assignment a = new Assignment(group, com);
this.commisionService.save(com); this.commisionService.save(com);
this.assignmentService.save(a); this.assignmentService.save(a);
return "Success"; return new ResponseEntity<>("Succes", HttpStatus.OK);
} }
} }

View File

@ -59,7 +59,7 @@ public class AuthenticationProvider extends AbstractUserDetailsAuthenticationPro
@Override @Override
public String getUsername() { public String getUsername() {
return user.getName() + " " + user.getSurname(); return user.getEmail();
} }
@Override @Override