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.skineditor.clientsideelement; 017 018import java.util.Collections; 019import java.util.List; 020import java.util.Map; 021import java.util.stream.Collectors; 022 023import org.apache.avalon.framework.service.ServiceException; 024import org.apache.avalon.framework.service.ServiceManager; 025 026import org.ametys.core.right.RightManager.RightResult; 027import org.ametys.core.ui.StaticClientSideElement; 028import org.ametys.core.user.UserIdentity; 029import org.ametys.plugins.skineditor.skin.SkinDAO; 030import org.ametys.web.skin.Skin; 031import org.ametys.web.skin.SkinsManager; 032 033/** 034 * Client side element to open the skin editor. It is available if at least one skin is modifiable 035 * 036 */ 037public class OpenSkinEditorClientSideElement extends StaticClientSideElement 038{ 039 private SkinsManager _skinManager; 040 041 @Override 042 public void service(ServiceManager smanager) throws ServiceException 043 { 044 super.service(smanager); 045 _skinManager = (SkinsManager) smanager.lookup(SkinsManager.ROLE); 046 } 047 048 @Override 049 public List<Script> getScripts(boolean ignoreRights, Map<String, Object> contextParameters) 050 { 051 if (!_hasModifiableSkin()) 052 { 053 return Collections.EMPTY_LIST; 054 } 055 else 056 { 057 return super.getScripts(ignoreRights, contextParameters); 058 } 059 } 060 061 private boolean _hasModifiableSkin() 062 { 063 UserIdentity user = _currentUserProvider.getUser(); 064 if (_rightManager.hasRight(user, SkinDAO.EDIT_SKINS_RIGHT_ID, "/${WorkspaceName}") == RightResult.RIGHT_ALLOW) 065 { 066 return !_skinManager.getSkins().stream() 067 .map(id -> _skinManager.getSkin(id)) 068 .filter(Skin::isModifiable) 069 .collect(Collectors.toList()).isEmpty(); 070 } 071 else if (_rightManager.hasRight(user, SkinDAO.EDIT_CURRENT_SKIN_RIGHT_ID, "/${WorkspaceName}") == RightResult.RIGHT_ALLOW) 072 { 073 String currentSkinName = _skinManager.getSkinNameFromRequest(); 074 return _skinManager.getSkin(currentSkinName).isModifiable(); 075 } 076 return false; 077 } 078}