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

File XWikiUser.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

8
24
10
1
131
77
14
0.58
2.4
10
1.4

Classes

Class Line # Actions
XWikiUser 32 24 0% 14 6
0.8571428785.7%
 

Contributing tests

This file is covered by 39 tests. .

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    package com.xpn.xwiki.user.api;
21   
22    import java.util.Collection;
23   
24    import org.apache.commons.lang3.StringUtils;
25    import org.xwiki.model.reference.DocumentReference;
26    import org.xwiki.model.reference.DocumentReferenceResolver;
27   
28    import com.xpn.xwiki.XWikiContext;
29    import com.xpn.xwiki.XWikiException;
30    import com.xpn.xwiki.web.Utils;
31   
 
32    public class XWikiUser
33    {
34    /**
35    * @see com.xpn.xwiki.internal.model.reference.CurrentMixedStringDocumentReferenceResolver
36    */
37    private DocumentReferenceResolver<String> currentMixedDocumentReferenceResolver = Utils.getComponent(
38    DocumentReferenceResolver.TYPE_STRING, "currentmixed");
39   
40    private String user;
41   
42    private boolean main;
43   
 
44  10581 toggle public XWikiUser(String user)
45    {
46  10576 this(user, false);
47    }
48   
 
49  23787 toggle public XWikiUser(String user, boolean main)
50    {
51  23785 setUser(user);
52  23778 setMain(main);
53    }
54   
 
55  12183 toggle public String getUser()
56    {
57  12195 return this.user;
58    }
59   
 
60  1 toggle private DocumentReference getUserReference(XWikiContext context)
61    {
62  1 return this.currentMixedDocumentReferenceResolver.resolve(getUser());
63    }
64   
 
65  23775 toggle public void setUser(String user)
66    {
67  23780 this.user = user;
68    }
69   
70    /**
71    * Check if the user belongs to a group or not. This method only check direct membership (no recursive checking) in
72    * the current wiki.
73    *
74    * @param groupName The group to check.
75    * @param context The current {@link XWikiContext context}.
76    * @return <tt>true</tt> if the user does belong to the specified group, false otherwise or if an exception occurs.
77    * @throws XWikiException If an error occurs when checking the groups.
78    * @since Platform-1.3
79    */
 
80  3 toggle public boolean isUserInGroup(String groupName, XWikiContext context) throws XWikiException
81    {
82  3 if (!StringUtils.isEmpty(getUser())) {
83  1 XWikiGroupService groupService = context.getWiki().getGroupService(context);
84   
85  1 DocumentReference groupReference = this.currentMixedDocumentReferenceResolver.resolve(groupName);
86   
87  1 Collection<DocumentReference> groups =
88    groupService.getAllGroupsReferencesForMember(getUserReference(context), 0, 0, context);
89   
90  1 if (groups.contains(groupReference)) {
91  0 return true;
92    }
93    }
94   
95  3 return false;
96    }
97   
 
98  2 toggle public boolean isMain()
99    {
100  2 return this.main;
101    }
102   
 
103  23777 toggle public void setMain(boolean main)
104    {
105  23782 this.main = main;
106    }
107   
 
108  0 toggle @Override
109    public String toString()
110    {
111  0 return getUser();
112    }
113   
 
114  3 toggle @Override
115    public boolean equals(Object obj)
116    {
117  3 if (super.equals(obj)) {
118  1 return true;
119    }
120   
121  2 boolean equals;
122  2 if (obj instanceof XWikiUser) {
123  2 XWikiUser otherUser = (XWikiUser) obj;
124  2 equals = otherUser.main == this.main && this.user.equals(otherUser.user);
125    } else {
126  0 equals = false;
127    }
128   
129  2 return equals;
130    }
131    }