1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.api

File Api.java

 

Coverage histogram

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

Code metrics

14
34
14
1
250
104
21
0.62
2.43
14
1.5

Classes

Class Line # Actions
Api 44 34 0% 21 24
0.6129032461.3%
 

Contributing tests

This file is covered by 81 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.api;
21   
22    import java.util.ArrayList;
23    import java.util.List;
24   
25    import javax.inject.Provider;
26   
27    import org.xwiki.model.reference.DocumentReference;
28    import org.xwiki.model.reference.EntityReferenceSerializer;
29    import org.xwiki.security.authorization.AuthorizationManager;
30   
31    import com.xpn.xwiki.XWikiContext;
32    import com.xpn.xwiki.XWikiException;
33    import com.xpn.xwiki.doc.XWikiAttachment;
34    import com.xpn.xwiki.doc.XWikiDocument;
35    import com.xpn.xwiki.user.api.XWikiRightService;
36    import com.xpn.xwiki.web.Utils;
37   
38    /**
39    * Base class for all API Objects. API Objects are the Java Objects that can be manipulated from Velocity or Groovy in
40    * XWiki documents.
41    *
42    * @version $Id: 2222ba28266f930991cc7cb76ce7b31d1a063a1e $
43    */
 
44    public class Api
45    {
46    /**
47    * The current context, needed by the underlying protected object.
48    *
49    * @see #getXWikiContext()
50    */
51    protected XWikiContext context;
52   
53    private Provider<XWikiContext> xcontextProvider;
54   
55    private AuthorizationManager authorizationManager;
56   
57    private EntityReferenceSerializer<String> defaultEntityReferenceSerializer;
58   
59    /**
60    * @param context the XWiki Context object
61    * @see #getXWikiContext()
62    */
 
63  7523246 toggle public Api(XWikiContext context)
64    {
65  7525525 this.context = context;
66    }
67   
68    /**
69    * Note 1: This method is protected so that users of this API do not get to see the XWikiContext object which should
70    * not be exposed.
71    * <p>
72    * Note 2: This is not longer the canonical way of retrieving the XWiki Context. The new way is to get it from the
73    * {@link org.xwiki.context.ExecutionContext}.
74    *
75    * @return the current context containing all state information about the current request
76    */
 
77  11325726 toggle protected XWikiContext getXWikiContext()
78    {
79  11328709 if (this.xcontextProvider == null) {
80  2979138 this.xcontextProvider = Utils.getComponent(XWikiContext.TYPE_PROVIDER);
81    }
82   
83    // TODO: We need to get rid of this.context but since it's been protected for a long time, a lot of code
84    // wrongly use it instead of calling this getXWikiContext() method. Thus the best we can do ATM is to sync
85    // the saved context with the dynamic one we just retrieved...
86  11328435 this.context = this.xcontextProvider.get();
87   
88  11329672 return this.context;
89    }
90   
91    /**
92    * @return the {@link AuthorizationManager} component
93    * @since 10.6RC1
94    */
 
95  260 toggle protected AuthorizationManager getAuthorizationManager()
96    {
97  260 if (this.authorizationManager == null) {
98  114 this.authorizationManager = Utils.getComponent(AuthorizationManager.class);
99    }
100   
101  260 return this.authorizationManager;
102    }
103   
 
104  0 toggle private EntityReferenceSerializer<String> getDefaultEntityReferenceSerializer()
105    {
106  0 if (this.defaultEntityReferenceSerializer == null) {
107  0 this.defaultEntityReferenceSerializer = Utils.getComponent(EntityReferenceSerializer.TYPE_STRING);
108    }
109   
110  0 return this.defaultEntityReferenceSerializer;
111    }
112   
113    /**
114    * Check if the current document has programming rights, meaning that it was last saved by a user with the
115    * programming right globally granted.
116    *
117    * @return <tt>true</tt> if the current document has the Programming right or <tt>false</tt> otherwise.
118    */
 
119  2390 toggle public boolean hasProgrammingRights()
120    {
121  2390 com.xpn.xwiki.XWiki xwiki = this.context.getWiki();
122  2390 return xwiki.getRightService().hasProgrammingRights(this.context);
123    }
124   
125    /**
126    * Check if the current user has administration rights either on the current wiki or on the current space.
127    *
128    * @return <code>true</code> if the current user has the <code>admin</code> right or <code>false</code> otherwise.
129    */
 
130  19087 toggle public boolean hasAdminRights()
131    {
132  19087 com.xpn.xwiki.XWiki xwiki = this.context.getWiki();
133  19087 return xwiki.getRightService().hasAdminRights(this.context);
134    }
135   
136    /**
137    * Check if the current user has administration rights on the current wiki, regardless of any space admin rights
138    * that might also be available.
139    *
140    * @return <code>true</code> if the current user has the <code>admin</code> right or <code>false</code> otherwise.
141    * @since 3.2M3
142    */
 
143  393 toggle public boolean hasWikiAdminRights()
144    {
145  393 com.xpn.xwiki.XWiki xwiki = this.context.getWiki();
146  393 return xwiki.getRightService().hasWikiAdminRights(this.context);
147    }
148   
149    /**
150    * Check if the current user has an access level on a given document.
151    *
152    * @param right The name of the right to verify (eg "programming", "admin", "register", etc).
153    * @param docname The document for which to verify the right.
154    * @return <tt>true</tt> if the current user has the specified right, <tt>false</tt> otherwise.
155    * @exception XWikiException In case of an error finding the document or accessing groups information.
156    */
 
157  43797 toggle public boolean hasAccessLevel(String right, String docname) throws XWikiException
158    {
159  43797 com.xpn.xwiki.XWiki xwiki = this.context.getWiki();
160  43797 return xwiki.getRightService().hasAccessLevel(right, this.context.getUser(), docname, this.context);
161    }
162   
163    /**
164    * Convert a list of internal representation of documents to public api documents.
165    *
166    * @param xdocList the internal documents.
167    * @return the plublic api documents.
168    */
 
169  50 toggle protected List<Document> convert(List<XWikiDocument> xdocList)
170    {
171  50 List<Document> docList = new ArrayList<Document>();
172  50 for (XWikiDocument xdoc : xdocList) {
173  107 docList.add(xdoc.newDocument(this.context));
174    }
175   
176  50 return docList;
177    }
178   
179    /**
180    * Convert an internal representation of document to public api document.
181    *
182    * @param xdoc the internal document.
183    * @return the plublic api document.
184    */
 
185  0 toggle protected Document convert(XWikiDocument xdoc)
186    {
187  0 return xdoc == null ? null : xdoc.newDocument(this.context);
188    }
189   
190    /**
191    * Get the name of the content author of the current document for security checking. If
192    * {@link XWikicontext#dropPermissions()} has been called then this will return the guest user no matter who the
193    * real author is. If there is no current document then the guest user is returned because there is no reason for
194    * script to have any permission if does not exist in any document.
195    *
196    * @return the name of the document author or guest.
197    * @deprecated use {@link #getEffectiveAuthorReference()} instead
198    */
 
199  0 toggle @Deprecated
200    String getEffectiveScriptAuthorName()
201    {
202  0 DocumentReference authorReference = getXWikiContext().getAuthorReference();
203   
204  0 return authorReference != null ? getDefaultEntityReferenceSerializer().serialize(authorReference)
205    : XWikiRightService.GUEST_USER;
206    }
207   
208    /**
209    * Get the reference of the current author for security checking. If {@link XWikicontext#dropPermissions()} has been
210    * called then this will return the guest user no matter who the real author is.
211    *
212    * @return the name of the document author or guest.
213    */
 
214  260 toggle DocumentReference getEffectiveAuthorReference()
215    {
216  260 if (!this.getXWikiContext().hasDroppedPermissions()) {
217  259 return getXWikiContext().getAuthorReference();
218    }
219   
220  1 return null;
221    }
222   
223    /**
224    * Convert an internal representation of an attachment to the public api Attachment.
225    *
226    * @param xattach The internal XWikiAttachment object
227    * @return The public api Attachment object
228    * @since 5.0M2
229    */
 
230  0 toggle protected Attachment convert(XWikiAttachment xattach)
231    {
232  0 return xattach == null ? null : new Attachment(convert(xattach.getDoc()), xattach, this.context);
233    }
234   
235    /**
236    * Convert a list of attachments in their internal form to a list of public api Attachments.
237    *
238    * @param xattaches The List of XWikiAttachment objects
239    * @return A List of Attachment objects
240    * @since 5.0M2
241    */
 
242  0 toggle protected List<Attachment> convertAttachments(List<XWikiAttachment> xattaches)
243    {
244  0 List<Attachment> outList = new ArrayList<Attachment>(xattaches.size());
245  0 for (XWikiAttachment xattach : xattaches) {
246  0 outList.add(convert(xattach));
247    }
248  0 return outList;
249    }
250    }