001/* 002 * Copyright 2016 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.cms.clientsideelement; 017 018import java.util.ArrayList; 019import java.util.HashMap; 020import java.util.List; 021import java.util.Map; 022import java.util.Set; 023 024import org.apache.avalon.framework.service.ServiceException; 025import org.apache.avalon.framework.service.ServiceManager; 026import org.apache.commons.lang.StringUtils; 027 028import org.ametys.cms.content.ContentHelper; 029import org.ametys.cms.repository.Content; 030import org.ametys.core.right.RightManager.RightResult; 031import org.ametys.core.ui.StaticClientSideElement; 032import org.ametys.core.user.User; 033import org.ametys.core.user.UserIdentity; 034import org.ametys.core.user.UserManager; 035import org.ametys.plugins.repository.lock.LockHelper; 036import org.ametys.plugins.repository.lock.LockableAmetysObject; 037import org.ametys.runtime.i18n.I18nizableText; 038 039/** 040 * Abstract client side element for contents. 041 */ 042public abstract class AbstractContentClientSideElement extends StaticClientSideElement 043{ 044 /** Runtime users manager */ 045 protected UserManager _userManager; 046 /** The content helper */ 047 protected ContentHelper _contentHelper; 048 049 @Override 050 public void service(ServiceManager smanager) throws ServiceException 051 { 052 super.service(smanager); 053 _userManager = (UserManager) smanager.lookup(UserManager.ROLE); 054 _contentHelper = (ContentHelper) smanager.lookup(ContentHelper.ROLE); 055 } 056 057 /** 058 * Get the default content's parameters 059 * @param content The content 060 * @return The default parameters 061 */ 062 protected Map<String, Object> getContentDefaultParameters (Content content) 063 { 064 Map<String, Object> contentParams = new HashMap<>(); 065 contentParams.put("id", content.getId()); 066 contentParams.put("title", _contentHelper.getTitle(content)); 067 068 return contentParams; 069 } 070 071 /** 072 * Get the description when user has no right on content 073 * @param content the content 074 * @return the description 075 */ 076 protected I18nizableText getNoRightContentDescription (Content content) 077 { 078 List<String> i18nParameters = new ArrayList<>(); 079 i18nParameters.add(_contentHelper.getTitle(content)); 080 081 I18nizableText ed = (I18nizableText) this._script.getParameters().get("noright-content-description"); 082 return new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters); 083 } 084 085 /** 086 * Get the description when content is not modifiable 087 * @param content the content 088 * @return the description 089 */ 090 protected I18nizableText getNoModifiableContentDescription (Content content) 091 { 092 List<String> i18nParameters = new ArrayList<>(); 093 i18nParameters.add(_contentHelper.getTitle(content)); 094 095 I18nizableText ed = (I18nizableText) this._script.getParameters().get("nomodifiable-content-description"); 096 return new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters); 097 } 098 099 /** 100 * Get the description when content is locked 101 * @param content the content 102 * @return the description 103 */ 104 protected I18nizableText getLockedContentDescription (Content content) 105 { 106 UserIdentity currentLockerIdentity = ((LockableAmetysObject) content).getLockOwner(); 107 User currentLocker = currentLockerIdentity != null ? _userManager.getUser(currentLockerIdentity.getPopulationId(), currentLockerIdentity.getLogin()) : null; 108 109 List<String> lockI18nParameters = new ArrayList<>(); 110 lockI18nParameters.add(_contentHelper.getTitle(content)); 111 lockI18nParameters.add(currentLocker != null ? currentLocker.getFullName() : ""); 112 lockI18nParameters.add(currentLockerIdentity != null ? currentLockerIdentity.getLogin() : "Anonymous"); 113 114 I18nizableText ed = (I18nizableText) this._script.getParameters().get("locked-content-description"); 115 return new I18nizableText(ed.getCatalogue(), ed.getKey(), lockI18nParameters); 116 } 117 118 /** 119 * Get i18n description for all right content 120 * @param content The content 121 * @return The {@link I18nizableText} description 122 */ 123 protected I18nizableText getAllRightContentDescription (Content content) 124 { 125 List<String> i18nParameters = new ArrayList<>(); 126 i18nParameters.add(_contentHelper.getTitle(content)); 127 128 I18nizableText ed = (I18nizableText) this._script.getParameters().get("allright-content-description"); 129 return new I18nizableText(ed.getCatalogue(), ed.getKey(), i18nParameters); 130 } 131 132 /** 133 * Determines if a content is locked 134 * @param content the content 135 * @return true if the content is locked. 136 */ 137 protected boolean isLocked(Content content) 138 { 139 if (content instanceof LockableAmetysObject) 140 { 141 LockableAmetysObject lockableContent = (LockableAmetysObject) content; 142 if (lockableContent.isLocked() && !LockHelper.isLockOwner(lockableContent, _currentUserProvider.getUser())) 143 { 144 return true; 145 } 146 } 147 148 return false; 149 } 150 151 /** 152 * Determines if user has convenient right on content 153 * @param content The content 154 * @return true if the user has convenient right 155 */ 156 protected boolean hasRight (Content content) 157 { 158 if (_rights.isEmpty()) 159 { 160 return true; 161 } 162 163 Set<String> rightsToCheck = _rights.keySet(); 164 for (String rightToCheck : rightsToCheck) 165 { 166 if (StringUtils.isNotEmpty(rightToCheck)) 167 { 168 if (_rightManager.hasRight(_currentUserProvider.getUser(), rightToCheck, content) == RightResult.RIGHT_ALLOW) 169 { 170 return true; 171 } 172 } 173 } 174 175 return false; 176 } 177}