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)
public void importData() {
User testUser = new User();
testUser.setEmail("filizy@st.amu.edu.pl");
testUser.setName("Filip");
testUser.setSurname("Izydorczyk");
testUser.setRole(UserRoles.STUDENT);
this.userService.save(testUser);
User filip = new User();
filip.setEmail("filizy@st.amu.edu.pl");
filip.setName("Filip");
filip.setSurname("Izydorczyk");
filip.setRole(UserRoles.STUDENT);
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.Commision;
import com.plannaplan.entities.Groups;
import com.plannaplan.entities.User;
import com.plannaplan.models.ConfigData;
import com.plannaplan.services.AssignmentService;
import com.plannaplan.services.CommisionService;
import com.plannaplan.services.GroupService;
import com.plannaplan.services.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
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
@CrossOrigin
@ -37,22 +45,32 @@ public class CommisionController {
@Autowired
private AssignmentService assignmentService;
@Autowired
private UserService userService;
public CommisionController() {
}
@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());
Commision com = new Commision(null);
// Object principal =
// SecurityContextHolder.getContext().getAuthentication().getPrincipal();
Commision com = new Commision(user);
Assignment a = new Assignment(group, com);
this.commisionService.save(com);
this.assignmentService.save(a);
return "Success";
return new ResponseEntity<>("Succes", HttpStatus.OK);
}
}

View File

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