Commit 0186019a authored by Kevin Robert's avatar Kevin Robert
Browse files

UNOTOPLYS-205 : Prise en compte des remarques de PR.

parent 6599a9c2
package com.unantes.orientactive.filariane;
import com.unantes.orientactive.security.AuthoritiesConstants;
import com.unantes.orientactive.security.permissions.PermissionService;
import com.unantes.orientactive.service.FormService;
import com.unantes.orientactive.service.ScreenService;
import com.unantes.orientactive.service.WorkspaceService;
import com.unantes.orientactive.service.dto.FormDTO;
import com.unantes.orientactive.service.dto.ScreenDTO;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.context.ApplicationContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
......@@ -15,6 +15,7 @@ import org.springframework.web.bind.annotation.RestController;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Optional;
/**
......@@ -23,11 +24,6 @@ import java.util.Optional;
@RestController
public class FilArianeController {
/**
* Service des workspaces.
*/
private final WorkspaceService workspaceService;
/**
* Service des formulaires.
*/
......@@ -38,14 +34,20 @@ public class FilArianeController {
*/
private final ScreenService screenService;
/**
* Elément du fil d'ariane correspondant à l'accueil.
*/
private final FilArianeElement filArianeHome;
/**
* Constructeur.
*
* @param workspaceService Le service des workspaces.
* @param formService Le service des formulaires.
* @param formService Le service des formulaires.
* @param screenService Le service des écrans.
*/
public FilArianeController(final WorkspaceService workspaceService, final FormService formService, final ScreenService screenService) {
this.workspaceService = workspaceService;
public FilArianeController(final FormService formService, final ScreenService screenService, final ApplicationContext applicationContext) {
String homeTitle = applicationContext.getMessage("filariane.home.title", new Object[]{}, Locale.getDefault());
this.filArianeHome = new FilArianeElement(homeTitle, Path.HOME);
this.formService = formService;
this.screenService = screenService;
}
......@@ -57,12 +59,12 @@ public class FilArianeController {
*/
@GetMapping("/home")
@ResponseBody
public String getUserHome(final Authentication authentication) {
if (authentication.getAuthorities().contains(new SimpleGrantedAuthority("VIEW_WORKSPACE"))) {
public String getUserHome() {
if (PermissionService.currentUserHasAuthority(AuthoritiesConstants.AUTHORITY_VIEW_WORKSPACE)) {
return Path.LIST_WORKSPACES;
} else if (authentication.getAuthorities().contains(new SimpleGrantedAuthority("VIEW_FORMS"))) {
} else if (PermissionService.currentUserHasAuthority(AuthoritiesConstants.AUTHORITY_VIEW_FORMS)) {
return Path.LIST_FORMS;
} else if (authentication.getAuthorities().contains(new SimpleGrantedAuthority("VIEW_FORM"))) {
} else if (PermissionService.currentUserHasAuthority(AuthoritiesConstants.AUTHORITY_VIEW_FORM)) {
return Path.LIST_FORMS;
}
return "#";
......@@ -76,7 +78,7 @@ public class FilArianeController {
@GetMapping("/filariane/workspaces")
@ResponseBody
public List<FilArianeElement> getFilArianeForWorkspaces() {
return Collections.singletonList(new FilArianeElement("Accueil", Path.HOME));
return Collections.singletonList(filArianeHome);
}
/**
......@@ -87,8 +89,8 @@ public class FilArianeController {
*/
@GetMapping("/filariane/workspace/{idWorkspace}")
@ResponseBody
public List<FilArianeElement> getFilArianeForWorkspace(@PathVariable("idWorkspace") final Long idWorkspace, final Authentication authentication) {
return Collections.singletonList(new FilArianeElement("Accueil", Path.HOME));
public List<FilArianeElement> getFilArianeForWorkspace(@PathVariable("idWorkspace") final Long idWorkspace) {
return Collections.singletonList(filArianeHome);
}
/**
......@@ -99,10 +101,10 @@ public class FilArianeController {
*/
@GetMapping("/filariane/form/{idForm}")
@ResponseBody
public List<FilArianeElement> getFilArianeForForm(@PathVariable("idForm") final Long idForm, final Authentication authentication) {
public List<FilArianeElement> getFilArianeForForm(@PathVariable("idForm") final Long idForm) {
final List<FilArianeElement> filAriane = new LinkedList<>();
filAriane.add(new FilArianeElement("Accueil", Path.HOME));
addFilArianeWorkspace(authentication, filAriane, idForm);
filAriane.add(filArianeHome);
addFilArianeWorkspace(filAriane, idForm);
return filAriane;
}
......@@ -112,19 +114,21 @@ public class FilArianeController {
* @param idScreen L'identifiant de l'écran.
* @return Le fil d'ariane.
*/
@GetMapping("/filariane/form/{idScreen}")
@GetMapping("/filariane/screen/{idScreen}")
@ResponseBody
public List<FilArianeElement> getFilArianeForScreen(@PathVariable("idScreen") final Long idScreen, final Authentication authentication) {
public List<FilArianeElement> getFilArianeForScreen(@PathVariable("idScreen") final Long idScreen) {
final List<FilArianeElement> filAriane = new LinkedList<>();
if (authentication.getAuthorities().contains(new SimpleGrantedAuthority("VIEW_FORM"))) {
if (PermissionService.currentUserHasAuthority(AuthoritiesConstants.AUTHORITY_VIEW_FORM)) {
final Optional<ScreenDTO> screenOptional = screenService.findOne(idScreen);
if (screenOptional.isPresent()) {
final ScreenDTO screen = screenOptional.get();
filAriane.add(new FilArianeElement(screen.getFormName(), Path.WORKSPACE + "/" + screen.getFormId()));
addFilArianeWorkspace(authentication, filAriane, screen.getFormId());
addFilArianeWorkspace(filAriane, screen.getFormId());
} else {
throw new FilArianeException("L'écran n'existe pas.");
}
}
filAriane.add(new FilArianeElement("Accueil", Path.HOME));
filAriane.add(filArianeHome);
Collections.reverse(filAriane);
return filAriane;
}
......@@ -132,16 +136,17 @@ public class FilArianeController {
/**
* Ajout du fil d'ariane du workspace concerné par le formulaire.
*
* @param authentication Les informations d'authentification de l'utilisateur.
* @param filAriane Le fil d'ariane.
* @param formId L'identifiant du formulaire.
* @param filAriane Le fil d'ariane.
* @param formId L'identifiant du formulaire.
*/
private void addFilArianeWorkspace(final Authentication authentication, final List<FilArianeElement> filAriane, final Long formId) {
if (authentication.getAuthorities().contains(new SimpleGrantedAuthority("VIEW_WORKSPACE"))) {
private void addFilArianeWorkspace(final List<FilArianeElement> filAriane, final Long formId) {
if (PermissionService.currentUserHasAuthority(AuthoritiesConstants.AUTHORITY_VIEW_WORKSPACE)) {
final Optional<FormDTO> formOptional = formService.findOne(formId);
if (formOptional.isPresent()) {
final FormDTO form = formOptional.get();
filAriane.add(new FilArianeElement(form.getWorkspaceName(), Path.WORKSPACE + "/" + form.getWorkspaceId()));
} else {
throw new FilArianeException("Le formulaire n'existe pas.");
}
}
}
......
......@@ -27,4 +27,12 @@ public class FilArianeElement implements Serializable {
this.title = title;
this.path = path;
}
public String getTitle() {
return title;
}
public String getPath() {
return path;
}
}
package com.unantes.orientactive.filariane;
/**
* Erreur lors du calcul du fil d'ariane.
*/
public class FilArianeException extends RuntimeException {
/**
* Constructeur.
*
* @param message Le message.
*/
public FilArianeException(String message) {
super(message);
}
}
package com.unantes.orientactive.filariane;
/**
* Chemins de correspondant aux chemins d'affichage des entités dans l'application vuejs.
*/
public class Path {
public final static String HOME = "/home";
public final static String LIST_WORKSPACES = "/workspaces";
public final static String WORKSPACE = "/workspace";
public final static String LIST_FORMS = "/forms";
public static final String HOME = "/home";
public static final String LIST_WORKSPACES = "/workspaces";
public static final String WORKSPACE = "/workspace";
public static final String LIST_FORMS = "/forms";
}
......@@ -38,7 +38,6 @@ public interface WorkspaceRepository extends JpaRepository<Workspace, Long>, Rol
@Query(value = "delete from scope_workspace sw using scope s where s.id = sw.id_scope and sw.id_workspace = :idWorkspace and s.id_user = :idUser", nativeQuery = true)
@Modifying
void removePermission(@Param("idUser") Long idUser, @Param("idWorkspace") Long idWorkspace);
/**
* Récupération des workspaces dont l'utilisateur possède les droits d'administrations.
*
......
package com.unantes.orientactive.security;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
/**
* Constants for Spring Security authorities.
*/
......@@ -8,14 +10,24 @@ public final class AuthoritiesConstants {
public static final String USER = "ROLE_USER";
public static final String ANONYMOUS = "ROLE_ANONYMOUS";
public static final String ADMIN = "ADMIN_SAAS";
public static final SimpleGrantedAuthority AUTHORITY_ADMIN = new SimpleGrantedAuthority(ADMIN);
public static final String VIEW_ORGANIZATION = "VIEW_ORGANIZATION";
public static final SimpleGrantedAuthority AUTHORITY_VIEW_ORGANIZATION = new SimpleGrantedAuthority(VIEW_ORGANIZATION);
public static final String EDIT_ORGANIZATION = "EDIT_ORGANIZATION";
public static final SimpleGrantedAuthority AUTHORITY_EDIT_ORGANIZATION = new SimpleGrantedAuthority(EDIT_ORGANIZATION);
public static final String VIEW_WORKSPACE = "VIEW_WORKSPACE";
public static final SimpleGrantedAuthority AUTHORITY_VIEW_WORKSPACE = new SimpleGrantedAuthority(VIEW_WORKSPACE);
public static final String EDIT_WORKSPACE = "EDIT_WORKSPACE";
public static final SimpleGrantedAuthority AUTHORITY_EDIT_WORKSPACE = new SimpleGrantedAuthority(EDIT_WORKSPACE);
public static final String VIEW_FORMS = "VIEW_FORMS";
public static final SimpleGrantedAuthority AUTHORITY_VIEW_FORMS = new SimpleGrantedAuthority(VIEW_FORMS);
public static final String VIEW_FORM = "VIEW_FORM";
public static final SimpleGrantedAuthority AUTHORITY_VIEW_FORM = new SimpleGrantedAuthority(VIEW_FORM);
public static final String EDIT_FORM = "EDIT_FORM";
public static final SimpleGrantedAuthority AUTHORITY_EDIT_FORM = new SimpleGrantedAuthority(EDIT_FORM);
public static final String VIEW_RESULT = "VIEW_RESULT";
public static final SimpleGrantedAuthority AUTHORITY_VIEW_RESULT = new SimpleGrantedAuthority(VIEW_RESULT);
private AuthoritiesConstants() {}
private AuthoritiesConstants() {
}
}
package com.unantes.orientactive.security.permissions;
import com.unantes.orientactive.domain.User;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import java.util.List;
import java.util.Optional;
......@@ -129,4 +133,15 @@ public abstract class PermissionService<T extends PermissionEntity> {
* @return Présence ou non du wildcard.
*/
protected abstract boolean hasWildcard(User user);
/**
* Vérification de la présence d'un {@link SimpleGrantedAuthority} pour l'utilisateur courant.
*
* @param authority La {@link SimpleGrantedAuthority} qui doit être présente.
* @return Vrai si l'utilisateur possède la {@link SimpleGrantedAuthority}, sinon faux.
*/
public static boolean currentUserHasAuthority(final SimpleGrantedAuthority authority) {
final Authentication currentUser = SecurityContextHolder.getContext().getAuthentication();
return currentUser.getAuthorities().contains(authority);
}
}
package com.unantes.orientactive.web.rest.errors;
import com.unantes.orientactive.filariane.FilArianeException;
import com.unantes.orientactive.navigation.exception.NavigationException;
import com.unantes.orientactive.security.permissions.web.PermissionException;
import java.net.URI;
......@@ -179,6 +180,12 @@ public class ExceptionTranslator implements ProblemHandling, SecurityAdviceTrait
return create(exception, problem, request);
}
@ExceptionHandler
public ResponseEntity<Problem> handlePermissionException(FilArianeException exception, NativeWebRequest request) {
Problem problem = Problem.builder().withType(null).withStatus(Status.FORBIDDEN).with(MESSAGE_KEY, exception.getMessage()).build();
return create(exception, problem, request);
}
@Override
public ProblemBuilder prepare(final Throwable throwable, final StatusType status, final URI type) {
Collection<String> activeProfiles = Arrays.asList(env.getActiveProfiles());
......
......@@ -19,3 +19,5 @@ email.reset.title=orientactive password reset
email.reset.greeting=Dear {0}
email.reset.text1=For your orientactive account a password reset was requested, please click on the URL below to reset it:
email.reset.text2=Regards,
filariane.home.title=Home
......@@ -19,3 +19,5 @@ email.reset.title=orientactive Réinitialisation de mot de passe
email.reset.greeting=Cher {0}
email.reset.text1=Un nouveau mot de passe pour votre compte orientactive a été demandé, veuillez cliquer sur le lien ci-dessous pour le réinitialiser :
email.reset.text2=Cordialement,
filariane.home.title=Accueil
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment