feat: created a better account creation flow
This commit is contained in:
parent
385c5cd8d0
commit
66be0baca6
@ -61,8 +61,6 @@ public class WebSecurityCustomConfiguration {
|
|||||||
.requestMatchers("/admin/**", "/shared/**")
|
.requestMatchers("/admin/**", "/shared/**")
|
||||||
.access(hasRole("REALM_MyINPulse-admin"))
|
.access(hasRole("REALM_MyINPulse-admin"))
|
||||||
.requestMatchers("/unauth/**")
|
.requestMatchers("/unauth/**")
|
||||||
.permitAll()
|
|
||||||
.anyRequest()
|
|
||||||
.authenticated())
|
.authenticated())
|
||||||
.oauth2ResourceServer(
|
.oauth2ResourceServer(
|
||||||
oauth2 ->
|
oauth2 ->
|
||||||
|
@ -99,4 +99,20 @@ public class AdminApi {
|
|||||||
public void deleteProject(@PathVariable long projectId) {
|
public void deleteProject(@PathVariable long projectId) {
|
||||||
adminApiService.deleteProject(projectId);
|
adminApiService.deleteProject(projectId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@GetMapping("/admin/setadmin/{userId}")
|
||||||
|
public void setAdmin(@PathVariable long userId, @AuthenticationPrincipal Jwt principal) {
|
||||||
|
this.adminApiService.setAdmin(userId, principal.getTokenValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/admin/validate_user_account/{userId}")
|
||||||
|
public void validateEntrepreneurAcc(
|
||||||
|
@PathVariable long userId, @AuthenticationPrincipal Jwt principal) {
|
||||||
|
this.adminApiService.validateEntrepreneurAccount(userId, principal.getTokenValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/admin/get_pending_accounts")
|
||||||
|
public Iterable<User> validateEntrepreneurAcc() {
|
||||||
|
return this.adminApiService.getPendingUsers();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,51 @@
|
|||||||
|
package enseirb.myinpulse.controller;
|
||||||
|
|
||||||
|
import enseirb.myinpulse.model.Entrepreneur;
|
||||||
|
import enseirb.myinpulse.service.EntrepreneurApiService;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
import org.springframework.security.core.annotation.AuthenticationPrincipal;
|
||||||
|
import org.springframework.security.oauth2.jwt.Jwt;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
@RestController
|
||||||
|
public class UnauthApi {
|
||||||
|
|
||||||
|
private final EntrepreneurApiService entrepreneurApiService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
UnauthApi(EntrepreneurApiService entrepreneurApiService) {
|
||||||
|
this.entrepreneurApiService = entrepreneurApiService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/unauth/create_account")
|
||||||
|
public void createAccount(@AuthenticationPrincipal Jwt principal) {
|
||||||
|
boolean sneeStatus;
|
||||||
|
if (principal.getClaimAsString("sneeStatus") != null) {
|
||||||
|
sneeStatus = principal.getClaimAsString("sneeStatus").equals("true");
|
||||||
|
} else {
|
||||||
|
sneeStatus = false;
|
||||||
|
}
|
||||||
|
String userSurname = principal.getClaimAsString("userSurname");
|
||||||
|
String username = principal.getClaimAsString("preferred_username");
|
||||||
|
String primaryMail = principal.getClaimAsString("email");
|
||||||
|
String secondaryMail = principal.getClaimAsString("secondaryMail");
|
||||||
|
String phoneNumber = principal.getClaimAsString("phoneNumber");
|
||||||
|
String school = principal.getClaimAsString("school");
|
||||||
|
String course = principal.getClaimAsString("course");
|
||||||
|
Entrepreneur e =
|
||||||
|
new Entrepreneur(
|
||||||
|
userSurname,
|
||||||
|
username,
|
||||||
|
primaryMail,
|
||||||
|
secondaryMail,
|
||||||
|
phoneNumber,
|
||||||
|
school,
|
||||||
|
course,
|
||||||
|
sneeStatus,
|
||||||
|
true);
|
||||||
|
entrepreneurApiService.createAccount(e);
|
||||||
|
}
|
||||||
|
}
|
@ -37,7 +37,7 @@ public class Administrator extends User {
|
|||||||
String primaryMail,
|
String primaryMail,
|
||||||
String secondaryMail,
|
String secondaryMail,
|
||||||
String phoneNumber) {
|
String phoneNumber) {
|
||||||
super(null, userSurname, username, primaryMail, secondaryMail, phoneNumber);
|
super(userSurname, username, primaryMail, secondaryMail, phoneNumber, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<Project> getListProject() {
|
public List<Project> getListProject() {
|
||||||
|
@ -44,15 +44,30 @@ public class Entrepreneur extends User {
|
|||||||
String phoneNumber,
|
String phoneNumber,
|
||||||
String school,
|
String school,
|
||||||
String course,
|
String course,
|
||||||
boolean sneeStatus) {
|
boolean sneeStatus,
|
||||||
super(userSurname, username, primaryMail, secondaryMail, phoneNumber);
|
boolean pending) {
|
||||||
|
super(userSurname, username, primaryMail, secondaryMail, phoneNumber, pending);
|
||||||
|
this.school = school;
|
||||||
|
this.course = course;
|
||||||
|
this.sneeStatus = sneeStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Entrepreneur(
|
||||||
|
String userSurname,
|
||||||
|
String username,
|
||||||
|
String primaryMail,
|
||||||
|
String secondaryMail,
|
||||||
|
String phoneNumber,
|
||||||
|
String school,
|
||||||
|
String course,
|
||||||
|
boolean sneeStatus) {
|
||||||
|
super(userSurname, username, primaryMail, secondaryMail, phoneNumber, false);
|
||||||
this.school = school;
|
this.school = school;
|
||||||
this.course = course;
|
this.course = course;
|
||||||
this.sneeStatus = sneeStatus;
|
this.sneeStatus = sneeStatus;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Entrepreneur(
|
public Entrepreneur(
|
||||||
Long idUser,
|
|
||||||
String userSurname,
|
String userSurname,
|
||||||
String userName,
|
String userName,
|
||||||
String primaryMail,
|
String primaryMail,
|
||||||
@ -63,8 +78,9 @@ public class Entrepreneur extends User {
|
|||||||
boolean sneeStatus,
|
boolean sneeStatus,
|
||||||
Project projectParticipation,
|
Project projectParticipation,
|
||||||
Project projectProposed,
|
Project projectProposed,
|
||||||
MakeAppointment makeAppointment) {
|
MakeAppointment makeAppointment,
|
||||||
super(idUser, userSurname, userName, primaryMail, secondaryMail, phoneNumber);
|
boolean pending) {
|
||||||
|
super(userSurname, userName, primaryMail, secondaryMail, phoneNumber, pending);
|
||||||
this.school = school;
|
this.school = school;
|
||||||
this.course = course;
|
this.course = course;
|
||||||
this.sneeStatus = sneeStatus;
|
this.sneeStatus = sneeStatus;
|
||||||
|
@ -26,36 +26,23 @@ public class User {
|
|||||||
@Column(length = 20)
|
@Column(length = 20)
|
||||||
private String phoneNumber;
|
private String phoneNumber;
|
||||||
|
|
||||||
|
@Column private boolean pending;
|
||||||
|
|
||||||
public User() {}
|
public User() {}
|
||||||
|
|
||||||
// TODO: this should be removed as we shouldn't be able to chose the ID. Leaving it for
|
|
||||||
// compatibility purposes, as soon as it's not used anymore, delete it
|
|
||||||
public User(
|
|
||||||
Long idUser,
|
|
||||||
String userSurname,
|
|
||||||
String userName,
|
|
||||||
String primaryMail,
|
|
||||||
String secondaryMail,
|
|
||||||
String phoneNumber) {
|
|
||||||
this.idUser = idUser;
|
|
||||||
this.userSurname = userSurname;
|
|
||||||
this.userName = userName;
|
|
||||||
this.primaryMail = primaryMail;
|
|
||||||
this.secondaryMail = secondaryMail;
|
|
||||||
this.phoneNumber = phoneNumber;
|
|
||||||
}
|
|
||||||
|
|
||||||
public User(
|
public User(
|
||||||
String userSurname,
|
String userSurname,
|
||||||
String userName,
|
String userName,
|
||||||
String primaryMail,
|
String primaryMail,
|
||||||
String secondaryMail,
|
String secondaryMail,
|
||||||
String phoneNumber) {
|
String phoneNumber,
|
||||||
|
boolean pending) {
|
||||||
this.userSurname = userSurname;
|
this.userSurname = userSurname;
|
||||||
this.userName = userName;
|
this.userName = userName;
|
||||||
this.primaryMail = primaryMail;
|
this.primaryMail = primaryMail;
|
||||||
this.secondaryMail = secondaryMail;
|
this.secondaryMail = secondaryMail;
|
||||||
this.phoneNumber = phoneNumber;
|
this.phoneNumber = phoneNumber;
|
||||||
|
this.pending = pending;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Long getIdUser() {
|
public Long getIdUser() {
|
||||||
@ -105,4 +92,12 @@ public class User {
|
|||||||
public void setPhoneNumber(String phoneNumber) {
|
public void setPhoneNumber(String phoneNumber) {
|
||||||
phoneNumber = phoneNumber;
|
phoneNumber = phoneNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isPending() {
|
||||||
|
return pending;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPending(boolean pending) {
|
||||||
|
this.pending = pending;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,6 +11,7 @@ import java.util.Optional;
|
|||||||
public interface UserRepository extends JpaRepository<User, Long> {
|
public interface UserRepository extends JpaRepository<User, Long> {
|
||||||
Optional<User> findByPrimaryMail(String primaryMail);
|
Optional<User> findByPrimaryMail(String primaryMail);
|
||||||
|
|
||||||
|
Iterable<User> findAllByPendingEquals(boolean pending);
|
||||||
/* @Query("SELECT u from User u")
|
/* @Query("SELECT u from User u")
|
||||||
User findAllUser(); */
|
User findAllUser(); */
|
||||||
|
|
||||||
|
@ -24,6 +24,7 @@ public class AdminApiService {
|
|||||||
private final ProjectService projectService;
|
private final ProjectService projectService;
|
||||||
private final UserService userService;
|
private final UserService userService;
|
||||||
private final AdministratorService administratorService;
|
private final AdministratorService administratorService;
|
||||||
|
private final EntrepreneurService entrepreneurService;
|
||||||
private final UtilsService utilsService;
|
private final UtilsService utilsService;
|
||||||
private final AppointmentService appointmentService;
|
private final AppointmentService appointmentService;
|
||||||
private final ReportService reportService;
|
private final ReportService reportService;
|
||||||
@ -35,6 +36,7 @@ public class AdminApiService {
|
|||||||
UserService userService,
|
UserService userService,
|
||||||
AdministratorService administratorService,
|
AdministratorService administratorService,
|
||||||
UtilsService utilsService,
|
UtilsService utilsService,
|
||||||
|
EntrepreneurService entrepreneurService,
|
||||||
AppointmentService appointmentService,
|
AppointmentService appointmentService,
|
||||||
ReportService reportService,
|
ReportService reportService,
|
||||||
SectionCellService sectionCellService) {
|
SectionCellService sectionCellService) {
|
||||||
@ -45,6 +47,7 @@ public class AdminApiService {
|
|||||||
this.appointmentService = appointmentService;
|
this.appointmentService = appointmentService;
|
||||||
this.reportService = reportService;
|
this.reportService = reportService;
|
||||||
this.sectionCellService = sectionCellService;
|
this.sectionCellService = sectionCellService;
|
||||||
|
this.entrepreneurService = entrepreneurService;
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: check if tests are sufficient - peer verification required
|
// TODO: check if tests are sufficient - peer verification required
|
||||||
@ -75,6 +78,12 @@ public class AdminApiService {
|
|||||||
}
|
}
|
||||||
if (user instanceof Entrepreneur) {
|
if (user instanceof Entrepreneur) {
|
||||||
Project project = ((Entrepreneur) user).getProjectParticipation();
|
Project project = ((Entrepreneur) user).getProjectParticipation();
|
||||||
|
if (project == null) {
|
||||||
|
throw new ResponseStatusException(
|
||||||
|
HttpStatus.NOT_FOUND,
|
||||||
|
"The user has no project, thus no appointments. No users should have no project");
|
||||||
|
}
|
||||||
|
|
||||||
project.getListSectionCell()
|
project.getListSectionCell()
|
||||||
.forEach(
|
.forEach(
|
||||||
sectionCell -> {
|
sectionCell -> {
|
||||||
@ -104,7 +113,7 @@ public class AdminApiService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// TODO: check if tests are sufficient - peer verification required
|
// TODO: check if tests are sufficient - peer verification required
|
||||||
public void addNewProject(Project project) {
|
public Project addNewProject(Project project) {
|
||||||
project.setIdProject(null);
|
project.setIdProject(null);
|
||||||
// We remove the ID from the request to be sure that it will be auto generated
|
// We remove the ID from the request to be sure that it will be auto generated
|
||||||
try {
|
try {
|
||||||
@ -136,6 +145,7 @@ public class AdminApiService {
|
|||||||
sectionCell -> {
|
sectionCell -> {
|
||||||
sectionCell.setProjectSectionCell(newProject);
|
sectionCell.setProjectSectionCell(newProject);
|
||||||
});
|
});
|
||||||
|
return newProject;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void createAppointmentReport(long appointmentId, Report report, String mail) {
|
public void createAppointmentReport(long appointmentId, Report report, String mail) {
|
||||||
@ -164,4 +174,36 @@ public class AdminApiService {
|
|||||||
public void deleteProject(long projectId) {
|
public void deleteProject(long projectId) {
|
||||||
this.projectService.deleteProjectById(projectId);
|
this.projectService.deleteProjectById(projectId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setAdmin(long userId, String token) {
|
||||||
|
Entrepreneur e = this.entrepreneurService.getEntrepreneurById(userId);
|
||||||
|
Administrator a =
|
||||||
|
new Administrator(
|
||||||
|
e.getUserSurname(),
|
||||||
|
e.getUserName(),
|
||||||
|
e.getPrimaryMail(),
|
||||||
|
e.getSecondaryMail(),
|
||||||
|
e.getPhoneNumber());
|
||||||
|
this.entrepreneurService.deleteEntrepreneur(e);
|
||||||
|
this.administratorService.addAdministrator(a);
|
||||||
|
try {
|
||||||
|
KeycloakApi.setRoleToUser(a.getUserName(), "MyINPulse-admin", token);
|
||||||
|
} catch (Exception err) {
|
||||||
|
logger.error(err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void validateEntrepreneurAccount(long userId, String token) {
|
||||||
|
Entrepreneur e = this.entrepreneurService.getEntrepreneurById(userId);
|
||||||
|
try {
|
||||||
|
KeycloakApi.setRoleToUser(e.getUserName(), "MyINPulse-entrepreneur", token);
|
||||||
|
} catch (Exception err) {
|
||||||
|
logger.error(err);
|
||||||
|
}
|
||||||
|
this.entrepreneurService.validateEntrepreneurById(userId);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Iterable<User> getPendingUsers() {
|
||||||
|
return this.userService.getPendingAccounts();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -2,10 +2,13 @@ package enseirb.myinpulse.service;
|
|||||||
|
|
||||||
import static enseirb.myinpulse.model.ProjectDecisionValue.PENDING;
|
import static enseirb.myinpulse.model.ProjectDecisionValue.PENDING;
|
||||||
|
|
||||||
|
import enseirb.myinpulse.model.Entrepreneur;
|
||||||
import enseirb.myinpulse.model.Project;
|
import enseirb.myinpulse.model.Project;
|
||||||
import enseirb.myinpulse.model.SectionCell;
|
import enseirb.myinpulse.model.SectionCell;
|
||||||
|
import enseirb.myinpulse.service.database.EntrepreneurService;
|
||||||
import enseirb.myinpulse.service.database.ProjectService;
|
import enseirb.myinpulse.service.database.ProjectService;
|
||||||
import enseirb.myinpulse.service.database.SectionCellService;
|
import enseirb.myinpulse.service.database.SectionCellService;
|
||||||
|
import enseirb.myinpulse.service.database.UserService;
|
||||||
|
|
||||||
import org.apache.logging.log4j.LogManager;
|
import org.apache.logging.log4j.LogManager;
|
||||||
import org.apache.logging.log4j.Logger;
|
import org.apache.logging.log4j.Logger;
|
||||||
@ -22,15 +25,21 @@ public class EntrepreneurApiService {
|
|||||||
private final SectionCellService sectionCellService;
|
private final SectionCellService sectionCellService;
|
||||||
private final ProjectService projectService;
|
private final ProjectService projectService;
|
||||||
private final UtilsService utilsService;
|
private final UtilsService utilsService;
|
||||||
|
private final UserService userService;
|
||||||
|
private final EntrepreneurService entrepreneurService;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
EntrepreneurApiService(
|
EntrepreneurApiService(
|
||||||
SectionCellService sectionCellService,
|
SectionCellService sectionCellService,
|
||||||
ProjectService projectService,
|
ProjectService projectService,
|
||||||
UtilsService utilsService) {
|
UtilsService utilsService,
|
||||||
|
UserService userService,
|
||||||
|
EntrepreneurService entrepreneurService) {
|
||||||
this.sectionCellService = sectionCellService;
|
this.sectionCellService = sectionCellService;
|
||||||
this.projectService = projectService;
|
this.projectService = projectService;
|
||||||
this.utilsService = utilsService;
|
this.utilsService = utilsService;
|
||||||
|
this.userService = userService;
|
||||||
|
this.entrepreneurService = entrepreneurService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void editSectionCell(Long sectionCellId, String content, String mail) {
|
public void editSectionCell(Long sectionCellId, String content, String mail) {
|
||||||
@ -128,4 +137,15 @@ public class EntrepreneurApiService {
|
|||||||
project.setProjectStatus(PENDING);
|
project.setProjectStatus(PENDING);
|
||||||
projectService.addNewProject(project);
|
projectService.addNewProject(project);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void createAccount(Entrepreneur e) {
|
||||||
|
try {
|
||||||
|
userService.getUserByEmail(e.getPrimaryMail());
|
||||||
|
logger.error("The user {} already exists in the system", e.getPrimaryMail());
|
||||||
|
} catch (ResponseStatusException err) {
|
||||||
|
this.entrepreneurService.addEntrepreneur(e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
throw new ResponseStatusException(HttpStatus.CONFLICT, "User already exists in the system");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -6,12 +6,17 @@ import enseirb.myinpulse.exception.UserNotFoundException;
|
|||||||
import enseirb.myinpulse.model.RoleRepresentation;
|
import enseirb.myinpulse.model.RoleRepresentation;
|
||||||
import enseirb.myinpulse.model.UserRepresentation;
|
import enseirb.myinpulse.model.UserRepresentation;
|
||||||
|
|
||||||
|
import org.apache.logging.log4j.LogManager;
|
||||||
|
import org.apache.logging.log4j.Logger;
|
||||||
import org.springframework.web.client.RestClient;
|
import org.springframework.web.client.RestClient;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import javax.management.relation.RoleNotFoundException;
|
import javax.management.relation.RoleNotFoundException;
|
||||||
|
|
||||||
public class KeycloakApi {
|
public class KeycloakApi {
|
||||||
|
|
||||||
|
protected static final Logger logger = LogManager.getLogger();
|
||||||
static final String keycloakUrl;
|
static final String keycloakUrl;
|
||||||
static final String realmName;
|
static final String realmName;
|
||||||
|
|
||||||
@ -29,44 +34,48 @@ public class KeycloakApi {
|
|||||||
realmName = System.getenv("VITE_KEYCLOAK_REALM");
|
realmName = System.getenv("VITE_KEYCLOAK_REALM");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static String toBearer(String b) {
|
||||||
|
return "Bearer " + b;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Uses Keycloak API to retrieve a role representation of a role by its name
|
* Uses Keycloak API to retrieve a role representation of a role by its name
|
||||||
*
|
*
|
||||||
* @param roleName name of the role
|
* @param roleName name of the role
|
||||||
* @param bearer authorization header used by the client to authenticate to keycloak
|
* @param token authorization header used by the client to authenticate to keycloak
|
||||||
*/
|
*/
|
||||||
public static RoleRepresentation getRoleRepresentationByName(String roleName, String bearer)
|
public static RoleRepresentation getRoleRepresentationByName(String roleName, String token)
|
||||||
throws RoleNotFoundException {
|
throws RoleNotFoundException {
|
||||||
RoleRepresentation[] response =
|
RoleRepresentation response =
|
||||||
RestClient.builder()
|
RestClient.builder()
|
||||||
.baseUrl(keycloakUrl)
|
.baseUrl(keycloakUrl)
|
||||||
.defaultHeader("Authorization", bearer)
|
.defaultHeader("Authorization", toBearer(token))
|
||||||
.build()
|
.build()
|
||||||
.get()
|
.get()
|
||||||
.uri("/admin/realms/{realmName}/roles/{roleName}", realmName, roleName)
|
.uri("/admin/realms/{realmName}/roles/{roleName}", realmName, roleName)
|
||||||
.retrieve()
|
.retrieve()
|
||||||
.body(RoleRepresentation[].class);
|
.body(RoleRepresentation.class);
|
||||||
|
/*
|
||||||
if (response == null || response.length == 0) {
|
{"id":"7a845f2e-c832-4465-8cd8-894d72bc13f1","name":"MyINPulse-entrepreneur","description":"Role for entrepreneur","composite":false,"clientRole":false,"containerId":"0d6f691b-e328-471a-b89e-c30bd7e5b6b0","attributes":{}}
|
||||||
throw new RoleNotFoundException("Role not found");
|
*/
|
||||||
}
|
// TODO: check what happens when role does not exist
|
||||||
return response[0];
|
return response;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Use keycloak API to to retreive a userID via his name or email.
|
* Use keycloak API to to retreive a userID via his name or email.
|
||||||
*
|
*
|
||||||
* @param username username or mail of the user
|
* @param username username or mail of the user
|
||||||
* @param bearer bearer of the user, allowing access to database
|
* @param token bearer of the user, allowing access to database
|
||||||
* @return the userid, as a String
|
* @return the userid, as a String
|
||||||
* @throws UserNotFoundException
|
* @throws UserNotFoundException
|
||||||
*/
|
*/
|
||||||
public static String getUserIdByName(String username, String bearer)
|
public static String getUserIdByName(String username, String token)
|
||||||
throws UserNotFoundException {
|
throws UserNotFoundException {
|
||||||
UserRepresentation[] response =
|
UserRepresentation[] response =
|
||||||
RestClient.builder()
|
RestClient.builder()
|
||||||
.baseUrl(keycloakUrl)
|
.baseUrl(keycloakUrl)
|
||||||
.defaultHeader("Authorization", bearer)
|
.defaultHeader("Authorization", toBearer(token))
|
||||||
.build()
|
.build()
|
||||||
.get()
|
.get()
|
||||||
.uri(
|
.uri(
|
||||||
@ -91,27 +100,26 @@ public class KeycloakApi {
|
|||||||
*
|
*
|
||||||
* @param username
|
* @param username
|
||||||
* @param roleName
|
* @param roleName
|
||||||
* @param bearer
|
* @param token
|
||||||
* @throws RoleNotFoundException
|
* @throws RoleNotFoundException
|
||||||
* @throws UserNotFoundException
|
* @throws UserNotFoundException
|
||||||
*/
|
*/
|
||||||
public static void setRoleToUser(String username, String roleName, String bearer)
|
public static void setRoleToUser(String username, String roleName, String token)
|
||||||
throws RoleNotFoundException, UserNotFoundException {
|
throws RoleNotFoundException, UserNotFoundException {
|
||||||
RoleRepresentation roleRepresentation = getRoleRepresentationByName(roleName, bearer);
|
RoleRepresentation roleRepresentation = getRoleRepresentationByName(roleName, token);
|
||||||
String userId = getUserIdByName(username, bearer);
|
String userId = getUserIdByName(username, token);
|
||||||
|
List<RoleRepresentation> rolesToAdd = List.of(roleRepresentation);
|
||||||
|
logger.debug("Adding role {} to user {}", roleRepresentation.id, userId);
|
||||||
RestClient.builder()
|
RestClient.builder()
|
||||||
.baseUrl(keycloakUrl)
|
.baseUrl(keycloakUrl)
|
||||||
.defaultHeader("Authorization", bearer)
|
.defaultHeader("Authorization", toBearer(token))
|
||||||
.build()
|
.build()
|
||||||
.post()
|
.post()
|
||||||
.uri(
|
.uri("/admin/realms/" + realmName + "/users/" + userId + "/role-mappings/realm")
|
||||||
"/admin/realms/${realmName}/users/${userId}/role-mappings/realm",
|
.body(rolesToAdd)
|
||||||
realmName,
|
|
||||||
userId)
|
|
||||||
.body(roleRepresentation)
|
|
||||||
.contentType(APPLICATION_JSON)
|
.contentType(APPLICATION_JSON)
|
||||||
.retrieve();
|
.retrieve()
|
||||||
|
.toBodilessEntity();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -122,4 +122,17 @@ public class EntrepreneurService {
|
|||||||
public Iterable<Entrepreneur> GetEntrepreneurByProject(Project project) {
|
public Iterable<Entrepreneur> GetEntrepreneurByProject(Project project) {
|
||||||
return this.entrepreneurRepository.getEntrepreneurByProjectParticipation(project);
|
return this.entrepreneurRepository.getEntrepreneurByProjectParticipation(project);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void deleteEntrepreneur(Entrepreneur e) {
|
||||||
|
this.entrepreneurRepository.delete(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void validateEntrepreneurById(Long id) {
|
||||||
|
Optional<Entrepreneur> e = this.entrepreneurRepository.findById(id);
|
||||||
|
if (e.isEmpty()) {
|
||||||
|
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Entrepreneur n'existe pas");
|
||||||
|
}
|
||||||
|
e.get().setPending(false);
|
||||||
|
this.entrepreneurRepository.save(e.get());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -117,4 +117,8 @@ public class UserService {
|
|||||||
}
|
}
|
||||||
return this.userRepository.save(user.get());
|
return this.userRepository.save(user.get());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Iterable<User> getPendingAccounts() {
|
||||||
|
return this.userRepository.findAllByPendingEquals(true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,12 +4,10 @@ import static enseirb.myinpulse.model.ProjectDecisionValue.*;
|
|||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
import enseirb.myinpulse.model.Administrator;
|
import enseirb.myinpulse.model.*;
|
||||||
import enseirb.myinpulse.model.Entrepreneur;
|
|
||||||
import enseirb.myinpulse.model.Project;
|
|
||||||
import enseirb.myinpulse.model.ProjectDecision;
|
|
||||||
import enseirb.myinpulse.service.AdminApiService;
|
import enseirb.myinpulse.service.AdminApiService;
|
||||||
import enseirb.myinpulse.service.database.AdministratorService;
|
import enseirb.myinpulse.service.database.AdministratorService;
|
||||||
|
import enseirb.myinpulse.service.database.AppointmentService;
|
||||||
import enseirb.myinpulse.service.database.EntrepreneurService;
|
import enseirb.myinpulse.service.database.EntrepreneurService;
|
||||||
import enseirb.myinpulse.service.database.ProjectService;
|
import enseirb.myinpulse.service.database.ProjectService;
|
||||||
|
|
||||||
@ -21,6 +19,7 @@ import org.springframework.transaction.annotation.Transactional;
|
|||||||
import org.springframework.web.server.ResponseStatusException;
|
import org.springframework.web.server.ResponseStatusException;
|
||||||
|
|
||||||
import java.time.LocalDate;
|
import java.time.LocalDate;
|
||||||
|
import java.time.LocalTime;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
@ -37,7 +36,8 @@ public class AdminApiServiceTest {
|
|||||||
static void setup(
|
static void setup(
|
||||||
@Autowired AdministratorService administratorService,
|
@Autowired AdministratorService administratorService,
|
||||||
@Autowired ProjectService projectService,
|
@Autowired ProjectService projectService,
|
||||||
@Autowired EntrepreneurService entrepreneurService) {
|
@Autowired EntrepreneurService entrepreneurService,
|
||||||
|
@Autowired AppointmentService appoitmentService) {
|
||||||
administratorService.addAdministrator(
|
administratorService.addAdministrator(
|
||||||
new Administrator(
|
new Administrator(
|
||||||
"admin",
|
"admin",
|
||||||
@ -54,6 +54,7 @@ public class AdminApiServiceTest {
|
|||||||
"testAdmin@example.com",
|
"testAdmin@example.com",
|
||||||
""));
|
""));
|
||||||
administratorid = administrator.getIdUser();
|
administratorid = administrator.getIdUser();
|
||||||
|
|
||||||
entrepreneur =
|
entrepreneur =
|
||||||
new Entrepreneur(
|
new Entrepreneur(
|
||||||
"JeSuisUnEntrepreneurDeCompet",
|
"JeSuisUnEntrepreneurDeCompet",
|
||||||
@ -65,14 +66,33 @@ public class AdminApiServiceTest {
|
|||||||
"info ofc",
|
"info ofc",
|
||||||
false);
|
false);
|
||||||
entrepreneurService.addEntrepreneur(entrepreneur);
|
entrepreneurService.addEntrepreneur(entrepreneur);
|
||||||
projectService.addNewProject(
|
|
||||||
new Project(
|
Entrepreneur entrepreneur2 =
|
||||||
"sampleProjectAdminApiService",
|
new Entrepreneur(
|
||||||
|
"GDProjets", "", "Entrepreneur2@inpulse.com", "", "", "", "info ofc", true);
|
||||||
|
entrepreneurService.addEntrepreneur(entrepreneur2);
|
||||||
|
|
||||||
|
Project p =
|
||||||
|
projectService.addNewProject(
|
||||||
|
new Project(
|
||||||
|
"sampleProjectAdminApiService",
|
||||||
|
null,
|
||||||
|
LocalDate.now(),
|
||||||
|
ACTIVE,
|
||||||
|
administratorService.getAdministratorByPrimaryMain(
|
||||||
|
"testAdminFull@example.com")));
|
||||||
|
|
||||||
|
entrepreneurService.updateEntrepreneurProjectParticipation(entrepreneur2.getIdUser(), p);
|
||||||
|
|
||||||
|
Appointment a =
|
||||||
|
new Appointment(
|
||||||
null,
|
null,
|
||||||
LocalDate.now(),
|
LocalDate.now(),
|
||||||
ACTIVE,
|
LocalTime.now(),
|
||||||
administratorService.getAdministratorByPrimaryMain(
|
LocalTime.now(),
|
||||||
"testAdminFull@example.com")));
|
"Salle TD 15",
|
||||||
|
"Discussion importante");
|
||||||
|
appoitmentService.addNewAppointment(a);
|
||||||
}
|
}
|
||||||
|
|
||||||
private <T> List<T> IterableToList(Iterable<T> iterable) {
|
private <T> List<T> IterableToList(Iterable<T> iterable) {
|
||||||
@ -221,4 +241,54 @@ public class AdminApiServiceTest {
|
|||||||
this.adminApiService.addNewProject(p1);
|
this.adminApiService.addNewProject(p1);
|
||||||
assertThrows(ResponseStatusException.class, () -> this.adminApiService.addNewProject(p2));
|
assertThrows(ResponseStatusException.class, () -> this.adminApiService.addNewProject(p2));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// We could do a delete active project, but it's not really useful.
|
||||||
|
@Test
|
||||||
|
void deletePendingProject() {
|
||||||
|
int oldsize = IterableToList(this.adminApiService.getPendingProjects()).size();
|
||||||
|
Project p1 =
|
||||||
|
new Project("PendingProjectAdminApiService2", null, LocalDate.now(), PENDING, null);
|
||||||
|
Project p2 = this.adminApiService.addNewProject(p1);
|
||||||
|
|
||||||
|
assertEquals(oldsize + 1, IterableToList(this.adminApiService.getPendingProjects()).size());
|
||||||
|
this.adminApiService.deleteProject(p2.getIdProject());
|
||||||
|
|
||||||
|
assertEquals(oldsize, IterableToList(this.adminApiService.getPendingProjects()).size());
|
||||||
|
for (int i = 0; i < oldsize; i++) {
|
||||||
|
assertNotEquals(
|
||||||
|
p1.getIdProject(),
|
||||||
|
IterableToList(this.adminApiService.getPendingProjects())
|
||||||
|
.get(i)
|
||||||
|
.getIdProject());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getUpcommingAppointmentUnkwnownUser() {
|
||||||
|
assertThrows(
|
||||||
|
ResponseStatusException.class,
|
||||||
|
() -> {
|
||||||
|
Iterable<Appointment> a =
|
||||||
|
this.adminApiService.getUpcomingAppointments(
|
||||||
|
"entrepreneur-inexistent@mail.fr");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getUpcommingAppointmentNoProject() {
|
||||||
|
assertThrows(
|
||||||
|
ResponseStatusException.class,
|
||||||
|
() -> {
|
||||||
|
Iterable<Appointment> a =
|
||||||
|
this.adminApiService.getUpcomingAppointments(
|
||||||
|
"Entrepreneur@inpulse.com");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void getUpcommingAppointmentEmpty() {
|
||||||
|
Iterable<Appointment> a =
|
||||||
|
this.adminApiService.getUpcomingAppointments("Entrepreneur2@inpulse.com");
|
||||||
|
assertEquals(0, IterableToList(a).size());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import { callApi } from "@/services/api.ts";
|
|||||||
import { ref } from "vue";
|
import { ref } from "vue";
|
||||||
|
|
||||||
const CustomRequest = ref("");
|
const CustomRequest = ref("");
|
||||||
|
const USERID = ref("");
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
@ -34,30 +35,7 @@ const CustomRequest = ref("");
|
|||||||
<td>Current refresh token</td>
|
<td>Current refresh token</td>
|
||||||
<td>{{ store.user.refreshToken }}</td>
|
<td>{{ store.user.refreshToken }}</td>
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
|
||||||
<td>Entrepreneur API call</td>
|
|
||||||
<td>
|
|
||||||
<button @click="callApi('random')">call</button>
|
|
||||||
</td>
|
|
||||||
<td>res</td>
|
|
||||||
<td></td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td>Admin API call</td>
|
|
||||||
<td>
|
|
||||||
<button @click="callApi('random2')">call</button>
|
|
||||||
</td>
|
|
||||||
<td>res</td>
|
|
||||||
<td></td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
|
||||||
<td>Unauth API call</td>
|
|
||||||
<td>
|
|
||||||
<button @click="callApi('unauth/dev')">call</button>
|
|
||||||
</td>
|
|
||||||
<td>res</td>
|
|
||||||
<td id="3"></td>
|
|
||||||
</tr>
|
|
||||||
<tr>
|
<tr>
|
||||||
<td>
|
<td>
|
||||||
<input v-model="CustomRequest" placeholder="edit me" />
|
<input v-model="CustomRequest" placeholder="edit me" />
|
||||||
@ -66,6 +44,83 @@ const CustomRequest = ref("");
|
|||||||
<button @click="callApi(CustomRequest)">call</button>
|
<button @click="callApi(CustomRequest)">call</button>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td>Create an account</td>
|
||||||
|
<td>
|
||||||
|
<button @click="callApi('unauth/create_account')">
|
||||||
|
call
|
||||||
|
</button>
|
||||||
|
</td>
|
||||||
|
<td>res</td>
|
||||||
|
<td id="4"></td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Get Pending Accounts</td>
|
||||||
|
<td>
|
||||||
|
<button @click="callApi('admin/get_pending_accounts')">
|
||||||
|
call
|
||||||
|
</button>
|
||||||
|
</td>
|
||||||
|
<td>res</td>
|
||||||
|
<td id="6"></td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>admin/validate_user_account/{id}</td>
|
||||||
|
<td>
|
||||||
|
<button
|
||||||
|
@click="
|
||||||
|
callApi('admin/validate_user_account/' + USERID)
|
||||||
|
"
|
||||||
|
>
|
||||||
|
call
|
||||||
|
</button>
|
||||||
|
</td>
|
||||||
|
|
||||||
|
<td>
|
||||||
|
<input v-model="USERID" placeholder="user ID" />
|
||||||
|
</td>
|
||||||
|
|
||||||
|
<td id="5"></td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td>admin/setadmin/{uid}</td>
|
||||||
|
<td>
|
||||||
|
<button @click="callApi('admin/setadmin/' + USERID)">
|
||||||
|
call
|
||||||
|
</button>
|
||||||
|
</td>
|
||||||
|
|
||||||
|
<td>
|
||||||
|
<input v-model="USERID" placeholder="user ID" />
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Unauth API call</td>
|
||||||
|
<td>
|
||||||
|
<button @click="callApi('unauth/dev')">call</button>
|
||||||
|
</td>
|
||||||
|
<td>res</td>
|
||||||
|
<td id="8"></td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td>Unauth API call</td>
|
||||||
|
<td>
|
||||||
|
<button @click="callApi('unauth/dev')">call</button>
|
||||||
|
</td>
|
||||||
|
<td>res</td>
|
||||||
|
<td id="9"></td>
|
||||||
|
</tr>
|
||||||
|
|
||||||
|
<tr>
|
||||||
|
<td>Unauth API call</td>
|
||||||
|
<td>
|
||||||
|
<button @click="callApi('unauth/dev')">call</button>
|
||||||
|
</td>
|
||||||
|
<td>res</td>
|
||||||
|
<td id="10"></td>
|
||||||
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</template>
|
</template>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user