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

File Api.java

 

Coverage histogram

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

Code metrics

10
28
11
1
206
81
16
0.57
2.55
11
1.45

Classes

Class Line # Actions
Api 40 28 0% 16 6
0.87755187.8%
 

Contributing tests

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