Skip to content

Commit 4dbf9ba

Browse files
SilverYoChammoqui
authored andcommitted
Feature #13197: implementing new community component.
Modifying component descriptor in order to specify the profile mapping between space and component. Adding an implementation of ComponentInstanceAccessControlExtension dedicated to Community component in order to implement specific rules with respect to the space manager status. Adding CommunityWebManager in order to centralize functional code between WEB Services and Web Component controller. Implementing the edition of the Wysiwyg content of the space community.
1 parent 5277682 commit 4dbf9ba

File tree

13 files changed

+347
-47
lines changed

13 files changed

+347
-47
lines changed

community/community-configuration/src/main/config/properties/org/silverpeas/components/community/multilang/communityBundle.properties

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,5 @@
2121
# You should have received a copy of the GNU Affero General Public License
2222
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2323
#
24-
25-
community.menu.item.subscribe = S'abonner \u00e0 community
26-
community.menu.item.unsubscribe = Se d\u00e9sabonner de community
27-
28-
community.notifLinkLabel = Voir cette contribution
24+
community.menu.item.editSpaceHomePage=\u00c9diter la page d'accueil de l'espace
25+
community.edition.spaceHomePage=\u00c9dition de la page d'accueil de l'espace

community/community-configuration/src/main/config/properties/org/silverpeas/components/community/multilang/communityBundle_de.properties

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,5 @@
2121
# You should have received a copy of the GNU Affero General Public License
2222
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2323
#
24-
community.menu.item.subscribe = Abonnieren Sie community
25-
community.menu.item.unsubscribe = Abmelden von community
26-
27-
community.notifLinkLabel = Gehe zu diesem Beitrag
24+
community.menu.item.editSpaceHomePage=Homepage des Raums bearbeiten
25+
community.edition.spaceHomePage=Ausgabe der Weltraum-Homepage

community/community-configuration/src/main/config/properties/org/silverpeas/components/community/multilang/communityBundle_en.properties

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,5 @@
2121
# You should have received a copy of the GNU Affero General Public License
2222
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2323
#
24-
community.menu.item.subscribe = Subscribe to community
25-
community.menu.item.unsubscribe = Unsubscribe from community
26-
27-
community.notifLinkLabel = Go to this contribution
24+
community.menu.item.editSpaceHomePage=Startseite des Bereichs bearbeiten
25+
community.edition.spaceHomePage=Edition of space homepage

community/community-configuration/src/main/config/properties/org/silverpeas/components/community/multilang/communityBundle_fr.properties

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,5 @@
2121
# You should have received a copy of the GNU Affero General Public License
2222
# along with this program. If not, see <http://www.gnu.org/licenses/>.
2323
#
24-
community.menu.item.subscribe = S'abonner \u00e0 community
25-
community.menu.item.unsubscribe = Se d\u00e9sabonner de community
26-
27-
community.notifLinkLabel = Voir cette contribution
24+
community.menu.item.editSpaceHomePage=\u00c9diter la page d'accueil de l'espace
25+
community.edition.spaceHomePage=\u00c9dition de la page d'accueil de l'espace

community/community-configuration/src/main/config/xmlcomponents/community.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@
5959
<message lang="en">Readers.</message>
6060
<message lang="de">Leser.</message>
6161
</help>
62+
<spaceMapping>
63+
<profile>admin</profile>
64+
<profile>publisher</profile>
65+
<profile>writer</profile>
66+
<profile>reader</profile>
67+
</spaceMapping>
6268
</profile>
6369
</profiles>
6470
<parameters>
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,22 @@
2424
package org.silverpeas.components.community;
2525

2626
import org.silverpeas.components.community.model.CommunityOfUsers;
27+
import org.silverpeas.core.admin.component.ComponentInstancePostConstruction;
2728
import org.silverpeas.core.admin.component.model.ComponentInst;
2829
import org.silverpeas.core.admin.service.OrganizationController;
2930
import org.silverpeas.core.annotation.Bean;
30-
import org.silverpeas.core.admin.component.ComponentInstancePostConstruction;
3131

3232
import javax.inject.Inject;
33+
import javax.inject.Named;
3334
import javax.transaction.Transactional;
3435

3536
/**
3637
* Once the Community application instance created, constructs an empty community of users for the
3738
* resource in Silverpeas specified in the instance parameter.
3839
*/
3940
@Bean
40-
public class CommunityPostConstruction implements ComponentInstancePostConstruction {
41+
@Named
42+
public class CommunityInstancePostConstruction implements ComponentInstancePostConstruction {
4143

4244
@Inject
4345
private OrganizationController controller;

community/community-library/src/main/java/org/silverpeas/components/community/CommunityPreDestruction.java renamed to community/community-library/src/main/java/org/silverpeas/components/community/CommunityInstancePreDestruction.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,38 @@
2323
*/
2424
package org.silverpeas.components.community;
2525

26+
import org.silverpeas.components.community.model.CommunityOfUsers;
2627
import org.silverpeas.components.community.repository.CommunityOfUsersRepository;
27-
import org.silverpeas.core.annotation.Bean;
2828
import org.silverpeas.core.admin.component.ComponentInstancePreDestruction;
29+
import org.silverpeas.core.annotation.Bean;
30+
import org.silverpeas.core.contribution.model.WysiwygContent;
2931

3032
import javax.inject.Inject;
33+
import javax.inject.Named;
3134
import javax.transaction.Transactional;
35+
import java.util.Optional;
36+
37+
import static org.silverpeas.components.community.model.CommunityOfUsers.getByComponentInstanceId;
3238

3339
/**
3440
* Wipe out for the spawned community instance the resources that were allocated to him.
3541
*/
3642
@Bean
37-
public class CommunityPreDestruction implements ComponentInstancePreDestruction {
43+
@Named
44+
public class CommunityInstancePreDestruction implements ComponentInstancePreDestruction {
3845

3946
@Inject
4047
private CommunityOfUsersRepository repository;
4148

4249
@Transactional
4350
@Override
4451
public void preDestroy(final String componentInstanceId) {
45-
repository.getByComponentInstanceId(componentInstanceId)
46-
.ifPresent(c -> repository.delete(c));
52+
final Optional<CommunityOfUsers> community = getByComponentInstanceId(componentInstanceId);
53+
// Cleaning space facade
54+
community.map(CommunityOfUsers::getSpaceFacadeContent)
55+
.map(WysiwygContent::getContribution)
56+
.ifPresent(WysiwygContent::deleteAllContents);
57+
// Cleaning database
58+
repository.getByComponentInstanceId(componentInstanceId).ifPresent(c -> repository.delete(c));
4759
}
4860
}

community/community-library/src/main/java/org/silverpeas/components/community/model/CommunityOfUsers.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,26 @@
2424
package org.silverpeas.components.community.model;
2525

2626
import org.silverpeas.components.community.repository.CommunityOfUsersRepository;
27+
import org.silverpeas.core.admin.service.OrganizationController;
28+
import org.silverpeas.core.admin.space.SpaceProfileInst;
29+
import org.silverpeas.core.admin.user.model.SilverpeasRole;
30+
import org.silverpeas.core.admin.user.model.User;
31+
import org.silverpeas.core.contribution.content.wysiwyg.service.WysiwygController;
32+
import org.silverpeas.core.contribution.model.WysiwygContent;
2733
import org.silverpeas.core.persistence.Transaction;
2834
import org.silverpeas.core.persistence.datasource.model.identifier.UuidIdentifier;
2935
import org.silverpeas.core.persistence.datasource.model.jpa.SilverpeasJpaEntity;
36+
import org.silverpeas.core.security.authorization.AccessControlContext;
37+
import org.silverpeas.core.security.authorization.ComponentAccessControl;
3038

3139
import javax.persistence.Column;
3240
import javax.persistence.Entity;
3341
import javax.persistence.NamedQuery;
3442
import javax.persistence.Table;
3543
import java.util.Optional;
44+
import java.util.Set;
45+
46+
import static java.util.function.Predicate.not;
3647

3748
/**
3849
* The community of users for a given Silverpeas resource, for instance a collaborative space. Users
@@ -49,6 +60,7 @@
4960
"order by c.componentInstanceId, c.id")
5061
public class CommunityOfUsers
5162
extends SilverpeasJpaEntity<CommunityOfUsers, UuidIdentifier> {
63+
private static final long serialVersionUID = -4908726669864467915L;
5264

5365
@Column(name = "resourceId", nullable = false)
5466
private String resourceId;
@@ -86,6 +98,14 @@ public static Optional<CommunityOfUsers> getByComponentInstanceId(final String i
8698
return repository.getByComponentInstanceId(instanceId);
8799
}
88100

101+
/**
102+
* Gets the space facade content of the Community.
103+
* @return a {@link WysiwygContent} instance.
104+
*/
105+
public WysiwygContent getSpaceFacadeContent() {
106+
return WysiwygController.get(getComponentInstanceId(), "SpaceFacade", null);
107+
}
108+
89109
public String getComponentInstanceId() {
90110
return componentInstanceId;
91111
}
@@ -110,4 +130,35 @@ public CommunityOfUsers save() {
110130
return repository.save(this);
111131
});
112132
}
133+
134+
/**
135+
* Indicates if the given user is a member.
136+
* <p>
137+
* A member MUST be directly specified into ADMIN, PUBLISHER, WRITER or READER role of direct
138+
* parent space.
139+
* </p>
140+
* @param user {@link User} instance.
141+
* @return true if member, false otherwise.
142+
*/
143+
public boolean isMember(final User user) {
144+
final OrganizationController controller = OrganizationController.get();
145+
final String currentRequesterId = User.getCurrentRequester().getId();
146+
return Optional.of(getResourceId())
147+
.map(controller::getSpaceInstById)
148+
.stream()
149+
.flatMap(s -> s.getAllSpaceProfilesInst().stream())
150+
.filter(not(SpaceProfileInst::isManager).and(not(SpaceProfileInst::isInherited)))
151+
.flatMap(p -> p.getAllUsers().stream())
152+
.anyMatch(i -> i.equals(currentRequesterId));
153+
}
154+
155+
/**
156+
* Gets the roles the given user has on the given community.
157+
* @param user {@link User} instance.
158+
* @return an unmodifiable set of {@link SilverpeasRole}.
159+
*/
160+
public Set<SilverpeasRole> getUserRoleOn(final User user) {
161+
return Set.copyOf(ComponentAccessControl.get()
162+
.getUserRoles(user.getId(), getComponentInstanceId(), AccessControlContext.init()));
163+
}
113164
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright (C) 2000 - 2022 Silverpeas
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Affero General Public License as
6+
* published by the Free Software Foundation, either version 3 of the
7+
* License, or (at your option) any later version.
8+
*
9+
* As a special exception to the terms and conditions of version 3.0 of
10+
* the GPL, you may redistribute this Program in connection with Free/Libre
11+
* Open Source Software ("FLOSS") applications as described in Silverpeas's
12+
* FLOSS exception. You should have received a copy of the text describing
13+
* the FLOSS exception, and it is also available here:
14+
* "https://www.silverpeas.org/legal/floss_exception.html"
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Affero General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Affero General Public License
22+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23+
*/
24+
25+
package org.silverpeas.components.community.security.authorization;
26+
27+
import org.silverpeas.core.admin.component.model.SilverpeasComponentInstance;
28+
import org.silverpeas.core.admin.user.model.User;
29+
import org.silverpeas.core.annotation.Service;
30+
import org.silverpeas.core.security.authorization.ComponentAccessController;
31+
import org.silverpeas.core.security.authorization.DefaultInstanceAccessControlExtension;
32+
33+
import javax.inject.Named;
34+
35+
import static org.silverpeas.core.admin.space.SpaceInst.SPACE_KEY_PREFIX;
36+
import static org.silverpeas.core.util.StringUtil.EMPTY;
37+
38+
/**
39+
* @author silveryocha
40+
*/
41+
@Named
42+
@Service
43+
public class CommunityInstanceAccessControlExtension
44+
extends DefaultInstanceAccessControlExtension {
45+
46+
@Override
47+
protected boolean mustUserBeComponentInstanceAdminIfManagerOfParentSpace(
48+
final ComponentAccessController.DataManager dataManager, final User user,
49+
final SilverpeasComponentInstance componentInstance) {
50+
return dataManager.getManageableSpaceIds(user.getId())
51+
.contains(componentInstance.getSpaceId().replace(SPACE_KEY_PREFIX, EMPTY));
52+
}
53+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Copyright (C) 2000 - 2022 Silverpeas
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU Affero General Public License as
6+
* published by the Free Software Foundation, either version 3 of the
7+
* License, or (at your option) any later version.
8+
*
9+
* As a special exception to the terms and conditions of version 3.0 of
10+
* the GPL, you may redistribute this Program in connection with Free/Libre
11+
* Open Source Software ("FLOSS") applications as described in Silverpeas's
12+
* FLOSS exception. You should have received a copy of the text describing
13+
* the FLOSS exception, and it is also available here:
14+
* "http://www.silverpeas.org/docs/core/legal/floss_exception.html"
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU Affero General Public License for more details.
20+
*
21+
* You should have received a copy of the GNU Affero General Public License
22+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
23+
*/
24+
25+
package org.silverpeas.components.community;
26+
27+
import org.silverpeas.components.community.model.CommunityOfUsers;
28+
import org.silverpeas.core.admin.user.model.SilverpeasRole;
29+
import org.silverpeas.core.admin.user.model.User;
30+
import org.silverpeas.core.annotation.Service;
31+
import org.silverpeas.core.cache.service.CacheServiceProvider;
32+
import org.silverpeas.core.util.ServiceProvider;
33+
import org.silverpeas.core.web.mvc.webcomponent.WebMessager;
34+
35+
import java.util.Set;
36+
import java.util.function.Supplier;
37+
38+
import static org.silverpeas.components.community.CommunityComponentSettings.getMessagesIn;
39+
40+
41+
/**
42+
* WEB manager which allows to centralize code to be used by REST Web Services and Web Component
43+
* Controller.
44+
* @author silveryocha
45+
*/
46+
@Service
47+
public class CommunityWebManager {
48+
49+
private static final String CACHE_KEY_PREFIX = CommunityWebManager.class.getSimpleName() + ":";
50+
51+
protected CommunityWebManager() {
52+
}
53+
54+
/**
55+
* Gets the singleton instance of the provider.
56+
*/
57+
public static CommunityWebManager get() {
58+
return ServiceProvider.getSingleton(CommunityWebManager.class);
59+
}
60+
61+
/**
62+
* Indicates if the current requester is a member.
63+
* <p>
64+
* A member MUST be directly specified into ADMIN, PUBLISHER, WRITER or READER role of direct
65+
* parent space.
66+
* </p>
67+
* @param community {@link CommunityOfUsers} instance.
68+
* @return true if member, false otherwise.
69+
*/
70+
public boolean isMemberOf(final CommunityOfUsers community) {
71+
return requestCache("isMemberOf", community.getId(), Boolean.class,
72+
() -> community.isMember(User.getCurrentRequester()));
73+
}
74+
75+
/**
76+
* Gets the roles the current requester has on the given community.
77+
* @param community {@link CommunityOfUsers} instance.
78+
* @return a set of {@link SilverpeasRole}.
79+
*/
80+
@SuppressWarnings("unchecked")
81+
public Set<SilverpeasRole> getUserRoleOn(final CommunityOfUsers community) {
82+
return requestCache("userRoleOf", community.getId(), Set.class,
83+
() -> community.getUserRoleOn(User.getCurrentRequester()));
84+
}
85+
86+
private <T> T requestCache(final String type, final String id, Class<T> classType,
87+
Supplier<T> supplier) {
88+
return CacheServiceProvider.getRequestCacheService()
89+
.getCache()
90+
.computeIfAbsent(CACHE_KEY_PREFIX + type + ":" + id, classType, supplier);
91+
}
92+
93+
/**
94+
* Push a success message to the current user.
95+
* @param messageKey the key of the message.
96+
* @param params the message parameters.
97+
*/
98+
private void successMessage(String messageKey, Object... params) {
99+
User owner = User.getCurrentRequester();
100+
String userLanguage = owner.getUserPreferences().getLanguage();
101+
getMessager().addSuccess(getMessagesIn(userLanguage).getStringWithParams(messageKey, params));
102+
}
103+
104+
private WebMessager getMessager() {
105+
return WebMessager.getInstance();
106+
}
107+
}

0 commit comments

Comments
 (0)