use of io.gravitee.management.model.UserEntity in project gravitee-management-rest-api by gravitee-io.
the class ApiService_CreateTest method testCreationWithContextPath.
private void testCreationWithContextPath(String existingContextPath, String contextPathToCreate) throws TechnicalException {
when(apiRepository.findById(anyString())).thenReturn(Optional.empty());
when(apiRepository.create(any())).thenReturn(api);
when(newApi.getName()).thenReturn(API_NAME);
when(newApi.getVersion()).thenReturn("v1");
when(newApi.getDescription()).thenReturn("Ma description");
when(apiRepository.findAll()).thenReturn(new HashSet<>(Arrays.asList(api)));
when(api.getId()).thenReturn(API_ID);
when(api.getDefinition()).thenReturn("{\"id\": \"" + API_ID + "\",\"name\": \"" + API_NAME + "\",\"proxy\": {\"context_path\": \"" + existingContextPath + "\"}}");
when(newApi.getContextPath()).thenReturn(contextPathToCreate);
when(userService.findById(USER_NAME)).thenReturn(new UserEntity());
Membership po = new Membership("admin", API_ID, MembershipReferenceType.API);
po.setRoles(Collections.singletonMap(RoleScope.API.getId(), SystemRole.PRIMARY_OWNER.name()));
when(membershipRepository.findByReferencesAndRole(MembershipReferenceType.API, Collections.singletonList(API_ID), RoleScope.API, SystemRole.PRIMARY_OWNER.name())).thenReturn(Collections.singleton(po));
apiService.create(newApi, USER_NAME);
}
use of io.gravitee.management.model.UserEntity in project gravitee-management-rest-api by gravitee-io.
the class ApplicationService_FindByIdTest method shouldFindById.
@Test
public void shouldFindById() throws TechnicalException {
when(applicationRepository.findById(APPLICATION_ID)).thenReturn(Optional.of(application));
when(application.getStatus()).thenReturn(ApplicationStatus.ACTIVE);
Membership po = new Membership(USER_NAME, APPLICATION_ID, MembershipReferenceType.APPLICATION);
po.setRoles(Collections.singletonMap(RoleScope.APPLICATION.getId(), SystemRole.PRIMARY_OWNER.name()));
when(membershipRepository.findByReferenceAndRole(any(), any(), eq(RoleScope.APPLICATION), any())).thenReturn(Collections.singleton(po));
when(userService.findByUsername(USER_NAME, false)).thenReturn(new UserEntity());
final ApplicationEntity applicationEntity = applicationService.findById(APPLICATION_ID);
assertNotNull(applicationEntity);
}
use of io.gravitee.management.model.UserEntity in project gravitee-management-rest-api by gravitee-io.
the class ApplicationService_FindByUserTest method shouldFindByUserAndGroup.
@Test
public void shouldFindByUserAndGroup() throws Exception {
when(appMembership.getReferenceId()).thenReturn(APPLICATION_ID);
when(groupAppMembership.getReferenceId()).thenReturn(GROUP_APPLICATION_ID);
when(groupAppMembership.getRoles()).thenReturn(Collections.singletonMap(RoleScope.APPLICATION.getId(), "USER"));
when(application.getId()).thenReturn(APPLICATION_ID);
when(application.getStatus()).thenReturn(ApplicationStatus.ACTIVE);
when(groupApplication.getId()).thenReturn(GROUP_APPLICATION_ID);
when(groupApplication.getStatus()).thenReturn(ApplicationStatus.ACTIVE);
when(membershipRepository.findByUserAndReferenceType(USERNAME, MembershipReferenceType.APPLICATION)).thenReturn(Collections.singleton(appMembership));
when(applicationRepository.findByIds(Collections.singletonList(APPLICATION_ID))).thenReturn(Collections.singleton(application));
when(membershipRepository.findByUserAndReferenceType(USERNAME, MembershipReferenceType.GROUP)).thenReturn(Collections.singleton(groupAppMembership));
when(applicationRepository.findByGroups(Collections.singletonList(GROUP_APPLICATION_ID), ApplicationStatus.ACTIVE)).thenReturn(Collections.singleton(groupApplication));
Membership poApp = new Membership(USERNAME, APPLICATION_ID, MembershipReferenceType.APPLICATION);
poApp.setRoles(Collections.singletonMap(RoleScope.APPLICATION.getId(), SystemRole.PRIMARY_OWNER.name()));
Membership poGroupApp = new Membership(USERNAME, GROUP_APPLICATION_ID, MembershipReferenceType.APPLICATION);
poGroupApp.setRoles(Collections.singletonMap(RoleScope.APPLICATION.getId(), SystemRole.PRIMARY_OWNER.name()));
Set<Membership> memberships = new HashSet<>();
memberships.add(poApp);
memberships.add(poGroupApp);
when(membershipRepository.findByReferencesAndRole(any(), any(), eq(RoleScope.APPLICATION), any())).thenReturn(memberships);
when(userService.findByUsername(USERNAME, false)).thenReturn(new UserEntity());
Set<ApplicationEntity> apps = applicationService.findByUser(USERNAME);
Assert.assertNotNull(apps);
Assert.assertFalse("should find apps", apps.isEmpty());
Assert.assertEquals(2, apps.size());
}
use of io.gravitee.management.model.UserEntity 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.UserEntity 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();
}
Aggregations