001/* 002 * Copyright 2023 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.workspaces.initialization; 017 018import java.util.HashMap; 019import java.util.Map; 020import java.util.Optional; 021import java.util.Set; 022 023import javax.jcr.RepositoryException; 024import javax.jcr.Value; 025 026import org.apache.avalon.framework.service.ServiceException; 027import org.apache.avalon.framework.service.ServiceManager; 028import org.apache.jackrabbit.value.StringValue; 029 030import org.ametys.cms.rights.Content2ContentTypeRightContextConvertor; 031import org.ametys.core.observation.Event; 032import org.ametys.core.right.ProfileAssignmentStorageExtensionPoint; 033import org.ametys.core.right.RightManager; 034import org.ametys.plugins.repository.AmetysObjectResolver; 035import org.ametys.plugins.repository.jcr.JCRAmetysObject; 036import org.ametys.plugins.userdirectory.UserDirectoryPageHandler; 037import org.ametys.plugins.userdirectory.page.VirtualUserDirectoryPageFactory; 038import org.ametys.plugins.workspaces.WorkspacesConstants; 039import org.ametys.web.repository.page.ModifiablePage; 040import org.ametys.web.repository.page.ModifiableSitemapElement; 041import org.ametys.web.repository.page.Page; 042 043/** 044 * Configure a user directory root page during initialization. 045 */ 046public class UserDirectoryPageInitializer extends DefaultStaticPageInitializer 047{ 048 /** the profile assignment storage EP */ 049 protected ProfileAssignmentStorageExtensionPoint _profileAssignmentStorageEP; 050 051 @Override 052 public void service(ServiceManager manager) throws ServiceException 053 { 054 super.service(manager); 055 _profileAssignmentStorageEP = (ProfileAssignmentStorageExtensionPoint) manager.lookup(ProfileAssignmentStorageExtensionPoint.ROLE); 056 } 057 058 @Override 059 public Optional< ? extends Page> createPage(ModifiableSitemapElement parent) 060 { 061 Optional< ? extends Page> result = super.createPage(parent); 062 if (result.isPresent() && result.get() instanceof ModifiablePage page) 063 { 064 try 065 { 066 // FIXME isn't there a better way to set the user directory root ? 067 if (page instanceof JCRAmetysObject jcrPage) 068 { 069 StringValue virtualUserDirectoryPageFactoryClassName = new StringValue(VirtualUserDirectoryPageFactory.class.getName()); 070 jcrPage.getNode().setProperty(AmetysObjectResolver.VIRTUAL_PROPERTY, new Value[] {virtualUserDirectoryPageFactoryClassName}); 071 } 072 073 page.setValue(UserDirectoryPageHandler.CONTENT_TYPE_DATA_NAME, WorkspacesConstants.MEMBER_CONTENT_TYPE_ID); 074 page.setValue(UserDirectoryPageHandler.USER_VIEW_NAME, "main"); 075 page.setValue(UserDirectoryPageHandler.CLASSIFICATION_ATTRIBUTE_DATA_NAME, "lastname"); 076 page.setValue(UserDirectoryPageHandler.DEPTH_DATA_NAME, 2); 077 078 page.saveChanges(); 079 080 Map<String, Object> eventParams = new HashMap<>(); 081 eventParams.put(org.ametys.web.ObservationConstants.ARGS_PAGE, page); 082 _observationManager.notify(new Event(org.ametys.web.ObservationConstants.EVENT_PAGE_CHANGED, _currentUserProvider.getUser(), eventParams)); 083 084 eventParams.put(org.ametys.plugins.userdirectory.observation.ObservationConstants.ARGS_USER_CONTENT_VIEW_UPDATED, true); 085 _observationManager.notify(new Event(org.ametys.plugins.userdirectory.observation.ObservationConstants.EVENT_USER_DIRECTORY_ROOT_UPDATED, _currentUserProvider.getUser(), eventParams)); 086 087 // Set read right on member content type for any connected user 088 String contentTypeContext = Content2ContentTypeRightContextConvertor.CONTENT_TYPE_RIGHT_CONTEXT_PREFIX + "/" + page.getSiteName() + "/" + WorkspacesConstants.MEMBER_CONTENT_TYPE_ID; 089 _profileAssignmentStorageEP.allowProfileToAnyConnectedUser(RightManager.READER_PROFILE_ID, contentTypeContext); 090 091 eventParams = new HashMap<>(); 092 eventParams.put(org.ametys.core.ObservationConstants.ARGS_ACL_CONTEXT, contentTypeContext); 093 eventParams.put(org.ametys.core.ObservationConstants.ARGS_ACL_PROFILES, Set.of(RightManager.READER_PROFILE_ID)); 094 095 _observationManager.notify(new Event(org.ametys.core.ObservationConstants.EVENT_ACL_UPDATED, _currentUserProvider.getUser(), eventParams)); 096 } 097 catch (RepositoryException e) 098 { 099 getLogger().error("Failed to set user directory root property", e); 100 } 101 } 102 return result; 103 } 104}