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

File SecurityReference.java

 

Coverage histogram

../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

26
28
10
1
176
90
28
1
2.8
10
2.8

Classes

Class Line # Actions
SecurityReference 42 28 0% 28 0
1.0100%
 

Contributing tests

This file is covered by 58 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;
21   
22    import java.util.Deque;
23    import java.util.LinkedList;
24   
25    import org.xwiki.model.EntityType;
26    import org.xwiki.model.reference.DocumentReference;
27    import org.xwiki.model.reference.EntityReference;
28    import org.xwiki.model.reference.SpaceReference;
29    import org.xwiki.model.reference.WikiReference;
30   
31    /**
32    * SecurityReference is a variant of EntityReference used internally in the security-authorization module, for the
33    * purpose of conveniently maintaining a hierarchy where all entities are rooted by the main wiki. This form of
34    * hierarchical view is required to deciding which access levels that should be enforced. There is always a one-to-one
35    * correspondence between a SecurityReference and a Wiki-, Space-, or DocumentReference. Moreover, a security reference
36    * built with a null EntityReference is equivalent to the main wiki reference (but with a null original reference).
37    *
38    * @see SecurityReferenceFactory
39    * @version $Id: bfaf0d6a69324ea3defee8a02a89fddf54083aa6 $
40    * @since 4.0M2
41    */
 
42    public class SecurityReference extends EntityReference
43    {
44    /** EntityType for the main wiki. */
45    public static final EntityType FARM = null;
46   
47    /** Serialization identifier. */
48    private static final long serialVersionUID = 1L;
49   
50    /** Main wiki reference. */
51    protected SecurityReference mainWikiReference;
52   
53    /** Original reference represented by this reference. */
54    private EntityReference originalReference;
55   
56    /**
57    * @param reference the reference to the main wiki that will be converted to a security reference
58    */
 
59  124 toggle SecurityReference(EntityReference reference)
60    {
61  124 super(reference);
62  124 this.originalReference = reference;
63  124 this.mainWikiReference = this;
64    }
65   
66    /**
67    * @param reference the reference to an entity that will be converted to a security reference
68    * @param mainWiki the security reference to the main wiki.
69    */
 
70  155058 toggle SecurityReference(EntityReference reference, SecurityReference mainWiki)
71    {
72  155053 super((reference != null) ? reference : mainWiki);
73  155059 this.originalReference = (reference != null) ? reference : mainWiki.getOriginalReference();
74  155056 this.mainWikiReference = mainWiki;
75    }
76   
77    /**
78    * @return the parent reference of this security reference. For a reference to a subwiki which do not have a
79    * parent by definition, this returns the main wiki reference.
80    */
 
81  30097 toggle public SecurityReference getParentSecurityReference()
82    {
83  30097 EntityReference parent = getParent();
84   
85  30097 if (parent == null && this.getType() == EntityType.WIKI && !this.equals(this.mainWikiReference)) {
86  242 return this.mainWikiReference;
87    }
88  29855 return (parent != null && !(parent instanceof SecurityReference))
89    ? new SecurityReference(parent, this.mainWikiReference)
90    : (SecurityReference) parent;
91    }
92   
93    /**
94    * @return the reversed reference chain using {@link #getParentSecurityReference}.
95    */
 
96  2647 toggle public Deque<SecurityReference> getReversedSecurityReferenceChain()
97    {
98  2647 Deque<SecurityReference> referenceList = new LinkedList<SecurityReference>();
99  2646 SecurityReference reference = this;
100  2647 do {
101  7961 referenceList.push(reference);
102  7962 reference = reference.getParentSecurityReference();
103  7962 } while (reference != null);
104  2647 return referenceList;
105    }
106   
107    /**
108    * @return the entity reference type, but for the main wiki, return {@link #FARM}.
109    */
 
110  86438 toggle public EntityType getSecurityType()
111    {
112  86436 EntityType type = getType();
113  86439 if (type != EntityType.WIKI || !this.equals(this.mainWikiReference)) {
114  78194 return type;
115    }
116  8244 return FARM;
117    }
118   
119    /**
120    * @return a SecurityReference representing the first reference of EntityType.WIKI in this security reference.
121    */
 
122  4774 toggle public SecurityReference getWikiReference() {
123  4774 SecurityReference result = this;
124  12080 while (result != null && result.getType() != EntityType.WIKI) {
125  7306 result = result.getParentSecurityReference();
126    }
127  4774 return result;
128    }
129   
130    /**
131    * @return the original reference used when this security reference was built.
132    */
 
133  9473 toggle public EntityReference getOriginalReference()
134    {
135  9473 return this.originalReference;
136    }
137   
138    /**
139    * @return the original wiki reference used when this security reference was built. Null if this is not
140    * the reference to a wiki.
141    */
 
142  15777 toggle public WikiReference getOriginalWikiReference()
143    {
144  15777 return (this.getType() == EntityType.WIKI)
145  15773 ? (this.originalReference instanceof WikiReference)
146    ? (WikiReference) this.originalReference
147    : new WikiReference(this.originalReference)
148    : null;
149    }
150   
151    /**
152    * @return the original space reference used when this security reference was built. Null if this is not
153    * the reference to a space.
154    */
 
155  569 toggle public SpaceReference getOriginalSpaceReference()
156    {
157  569 return (this.getType() == EntityType.SPACE)
158  564 ? (this.originalReference instanceof SpaceReference)
159    ? (SpaceReference) this.originalReference
160    : new SpaceReference(this.originalReference)
161    : null;
162    }
163   
164    /**
165    * @return the original document reference used when this security reference was built. Null if this is not
166    * the reference to a document.
167    */
 
168  38598 toggle public DocumentReference getOriginalDocumentReference()
169    {
170  38600 return (this.getType() == EntityType.DOCUMENT)
171  23813 ? (this.originalReference instanceof DocumentReference)
172    ? (DocumentReference) this.originalReference
173    : new DocumentReference(this.originalReference)
174    : null;
175    }
176    }