use of io.gravitee.management.model.RoleEntity in project gravitee-management-rest-api by gravitee-io.
the class RoleServiceImpl method update.
@Override
public RoleEntity update(final UpdateRoleEntity roleEntity) {
if (isReserved(roleEntity.getName())) {
throw new RoleReservedNameException(SystemRole.ADMIN.name());
}
RoleScope scope = convert(roleEntity.getScope());
try {
Optional<Role> optRole = roleRepository.findById(scope, roleEntity.getName());
if (!optRole.isPresent()) {
throw new RoleNotFoundException(scope, roleEntity.getName());
}
Role role = optRole.get();
Role updatedRole = convert(roleEntity);
updatedRole.setCreatedAt(role.getCreatedAt());
RoleEntity entity = convert(roleRepository.update(updatedRole));
auditService.createPortalAuditLog(Collections.singletonMap(ROLE, role.getScope() + ":" + role.getName()), ROLE_UPDATED, updatedRole.getUpdatedAt(), role, updatedRole);
if (entity.isDefaultRole()) {
toggleDefaultRole(scope, entity.getName());
}
return entity;
} catch (TechnicalException ex) {
LOGGER.error("An error occurs while trying to update role {}", roleEntity.getName(), ex);
throw new TechnicalManagementException("An error occurs while trying to update role " + roleEntity.getName(), ex);
}
}
use of io.gravitee.management.model.RoleEntity in project gravitee-management-rest-api by gravitee-io.
the class MembershipService_AddOrUpdateMemberTest method shouldUpdateApiGroupMembership.
@Test
public void shouldUpdateApiGroupMembership() throws Exception {
UserEntity userEntity = new UserEntity();
userEntity.setId("my name");
userEntity.setUsername("my name");
userEntity.setEmail("me@mail.com");
Membership membership = new Membership();
membership.setUserId(userEntity.getUsername());
membership.setReferenceType(MembershipReferenceType.GROUP);
membership.setReferenceId(GROUP_ID);
Map<Integer, String> roles = new HashMap<>();
roles.put(RoleScope.API.getId(), "USER");
membership.setRoles(roles);
Membership newMembership = new Membership();
newMembership.setUserId(userEntity.getUsername());
newMembership.setReferenceType(MembershipReferenceType.GROUP);
newMembership.setReferenceId(GROUP_ID);
GroupEntity groupEntityMock = mock(GroupEntity.class);
when(groupEntityMock.getName()).thenReturn("foo");
RoleEntity role = mock(RoleEntity.class);
when(role.getScope()).thenReturn(io.gravitee.management.model.permissions.RoleScope.API);
when(roleService.findById(any(), any())).thenReturn(role);
when(userService.findById(userEntity.getId())).thenReturn(userEntity);
when(groupService.findById(GROUP_ID)).thenReturn(groupEntityMock);
when(membershipRepository.findById(userEntity.getId(), MembershipReferenceType.GROUP, GROUP_ID)).thenReturn(of(membership));
when(membershipRepository.update(any())).thenReturn(newMembership);
MemberEntity updateMember = membershipService.addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.GROUP, GROUP_ID), new MembershipService.MembershipUser(userEntity.getUsername(), null), new MembershipService.MembershipRole(RoleScope.API, "OWNER"));
verify(userService, times(2)).findById(userEntity.getId());
verify(membershipRepository, times(2)).findById(userEntity.getId(), MembershipReferenceType.GROUP, GROUP_ID);
verify(membershipRepository, never()).create(any());
verify(membershipRepository, times(1)).update(any());
verify(emailService, never()).sendAsyncEmailNotification(any());
}
use of io.gravitee.management.model.RoleEntity in project gravitee-management-rest-api by gravitee-io.
the class AbstractAuthenticationResource method connectUser.
protected Response connectUser(String userId) {
UserEntity user = userService.connect(userId);
final Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
final UserDetails userDetails = (UserDetails) authentication.getPrincipal();
// Manage authorities, initialize it with dynamic permissions from the IDP
Set<GrantedAuthority> authorities = new HashSet<>(userDetails.getAuthorities());
// We must also load permissions from repository for configured management or portal role
RoleEntity role = membershipService.getRole(MembershipReferenceType.MANAGEMENT, MembershipDefaultReferenceId.DEFAULT.toString(), userDetails.getUsername(), RoleScope.MANAGEMENT);
if (role != null) {
authorities.add(new SimpleGrantedAuthority(role.getScope().toString() + ':' + role.getName()));
}
role = membershipService.getRole(MembershipReferenceType.PORTAL, MembershipDefaultReferenceId.DEFAULT.toString(), userDetails.getUsername(), RoleScope.PORTAL);
if (role != null) {
authorities.add(new SimpleGrantedAuthority(role.getScope().toString() + ':' + role.getName()));
}
// JWT signer
final Map<String, Object> claims = new HashMap<>();
claims.put(JWTHelper.Claims.ISSUER, environment.getProperty("jwt.issuer", JWTHelper.DefaultValues.DEFAULT_JWT_ISSUER));
claims.put(JWTHelper.Claims.SUBJECT, user.getId());
claims.put(JWTHelper.Claims.PERMISSIONS, authorities);
claims.put(JWTHelper.Claims.EMAIL, user.getEmail());
claims.put(JWTHelper.Claims.FIRSTNAME, user.getFirstname());
claims.put(JWTHelper.Claims.LASTNAME, user.getLastname());
final JWTSigner.Options options = new JWTSigner.Options();
options.setExpirySeconds(environment.getProperty("jwt.expire-after", Integer.class, DEFAULT_JWT_EXPIRE_AFTER));
options.setIssuedAt(true);
options.setJwtId(true);
return Response.ok().entity(user).cookie(new NewCookie(HttpHeaders.AUTHORIZATION, "Bearer " + new JWTSigner(environment.getProperty("jwt.secret")).sign(claims, options), environment.getProperty("jwt.cookie-path", "/"), environment.getProperty("jwt.cookie-domain"), "", environment.getProperty("jwt.expire-after", Integer.class, DEFAULT_JWT_EXPIRE_AFTER), environment.getProperty("jwt.cookie-secure", Boolean.class, false), true)).build();
}
use of io.gravitee.management.model.RoleEntity in project gravitee-management-rest-api by gravitee-io.
the class AuthenticationSuccessListenerTest method shouldCreateUserWithCustomGlobalRole.
@Test
public void shouldCreateUserWithCustomGlobalRole() {
when(eventMock.getAuthentication()).thenReturn(authenticationMock);
when(authenticationMock.getPrincipal()).thenReturn(userDetailsMock);
Collection authorities = Collections.singleton(new SimpleGrantedAuthority("ROLE"));
when(authenticationMock.getAuthorities()).thenReturn(authorities);
when(userServiceMock.findByUsername(userDetailsMock.getUsername(), false)).thenThrow(UserNotFoundException.class);
RoleEntity roleEntity = mock(RoleEntity.class);
when(roleEntity.getName()).thenReturn("ROLE");
when(roleServiceMock.findById(RoleScope.MANAGEMENT, "ROLE")).thenReturn(roleEntity);
when(roleServiceMock.findById(RoleScope.PORTAL, "ROLE")).thenReturn(roleEntity);
when(userServiceMock.create(any(NewExternalUserEntity.class), eq(false))).thenReturn(userEntity);
listener.onApplicationEvent(eventMock);
verify(userServiceMock, times(1)).findByUsername(userDetailsMock.getUsername(), false);
verify(userServiceMock, times(1)).create(any(NewExternalUserEntity.class), eq(false));
verify(membershipServiceMock, times(1)).addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.MANAGEMENT, MembershipDefaultReferenceId.DEFAULT.name()), new MembershipService.MembershipUser(userDetailsMock.getUsername(), null), new MembershipService.MembershipRole(RoleScope.MANAGEMENT, "ROLE"));
verify(membershipServiceMock, times(1)).addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.PORTAL, MembershipDefaultReferenceId.DEFAULT.name()), new MembershipService.MembershipUser(userDetailsMock.getUsername(), null), new MembershipService.MembershipRole(RoleScope.PORTAL, "ROLE"));
verify(userServiceMock, times(1)).connect(userDetailsMock.getUsername());
}
use of io.gravitee.management.model.RoleEntity in project gravitee-management-rest-api by gravitee-io.
the class AuthenticationSuccessListenerTest method shouldCreateUserWithCustomSpecificRole.
@Test
public void shouldCreateUserWithCustomSpecificRole() {
when(eventMock.getAuthentication()).thenReturn(authenticationMock);
when(authenticationMock.getPrincipal()).thenReturn(userDetailsMock);
Collection authorities = Arrays.asList(new SimpleGrantedAuthority("MANAGEMENT:ROLE1"), new SimpleGrantedAuthority("PORTAL:ROLE2"));
when(authenticationMock.getAuthorities()).thenReturn(authorities);
when(userServiceMock.findByUsername(userDetailsMock.getUsername(), false)).thenThrow(UserNotFoundException.class);
RoleEntity roleEntity1 = mock(RoleEntity.class);
when(roleEntity1.getName()).thenReturn("ROLE1");
RoleEntity roleEntity2 = mock(RoleEntity.class);
when(roleEntity2.getName()).thenReturn("ROLE2");
when(roleServiceMock.findById(RoleScope.MANAGEMENT, "ROLE1")).thenReturn(roleEntity1);
when(roleServiceMock.findById(RoleScope.PORTAL, "ROLE2")).thenReturn(roleEntity2);
when(userServiceMock.create(any(NewExternalUserEntity.class), eq(false))).thenReturn(userEntity);
listener.onApplicationEvent(eventMock);
verify(userServiceMock, times(1)).findByUsername(userDetailsMock.getUsername(), false);
verify(userServiceMock, times(1)).create(any(NewExternalUserEntity.class), eq(false));
verify(membershipServiceMock, times(1)).addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.MANAGEMENT, MembershipDefaultReferenceId.DEFAULT.name()), new MembershipService.MembershipUser(userDetailsMock.getUsername(), null), new MembershipService.MembershipRole(RoleScope.MANAGEMENT, "ROLE1"));
verify(membershipServiceMock, times(1)).addOrUpdateMember(new MembershipService.MembershipReference(MembershipReferenceType.PORTAL, MembershipDefaultReferenceId.DEFAULT.name()), new MembershipService.MembershipUser(userDetailsMock.getUsername(), null), new MembershipService.MembershipRole(RoleScope.PORTAL, "ROLE2"));
verify(userServiceMock, times(1)).connect(userDetailsMock.getUsername());
}
Aggregations