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

File DefaultAuthorExecutor.java

 

Coverage histogram

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

Code metrics

12
30
5
2
147
84
13
0.43
6
2.5
2.6

Classes

Class Line # Actions
DefaultAuthorExecutor 48 29 0% 11 2
0.9545454495.5%
DefaultAuthorExecutor.DefaultAuthorExecutorContext 55 1 0% 2 0
1.0100%
 

Contributing tests

This file is covered by 7 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.internal.security.authorization;
21   
22    import java.util.concurrent.Callable;
23   
24    import javax.inject.Inject;
25    import javax.inject.Provider;
26    import javax.inject.Singleton;
27   
28    import org.xwiki.component.annotation.Component;
29    import org.xwiki.context.Execution;
30    import org.xwiki.context.ExecutionContext;
31    import org.xwiki.model.reference.DocumentReference;
32    import org.xwiki.security.authorization.AuthorExecutor;
33   
34    import com.xpn.xwiki.XWikiConstant;
35    import com.xpn.xwiki.XWikiContext;
36    import com.xpn.xwiki.doc.XWikiDocument;
37   
38    /**
39    * Default implementation of {@link AuthorExecutor}.
40    *
41    * @version $Id: 42d9e7d05e12fe51f2858c7892b3d498f4512c28 $
42    * @since 8.3RC1
43    */
44    @Component
45    @Singleton
46    // FIXME: It would probably make more sense to put it in xwiki-platform-security-bridge but it's a lot easier for tests
47    // to have it in oldcore right now
 
48    public class DefaultAuthorExecutor implements AuthorExecutor
49    {
50    /**
51    * Contain the informations to restore.
52    *
53    * @version $Id: 42d9e7d05e12fe51f2858c7892b3d498f4512c28 $
54    */
 
55    private final class DefaultAuthorExecutorContext implements AutoCloseable
56    {
57    private XWikiDocument currentSecureDocument;
58   
59    private Object xwikiContextDropPermissionHack;
60   
61    private Object documentDropPermissionHack;
62   
 
63  82147 toggle private DefaultAuthorExecutorContext()
64    {
65    // Only SUExecutor can create SUExecutorContext
66    }
67   
 
68  82013 toggle @Override
69    public void close() throws Exception
70    {
71  82012 after(this);
72    }
73    }
74   
75    @Inject
76    private Provider<XWikiContext> xcontextProvider;
77   
78    @Inject
79    private Execution execution;
80   
 
81  82008 toggle @Override
82    public <V> V call(Callable<V> callable, DocumentReference authorReference) throws Exception
83    {
84  81994 try (AutoCloseable context = before(authorReference)) {
85  82006 return callable.call();
86    }
87    }
88   
 
89  82130 toggle @Override
90    public AutoCloseable before(DocumentReference authorReference)
91    {
92  82138 DefaultAuthorExecutorContext suContext;
93   
94  82141 XWikiContext xwikiContext = this.xcontextProvider.get();
95   
96  82166 if (xwikiContext != null) {
97  82155 suContext = new DefaultAuthorExecutorContext();
98   
99    // Make sure to have the right secure document
100  82145 suContext.currentSecureDocument = (XWikiDocument) xwikiContext.get(XWikiDocument.CKEY_SDOC);
101  82156 XWikiDocument secureDocument = new XWikiDocument(new DocumentReference(
102  82154 authorReference != null ? authorReference.getWikiReference().getName() : "xwiki", "SUSpace", "SUPage"));
103  82162 secureDocument.setContentAuthorReference(authorReference);
104  82151 secureDocument.setAuthorReference(authorReference);
105  82151 secureDocument.setCreatorReference(authorReference);
106  82148 xwikiContext.put(XWikiDocument.CKEY_SDOC, secureDocument);
107   
108    // Make sure to disable XWikiContext#dropPermission hack
109  82164 suContext.xwikiContextDropPermissionHack = xwikiContext.remove(XWikiConstant.DROPPED_PERMISSIONS);
110   
111    // Make sure to disable Document#dropPermission hack
112  82148 ExecutionContext econtext = this.execution.getContext();
113  82166 if (econtext != null) {
114  82156 suContext.documentDropPermissionHack = econtext.getProperty(XWikiConstant.DROPPED_PERMISSIONS);
115  82154 econtext.removeProperty(XWikiConstant.DROPPED_PERMISSIONS);
116    }
117    } else {
118  2 suContext = null;
119    }
120   
121  82152 return suContext;
122    }
123   
 
124  82159 toggle @Override
125    public void after(AutoCloseable context)
126    {
127  82160 XWikiContext xwikiContext = this.xcontextProvider.get();
128   
129  82168 if (xwikiContext != null) {
130  82160 DefaultAuthorExecutorContext internalContext = (DefaultAuthorExecutorContext) context;
131   
132    // Restore context document's content author
133  82153 xwikiContext.put(XWikiDocument.CKEY_SDOC, internalContext.currentSecureDocument);
134   
135    // Restore XWikiContext#dropPermission hack
136  82163 if (internalContext.xwikiContextDropPermissionHack != null) {
137  767 xwikiContext.put(XWikiConstant.DROPPED_PERMISSIONS, internalContext.xwikiContextDropPermissionHack);
138    }
139   
140    // Restore Document#dropPermission hack
141  82159 if (internalContext.documentDropPermissionHack != null) {
142  1 ExecutionContext econtext = this.execution.getContext();
143  1 econtext.setProperty(XWikiConstant.DROPPED_PERMISSIONS, internalContext.documentDropPermissionHack);
144    }
145    }
146    }
147    }