SharingController { @RequestMapping( method = { RequestMethod.POST, RequestMethod.PUT }, consumes = MediaType.APPLICATION_JSON_VALUE ) public void setSharing( @RequestParam String type, @RequestParam String id, HttpServletResponse response, HttpServletRequest request ) throws IOException, WebMessageException { type = getSharingType( type ); Class<? extends IdentifiableObject> sharingClass = aclService.classForType( type ); if ( sharingClass == null || !aclService.isShareable( sharingClass ) ) { throw new WebMessageException( WebMessageUtils.conflict( "Type " + type + " is not supported." ) ); } BaseIdentifiableObject object = (BaseIdentifiableObject) manager.get( sharingClass, id ); if ( object == null ) { throw new WebMessageException( WebMessageUtils.notFound( "Object of type " + type + " with ID " + id + " was not found." ) ); } if ( ( object instanceof SystemDefaultMetadataObject ) && ( (SystemDefaultMetadataObject) object ).isDefault() ) { throw new WebMessageException( WebMessageUtils.conflict( "Sharing settings of system default metadata object of type " + type + " cannot be modified." ) ); } User user = currentUserService.getCurrentUser(); if ( !aclService.canManage( user, object ) ) { throw new AccessDeniedException( "You do not have manage access to this object." ); } Sharing sharing = renderService.fromJson( request.getInputStream(), Sharing.class ); if ( !AccessStringHelper.isValid( sharing.getObject().getPublicAccess() ) ) { throw new WebMessageException( WebMessageUtils.conflict( "Invalid public access string: " + sharing.getObject().getPublicAccess() ) ); } if ( aclService.canMakeExternal( user, object.getClass() ) ) { object.setExternalAccess( sharing.getObject().hasExternalAccess() ); } Schema schema = schemaService.getDynamicSchema( sharingClass ); if ( aclService.canMakePublic( user, object.getClass() ) ) { object.setPublicAccess( sharing.getObject().getPublicAccess() ); } if ( !schema.isDataShareable() ) { if ( AccessStringHelper.hasDataSharing( object.getPublicAccess() ) ) { throw new WebMessageException( WebMessageUtils.conflict( "Data sharing is not enabled for this object." ) ); } } if ( object.getUser() == null ) { object.setUser( user ); } Iterator<UserGroupAccess> userGroupAccessIterator = object.getUserGroupAccesses().iterator(); while ( userGroupAccessIterator.hasNext() ) { UserGroupAccess userGroupAccess = userGroupAccessIterator.next(); userGroupAccessIterator.remove(); userGroupAccessService.deleteUserGroupAccess( userGroupAccess ); } for ( SharingUserGroupAccess sharingUserGroupAccess : sharing.getObject().getUserGroupAccesses() ) { UserGroupAccess userGroupAccess = new UserGroupAccess(); if ( !AccessStringHelper.isValid( sharingUserGroupAccess.getAccess() ) ) { throw new WebMessageException( WebMessageUtils.conflict( "Invalid user group access string: " + sharingUserGroupAccess.getAccess() ) ); } if ( !schema.isDataShareable() ) { if ( AccessStringHelper.hasDataSharing( sharingUserGroupAccess.getAccess() ) ) { throw new WebMessageException( WebMessageUtils.conflict( "Data sharing is not enabled for this object." ) ); } } userGroupAccess.setAccess( sharingUserGroupAccess.getAccess() ); UserGroup userGroup = manager.get( UserGroup.class, sharingUserGroupAccess.getId() ); if ( userGroup != null ) { userGroupAccess.setUserGroup( userGroup ); userGroupAccessService.addUserGroupAccess( userGroupAccess ); object.getUserGroupAccesses().add( userGroupAccess ); } } Iterator<UserAccess> userAccessIterator = object.getUserAccesses().iterator(); while ( userAccessIterator.hasNext() ) { UserAccess userAccess = userAccessIterator.next(); userAccessIterator.remove(); userAccessService.deleteUserAccess( userAccess ); } for ( SharingUserAccess sharingUserAccess : sharing.getObject().getUserAccesses() ) { UserAccess userAccess = new UserAccess(); if ( !AccessStringHelper.isValid( sharingUserAccess.getAccess() ) ) { throw new WebMessageException( WebMessageUtils.conflict( "Invalid user access string: " + sharingUserAccess.getAccess() ) ); } if ( !schema.isDataShareable() ) { if ( AccessStringHelper.hasDataSharing( sharingUserAccess.getAccess() ) ) { throw new WebMessageException( WebMessageUtils.conflict( "Data sharing is not enabled for this object." ) ); } } userAccess.setAccess( sharingUserAccess.getAccess() ); User sharingUser = manager.get( User.class, sharingUserAccess.getId() ); if ( sharingUser != null ) { userAccess.setUser( sharingUser ); userAccessService.addUserAccess( userAccess ); object.getUserAccesses().add( userAccess ); } } manager.updateNoAcl( object ); if ( Program.class.isInstance( object ) ) { syncSharingForEventProgram( (Program) object ); } log.info( sharingToString( object ) ); webMessageService.send( WebMessageUtils.ok( "Access control set" ), response, request ); }  @RequestMapping( method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE ) void getSharing( @RequestParam String type, @RequestParam String id, HttpServletResponse response ); @RequestMapping( method = { RequestMethod.POST, RequestMethod.PUT }, consumes = MediaType.APPLICATION_JSON_VALUE ) void setSharing( @RequestParam String type, @RequestParam String id, HttpServletResponse response, HttpServletRequest request ); @RequestMapping( value = "/search", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE ) void searchUserGroups( @RequestParam String key, @RequestParam( required = false ) Integer pageSize,
        HttpServletResponse response ); static final String RESOURCE_PATH; }
@Test( expected = AccessDeniedException.class ) public void notSystemDefaultMetadataNoAccess() throws Exception { final OrganisationUnit organisationUnit = new OrganisationUnit(); Mockito.doReturn( OrganisationUnit.class ).when( aclService ).classForType( Mockito.eq( "organisationUnit" ) ); Mockito.when( aclService.isShareable( Mockito.eq( OrganisationUnit.class ) ) ).thenReturn( true ); Mockito.doReturn( organisationUnit ).when( manager ).get( Mockito.eq( OrganisationUnit.class ), Mockito.eq( "kkSjhdhks" ) ); sharingController.setSharing( "organisationUnit", "kkSjhdhks", response, request ); }
@Test( expected = AccessDeniedException.class ) public void systemDefaultMetadataNoAccess() throws Exception { final Category category = new Category(); category.setName( Category.DEFAULT_NAME + "x" ); Mockito.doReturn( Category.class ).when( aclService ).classForType( Mockito.eq( "category" ) ); Mockito.when( aclService.isShareable( Mockito.eq( Category.class ) ) ).thenReturn( true ); Mockito.when( manager.get( Mockito.eq( Category.class ), Mockito.eq( "kkSjhdhks" ) ) ).thenReturn( category ); sharingController.setSharing( "category", "kkSjhdhks", response, request ); }
@Test( expected = WebMessageException.class ) public void systemDefaultMetadata() throws Exception { final Category category = new Category(); category.setName( Category.DEFAULT_NAME ); Mockito.doReturn( Category.class ).when( aclService ).classForType( Mockito.eq( "category" ) ); Mockito.when( aclService.isShareable( Mockito.eq( Category.class ) ) ).thenReturn( true ); Mockito.when( manager.get( Mockito.eq( Category.class ), Mockito.eq( "kkSjhdhks" ) ) ).thenReturn( category ); try { sharingController.setSharing( "category", "kkSjhdhks", response, request ); } catch ( WebMessageException e ) { Assert.assertThat( e.getWebMessage().getMessage(), containsString( "Sharing settings of system default metadata object" ) ); throw e; } }