1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.security.authorization.internal

File XWikiSecurityRule.java

 

Coverage histogram

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

Code metrics

20
39
10
1
217
132
24
0.62
3.9
10
2.4

Classes

Class Line # Actions
XWikiSecurityRule 52 39 0% 24 10
0.8550724485.5%
 

Contributing tests

This file is covered by 18 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 org.xwiki.security.authorization.internal;
21   
22    import java.util.Collection;
23    import java.util.HashSet;
24    import java.util.Set;
25   
26    import org.apache.commons.lang3.builder.HashCodeBuilder;
27    import org.apache.commons.lang3.builder.ToStringBuilder;
28    import org.xwiki.model.reference.DocumentReference;
29    import org.xwiki.model.reference.DocumentReferenceResolver;
30    import org.xwiki.model.reference.WikiReference;
31    import org.xwiki.security.GroupSecurityReference;
32    import org.xwiki.security.UserSecurityReference;
33    import org.xwiki.security.authorization.Right;
34    import org.xwiki.security.authorization.RightSet;
35    import org.xwiki.security.authorization.RuleState;
36    import org.xwiki.security.authorization.SecurityRule;
37    import org.xwiki.security.internal.XWikiConstants;
38    import org.xwiki.text.XWikiToStringStyle;
39   
40    import com.xpn.xwiki.objects.BaseObject;
41    import com.xpn.xwiki.objects.classes.GroupsClass;
42    import com.xpn.xwiki.objects.classes.LevelsClass;
43    import com.xpn.xwiki.objects.classes.UsersClass;
44   
45   
46    /**
47    * Wrapper around xwiki rights objects to convert them into security rules.
48    *
49    * @version $Id: 8c368b22f5bc8f239791f0a27b399f4208e944aa $
50    * @since 4.0M2
51    */
 
52    public final class XWikiSecurityRule implements SecurityRule
53    {
54    /** The set of users. */
55    private final Set<DocumentReference> users = new HashSet<DocumentReference>();
56   
57    /** The set of groups. */
58    private final Set<DocumentReference> groups = new HashSet<DocumentReference>();
59   
60    /** The set of right levels. */
61    private final RightSet rights = new RightSet();
62   
63    /** The state specified by this object. */
64    private final RuleState state;
65   
66    /**
67    * Constructor to used for implied rules.
68    * @param rights The set of rights.
69    * @param state The state of this rights object.
70    * @param users The set of users.
71    * @param groups The set of groups.
72    */
 
73  13915 toggle protected XWikiSecurityRule(Set<Right> rights, RuleState state, Collection<DocumentReference> users,
74    Collection<DocumentReference> groups)
75    {
76  13923 if (users != null) {
77  13921 this.users.addAll(users);
78    }
79  13925 if (groups != null) {
80  0 this.groups.addAll(groups);
81    }
82  13929 this.rights.addAll(rights);
83  13920 this.state = state;
84    }
85   
86    /**
87    * Construct a more manageable java object from the corresponding
88    * xwiki object.
89    * @param obj An xwiki rights object.
90    * @param resolver A document reference resolver for user and group pages.
91    * @param wikiReference A reference to the wiki from which these rules are extracted.
92    * @param disableEditRight when true, edit right is disregarded while building this rule.
93    * @throws IllegalArgumentException if the source object for the rules is badly formed.
94    */
 
95  1963 toggle private XWikiSecurityRule(BaseObject obj, DocumentReferenceResolver<String> resolver,
96    WikiReference wikiReference, boolean disableEditRight)
97    {
98  1963 state = (obj.getIntValue(XWikiConstants.ALLOW_FIELD_NAME) == 1) ? RuleState.ALLOW : RuleState.DENY;
99   
100  1963 for (String level : LevelsClass.getListFromString(obj.getStringValue(XWikiConstants.LEVELS_FIELD_NAME))) {
101  2901 Right right = Right.toRight(level);
102  2901 if (right != Right.ILLEGAL && (!disableEditRight || right != Right.EDIT)) {
103  2729 rights.add(right);
104    }
105    }
106   
107    // No need to computes users when no right will match.
108  1963 if (rights.size() > 0) {
109  1952 for (String user : UsersClass.getListFromString(obj.getStringValue(XWikiConstants.USERS_FIELD_NAME))) {
110  1021 DocumentReference ref = resolver.resolve(user, wikiReference);
111  1021 if (XWikiConstants.GUEST_USER.equals(ref.getName())) {
112    // In the database, Rights for public users (not logged in) are stored using a user named
113    // XWikiGuest, while in SecurityUserReference the original reference for those users is null. So,
114    // store rules for XWikiGuest to be matched by null.
115  271 ref = null;
116    }
117  1021 this.users.add(ref);
118    }
119   
120  1952 for (String group : GroupsClass.getListFromString(obj.getStringValue(XWikiConstants.GROUPS_FIELD_NAME))) {
121  1172 DocumentReference ref = resolver.resolve(group, wikiReference);
122  1172 this.groups.add(ref);
123    }
124    }
125    }
126   
127    /**
128    * Create and return a new Security rule based on an existing BaseObject.
129    * @param obj An xwiki rights object.
130    * @param resolver A document reference resolver for user and group pages.
131    * @param wikiReference A reference to the wiki from which these rules are extracted.
132    * @param disableEditRight when true, edit right is disregarded while building this rule.
133    * @return a newly created security rule.
134    * @throws IllegalArgumentException if the source object for the rules is badly formed.
135    */
 
136  1963 toggle static SecurityRule createNewRule(BaseObject obj, DocumentReferenceResolver<String> resolver,
137    WikiReference wikiReference, boolean disableEditRight) throws IllegalArgumentException
138    {
139  1963 XWikiSecurityRule rule = new XWikiSecurityRule(obj, resolver, wikiReference, disableEditRight);
140   
141  1963 if (rule.rights.size() == 0) {
142  11 throw new IllegalArgumentException("No rights to build this rule.");
143    }
144   
145  1952 if (rule.users.size() == 0 && rule.groups.size() == 0) {
146  18 throw new IllegalArgumentException("No user/group to build this rule.");
147    }
148   
149  1934 return rule;
150    }
151   
 
152  1622444 toggle @Override
153    public boolean match(Right right)
154    {
155  1622508 return rights.contains(right);
156    }
157   
 
158  630368 toggle @Override
159    public boolean match(GroupSecurityReference group)
160    {
161  630367 return groups.contains(group.getOriginalReference());
162    }
163   
 
164  397495 toggle @Override
165    public boolean match(UserSecurityReference user)
166    {
167  397550 return users.contains(user.getOriginalReference());
168    }
169   
 
170  795036 toggle @Override
171    public RuleState getState()
172    {
173  795084 return state;
174    }
175   
 
176  592 toggle @Override
177    public boolean equals(Object object)
178    {
179  592 if (object == this) {
180  0 return true;
181    }
182  592 if (object == null || object.getClass() != getClass()) {
183  0 return false;
184    }
185   
186  592 XWikiSecurityRule other = (XWikiSecurityRule) object;
187   
188  592 return state == other.state
189    && rights.equals(other.rights)
190    && users.equals(other.users)
191    && groups.equals(other.groups);
192    }
193   
 
194  3552 toggle @Override
195    public int hashCode()
196    {
197  3552 return new HashCodeBuilder()
198    .append(state)
199    .append(rights)
200    .append(users)
201    .append(groups)
202    .toHashCode();
203    }
204   
 
205  0 toggle @Override
206    public String toString()
207    {
208  0 ToStringBuilder builder = new ToStringBuilder(this, new XWikiToStringStyle());
209   
210  0 return builder
211    .append("State", state)
212    .append("Rights", rights)
213    .append("Users", users)
214    .append("Groups", groups)
215    .toString();
216    }
217    }