001/* 002 * Copyright 2021 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.tasks; 017 018import java.util.HashMap; 019import java.util.Map; 020 021import org.ametys.core.observation.ObservationManager; 022import org.ametys.core.right.RightManager; 023import org.ametys.core.right.RightManager.RightResult; 024import org.ametys.core.ui.Callable; 025import org.ametys.core.user.CurrentUserProvider; 026import org.ametys.core.user.UserIdentity; 027import org.ametys.plugins.explorer.resources.ModifiableResourceCollection; 028import org.ametys.plugins.repository.ModifiableTraversableAmetysObject; 029import org.ametys.plugins.workspaces.project.ProjectManager; 030import org.ametys.plugins.workspaces.project.modules.WorkspaceModuleExtensionPoint; 031import org.ametys.plugins.workspaces.project.objects.Project; 032import org.ametys.runtime.plugin.component.AbstractLogEnabled; 033import org.apache.avalon.framework.component.Component; 034import org.apache.avalon.framework.context.Context; 035import org.apache.avalon.framework.context.ContextException; 036import org.apache.avalon.framework.context.Contextualizable; 037import org.apache.avalon.framework.service.ServiceException; 038import org.apache.avalon.framework.service.ServiceManager; 039import org.apache.avalon.framework.service.Serviceable; 040import org.apache.cocoon.components.ContextHelper; 041import org.apache.cocoon.environment.Request; 042 043/** 044 * DAO for interacting with tasks of a project 045 */ 046public abstract class AbstractWorkspaceTaskDAO extends AbstractLogEnabled implements Serviceable, Component, Contextualizable 047{ 048 /** Rights to handle task */ 049 public static final String RIGHTS_HANDLE_TASK = "Plugin_Workspace_Handle_Task"; 050 051 /** Rights to delete task */ 052 public static final String RIGHTS_DELETE_TASK = "Plugin_Workspace_Delete_Task"; 053 054 /** Rights to comment task */ 055 public static final String RIGHTS_COMMENT_TASK = "Plugin_Workspace_Comment_Task"; 056 057 /** Rights to handle task list */ 058 public static final String RIGHTS_HANDLE_TASK_LIST = "Plugin_Workspace_Handle_Tasks_List"; 059 060 /** Rights to delete task list */ 061 public static final String RIGHTS_DELETE_TASK_LIST = "Plugin_Workspace_Delete_Tasks_List"; 062 063 /** Rights to delete task comment */ 064 public static final String RIGHTS_DELETE_COMMENT_TASK = "Plugin_Workspace_Delete_Comment_Task"; 065 066 /** The right manager */ 067 protected RightManager _rightManager; 068 069 /** The current user provider */ 070 protected CurrentUserProvider _currentUserProvider; 071 072 /** The project manager */ 073 protected ProjectManager _projectManager; 074 075 /** The workspace module EP */ 076 protected WorkspaceModuleExtensionPoint _moduleEP; 077 078 /** The observation manager */ 079 protected ObservationManager _observationManager; 080 081 /** The context */ 082 protected Context _context; 083 084 public void service(ServiceManager manager) throws ServiceException 085 { 086 _rightManager = (RightManager) manager.lookup(RightManager.ROLE); 087 _currentUserProvider = (CurrentUserProvider) manager.lookup(CurrentUserProvider.ROLE); 088 _projectManager = (ProjectManager) manager.lookup(ProjectManager.ROLE); 089 _moduleEP = (WorkspaceModuleExtensionPoint) manager.lookup(WorkspaceModuleExtensionPoint.ROLE); 090 _observationManager = (ObservationManager) manager.lookup(ObservationManager.ROLE); 091 } 092 093 public void contextualize(Context context) throws ContextException 094 { 095 _context = context; 096 } 097 098 /** 099 * Get the project name 100 * @return the project name 101 */ 102 protected String _getProjectName() 103 { 104 Request request = ContextHelper.getRequest(_context); 105 return (String) request.getAttribute("projectName"); 106 } 107 108 /** 109 * Get the sitemap language 110 * @return the sitemap language 111 */ 112 protected String _getSitemapLanguage() 113 { 114 Request request = ContextHelper.getRequest(_context); 115 return (String) request.getAttribute("sitemapLanguage"); 116 } 117 118 /** 119 * Get the site name 120 * @return the site name 121 */ 122 protected String _getSiteName() 123 { 124 Request request = ContextHelper.getRequest(_context); 125 return (String) request.getAttribute("siteName"); 126 } 127 128 /** 129 * Get the module root of task 130 * @param projectName the project name 131 * @return the module root 132 */ 133 protected ModifiableResourceCollection _getModuleRoot(String projectName) 134 { 135 Project project = _projectManager.getProject(projectName); 136 137 TasksWorkspaceModule taskModule = _moduleEP.getModule(TasksWorkspaceModule.TASK_MODULE_ID); 138 return taskModule.getModuleRoot(project, false); 139 } 140 141 /** 142 * Get the tasks lists root 143 * @param projectName the project name 144 * @return the tasks lists root 145 */ 146 protected ModifiableTraversableAmetysObject _getTasksListsRoot(String projectName) 147 { 148 Project project = _projectManager.getProject(projectName); 149 150 TasksWorkspaceModule taskModule = _moduleEP.getModule(TasksWorkspaceModule.TASK_MODULE_ID); 151 return taskModule.getTasksListsRoot(project, true); 152 } 153 154 /** 155 * Get the tasks root 156 * @param projectName the project name 157 * @return the tasks root 158 */ 159 protected ModifiableTraversableAmetysObject _getTasksRoot(String projectName) 160 { 161 Project project = _projectManager.getProject(projectName); 162 163 TasksWorkspaceModule taskModule = _moduleEP.getModule(TasksWorkspaceModule.TASK_MODULE_ID); 164 return taskModule.getTasksRoot(project, true); 165 } 166 167 /** 168 * Get user rights from project name 169 * @param projectName the project name 170 * @return the user rights 171 */ 172 @Callable 173 public Map<String, Object> getUserRights(String projectName) 174 { 175 Map<String, Object> results = new HashMap<>(); 176 177 ModifiableTraversableAmetysObject tasksRoot = _getTasksRoot(projectName); 178 ModifiableTraversableAmetysObject tasksListsRoot = _getTasksListsRoot(projectName); 179 180 UserIdentity user = _currentUserProvider.getUser(); 181 results.put("canWriteTaskList", _rightManager.hasRight(user, RIGHTS_HANDLE_TASK_LIST, tasksListsRoot) == RightResult.RIGHT_ALLOW); 182 results.put("canDeleteTaskList", _rightManager.hasRight(user, RIGHTS_DELETE_TASK_LIST, tasksListsRoot) == RightResult.RIGHT_ALLOW); 183 results.put("canWriteTask", _rightManager.hasRight(user, RIGHTS_HANDLE_TASK, tasksRoot) == RightResult.RIGHT_ALLOW); 184 results.put("canDeleteTask", _rightManager.hasRight(user, RIGHTS_DELETE_TASK, tasksRoot) == RightResult.RIGHT_ALLOW); 185 results.put("canCommentTask", _rightManager.hasRight(user, RIGHTS_COMMENT_TASK, tasksRoot) == RightResult.RIGHT_ALLOW); 186 results.put("canDeleteCommentTask", _rightManager.hasRight(user, RIGHTS_DELETE_COMMENT_TASK, tasksRoot) == RightResult.RIGHT_ALLOW); 187 188 return results; 189 } 190}