Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: user roles management #669

Closed
wants to merge 33 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
94a60bb
feat(rbac): init user at OAuth2 login
omar-chahbouni-decathlon Oct 28, 2022
7bdf23c
feat(rbac): manage user resources access
omar-chahbouni-decathlon Nov 29, 2022
8638dc1
feat(web-ui): Added front part for admin rights and some user access …
Vincent59 Dec 12, 2022
b6952fa
feat(web-ui): The demo project is not displayed in projects list anym…
Vincent59 Dec 13, 2022
7dbd4a6
fix(rbac): enable github login
omar-chahbouni-decathlon Dec 15, 2022
8789a67
fix(rbac): enable the demo project access for authenticated users
omar-chahbouni-decathlon Dec 21, 2022
22e47b5
feat(rbac): display project creation and update details (date and user)
omar-chahbouni-decathlon Dec 25, 2022
6fa60a2
feat(rbac): link default project to each user
omar-chahbouni-decathlon Dec 27, 2022
e1d9b2c
feat(rbac): fetch user accounts
omar-chahbouni-decathlon Jan 4, 2023
7ab805d
feat(rbac): manage other users scopes
omar-chahbouni-decathlon Jan 13, 2023
50b0477
feat(web-ui): Add member projects part and fix multiple feedback
Vincent59 Jan 18, 2023
29e2e60
feat(web-ui): Add user project service to front and fix some issues
Vincent59 Jan 19, 2023
a1d1494
refactor(rbac): fetch current user details from session
omar-chahbouni-decathlon Jan 19, 2023
c8a3212
feat(rbac): update users profile
omar-chahbouni-decathlon Jan 20, 2023
260627e
feat(web-ui): Add clear default project button and projects filter
Vincent59 Jan 26, 2023
e509437
fix(web-ui): Add warn message if the user need an admin and rework pr…
Vincent59 Jan 30, 2023
ef24893
feat(rbac): add description field to projects
omar-chahbouni-decathlon Jan 28, 2023
8d079e1
fix(web-ui): Rework top menu navigation
Vincent59 Jan 31, 2023
5a16a04
refactor(rbac): change local oauth2 user profile pictures
omar-chahbouni-decathlon Jan 31, 2023
0a3a1a5
fix(web-ui): hide CTA if the user is a member
Vincent59 Feb 1, 2023
2e7a090
refactor(rbac): rename user entity related classes
omar-chahbouni-decathlon Feb 2, 2023
a783142
feat(rbac): create, read, update and read user groups
omar-chahbouni-decathlon Feb 19, 2023
1c0188e
fix(web-ui): rework UX and fix some front issues
Vincent59 Feb 17, 2023
8815645
fix(web-ui): Affect user to a project depending of the profile and is…
Vincent59 Feb 22, 2023
bc0fa67
fix(web-ui): Buttons not showing correctly
Vincent59 Feb 22, 2023
4959603
feat(rbac): manage group members and managers
omar-chahbouni-decathlon Feb 22, 2023
6b737b4
fix(rbac): return updated user account after updating its scope or pr…
omar-chahbouni-decathlon Feb 23, 2023
479e82f
feat(rbac): manage user group scopes
omar-chahbouni-decathlon Mar 1, 2023
8c6848d
feat(web-ui): group management and fix some issues
Vincent59 Mar 1, 2023
5037445
feat: add a dynamic breadcrumb on each pages, personal user page, use…
Vincent59 Mar 7, 2023
0e6e9c2
fix(web-ui): code improvment, fix breadcrumb on refresh, add elements…
Vincent59 Mar 14, 2023
6b0504e
fix(web-ui): rework group management code
Vincent59 Mar 17, 2023
e0cf0ca
feat(rbac): get the actually applied scopes from user and groups
omar-chahbouni-decathlon Mar 6, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
feat(rbac): link default project to each user
  • Loading branch information
omar-chahbouni-decathlon committed Dec 27, 2022
commit 6fa60a29fa7c0e12235b1ae53972f0d61c788ba6
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public DemoSettingsLoader(

public ProjectDTO createProjectWithCommunicationsAndRootCauses() throws NotUniqueException, ForbiddenException {
var creationUser = userAccountService.getCurrentUserEntity().orElseThrow(() -> new ForbiddenException(Entities.PROJECT, "demo project creation"));
var demoProject = new ProjectDTO(DEMO_PROJECT_CODE, DEMO_PROJECT_NAME, false);
var demoProject = new ProjectDTO(DEMO_PROJECT_CODE, DEMO_PROJECT_NAME);
return projectService.create(demoProject, creationUser);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ public SecurityFilterChain configure(HttpSecurity http) throws Exception {
.authorizeRequests() //NOSONAR
.antMatchers(AuthenticationResource.PATHS, RestConstants.ACTUATOR_PATHS).permitAll()
// user
.antMatchers(HttpMethod.DELETE, UserResource.DEFAULT_PROJECT_PATH).authenticated()
.antMatchers(HttpMethod.PUT, UserResource.DEFAULT_PROJECT_CODE_PATH).access(PROJECT_INSTANCE_FETCH_PERMISSION)
.antMatchers(HttpMethod.GET, UserResource.PATHS).authenticated()

// projects > demo
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@

package com.decathlon.ara.security.dto.user;

import com.decathlon.ara.domain.Project;
import com.decathlon.ara.domain.security.member.user.entity.UserEntity;
import com.decathlon.ara.domain.security.member.user.entity.UserEntityRoleOnProject;
import com.decathlon.ara.security.dto.user.scope.UserAccountScope;
import com.decathlon.ara.security.dto.user.scope.UserAccountScopeRole;
import com.decathlon.ara.security.service.AuthorityService;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.lang.NonNull;
import org.springframework.security.core.GrantedAuthority;
Expand Down Expand Up @@ -50,11 +52,15 @@ public class UserAccount {

private List<UserAccountScope> scopes;

@JsonProperty("default_project")
private String defaultProjectCode;

public UserAccount(@NonNull Map<String, String> userAttributes, @NonNull UserEntity userEntity) {
this.providerName = userEntity.getProviderName();
this.login = userEntity.getLogin();
this.profile = getProfileFromUserEntity(userEntity);
this.scopes = getScopesFromUserEntity(userEntity);
this.defaultProjectCode = userEntity.getDefaultProject().map(Project::getCode).orElse(null);

this.firstName = userAttributes.get(StandardClaimNames.GIVEN_NAME);
this.lastName = userAttributes.get(StandardClaimNames.FAMILY_NAME);
Expand Down Expand Up @@ -94,6 +100,10 @@ public List<UserAccountScope> getScopes() {
return scopes;
}

public String getDefaultProjectCode() {
return defaultProjectCode;
}

private UserAccountProfile getProfileFromUserEntity(@NonNull UserEntity userEntity) {
var userEntityProfile = userEntity.getProfile();
return UserAccountProfile.valueOf(userEntityProfile.name());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,45 @@ private static void updateUserEntityRoles(String projectCode, Project project, U
userRoles.add(new UserEntityRoleOnProject(userToUpdate, project, roleToUpdate));
}
}

/**
* Clear the current user default project
* @return the updated user account
* @throws ForbiddenException thrown if the operation failed
*/
public UserAccount clearDefaultProject() throws ForbiddenException {
var userToUpdate = getCurrentUserEntity().orElseThrow(() -> new ForbiddenException(Entities.PROJECT, "clear default project"));
userToUpdate.setDefaultProject(null);
var updatedUser = userEntityRepository.save(userToUpdate);
return getUserAccountFromUserEntity(updatedUser);
}

private UserAccount getUserAccountFromUserEntity(@NonNull UserEntity userEntity) {
var authentication = (OAuth2AuthenticationToken) SecurityContextHolder.getContext().getAuthentication();
var oauth2User = authentication.getPrincipal();
var providerName = userEntity.getProviderName();
var strategy = userStrategySelector.selectUserStrategyFromProviderName(providerName);
return strategy.getUserAccount(oauth2User, userEntity);
}

/**
* Update the user default project
* @param projectCode the project code. Note that the code must exist and the user must have access to this project.
* @return the updated user account
* @throws ForbiddenException thrown if the operation failed
*/
public UserAccount updateDefaultProject(@NonNull String projectCode) throws ForbiddenException {
var projectCodeContext = getProjectCodeExceptionContext(projectCode);
var exception = new ForbiddenException(Entities.PROJECT, "update default project", projectCodeContext);

if (StringUtils.isBlank(projectCode)) {
throw exception;
}

var defaultProject = projectRepository.findByCode(projectCode).orElseThrow(() -> exception);
var userToUpdate = getCurrentUserEntity().orElseThrow(() -> exception);
userToUpdate.setDefaultProject(defaultProject);
var updatedUser = userEntityRepository.save(userToUpdate);
return getUserAccountFromUserEntity(updatedUser);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public ProjectDTO create(@NonNull ProjectDTO dtoToCreate, @NonNull UserEntity cr
*/
public ProjectDTO createFromCode(@NonNull String projectCode, @NonNull UserEntity creationUser) throws NotUniqueException {
var projectName = getProjectNameFromCode(projectCode);
var projectToCreate = new ProjectDTO(projectCode, projectName, false);
var projectToCreate = new ProjectDTO(projectCode, projectName);
return create(projectToCreate, creationUser);
}

Expand Down Expand Up @@ -209,7 +209,6 @@ public long toId(String code) throws NotFoundException {
private void validateBusinessRules(ProjectDTO dto) throws NotUniqueException {
validateUniqueCode(dto);
validateUniqueName(dto);
switchProjectAsDefault(dto);
}

private void validateUniqueCode(ProjectDTO dto) throws NotUniqueException {
Expand All @@ -226,13 +225,4 @@ private void validateUniqueName(ProjectDTO dto) throws NotUniqueException {
}
}

private void switchProjectAsDefault(ProjectDTO dto) {
if (dto.isDefaultAtStartup()) {
Project entityDataBaseWithDefaultAtStartup = projectRepository.findByDefaultAtStartup(true);
if (entityDataBaseWithDefaultAtStartup != null && !entityDataBaseWithDefaultAtStartup.getCode().equals(dto.getCode())) {
entityDataBaseWithDefaultAtStartup.setDefaultAtStartup(false);
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@ public class ProjectDTO {
@Size(min = 1, max = 64, message = "The name is required and must not exceed {max} characters.")
private String name;

/**
* True to use that project as the default one appearing at ARA's client startup when no project code is present in
* URL. Only one project can be declared as the default.
*/
private boolean defaultAtStartup;

@JsonProperty("creation_date")
@JsonFormat(pattern = DATE_FORMAT_YEAR_TO_SECOND)
private Date creationDate;
Expand All @@ -65,20 +59,15 @@ public class ProjectDTO {
public ProjectDTO() {
}

public ProjectDTO(Long id,
String code,
String name,
boolean defaultAtStartup) {
public ProjectDTO(Long id, String code, String name) {
this.id = id;
this.code = code;
this.name = name;
this.defaultAtStartup = defaultAtStartup;
}

public ProjectDTO(String code, String name, boolean defaultAtStartup) {
public ProjectDTO(String code, String name) {
this.code = code;
this.name = name;
this.defaultAtStartup = defaultAtStartup;
}

public Long getId() {
Expand All @@ -97,10 +86,6 @@ public String getName() {
return name;
}

public boolean isDefaultAtStartup() {
return defaultAtStartup;
}

public Date getCreationDate() {
return creationDate;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ public ProjectDTO getProjectDTOFromProjectEntity(@NonNull Project project) {
var id = project.getId();
var code = project.getCode();
var name = project.getName();
var isDefaultAtStartup = project.isDefaultAtStartup();
var projectToConvert = new ProjectDTO(id, code, name, isDefaultAtStartup);
var projectToConvert = new ProjectDTO(id, code, name);

var creationDate = project.getCreationDate();
if (creationDate != null) {
Expand All @@ -41,9 +40,6 @@ public Project getProjectEntityFromProjectDTO(@NonNull ProjectDTO projectDTO) {
var id = projectDTO.getId();
var code = projectDTO.getCode();
var name = projectDTO.getName();
var isDefaultAtStartup = projectDTO.isDefaultAtStartup();
var projectToConvert = new Project(id, code, name);
projectToConvert.setDefaultAtStartup(isDefaultAtStartup);
return projectToConvert;
return new Project(id, code, name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

import com.decathlon.ara.security.dto.user.UserAccount;
import com.decathlon.ara.security.service.user.UserAccountService;
import com.decathlon.ara.service.exception.ForbiddenException;
import com.decathlon.ara.web.rest.util.ResponseUtil;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import static com.decathlon.ara.web.rest.util.RestConstants.API_PATH;
import static com.decathlon.ara.web.rest.util.RestConstants.PROJECT_CODE_REQUEST_PARAMETER;

@RestController
@RequestMapping(UserResource.PATH)
Expand All @@ -19,6 +20,11 @@ public class UserResource {
static final String PATH = API_PATH + "/user";
public static final String PATHS = PATH + "/**";

private static final String DEFAULT_PROJECT = "/default-project";
private static final String DEFAULT_PROJECT_CODE = DEFAULT_PROJECT + "/" + PROJECT_CODE_REQUEST_PARAMETER;
public static final String DEFAULT_PROJECT_PATH = PATH + DEFAULT_PROJECT;
public static final String DEFAULT_PROJECT_CODE_PATH = PATH + DEFAULT_PROJECT_CODE;

public UserResource(UserAccountService userAccountService) {
this.userAccountService = userAccountService;
}
Expand All @@ -29,4 +35,23 @@ public ResponseEntity<UserAccount> getUserDetails(OAuth2AuthenticationToken auth
return user.map(ResponseEntity::ok).orElse(ResponseEntity.badRequest().build());
}

@DeleteMapping(DEFAULT_PROJECT)
public ResponseEntity<UserAccount> clearDefaultProject() {
try {
var updatedAccount = userAccountService.clearDefaultProject();
return ResponseEntity.ok().body(updatedAccount);
} catch (ForbiddenException e) {
return ResponseUtil.handle(e);
}
}

@PutMapping(DEFAULT_PROJECT_CODE)
public ResponseEntity<UserAccount> updateDefaultProject(@PathVariable String projectCode) {
try {
var updatedAccount = userAccountService.updateDefaultProject(projectCode);
return ResponseEntity.ok().body(updatedAccount);
} catch (ForbiddenException e) {
return ResponseUtil.handle(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@TestPropertySource(properties = {
"ara.database.target=h2"
})
class AraExporterTest {
class AraExporterIT {

@Autowired
private AraExporter sut;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

@DataJpaTest
@ExtendWith(MockitoExtension.class)
class SpecificationUtilTest {
class SpecificationUtilIT {

@Autowired
private EntityManager entityManager;
Expand Down Expand Up @@ -134,7 +134,7 @@ public Predicate not() {
}

private String getName() {
return SpecificationUtilTest.getName(expression);
return SpecificationUtilIT.getName(expression);
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.oauth2.core.oidc.StandardClaimNames;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.*;

import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.mock;
Expand All @@ -34,6 +31,9 @@ void UserAccountConstructor_createUserAccountWithoutAttribute_whenUserAttributes
var userLogin = "user-login";
var providerName = "provider-name";

var defaultProject = mock(Project.class);
var defaultProjectCode = "default-project-code";

var userEntityRole1 = mock(UserEntityRoleOnProject.class);
var userEntityScope1 = UserEntityRoleOnProject.ScopedUserRoleOnProject.ADMIN;
var project1 = mock(Project.class);
Expand All @@ -55,6 +55,8 @@ void UserAccountConstructor_createUserAccountWithoutAttribute_whenUserAttributes
when(userEntity.getLogin()).thenReturn(userLogin);
when(userEntity.getProviderName()).thenReturn(providerName);
when(userEntity.getProfile()).thenReturn(userEntityProfile);
when(userEntity.getDefaultProject()).thenReturn(Optional.of(defaultProject));
when(defaultProject.getCode()).thenReturn(defaultProjectCode);

when(userEntity.getRolesOnProjectWhenScopedUser()).thenReturn(userEntityRoles);
when(userEntityRole1.getRole()).thenReturn(userEntityScope1);
Expand All @@ -74,6 +76,7 @@ void UserAccountConstructor_createUserAccountWithoutAttribute_whenUserAttributes
"providerName",
"login",
"profile",
"defaultProjectCode",
"firstName",
"lastName",
"email",
Expand All @@ -83,6 +86,7 @@ void UserAccountConstructor_createUserAccountWithoutAttribute_whenUserAttributes
providerName,
userLogin,
UserAccountProfile.valueOf(userEntityProfile.name()),
defaultProjectCode,
null,
null,
null,
Expand Down Expand Up @@ -116,6 +120,9 @@ void UserAccountConstructor_createUserAccountWithAttributes_whenUserAttributesIs
var userLogin = "user-login";
var providerName = "provider-name";

var defaultProject = mock(Project.class);
var defaultProjectCode = "default-project-code";

var userEntityRole1 = mock(UserEntityRoleOnProject.class);
var userEntityScope1 = UserEntityRoleOnProject.ScopedUserRoleOnProject.ADMIN;
var project1 = mock(Project.class);
Expand All @@ -137,6 +144,8 @@ void UserAccountConstructor_createUserAccountWithAttributes_whenUserAttributesIs
when(userEntity.getLogin()).thenReturn(userLogin);
when(userEntity.getProviderName()).thenReturn(providerName);
when(userEntity.getProfile()).thenReturn(userEntityProfile);
when(userEntity.getDefaultProject()).thenReturn(Optional.of(defaultProject));
when(defaultProject.getCode()).thenReturn(defaultProjectCode);

when(userEntity.getRolesOnProjectWhenScopedUser()).thenReturn(userEntityRoles);
when(userEntityRole1.getRole()).thenReturn(userEntityScope1);
Expand All @@ -156,6 +165,7 @@ void UserAccountConstructor_createUserAccountWithAttributes_whenUserAttributesIs
"providerName",
"login",
"profile",
"defaultProjectCode",
"firstName",
"lastName",
"email",
Expand All @@ -165,6 +175,7 @@ void UserAccountConstructor_createUserAccountWithAttributes_whenUserAttributesIs
providerName,
userLogin,
UserAccountProfile.valueOf(userEntityProfile.name()),
defaultProjectCode,
userFirstName,
userLastName,
userEmail,
Expand Down
Loading