Skip to content

Commit

Permalink
Merge Space Settings Review - Meeds-io/MIPs#147 (#975)
Browse files Browse the repository at this point in the history
  • Loading branch information
boubaker authored Sep 26, 2024
2 parents 5c51e5e + 55cd06f commit be9abcd
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/**
* 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.services.organization.listener;

import java.util.Objects;

import org.exoplatform.services.organization.Membership;
import org.exoplatform.services.organization.MembershipEventListener;
import org.exoplatform.services.security.Identity;
import org.exoplatform.services.security.IdentityRegistry;
import org.exoplatform.services.security.MembershipEntry;

public class IdentityRegistryMembershipListener extends MembershipEventListener {

private IdentityRegistry identityRegistry;

public IdentityRegistryMembershipListener(IdentityRegistry identityRegistry) {
this.identityRegistry = identityRegistry;
}

@Override
public void postDelete(Membership membership) throws Exception {
Identity identity = identityRegistry.getIdentity(membership.getUserName());
if (identity != null) {
MembershipEntry membershipEntry = toMembershipEntry(membership);
identity.getMemberships()
.removeIf(m -> Objects.equals(m, membershipEntry));
}
}

@Override
public void postSave(Membership membership, boolean isNew) throws Exception {
Identity identity = identityRegistry.getIdentity(membership.getUserName());
if (identity != null) {
MembershipEntry membershipEntry = toMembershipEntry(membership);
if (identity.getMemberships().stream().noneMatch(m -> m.equals(membershipEntry))) {
identity.getMemberships().add(membershipEntry);
}
}
}

private MembershipEntry toMembershipEntry(Membership membership) {
return new MembershipEntry(membership.getGroupId(), membership.getMembershipType());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package org.exoplatform.services.organization.mock;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -90,7 +89,7 @@ public Membership removeMembership(String membershipId, boolean broadcast) {
public List<Membership> removeMembershipByUser(String userName, boolean broadcast) {
List<Membership> memberships = userMemberships.compute(userName,
(key,
existingMemberships) -> existingMemberships == null ? Collections.emptyList()
existingMemberships) -> existingMemberships == null ? new ArrayList<>()
: new ArrayList<>(existingMemberships));
removeMemberships(memberships, broadcast);
return memberships;
Expand All @@ -99,7 +98,7 @@ public List<Membership> removeMembershipByUser(String userName, boolean broadcas
public List<Membership> removeMembershipByGroup(String groupId, boolean broadcast) {
List<Membership> memberships = groupMemberships.compute(groupId,
(key,
existingMemberships) -> existingMemberships == null ? Collections.emptyList()
existingMemberships) -> existingMemberships == null ? new ArrayList<>()
: new ArrayList<>(existingMemberships));
removeMemberships(memberships, broadcast);
return memberships;
Expand All @@ -108,7 +107,7 @@ public List<Membership> removeMembershipByGroup(String groupId, boolean broadcas
public List<Membership> removeMembershipByMembershipType(String membershipType, boolean broadcast) {
List<Membership> memberships = membershipTypeMemberships.compute(membershipType,
(key,
existingMemberships) -> existingMemberships == null ? Collections.emptyList()
existingMemberships) -> existingMemberships == null ? new ArrayList<>()
: new ArrayList<>(existingMemberships));
removeMemberships(memberships, broadcast);
return memberships;
Expand Down Expand Up @@ -142,7 +141,7 @@ public List<Membership> findMembershipsByUserAndGroup(String userName, String gr

@Override
public List<Membership> findMembershipsByUser(String userName) {
return userMemberships.computeIfAbsent(userName, key -> Collections.emptyList());
return userMemberships.computeIfAbsent(userName, key -> new ArrayList<>());
}

@Override
Expand All @@ -152,11 +151,11 @@ public ListAccess<Membership> findAllMembershipsByUser(User user) {

@Override
public List<Membership> findMembershipsByGroup(Group group) {
return groupMemberships.computeIfAbsent(group.getId(), key -> Collections.emptyList());
return groupMemberships.computeIfAbsent(group.getId(), key -> new ArrayList<>());
}

public List<Membership> findMembershipsByGroupId(String groupId) {
return groupMemberships.computeIfAbsent(groupId, key -> Collections.emptyList());
return groupMemberships.computeIfAbsent(groupId, key -> new ArrayList<>());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,16 @@
</object-param>
</init-params>
</component-plugin>
<component-plugin>
<name>MembershipUpdateListener</name>
<set-method>addListenerPlugin</set-method>
<type>org.exoplatform.services.organization.impl.MembershipUpdateListener</type>
</component-plugin>
<component-plugin>
<name>MembershipUpdateListener</name>
<set-method>addListenerPlugin</set-method>
<type>io.meeds.services.organization.listener.IdentityRegistryMembershipListener</type>
</component-plugin>
</component-plugins>
</component>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -331,5 +331,11 @@
<set-method>addListenerPlugin</set-method>
<type>org.exoplatform.services.organization.impl.MembershipUpdateListener</type>
</component-plugin>

<component-plugin>
<name>MembershipUpdateListener</name>
<set-method>addListenerPlugin</set-method>
<type>io.meeds.services.organization.listener.IdentityRegistryMembershipListener</type>
</component-plugin>
</external-component-plugins>
</configuration>

0 comments on commit be9abcd

Please sign in to comment.