001/* 002 * Copyright 2011 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.web.clientsideelement; 017 018import java.util.ArrayList; 019import java.util.HashMap; 020import java.util.List; 021import java.util.Map; 022 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025 026import org.ametys.cms.content.ContentHelper; 027import org.ametys.cms.repository.Content; 028import org.ametys.core.observation.Event; 029import org.ametys.core.observation.ObservationManager; 030import org.ametys.core.right.RightManager.RightResult; 031import org.ametys.core.ui.Callable; 032import org.ametys.core.ui.SimpleMenu; 033import org.ametys.core.user.UserManager; 034import org.ametys.plugins.repository.AmetysObjectResolver; 035import org.ametys.runtime.i18n.I18nizableText; 036import org.ametys.web.ObservationConstants; 037import org.ametys.web.repository.content.ModifiableWebContent; 038import org.ametys.web.repository.content.WebContent; 039import org.ametys.web.site.SiteConfigurationExtensionPoint; 040 041/** 042 * This element represents the privacy level of contents 043 */ 044public class ContentPrivacyMenu extends SimpleMenu 045{ 046 /** Runtime users manager */ 047 protected UserManager _userManager; 048 /** Repository content */ 049 protected AmetysObjectResolver _resolver; 050 private SiteConfigurationExtensionPoint _siteConfigurationEP; 051 private ObservationManager _observationManager; 052 private ContentHelper _contentHelper; 053 054 @Override 055 public void service(ServiceManager smanager) throws ServiceException 056 { 057 super.service(smanager); 058 _userManager = (UserManager) smanager.lookup(UserManager.ROLE); 059 _resolver = (AmetysObjectResolver) smanager.lookup(AmetysObjectResolver.ROLE); 060 _siteConfigurationEP = (SiteConfigurationExtensionPoint) smanager.lookup(SiteConfigurationExtensionPoint.ROLE); 061 _observationManager = (ObservationManager) smanager.lookup(ObservationManager.ROLE); 062 _contentHelper = (ContentHelper) smanager.lookup(ContentHelper.ROLE); 063 } 064 065 /** 066 * Get the privacy status of contents 067 * @param contentIds the id of contents 068 * @return the privacy status 069 */ 070 @Callable 071 public Map<String, Object> getStatus (List<String> contentIds) 072 { 073 Map<String, Object> results = new HashMap<>(); 074 075 results.put("allright-contents", new ArrayList<Map<String, Object>>()); 076 results.put("noright-contents", new ArrayList<Map<String, Object>>()); 077 results.put("public-contents", new ArrayList<Map<String, Object>>()); 078 results.put("private-contents", new ArrayList<Map<String, Object>>()); 079 results.put("protected-contents", new ArrayList<Map<String, Object>>()); 080 results.put("unmodifiable-contents", new ArrayList<Map<String, Object>>()); 081 082 for (String contentId : contentIds) 083 { 084 Content content = _resolver.resolveById(contentId); 085 086 if (content instanceof ModifiableWebContent) 087 { 088 boolean canChangePrivacy = _rightManager.hasRight(_currentUserProvider.getUser(), "WEB_Rights_Content_ChangePrivacy", content) == RightResult.RIGHT_ALLOW; 089 if (!canChangePrivacy) 090 { 091 Map<String, Object> contentParams = getContentDefaultParameters (content); 092 contentParams.put("description", _getContentDescription(content, "noright")); 093 094 @SuppressWarnings("unchecked") 095 List<Map<String, Object>> norightContents = (List<Map<String, Object>>) results.get("noright-contents"); 096 norightContents.add(contentParams); 097 } 098 else 099 { 100 ModifiableWebContent webContent = (ModifiableWebContent) content; 101 String privacy = webContent.getMetadataHolder().getString("privacy", null); 102 if (privacy == null) 103 { 104 privacy = _siteConfigurationEP.getValueAsString(((WebContent) content).getSiteName(), "content-privacy"); 105 } 106 107 Map<String, Object> contentParams = getContentDefaultParameters (content); 108 109 if ("public".equals(privacy)) 110 { 111 @SuppressWarnings("unchecked") 112 List<Map<String, Object>> publicContents = (List<Map<String, Object>>) results.get("public-contents"); 113 contentParams.put("description", _getContentDescription(content, "public")); 114 publicContents.add(contentParams); 115 } 116 else if ("private".equals(privacy)) 117 { 118 @SuppressWarnings("unchecked") 119 List<Map<String, Object>> privateContents = (List<Map<String, Object>>) results.get("private-contents"); 120 contentParams.put("description", _getContentDescription(content, "private")); 121 privateContents.add(contentParams); 122 } 123 else if ("protected".equals(privacy)) 124 { 125 @SuppressWarnings("unchecked") 126 List<Map<String, Object>> protectedContents = (List<Map<String, Object>>) results.get("protected-contents"); 127 contentParams.put("description", _getContentDescription(content, "protected")); 128 protectedContents.add(contentParams); 129 } 130 131 @SuppressWarnings("unchecked") 132 List<Map<String, Object>> allRightContents = (List<Map<String, Object>>) results.get("allright-contents"); 133 allRightContents.add(contentParams); 134 } 135 } 136 else 137 { 138 Map<String, Object> contentParams = getContentDefaultParameters (content); 139 contentParams.put("description", _getContentDescription(content, "nomodifiable")); 140 141 @SuppressWarnings("unchecked") 142 List<Map<String, Object>> unmodifiableContents = (List<Map<String, Object>>) results.get("unmodifiable-contents"); 143 unmodifiableContents.add(contentParams); 144 } 145 } 146 147 return results; 148 } 149 150 /** 151 * Update the privacy level of a list of contents 152 * @param contentsId The ids of the contents 153 * @param level The level of privacy 154 */ 155 @Callable 156 public void updatePrivacy(List<String> contentsId, String level) 157 { 158 for (String contentId : contentsId) 159 { 160 ModifiableWebContent content = _resolver.resolveById(contentId); 161 content.getMetadataHolder().setMetadata("privacy", level); 162 content.saveChanges(); 163 164 // Notify observers that the tag has been added. 165 166 Map<String, Object> eventParams = new HashMap<>(); 167 eventParams.put(org.ametys.cms.ObservationConstants.ARGS_CONTENT, content); 168 eventParams.put("level", level); 169 _observationManager.notify(new Event(ObservationConstants.EVENT_CONTENT_PRIVACY_CHANGED, _currentUserProvider.getUser(), eventParams)); 170 } 171 } 172 173 /** 174 * Get the default content's parameters 175 * @param content The content 176 * @return The default parameters 177 */ 178 protected Map<String, Object> getContentDefaultParameters (Content content) 179 { 180 Map<String, Object> contentParams = new HashMap<>(); 181 contentParams.put("id", content.getId()); 182 contentParams.put("title", _contentHelper.getTitle(content)); 183 184 return contentParams; 185 } 186 187 private I18nizableText _getContentDescription (Content content, String prefix) 188 { 189 List<String> modifiableI18nParameters = new ArrayList<>(); 190 modifiableI18nParameters.add(_contentHelper.getTitle(content)); 191 192 I18nizableText ed = (I18nizableText) this._script.getParameters().get(prefix + "-content-description"); 193 return new I18nizableText(ed.getCatalogue(), ed.getKey(), modifiableI18nParameters); 194 } 195}