Skip to content

Commit

Permalink
feat: Allow to change Space permission through Admin UI - MEED-7737 - M…
Browse files Browse the repository at this point in the history
  • Loading branch information
boubaker committed Nov 7, 2024
1 parent 580c455 commit b102dd7
Show file tree
Hide file tree
Showing 57 changed files with 887 additions and 1,314 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/**
* This file is part of the Meeds project (https://meeds.io/).
*
* Copyright (C) 2020 - 2024 Meeds Association [email protected]
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package io.meeds.social.space.administration.model;

import java.util.List;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class SpacePermissions {

private List<String> layoutPermissions;

private List<String> publicSitePermissions;

private List<String> deletePermissions;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.meeds.social.space.administration.service;

import org.exoplatform.commons.exception.ObjectNotFoundException;
import org.exoplatform.social.core.space.model.Space;

import io.meeds.social.space.administration.model.SpacePermissions;

public interface SpaceAdministrationService {

/**
* @param spaceId {@link Space} technical id
* @return Space permissions with layoutPermissions, deletePermissions and
* publicSitePermissions
* @throws ObjectNotFoundException when the space doesn't exist
*/
SpacePermissions getSpacePermissions(long spaceId) throws ObjectNotFoundException;

/**
* @param spaceId {@link Space} technical id
* @param permissions Space permissions with layoutPermissions,
* deletePermissions and publicSitePermissions
* @throws ObjectNotFoundException when the space doesn't exist
*/
void updateSpacePermissions(long spaceId, SpacePermissions permissions) throws ObjectNotFoundException;

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public class Space implements CacheEntry {
public static final String HOME_URL = "home";

/** The id. */
private String id;
private long id;

/** The display name. */
private String displayName;
Expand Down Expand Up @@ -235,6 +235,10 @@ public void setField(UpdatedField field) {
* @param id the new id
*/
public void setId(String id) {
this.id = id == null ? 0 : Long.parseLong(id);
}

public void setId(long id) {
this.id = id;
}

Expand All @@ -244,6 +248,10 @@ public void setId(String id) {
* @return the id
*/
public String getId() {
return String.valueOf(id);
}

public long getSpaceId() {
return id;
}

Expand Down Expand Up @@ -653,26 +661,15 @@ public void setCreatedTime(Long createdTime) {

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
return Long.hashCode(id);
}

@Override
public boolean equals(Object obj) {
if (this == obj)
public boolean equals(Object o) {
if (this == o) {
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Space other = (Space) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
}
return o instanceof Space space && id == space.id;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ default Space getSpaceByGroupId(String groupId) {
* @since 1.2.0-GA
*/
default Space getSpaceById(String spaceId) {
try {
return getSpaceById(Long.parseLong(spaceId));
} catch (NumberFormatException e) {
return null;
}
}

/**
* Gets a space by its Id.
*
* @param spaceId Id of the space.
* @return The space.
* @LevelAPI Platform
* @since 1.2.0-GA
*/
default Space getSpaceById(long spaceId) {
throw new UnsupportedOperationException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,16 @@
*/
package io.meeds.social.authorization;

import java.util.List;
import java.util.stream.Stream;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import org.exoplatform.container.ExoContainerContext;
import org.exoplatform.container.xml.InitParams;
import org.exoplatform.portal.config.UserACL;
import org.exoplatform.portal.config.model.PortalConfig;
import org.exoplatform.services.security.Identity;
import org.exoplatform.services.security.MembershipEntry;
import org.exoplatform.social.core.space.SpaceUtils;
import org.exoplatform.social.core.space.SpacesAdministrationService;
import org.exoplatform.social.core.space.model.Space;
import org.exoplatform.social.core.space.spi.SpaceService;

Expand All @@ -42,9 +38,6 @@

public class AuthorizationManager extends UserACL {

@Setter
private SpacesAdministrationService spacesAdministrationService;

@Setter
private SpaceService spaceService;

Expand Down Expand Up @@ -88,10 +81,7 @@ private boolean isSpacesAdministrator(Identity identity) {
if (isAdministrator(identity)) {
return true;
} else {
List<MembershipEntry> spacesAdministrators = getSpacesAdministrationService().getSpacesAdministratorsMemberships();
return CollectionUtils.isNotEmpty(spacesAdministrators)
&& spacesAdministrators.stream()
.anyMatch(permission -> isMemberOf(identity, permission.toString()));
return getSpaceService().isSuperManager(identity.getUserId());
}
}

Expand All @@ -100,13 +90,6 @@ private boolean isSpaceSite(String ownerType, String ownerId) {
&& StringUtils.startsWith(ownerId, SpaceUtils.SPACE_GROUP_PREFIX);
}

private SpacesAdministrationService getSpacesAdministrationService() {
if (spacesAdministrationService == null) {
spacesAdministrationService = ExoContainerContext.getService(SpacesAdministrationService.class);
}
return spacesAdministrationService;
}

private SpaceService getSpaceService() {
if (spaceService == null) {
spaceService = ExoContainerContext.getService(SpaceService.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.meeds.social.space.administration.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import org.exoplatform.commons.exception.ObjectNotFoundException;
import org.exoplatform.social.core.space.model.Space;
import org.exoplatform.social.core.space.spi.SpaceService;

import io.meeds.social.space.administration.model.SpacePermissions;

import lombok.Setter;

@Service
public class SpaceAdministrationServiceImpl implements SpaceAdministrationService {

private static final String SPACE_NOT_FOUN_MESSAGE = "Space with id %s doesn't exist";

@Setter
@Autowired
private SpaceService spaceService;

@Override
public SpacePermissions getSpacePermissions(long spaceId) throws ObjectNotFoundException {
Space space = spaceService.getSpaceById(spaceId);
if (space == null) {
throw new ObjectNotFoundException(String.format(SPACE_NOT_FOUN_MESSAGE, spaceId));
}
return new SpacePermissions(space.getLayoutPermissions(),
space.getPublicSitePermissions(),
space.getDeletePermissions());
}

@Override
public void updateSpacePermissions(long spaceId, SpacePermissions permissions) throws ObjectNotFoundException {
Space space = spaceService.getSpaceById(spaceId);
if (space == null) {
throw new ObjectNotFoundException(String.format(SPACE_NOT_FOUN_MESSAGE, spaceId));
}
space.setLayoutPermissions(permissions.getLayoutPermissions());
space.setPublicSitePermissions(permissions.getPublicSitePermissions());
space.setDeletePermissions(permissions.getDeletePermissions());
spaceService.updateSpace(space);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@
import org.exoplatform.social.core.space.SpaceListAccess;
import org.exoplatform.social.core.space.SpaceListAccessType;
import org.exoplatform.social.core.space.SpaceListenerPlugin;
import org.exoplatform.social.core.space.SpacesAdministrationService;
import org.exoplatform.social.core.space.model.Space;
import org.exoplatform.social.core.space.spi.SpaceLifeCycleEvent.Type;
import org.exoplatform.social.core.space.spi.SpaceLifeCycleListener;
Expand Down Expand Up @@ -116,8 +115,6 @@ public class SpaceServiceImpl implements SpaceService {

private UserACL userAcl;

private SpacesAdministrationService spacesAdministrationService;

private ResourceBundleService resourceBundleService;

private LocaleConfigService localeConfigService;
Expand All @@ -134,7 +131,6 @@ public SpaceServiceImpl(SpaceStorage spaceStorage, // NOSONAR
GroupSpaceBindingStorage groupSpaceBindingStorage,
SpaceSearchConnector spaceSearchConnector,
IdentityManager identityManager,
SpacesAdministrationService spacesAdministrationService,
UserACL userAcl,
ResourceBundleService resourceBundleService,
LocaleConfigService localeConfigService,
Expand All @@ -143,7 +139,6 @@ public SpaceServiceImpl(SpaceStorage spaceStorage, // NOSONAR
this.groupSpaceBindingStorage = groupSpaceBindingStorage;
this.spaceSearchConnector = spaceSearchConnector;
this.identityManager = identityManager;
this.spacesAdministrationService = spacesAdministrationService;
this.userAcl = userAcl;
this.resourceBundleService = resourceBundleService;
this.localeConfigService = localeConfigService;
Expand Down Expand Up @@ -171,8 +166,8 @@ public Space getSpaceByGroupId(String groupId) {
}

@Override
public Space getSpaceById(String id) {
return spaceStorage.getSpaceById(id);
public Space getSpaceById(long spaceId) {
return spaceStorage.getSpaceById(spaceId);
}

@Override
Expand Down Expand Up @@ -790,7 +785,7 @@ public Space updateSpace(Space existingSpace) {

@Override
public Space updateSpace(Space space, List<Identity> identitiesToInvite) {
Space storedSpace = spaceStorage.getSpaceById(space.getId());
Space storedSpace = spaceStorage.getSpaceById(space.getSpaceId());
spaceStorage.saveSpace(space, false);
triggerSpaceUpdate(space, storedSpace);

Expand All @@ -815,7 +810,7 @@ public Space updateSpaceAvatar(Space existingSpace, String username) {
identityManager.updateProfile(profile);
spaceLifeCycle.spaceAvatarEdited(existingSpace, existingSpace.getEditor());

existingSpace = spaceStorage.getSpaceById(existingSpace.getId());
existingSpace = spaceStorage.getSpaceById(existingSpace.getSpaceId());
existingSpace.setAvatarLastUpdated(System.currentTimeMillis());
spaceStorage.saveSpace(existingSpace, false);
return existingSpace;
Expand All @@ -836,7 +831,7 @@ public Space updateSpaceBanner(Space existingSpace, String username) {
}
identityManager.updateProfile(profile);

existingSpace = spaceStorage.getSpaceById(existingSpace.getId());
existingSpace = spaceStorage.getSpaceById(existingSpace.getSpaceId());
existingSpace.setBannerLastUpdated(System.currentTimeMillis());
spaceStorage.saveSpace(existingSpace, false);

Expand Down Expand Up @@ -919,7 +914,7 @@ public void deleteExternalUserInvitations(String email) {

@Override
public boolean isSuperManager(String username) {
return spacesAdministrationService.isSuperManager(username);
return userAcl.isAdministrator(userAcl.getUserIdentity(username));
}

@Override
Expand Down
Loading

0 comments on commit b102dd7

Please sign in to comment.