1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.notifications.notifiers.internal

File AbstractWikiNotificationRendererComponentBuilder.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart8.png
56% of files have more coverage

Code metrics

2
7
2
1
92
41
4
0.57
3.5
2
2

Classes

Class Line # Actions
AbstractWikiNotificationRendererComponentBuilder 45 7 0% 4 3
0.7272727572.7%
 

Contributing tests

No tests hitting this source file were found.

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.notifications.notifiers.internal;
21   
22    import java.util.Arrays;
23    import java.util.List;
24   
25    import javax.inject.Inject;
26   
27    import org.xwiki.component.wiki.WikiComponent;
28    import org.xwiki.component.wiki.WikiComponentException;
29    import org.xwiki.component.wiki.internal.bridge.WikiBaseObjectComponentBuilder;
30    import org.xwiki.model.reference.DocumentReference;
31    import org.xwiki.notifications.NotificationException;
32    import org.xwiki.security.authorization.AuthorizationManager;
33    import org.xwiki.security.authorization.Right;
34   
35    import com.xpn.xwiki.doc.XWikiDocument;
36    import com.xpn.xwiki.objects.BaseObject;
37   
38    /**
39    * This component allows the definition of a notification templates in wiki pages.
40    * It uses {@link org.xwiki.eventstream.UntypedRecordableEvent#getEventType} to be bound to a specific event type.
41    *
42    * @version $Id: 9d369128adf05a8998dd256fb9bfd572baa03639 $
43    * @since 9.11.1
44    */
 
45    public abstract class AbstractWikiNotificationRendererComponentBuilder implements WikiBaseObjectComponentBuilder
46    {
47    @Inject
48    protected AuthorizationManager authorizationManager;
49   
 
50  2 toggle @Override
51    public List<WikiComponent> buildComponents(BaseObject baseObject) throws WikiComponentException
52    {
53  2 try {
54    // Check that the document owner is allowed to build the components
55  2 XWikiDocument parentDocument = baseObject.getOwnerDocument();
56  2 this.checkRights(parentDocument.getDocumentReference(), parentDocument.getAuthorReference());
57   
58    // Instantiate the component
59  2 return Arrays.asList(instantiateComponent(parentDocument.getAuthorReference(), baseObject));
60    } catch (Exception e) {
61  0 throw new WikiComponentException(String.format(
62    "Unable to build the WikiNotificationDisplayer wiki component "
63    + "for [%s].", baseObject), e);
64    }
65    }
66   
67    /**
68    * Instantiate the wiki component.
69    * @param authorReference reference of the author of the component
70    * @param baseObject object holding the component
71    * @return the corresponding wiki component
72    * @throws NotificationException if an error occurs
73    */
74    protected abstract WikiComponent instantiateComponent(DocumentReference authorReference, BaseObject baseObject)
75    throws NotificationException;
76   
77    /**
78    * Ensure that the given author has the administrative rights in the current context.
79    *
80    * @param documentReference the working entity
81    * @param authorReference the author that should have its rights checked
82    * @throws NotificationException if the author rights are not sufficient
83    */
 
84  2 toggle protected void checkRights(DocumentReference documentReference, DocumentReference authorReference)
85    throws NotificationException
86    {
87  2 if (!this.authorizationManager.hasAccess(Right.ADMIN, authorReference, documentReference.getWikiReference())) {
88  0 throw new NotificationException(
89    "Registering custom Notification Displayers requires wiki administration rights.");
90    }
91    }
92    }