1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.plugin.rightsmanager

File RightsManagerListener.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart6.png
69% of files have more coverage

Code metrics

8
30
8
1
178
93
14
0.47
3.75
8
1.75

Classes

Class Line # Actions
RightsManagerListener 44 30 0% 14 21
0.5434782554.3%
 

Contributing tests

No tests hitting this source file were found.

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20   
21    package com.xpn.xwiki.plugin.rightsmanager;
22   
23    import java.util.ArrayList;
24    import java.util.List;
25   
26    import org.slf4j.Logger;
27    import org.slf4j.LoggerFactory;
28    import org.xwiki.bridge.event.DocumentDeletedEvent;
29    import org.xwiki.observation.EventListener;
30    import org.xwiki.observation.event.Event;
31    import org.xwiki.observation.remote.RemoteObservationManagerContext;
32   
33    import com.xpn.xwiki.XWikiContext;
34    import com.xpn.xwiki.XWikiException;
35    import com.xpn.xwiki.doc.XWikiDocument;
36    import com.xpn.xwiki.web.Utils;
37   
38    /**
39    * Listener to user and groups events to apply related automatic task like cleaning groups and rights objects.
40    *
41    * @version $Id: 05241ee261af4a66f617336cd8c5b5e2528bd67a $
42    * @since XWiki Core 2.2.3
43    */
 
44    public final class RightsManagerListener implements EventListener
45    {
46    /**
47    * The logging tool.
48    */
49    private static final Logger LOGGER = LoggerFactory.getLogger(RightsManagerListener.class);
50   
51    /**
52    * The name of the listener.
53    */
54    private static final String NAME = "rightsmanager";
55   
56    /**
57    * The events to match.
58    */
59    private static final List<Event> EVENTS = new ArrayList<Event>()
60    {
 
61  3 toggle {
62  3 add(new DocumentDeletedEvent());
63    }
64    };
65   
66    // ////////////////////////////////////////////////////////////////////////////
67   
68    /**
69    * Unique instance of RightsManager.
70    */
71    private static RightsManagerListener instance;
72   
73    /**
74    * Hidden constructor of RightsManager only access via getInstance().
75    */
 
76  3 toggle private RightsManagerListener()
77    {
78    }
79   
80    /**
81    * @return a unique instance of RightsManager. Thread safe.
82    */
 
83  3 toggle public static RightsManagerListener getInstance()
84    {
85  3 synchronized (RightsManagerListener.class) {
86  3 if (instance == null) {
87  3 instance = new RightsManagerListener();
88    }
89    }
90   
91  3 return instance;
92    }
93   
 
94  15 toggle @Override
95    public String getName()
96    {
97  15 return NAME;
98    }
99   
 
100  3 toggle @Override
101    public List<Event> getEvents()
102    {
103  3 return EVENTS;
104    }
105   
 
106  31 toggle @Override
107    public void onEvent(Event event, Object source, Object data)
108    {
109    // Only take into account local events
110  31 if (!Utils.getComponent(RemoteObservationManagerContext.class).isRemoteState()) {
111  31 XWikiDocument document = ((XWikiDocument) source).getOriginalDocument();
112  31 XWikiContext context = (XWikiContext) data;
113   
114  31 String userOrGroupWiki = document.getDatabase();
115  31 String userOrGroupSpace = document.getSpace();
116  31 String userOrGroupName = document.getName();
117   
118  31 if (document.getObject("XWiki.XWikiUsers") != null) {
119  0 try {
120  0 cleanDeletedUserOrGroup(userOrGroupWiki, userOrGroupSpace, userOrGroupName, true, context);
121    } catch (XWikiException e) {
122  0 LOGGER.warn("Error when cleaning for deleted user", e);
123    }
124  31 } else if (document.getObject("XWiki.XWikiGroups") != null) {
125  0 try {
126  0 cleanDeletedUserOrGroup(userOrGroupWiki, userOrGroupSpace, userOrGroupName, false, context);
127    } catch (XWikiException e) {
128  0 LOGGER.warn("Error when cleaning for deleted group", e);
129    }
130    }
131    }
132    }
133   
134    /**
135    * Remove reference to provided user or group in all groups and rights in current wiki.
136    *
137    * @param userOrGroupWiki the wiki name of the group or user.
138    * @param userOrGroupSpace the space name of the group or user.
139    * @param userOrGroupName the name of the group or user.
140    * @param user indicate if it is a user or a group.
141    * @param context the XWiki context.
142    * @throws XWikiException error when browsing groups or rights.
143    */
 
144  0 toggle private void cleanDeletedUserOrGroupInLocalWiki(String userOrGroupWiki, String userOrGroupSpace,
145    String userOrGroupName, boolean user, XWikiContext context) throws XWikiException
146    {
147  0 RightsManager.getInstance().removeUserOrGroupFromAllRights(userOrGroupWiki, userOrGroupSpace, userOrGroupName,
148    user, context);
149  0 context.getWiki().getGroupService(context).removeUserOrGroupFromAllGroups(userOrGroupWiki, userOrGroupSpace,
150    userOrGroupName, context);
151    }
152   
153    /**
154    * Remove reference to provided user or group in all groups and rights in all wikis.
155    *
156    * @param userOrGroupWiki the wiki name of the group or user.
157    * @param userOrGroupSpace the space name of the group or user.
158    * @param userOrGroupName the name of the group or user.
159    * @param user indicate if it is a user or a group.
160    * @param context the XWiki context.
161    * @throws XWikiException error when browsing groups or rights.
162    */
 
163  0 toggle private void cleanDeletedUserOrGroup(String userOrGroupWiki, String userOrGroupSpace, String userOrGroupName,
164    boolean user, XWikiContext context) throws XWikiException
165    {
166  0 List<String> wikiList = context.getWiki().getVirtualWikisDatabaseNames(context);
167   
168  0 String database = context.getWikiId();
169  0 try {
170  0 for (String wikiName : wikiList) {
171  0 context.setWikiId(wikiName);
172  0 cleanDeletedUserOrGroupInLocalWiki(userOrGroupWiki, userOrGroupSpace, userOrGroupName, user, context);
173    }
174    } finally {
175  0 context.setWikiId(database);
176    }
177    }
178    }