001/* 002 * Copyright 2020 Anyware Services 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.ametys.plugins.linkdirectory.theme; 017 018import java.util.HashSet; 019import java.util.Map; 020import java.util.Set; 021 022import javax.jcr.RepositoryException; 023 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026import org.apache.cocoon.ProcessingException; 027import org.apache.commons.lang.StringUtils; 028 029import org.ametys.cms.tag.Tag; 030import org.ametys.cms.tag.TagProvider; 031import org.ametys.cms.tag.jcr.AbstractJCRTagsDAO; 032import org.ametys.cms.tag.jcr.JCRTag; 033import org.ametys.core.right.RightManager; 034import org.ametys.core.right.RightManager.RightResult; 035import org.ametys.core.ui.Callable; 036import org.ametys.core.user.UserIdentity; 037import org.ametys.plugins.repository.ModifiableTraversableAmetysObject; 038import org.ametys.plugins.repository.UnknownAmetysObjectException; 039 040/** 041 * Component for operations on JCR tags 042 */ 043public class JCRThemesDAO extends AbstractJCRTagsDAO 044{ 045 /** The Avalon role */ 046 public static final String ROLE = JCRThemesDAO.class.getName(); 047 048 /** The tag provider extension point */ 049 protected ThemeProviderExtensionPoint _tagProviderExtPt; 050 051 /** The rights manager */ 052 protected RightManager _rightManager; 053 054 @Override 055 public void service(ServiceManager serviceManager) throws ServiceException 056 { 057 super.service(serviceManager); 058 _tagProviderExtPt = (ThemeProviderExtensionPoint) serviceManager.lookup(ThemeProviderExtensionPoint.ROLE); 059 _rightManager = (RightManager) serviceManager.lookup(RightManager.ROLE); 060 } 061 062 @Override 063 public ModifiableTraversableAmetysObject _getTagRootObject (String tagProviderId, Map<String, Object> contextualParameters) throws RepositoryException 064 { 065 JCRThemeProvider provider = (JCRThemeProvider) _tagProviderExtPt.getExtension(tagProviderId); 066 return provider.getRootNode(contextualParameters); 067 } 068 069 @Override 070 protected void _checkUserRight() throws IllegalStateException 071 { 072 String rightId = "LinkDirectory_Rights_Themes_Handle"; 073 String context = "/cms"; 074 075 UserIdentity user = _currentUserProvider.getUser(); 076 if (_rightManager.hasRight(user, rightId, context) != RightResult.RIGHT_ALLOW) 077 { 078 getLogger().error("User '" + user + "' tried to access a privileged feature without convenient right. Should have right '" + rightId + "' on context '" + context + "'"); 079 throw new IllegalStateException("You have no right to access this feature."); 080 } 081 } 082 083 @Override 084 protected Tag _getTagFromName(String name, Map<String, Object> contextualParameters) 085 { 086 return _tagProviderExtPt.getTag(name, contextualParameters); 087 } 088 089 @Override 090 protected JCRTag _createJCRTag(String parentId, String name, String title, String description, Map<String, Object> otherParameters, Map<String, Object> contextualParameters) throws RepositoryException 091 { 092 ModifiableTraversableAmetysObject parent = null; 093 if (StringUtils.isEmpty(parentId)) 094 { 095 parent = _getTagRootObject(JCRThemeProvider.class.getName(), contextualParameters); 096 } 097 else 098 { 099 parent = _resolver.resolveById(parentId); 100 } 101 102 if (parent.hasChild(name)) 103 { 104 throw new RepositoryException("This theme already exists."); 105 } 106 ThemeJCR jcrTag = parent.createChild(name, ThemeFactory.TAG_NODETYPE); 107 jcrTag.setTitle(title); 108 jcrTag.setDescription(description); 109 110 parent.saveChanges(); 111 112 return jcrTag; 113 } 114 115 @Override 116 protected JCRTag _updateJCRTag(String tagId, String title, String description, Map<String, Object> otherParameters) throws UnknownAmetysObjectException 117 { 118 ThemeJCR jcrTag = _resolver.resolveById(tagId); 119 jcrTag.setTitle(title); 120 jcrTag.setDescription(description); 121 122 jcrTag.saveChanges(); 123 124 return jcrTag; 125 } 126 127 @Override 128 protected Set<TagProvider< ? extends Tag>> _getTagProviders() 129 { 130 Set<TagProvider<? extends Tag>> providers = new HashSet<>(); 131 132 Set<String> ids = _tagProviderExtPt.getExtensionsIds(); 133 for (String id : ids) 134 { 135 TagProvider<? extends Tag> provider = _tagProviderExtPt.getExtension(id); 136 providers.add(provider); 137 } 138 139 return providers; 140 } 141 142 @Override 143 // Change right required to call the callable 144 @Callable(rights = "LinkDirectory_Rights_Themes_Handle") 145 public Map<String, Object> getTagRootNode(String tagProviderId, Map<String, Object> contextualParameters) throws ProcessingException 146 { 147 return super.getTagRootNode(tagProviderId, contextualParameters); 148 } 149 150 @Override 151 // Change right required to call the callable 152 @Callable(rights = "LinkDirectory_Rights_Themes_Handle") 153 public Map<String, Object> getTag(String tagId) 154 { 155 return super.getTag(tagId); 156 } 157}