View Javadoc
1   /*
2    * Copyright (C) 2003-2012 eXo Platform SAS.
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 published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU Affero General Public License for more details.
13   *
14   * You should have received a copy of the GNU Affero General Public License
15   * along with this program. If not, see <http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.social.core.listeners;
18  
19  import org.exoplatform.commons.utils.CommonsUtils;
20  import org.exoplatform.container.ExoContainer;
21  import org.exoplatform.container.ExoContainerContext;
22  import org.exoplatform.portal.config.UserACL;
23  import org.exoplatform.services.organization.Membership;
24  import org.exoplatform.services.organization.MembershipEventListener;
25  import org.exoplatform.services.organization.MembershipTypeHandler;
26  import org.exoplatform.services.organization.OrganizationService;
27  import org.exoplatform.services.security.ConversationState;
28  import org.exoplatform.social.core.space.SpaceUtils;
29  import org.exoplatform.social.core.space.model.Space;
30  import org.exoplatform.social.core.space.spi.SpaceService;
31  import org.exoplatform.social.core.storage.api.IdentityStorage;
32  
33  /**
34   * SocialMembershipListenerImpl is registered to OrganizationService to handle membership operation associated
35   * with space's groups.
36   * {@literal - When a user's membership is removed (member or manager membership) => that user membership will be removed from spaces.}
37   * {@literal - When a user's membership is updated (member or manager membership) -> that user membership will be added to spaces.}
38   *
39   * @author <a href="mailto:hoatlevan@gmail.com">hoatle</a>
40   * @since Jan 11, 2012
41   */
42  public class SocialMembershipListenerImpl extends MembershipEventListener {
43  
44    public SocialMembershipListenerImpl() {
45      
46    }
47    
48    @Override
49    public void postDelete(Membership m) throws Exception {
50      if (m.getGroupId().startsWith(SpaceUtils.SPACE_GROUP)) {
51        OrganizationService orgService = CommonsUtils.getService(OrganizationService.class);
52  
53        UserACL acl =  CommonsUtils.getService(UserACL.class);
54        
55        //only handles these memberships have types likes 'manager' 
56        //and 'member', except 'validator', ...so on.
57        SpaceService spaceService = CommonsUtils.getService(SpaceService.class);
58        Space space = spaceService.getSpaceByGroupId(m.getGroupId());
59        if (space != null) {
60          ConversationState state = ConversationState.getCurrent();
61          if(state != null && state.getIdentity() != null && space.getEditor() == null) {
62            space.setEditor(state.getIdentity().getUserId());
63          }
64          if (acl.getAdminMSType().equalsIgnoreCase(m.getMembershipType()) ||
65              MembershipTypeHandler.ANY_MEMBERSHIP_TYPE.equalsIgnoreCase(m.getMembershipType())) {
66            spaceService.setManager(space, m.getUserName(), false);
67            SpaceUtils.refreshNavigation();
68          } else if (SpaceUtils.MEMBER.equalsIgnoreCase(m.getMembershipType())) {
69            spaceService.removeMember(space, m.getUserName());
70            spaceService.setManager(space, m.getUserName(), false);
71            SpaceUtils.refreshNavigation();
72          }
73        }
74      } else if (m.getGroupId().startsWith(SpaceUtils.PLATFORM_USERS_GROUP)) {
75        clearIdentityCaching();
76      }
77    }
78  	
79    @Override
80    public void postSave(Membership m, boolean isNew) throws Exception {
81      //only trigger when the Organization service adds new membership to existing SpaceGroup
82      if (m.getGroupId().startsWith(SpaceUtils.SPACE_GROUP)) {
83  
84        ExoContainer container = ExoContainerContext.getCurrentContainer();
85        UserACL acl = (UserACL) container.getComponentInstanceOfType(UserACL.class);
86        //only handles these memberships have types likes 'manager' and 'member'
87        //, except 'validator', ...so on.
88        SpaceService spaceService = (SpaceService) container.getComponentInstanceOfType(SpaceService.class);
89        Space space = spaceService.getSpaceByGroupId(m.getGroupId());
90        //TODO A case to confirm: will we create a new space here when a new group is created via organization portlet
91        if (space != null) {
92          ConversationState state = ConversationState.getCurrent();
93          if(state != null && state.getIdentity() != null && space.getEditor() == null) {
94            space.setEditor(state.getIdentity().getUserId());
95          }
96          String userName = m.getUserName();
97          if (acl.getAdminMSType().equalsIgnoreCase(m.getMembershipType()) ||
98              MembershipTypeHandler.ANY_MEMBERSHIP_TYPE.equalsIgnoreCase(m.getMembershipType())) {
99            if (spaceService.isManager(space, userName)) {
100             return;
101           }
102           if (spaceService.isMember(space, userName)) {
103             spaceService.setManager(space, userName, true);
104           } else {
105             spaceService.addMember(space, userName);
106             spaceService.setManager(space, userName, true);
107           }
108         } else if (SpaceUtils.MEMBER.equalsIgnoreCase(m.getMembershipType())) {
109           if (spaceService.isMember(space, userName)) {
110             return;
111           }
112           spaceService.addMember(space, userName);
113         }
114         
115         //Refresh GroupNavigation
116         SpaceUtils.refreshNavigation();
117       }
118 
119     } else if (m.getGroupId().startsWith(SpaceUtils.PLATFORM_USERS_GROUP)) {
120       clearIdentityCaching();
121     }
122   }
123   
124   private void clearIdentityCaching() {
125     IdentityStorage storage = (IdentityStorage) ExoContainerContext.getCurrentContainer().getComponentInstanceOfType(IdentityStorage.class);
126     
127     //clear caching for identity
128     storage.updateIdentityMembership(null);
129   }
130 }