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