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.right.RightManager.RightResult;
022import org.ametys.core.ui.Callable;
023import org.ametys.core.user.UserIdentity;
024import org.ametys.plugins.explorer.resources.ModifiableResourceCollection;
025import org.ametys.plugins.repository.ModifiableTraversableAmetysObject;
026import org.ametys.plugins.workspaces.AbstractWorkspaceDAO;
027import org.ametys.plugins.workspaces.project.objects.Project;
028
029/**
030 * DAO for interacting with tasks of a project
031 */
032public abstract class AbstractWorkspaceTaskDAO extends AbstractWorkspaceDAO
033{
034    /** Rights to handle task */
035    public static final String RIGHTS_HANDLE_TASK = "Plugin_Workspace_Handle_Task";
036    
037    /** Rights to delete task */
038    public static final String RIGHTS_DELETE_TASK = "Plugin_Workspace_Delete_Task";
039    
040    /** Rights to comment task */
041    public static final String RIGHTS_COMMENT_TASK = "Plugin_Workspace_Comment_Task";
042    
043    /** Rights to handle task list */
044    public static final String RIGHTS_HANDLE_TASK_LIST = "Plugin_Workspace_Handle_Tasks_List";
045    
046    /** Rights to delete task  list */
047    public static final String RIGHTS_DELETE_TASK_LIST = "Plugin_Workspace_Delete_Tasks_List";
048    
049    /** Rights to delete task comment */
050    public static final String RIGHTS_DELETE_COMMENT_TASK = "Plugin_Workspace_Delete_Comment_Task";
051    
052    /**
053     * Get the module root of task
054     * @param projectName the project name
055     * @return the module root
056     */
057    protected ModifiableResourceCollection _getModuleRoot(String projectName)
058    {
059        Project project = _projectManager.getProject(projectName);
060        
061        TasksWorkspaceModule taskModule = _workspaceModuleEP.getModule(TasksWorkspaceModule.TASK_MODULE_ID);
062        return taskModule.getModuleRoot(project, false);
063    }
064    
065    /**
066     * Get the tasks lists root
067     * @param projectName the project name
068     * @return the tasks lists root
069     */
070    protected ModifiableTraversableAmetysObject _getTasksListsRoot(String projectName)
071    {
072        Project project = _projectManager.getProject(projectName);
073        
074        TasksWorkspaceModule taskModule = _workspaceModuleEP.getModule(TasksWorkspaceModule.TASK_MODULE_ID);
075        return taskModule.getTasksListsRoot(project, true);
076    }
077    
078    /**
079     * Get the tasks root
080     * @param projectName the project name
081     * @return the tasks root
082     */
083    protected ModifiableTraversableAmetysObject _getTasksRoot(String projectName)
084    {
085        Project project = _projectManager.getProject(projectName);
086        
087        TasksWorkspaceModule taskModule = _workspaceModuleEP.getModule(TasksWorkspaceModule.TASK_MODULE_ID);
088        return taskModule.getTasksRoot(project, true);
089    }
090    
091    /**
092     * Get user rights from project name
093     * @param projectName the project name
094     * @return the user rights
095     */
096    @Callable
097    public Map<String, Object> getUserRights(String projectName)
098    {
099        Map<String, Object> results = new HashMap<>();
100        
101        ModifiableTraversableAmetysObject tasksRoot = _getTasksRoot(projectName);
102        ModifiableTraversableAmetysObject tasksListsRoot = _getTasksListsRoot(projectName);
103        
104        UserIdentity user = _currentUserProvider.getUser();
105        results.put("canWriteTaskList", _rightManager.hasRight(user, RIGHTS_HANDLE_TASK_LIST, tasksListsRoot) == RightResult.RIGHT_ALLOW);
106        results.put("canDeleteTaskList", _rightManager.hasRight(user, RIGHTS_DELETE_TASK_LIST, tasksListsRoot) == RightResult.RIGHT_ALLOW);
107        results.put("canWriteTask", _rightManager.hasRight(user, RIGHTS_HANDLE_TASK, tasksRoot) == RightResult.RIGHT_ALLOW);
108        results.put("canDeleteTask", _rightManager.hasRight(user, RIGHTS_DELETE_TASK, tasksRoot) == RightResult.RIGHT_ALLOW);
109        results.put("canCommentTask", _rightManager.hasRight(user, RIGHTS_COMMENT_TASK, tasksRoot) == RightResult.RIGHT_ALLOW);
110        results.put("canDeleteCommentTask", _rightManager.hasRight(user, RIGHTS_DELETE_COMMENT_TASK, tasksRoot) == RightResult.RIGHT_ALLOW);
111        
112        return results;
113    }
114
115}