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