1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.security.authorization.internal

File XWikiSecurityAccess.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart7.png
64% of files have more coverage

Code metrics

12
48
10
1
174
109
20
0.42
4.8
10
2

Classes

Class Line # Actions
XWikiSecurityAccess 34 48 0% 20 22
0.685714368.6%
 

Contributing tests

This file is covered by 42 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 org.apache.commons.lang3.builder.HashCodeBuilder;
23    import org.xwiki.security.authorization.RightSet;
24    import org.xwiki.security.authorization.SecurityAccess;
25    import org.xwiki.security.authorization.Right;
26    import org.xwiki.security.authorization.RuleState;
27   
28    /**
29    * Default implementation for an {@link org.xwiki.security.authorization.SecurityAccess}.
30    *
31    * @version $Id: 70cc1147b4093e64bf5ff726029bc5f592c41a87 $
32    * @since 4.0M2
33    */
 
34    public class XWikiSecurityAccess implements SecurityAccess
35    {
36    /** The default access. */
37    private static XWikiSecurityAccess defaultAccess;
38   
39    /** The default access size. Check to update defaultAccess if a new Right is added. */
40    private static int defaultAccessSize;
41   
42    /** Allowed rights. */
43    protected RightSet allowed = new RightSet();
44   
45    /** Denied rights. */
46    protected RightSet denied = new RightSet();
47   
48    /**
49    * @return the default access, using the default value of all rights.
50    */
 
51  13 toggle public static synchronized XWikiSecurityAccess getDefaultAccess()
52    {
53  13 if (defaultAccess == null || Right.size() != defaultAccessSize) {
54  3 defaultAccessSize = Right.size();
55  3 defaultAccess = new XWikiSecurityAccess();
56  3 for (Right right : Right.values()) {
57  57 defaultAccess.set(right, right.getDefaultState());
58    }
59    }
60  13 return defaultAccess;
61    }
62   
63    /**
64    * Set the state of a right in this access.
65    * @param right the right to set.
66    * @param state the state to set the right to.
67    */
 
68  32929 toggle void set(Right right, RuleState state)
69    {
70  32929 switch (state) {
71  13380 case ALLOW:
72  13380 allow(right);
73  13380 break;
74  19548 case DENY:
75  19548 deny(right);
76  19549 break;
77  0 case UNDETERMINED:
78  0 clear(right);
79  0 break;
80  0 default:
81  0 break;
82    }
83    }
84   
85    /**
86    * Allow this right.
87    * @param right the right to allow.
88    */
 
89  13598 toggle void allow(Right right)
90    {
91  13598 allowed.add(right);
92  13598 denied.remove(right);
93    }
94   
95    /**
96    * Deny this right.
97    * @param right the right to deny.
98    */
 
99  23959 toggle void deny(Right right)
100    {
101  23960 denied.add(right);
102  23960 allowed.remove(right);
103    }
104   
105    /**
106    * Clear this right, it will return to the undetermined state.
107    * @param right the right to clear.
108    */
 
109  13 toggle void clear(Right right)
110    {
111  13 allowed.remove(right);
112  13 denied.remove(right);
113    }
114   
 
115  174004 toggle @Override
116    public RuleState get(Right right)
117    {
118  174009 if (denied.contains(right)) {
119  33274 return RuleState.DENY;
120    }
121  140735 if (allowed.contains(right)) {
122  57490 return RuleState.ALLOW;
123    }
124  83245 return RuleState.UNDETERMINED;
125    }
126   
 
127  17 toggle @Override
128    public XWikiSecurityAccess clone() throws CloneNotSupportedException
129    {
130  17 XWikiSecurityAccess clone = (XWikiSecurityAccess) super.clone();
131  17 clone.allowed = allowed.clone();
132  17 clone.denied = denied.clone();
133  17 return clone;
134    }
135   
 
136  205 toggle @Override
137    public boolean equals(Object object)
138    {
139  205 if (object == this) {
140  0 return true;
141    }
142  205 if (!(object instanceof XWikiSecurityAccess)) {
143  0 return false;
144    }
145  205 XWikiSecurityAccess other = (XWikiSecurityAccess) object;
146   
147  205 return other.allowed.equals(allowed) && other.denied.equals(denied);
148    }
149   
 
150  0 toggle @Override
151    public int hashCode()
152    {
153  0 return new HashCodeBuilder()
154    .append(allowed)
155    .append(denied)
156    .toHashCode();
157    }
158   
 
159  0 toggle @Override
160    public String toString()
161    {
162  0 StringBuilder b = new StringBuilder();
163  0 boolean first = true;
164  0 for (Right r : Right.values()) {
165  0 if (first) {
166  0 first = false;
167    } else {
168  0 b.append(", ");
169    }
170  0 b.append(r).append(": ").append(get(r));
171    }
172  0 return b.toString();
173    }
174    }