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

File RightsManagerPluginApi.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart4.png
78% of files have more coverage

Code metrics

14
63
13
1
316
144
26
0.41
4.85
13
2

Classes

Class Line # Actions
RightsManagerPluginApi 43 63 0% 26 56
0.3777777937.8%
 

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.text.MessageFormat;
24    import java.util.Collection;
25    import java.util.Collections;
26    import java.util.List;
27    import java.util.Map;
28   
29    import org.slf4j.Logger;
30    import org.slf4j.LoggerFactory;
31   
32    import com.xpn.xwiki.XWikiContext;
33    import com.xpn.xwiki.XWikiException;
34    import com.xpn.xwiki.plugin.PluginApi;
35   
36    /**
37    * API for managing rights, users and groups.
38    *
39    * @version $Id: 0b569bd06344a0730e1b281c93c1af48dd35138c $
40    * @since 1.1.2
41    * @since 1.2M2
42    */
 
43    public class RightsManagerPluginApi extends PluginApi<RightsManagerPlugin>
44    {
45    /**
46    * Field name of the last error code inserted in context.
47    */
48    public static final String CONTEXT_LASTERRORCODE = "lasterrorcode";
49   
50    /**
51    * Field name of the last api exception inserted in context.
52    */
53    public static final String CONTEXT_LASTEXCEPTION = "lastexception";
54   
55    /**
56    * The logging toolkit.
57    */
58    protected static final Logger LOGGER = LoggerFactory.getLogger(RightsManagerPluginApi.class);
59   
60    /**
61    * API for managing rights and inheritance.
62    */
63    private RightsManagerRightsApi rightsApi;
64   
65    /**
66    * API for managing users.
67    */
68    private RightsManagerUsersApi usersApi;
69   
70    /**
71    * API for managing groups.
72    */
73    private RightsManagerGroupsApi groupsApi;
74   
75    /**
76    * Create an instance of the Rights Manager plugin user api.
77    *
78    * @param plugin the entry point of the Rights Manager plugin.
79    * @param context the XWiki context.
80    */
 
81  22 toggle public RightsManagerPluginApi(RightsManagerPlugin plugin, XWikiContext context)
82    {
83  22 super(plugin, context);
84   
85  22 this.rightsApi = new RightsManagerRightsApi(context);
86  22 this.usersApi = new RightsManagerUsersApi(context);
87  22 this.groupsApi = new RightsManagerGroupsApi(context);
88    }
89   
90    /**
91    * @return the API for managing rights and inheritance.
92    */
 
93  0 toggle public RightsManagerRightsApi getRightsApi()
94    {
95  0 return this.rightsApi;
96    }
97   
98    /**
99    * @return the API for managing users.
100    */
 
101  2 toggle public RightsManagerUsersApi getUsersApi()
102    {
103  2 return this.usersApi;
104    }
105   
106    /**
107    * @return the API for managing groups.
108    */
 
109  5 toggle public RightsManagerGroupsApi getGroupsApi()
110    {
111  5 return this.groupsApi;
112    }
113   
114    /**
115    * Convert Map/List pattern matching parameter from Velocity to [][] used in {@link RightsManager}.
116    *
117    * @param map a map of list from Velocity.
118    * @return a table of table for {@link RightsManager} methods.
119    */
 
120  10 toggle static Object[][] createMatchingTable(Map<?, ?> map)
121    {
122  10 if (map == null || map.size() == 0) {
123  10 return null;
124    }
125   
126  0 Object[][] table = new Object[map.size()][3];
127   
128  0 int i = 0;
129  0 for (Map.Entry<?, ?> entry : map.entrySet()) {
130  0 table[i][0] = entry.getKey();
131   
132  0 if (entry.getValue() instanceof List) {
133  0 List<?> typeValue = (List<?>) entry.getValue();
134  0 table[i][1] = typeValue.get(0);
135  0 if (typeValue.size() > 1) {
136  0 table[i][2] = typeValue.get(1);
137    }
138    } else {
139  0 table[i][2] = entry.getValue();
140    }
141   
142  0 ++i;
143    }
144   
145  0 return table;
146    }
147   
148    /**
149    * Convert List/List order fields from Velocity to [][] used in {@link RightsManager}.
150    *
151    * @param list a list of list from Velocity.
152    * @return a table of table for {@link RightsManager} methods.
153    */
 
154  5 toggle static Object[][] createOrderTable(List<?> list)
155    {
156  5 if (list == null || list.size() == 0) {
157  0 return null;
158    }
159   
160  5 Object[][] table = new Object[list.size()][3];
161   
162  5 int i = 0;
163  5 for (Object entry : list) {
164  5 if (entry instanceof List) {
165  0 List<?> fieldParams = (List<?>) entry;
166  0 table[i][0] = fieldParams.get(0);
167  0 if (fieldParams.size() > 1) {
168  0 table[i][1] = fieldParams.get(1);
169    }
170  0 if (fieldParams.size() > 2) {
171  0 table[i][2] = fieldParams.get(2);
172    }
173    } else {
174  5 table[i][0] = entry.toString();
175    }
176   
177  5 ++i;
178    }
179   
180  5 return table;
181    }
182   
183    /**
184    * Log error and register {@link #CONTEXT_LASTERRORCODE} and {@link #CONTEXT_LASTEXCEPTION}.
185    *
186    * @param comment the comment to use with {@link #LOGGER}.
187    * @param e the exception.
188    */
 
189  0 toggle private void logError(String comment, XWikiException e)
190    {
191  0 LOGGER.error(comment, e);
192   
193  0 this.context.put(CONTEXT_LASTERRORCODE, e.getCode());
194  0 this.context.put(CONTEXT_LASTEXCEPTION, e);
195    }
196   
197    // Groups management
198   
199    /**
200    * Get all groups containing provided user.
201    *
202    * @param member the name of the member (user or group).
203    * @return the {@link Collection} of {@link String} containing group name.
204    * @throws XWikiException error when browsing groups.
205    */
 
206  0 toggle public Collection<String> getAllGroupsNamesForMember(String member) throws XWikiException
207    {
208  0 Collection<String> memberList;
209   
210  0 try {
211  0 memberList = RightsManager.getInstance().getAllGroupsNamesForMember(member, 0, 0, this.context);
212    } catch (RightsManagerException e) {
213  0 logError(MessageFormat.format("Try to get all groups containing user [{0}]", new Object[] { member }), e);
214   
215  0 memberList = Collections.emptyList();
216    }
217   
218  0 return memberList;
219    }
220   
221    /**
222    * Get all members (users or groups) provided group contains.
223    *
224    * @param group the name of the group.
225    * @return the {@link Collection} of {@link String} containing member (user or group) name.
226    * @throws XWikiException error when browsing groups.
227    */
 
228  0 toggle public Collection<String> getAllMembersNamesForGroup(String group) throws XWikiException
229    {
230  0 return getAllMembersNamesForGroup(group, 0, 0);
231    }
232   
233    /**
234    * Get all members (users or groups) provided group contains.
235    *
236    * @param group the name of the group.
237    * @param nb the maximum number of result to return.
238    * @param start the index of the first found user to return.
239    * @return the {@link Collection} of {@link String} containing member (user or group) name.
240    * @throws XWikiException error when browsing groups.
241    */
 
242  0 toggle public Collection<String> getAllMembersNamesForGroup(String group, int nb, int start) throws XWikiException
243    {
244  0 return getAllMatchedMembersNamesForGroup(group, null, nb, start, null);
245    }
246   
247    /**
248    * Get members of provided group.
249    *
250    * @param group the group.
251    * @param matchField a string to search in result to filter.
252    * @param nb the maximum number of result to return.
253    * @param start the index of the first found user to return.
254    * @param orderAsc if true, the result is ordered ascendent, if false it descendant. If null no order is applied.
255    * @return the {@link Collection} of {@link String} containing member name.
256    * @throws XWikiException error when browsing groups.
257    * @since 1.6M1
258    */
 
259  7 toggle public Collection<String> getAllMatchedMembersNamesForGroup(String group, String matchField, int nb, int start,
260    Boolean orderAsc) throws XWikiException
261    {
262  7 Collection<String> memberList;
263   
264  7 try {
265  7 memberList =
266    RightsManager.getInstance().getAllMatchedMembersNamesForGroup(group, matchField, nb, start, orderAsc,
267    this.context);
268    } catch (RightsManagerException e) {
269  0 logError(MessageFormat.format("Try to get all matched member of group [{0}]", new Object[] { group }), e);
270   
271  0 memberList = Collections.emptyList();
272    }
273   
274  7 return memberList;
275    }
276   
277    /**
278    * Return the number of groups containing provided member.
279    *
280    * @param member the name of the member (user or group).
281    * @return the number of groups.
282    * @throws XWikiException error when getting number of users.
283    */
 
284  0 toggle public int countAllGroupsNamesForMember(String member) throws XWikiException
285    {
286  0 int count = 0;
287   
288  0 try {
289  0 count = RightsManager.getInstance().countAllGroupsNamesForMember(member, this.context);
290    } catch (RightsManagerException e) {
291  0 logError("Try to count groups containing provided user", e);
292    }
293   
294  0 return count;
295    }
296   
297    /**
298    * Return the number of members provided group contains.
299    *
300    * @param group the name of the group.
301    * @return the number of members.
302    * @throws XWikiException error when getting number of groups.
303    */
 
304  9 toggle public int countAllMembersNamesForGroup(String group) throws XWikiException
305    {
306  9 int count = 0;
307   
308  9 try {
309  9 count = RightsManager.getInstance().countAllMembersNamesForGroup(group, this.context);
310    } catch (RightsManagerException e) {
311  0 logError("Try to count all members provided group contains", e);
312    }
313   
314  9 return count;
315    }
316    }