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.plugins.userdirectory.page;
017
018import java.util.ArrayList;
019import java.util.List;
020import java.util.Map;
021import java.util.Set;
022
023import org.apache.commons.lang3.LocaleUtils;
024import org.apache.commons.lang3.StringUtils;
025
026import org.ametys.cms.repository.Content;
027import org.ametys.core.group.GroupIdentity;
028import org.ametys.core.right.ProfileAssignmentStorage.AnonymousOrAnyConnectedKeys;
029import org.ametys.core.right.ProfileAssignmentStorage.UserOrGroup;
030import org.ametys.core.right.RightManager;
031import org.ametys.core.user.UserIdentity;
032import org.ametys.plugins.contentio.synchronize.SynchronizableContentsCollection;
033import org.ametys.plugins.repository.ACLAmetysObject;
034import org.ametys.plugins.repository.AmetysObject;
035import org.ametys.plugins.repository.AmetysObjectIterable;
036import org.ametys.plugins.repository.AmetysRepositoryException;
037import org.ametys.plugins.repository.CollectionIterable;
038import org.ametys.plugins.repository.UnknownAmetysObjectException;
039import org.ametys.web.repository.page.Page;
040import org.ametys.web.repository.page.virtual.AbstractConfigurableVirtualPage;
041import org.ametys.web.repository.page.virtual.VirtualPageConfiguration;
042
043/**
044 * Page representing a second-level page.
045 */
046public class UserPage extends AbstractConfigurableVirtualPage<UserPageFactory> implements ACLAmetysObject
047{
048    private int _initialDepth;
049    private String _title;
050    private String _path;
051    private Content _syncContent;
052    
053    /**
054     * Constructor.
055     * @param root the root page.
056     * @param syncContent the synchronized content
057     * @param path the path
058     * @param configuration The abstract virtual page's configuration
059     * @param scheme The scheme
060     * @param factory The user page factory
061     */
062    public UserPage(Page root, VirtualPageConfiguration configuration, String scheme, UserPageFactory factory, Content syncContent, String path)
063    {
064        super(root, configuration, scheme, factory);
065        
066        _path = path;
067        _syncContent = syncContent;
068        
069        _title = _syncContent.getTitle(LocaleUtils.toLocale(root.getSitemapName()));
070        _initialDepth = _factory.getUserDirectoryPageHandler().getDepth(_root);
071    }
072
073    /**
074     * Compute a page id
075     * @param path The path
076     * @param rootId The root page id
077     * @param contentId The content id
078     * @return The id
079     */
080    public static String getId(String path, String rootId, String contentId)
081    {
082        // E.g: uduser://path?rootId=...&contentId=...
083        return "uduser://" + (StringUtils.isEmpty(path) ? "_root" : path) + "?rootId=" + rootId + "&contentId=" + contentId;
084    }
085    
086    /**
087     * Returns the associated synchronizable {@link Content}.
088     * @return the associated synchronizable {@link Content}.
089     */
090    @SuppressWarnings("unchecked")
091    @Override
092    public Content getContent()
093    {
094        return _syncContent;
095    }
096    
097    
098    public int getDepth() throws AmetysRepositoryException
099    {
100        return _root.getDepth() + _initialDepth + 1;
101    }
102
103    @Override
104    public Set<String> getReferers() throws AmetysRepositoryException
105    {
106        return null;
107    }
108
109    public String getTitle() throws AmetysRepositoryException
110    {
111        return _title;
112    }
113    
114    
115    public String getLongTitle() throws AmetysRepositoryException
116    {
117        return _title;
118    }
119    
120    public AmetysObjectIterable<? extends Page> getChildrenPages() throws AmetysRepositoryException
121    {
122        List<Page> children = new ArrayList<>();
123        return new CollectionIterable<>(children);
124    }
125
126    
127    public String getPathInSitemap() throws AmetysRepositoryException
128    {
129        if (_path.equals("_root"))
130        {
131            return _root.getPathInSitemap() + "/" + getName();
132        }
133        else
134        {
135            String path = StringUtils.lowerCase(_path);
136            return _root.getPathInSitemap() + "/" + path + "/" + getName();
137        }
138    }
139    
140    public <A extends AmetysObject> A getChild(String path) throws AmetysRepositoryException, UnknownAmetysObjectException
141    {
142        if (path.isEmpty())
143        {
144            throw new AmetysRepositoryException("path must be non empty");
145        }
146        
147        return null;
148    }
149
150    @SuppressWarnings("unchecked")
151    
152    public AmetysObjectIterable<? extends AmetysObject> getChildren() throws AmetysRepositoryException
153    {
154        return getChildrenPages();
155    }
156
157    
158    public boolean hasChild(String name) throws AmetysRepositoryException
159    {
160        return false;
161    }
162    
163    
164    public String getId() throws AmetysRepositoryException
165    {
166        return getId(_path, _root.getId(), _syncContent.getId());
167    }
168
169    
170    public String getName() throws AmetysRepositoryException
171    {
172        return _syncContent.getName();
173    }
174    
175    @SuppressWarnings("unchecked")
176    
177    public Page getParent() throws AmetysRepositoryException
178    {
179        if (_initialDepth > 0)
180        {
181            String pathName = StringUtils.substringAfterLast(_path, "/");
182            String name = _factory.getUserDirectoryPageHandler().getName(pathName);
183            
184            return _factory.getTransitionalPageFactory().createTransitionalPage(_root, name, _path);
185        }
186        else
187        {
188            return _root;
189        }
190    }
191
192    
193    public String getParentPath() throws AmetysRepositoryException
194    {
195        if (_initialDepth > 0)
196        {
197            String path = StringUtils.lowerCase(_path);
198            return _root.getPath() + "/" + path;
199        }
200        else
201        {
202            return _root.getPath();
203        }
204    }
205
206    public AmetysObjectIterable< ? extends Page> getChildrenPages(boolean includeInvisiblePage) throws AmetysRepositoryException
207    {
208        List<Page> children = new ArrayList<>();
209        return new CollectionIterable<>(children);
210    }
211
212    @Override
213    public boolean isVisible() throws AmetysRepositoryException
214    {
215        return false;
216    }
217
218    
219    public Page getChildPageAt(int index) throws UnknownAmetysObjectException, AmetysRepositoryException
220    {
221        throw new UnknownAmetysObjectException("There is no child for user page");
222    }
223
224    public Map<AnonymousOrAnyConnectedKeys, Set<String>> getProfilesForAnonymousAndAnyConnectedUser()
225    {
226        Set<String> collectionIds = _factory.getSccHelper().getSynchronizableCollectionIds(_syncContent);
227        for (String collectionId : collectionIds)
228        {
229            SynchronizableContentsCollection collection = _factory.getSynchronizableContentsCollectionDAO().getSynchronizableContentsCollection(collectionId);
230            if (collection != null)
231            {
232                String restrictedField = collection.getRestrictedField();
233                if (!StringUtils.isEmpty(restrictedField))
234                {
235                    Boolean restricted = _syncContent.getValue(restrictedField);
236                    if (restricted != null && restricted.booleanValue())
237                    {
238                        return Map.of(AnonymousOrAnyConnectedKeys.ANONYMOUS_DENIED, Set.of(RightManager.READER_PROFILE_ID));
239                    }
240                }
241            }
242        }
243        
244        return Map.of();
245    }
246    
247    public Map<GroupIdentity, Map<UserOrGroup, Set<String>>> getProfilesForGroups(Set<GroupIdentity> groups)
248    {
249        return Map.of();
250    }
251    
252    public Map<UserIdentity, Map<UserOrGroup, Set<String>>> getProfilesForUsers(UserIdentity user)
253    {
254        return Map.of();
255    }
256    
257    public boolean isInheritanceDisallowed()
258    {
259        return false;
260    }
261}