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

File CurrentColorThemeGetter.java

 

Coverage histogram

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

Code metrics

4
13
2
1
113
55
6
0.46
6.5
2
3

Classes

Class Line # Actions
CurrentColorThemeGetter 48 13 0% 6 0
1.0100%
 

Contributing tests

This file is covered by 4 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.lesscss.internal.colortheme;
21   
22    import javax.inject.Inject;
23    import javax.inject.Provider;
24    import javax.inject.Singleton;
25   
26    import org.apache.commons.lang3.StringUtils;
27    import org.xwiki.component.annotation.Component;
28    import org.xwiki.model.reference.DocumentReference;
29    import org.xwiki.model.reference.DocumentReferenceResolver;
30    import org.xwiki.model.reference.EntityReferenceSerializer;
31    import org.xwiki.model.reference.WikiReference;
32    import org.xwiki.security.authorization.AuthorizationManager;
33    import org.xwiki.security.authorization.Right;
34    import org.xwiki.wiki.descriptor.WikiDescriptorManager;
35   
36    import com.xpn.xwiki.XWiki;
37    import com.xpn.xwiki.XWikiContext;
38    import com.xpn.xwiki.web.XWikiRequest;
39   
40    /**
41    * Component to get the current color theme set by the request.
42    *
43    * @since 6.4M2
44    * @version $Id: 524a5c276c6c95f6e183e9ddbf01909f6c4bf4af $
45    */
46    @Component(roles = CurrentColorThemeGetter.class)
47    @Singleton
 
48    public class CurrentColorThemeGetter
49    {
50    private static final String COLOR_THEME_FIELD = "colorTheme";
51   
52    @Inject
53    private DocumentReferenceResolver<String> documentReferenceResolver;
54   
55    @Inject
56    private EntityReferenceSerializer<String> entityReferenceSerializer;
57   
58    @Inject
59    private Provider<XWikiContext> xcontextProvider;
60   
61    @Inject
62    private WikiDescriptorManager wikiDescriptorManager;
63   
64    @Inject
65    private AuthorizationManager authorizationManager;
66   
67    /**
68    * @param fallbackValue value to return if the current color theme is invalid
69    * @return the full name of the current color theme or fallbackValue if the current color theme is invalid
70    */
 
71  4 toggle public String getCurrentColorTheme(String fallbackValue)
72    {
73  4 return getCurrentColorTheme(true, fallbackValue);
74    }
75   
76    /**
77    * @param checkRights check if the user has the right to see the color theme
78    * @param fallbackValue value to return if the current color theme is invalid
79    * @return the full name of the current color theme or fallbackValue if the current color theme is invalid
80    * @since 7.0-M2
81    */
 
82  2169 toggle public String getCurrentColorTheme(boolean checkRights, String fallbackValue)
83    {
84    // Get information about the context
85  2169 String wikiId = wikiDescriptorManager.getCurrentWikiId();
86  2169 XWikiContext context = xcontextProvider.get();
87  2169 XWiki xwiki = context.getWiki();
88  2169 XWikiRequest request = context.getRequest();
89   
90    // Get the current color theme
91  2167 String colorTheme = request.getParameter(COLOR_THEME_FIELD);
92  2169 if (StringUtils.isEmpty(colorTheme)) {
93    // Get it from the preferences
94  1914 colorTheme = xwiki.getUserPreference(COLOR_THEME_FIELD, context);
95    }
96   
97    // Getting the full name representation of colorTheme
98  2169 DocumentReference colorThemeReference =
99    documentReferenceResolver.resolve(colorTheme, new WikiReference(wikiId));
100  2169 colorTheme = entityReferenceSerializer.serialize(colorThemeReference);
101   
102    // Check that the color theme exists, to avoid a DOS if some user tries to getResult a skin file
103    // with random colorTheme names
104    // Also check that the user has the right to see the color theme
105  2169 if (!xwiki.exists(colorThemeReference, context) || (checkRights && !authorizationManager.hasAccess(Right.VIEW,
106    context.getUserReference(), colorThemeReference))) {
107  1930 colorTheme = fallbackValue;
108    }
109   
110  2169 return colorTheme;
111    }
112   
113    }