Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveProjectManagement/HeuristicLab.Services.Hive/3.3/Manager/AuthorizationManager.cs @ 15577

Last change on this file since 15577 was 15577, checked in by jzenisek, 6 years ago

#2839 worked on service side mgmt of project-resource assignments and project-user permissions

File size: 8.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Security;
24using HeuristicLab.Services.Access;
25using HeuristicLab.Services.Hive.DataAccess;
26using HeuristicLab.Services.Hive.DataAccess.Interfaces;
27using DA = HeuristicLab.Services.Hive.DataAccess;
28using DT = HeuristicLab.Services.Hive.DataTransfer;
29using System.Collections.Generic;
30using System.Linq;
31
32namespace HeuristicLab.Services.Hive {
33  public class AuthorizationManager : IAuthorizationManager {
34
35    private const string NOT_AUTHORIZED_USERRESOURCE = "Current user is not authorized to access the requested resource";
36    private const string NOT_AUTHORIZED_USERPROJECT = "Current user is not authorized to access the requested project";
37    private const string NOT_AUTHORIZED_USERJOB = "Current user is not authorized to access the requested job";
38    private const string NOT_AUTHORIZED_PROJECTRESOURCE = "Selected project is not authorized to access the requested resource";
39
40    private IPersistenceManager PersistenceManager {
41      get { return ServiceLocator.Instance.PersistenceManager; }
42    }
43
44    private IUserManager UserManager {
45      get { return ServiceLocator.Instance.UserManager; }
46    }
47
48    private IRoleVerifier RoleVerifier {
49      get { return ServiceLocator.Instance.RoleVerifier; }
50    }
51
52    public void Authorize(Guid userId) {
53      if (userId != ServiceLocator.Instance.UserManager.CurrentUserId)
54        throw new SecurityException(NOT_AUTHORIZED_USERRESOURCE);
55    }
56
57    public void AuthorizeForTask(Guid taskId, DT.Permission requiredPermission) {
58      if (ServiceLocator.Instance.RoleVerifier.IsInRole(HiveRoles.Slave)) return; // slave-users can access all tasks
59      var pm = PersistenceManager;
60      var taskDao = pm.TaskDao;
61      pm.UseTransaction(() => {
62        var task = taskDao.GetById(taskId);
63        if (task == null) throw new SecurityException(NOT_AUTHORIZED_USERRESOURCE);
64        AuthorizeJob(pm, task.JobId, requiredPermission);
65      });
66    }
67
68    public void AuthorizeForJob(Guid jobId, DT.Permission requiredPermission) {
69      var pm = PersistenceManager;
70      pm.UseTransaction(() => {
71        AuthorizeJob(pm, jobId, requiredPermission);
72      });
73    }
74
75    // authorize if user is admin or resource owner
76    public void AuthorizeForResourceAdministration(Guid resourceId) {
77      var currentUserId = UserManager.CurrentUserId;
78      var pm = PersistenceManager;
79      var resourceDao = pm.ResourceDao;
80      pm.UseTransaction(() => {
81        var resource = resourceDao.GetById(resourceId);
82        if (resource == null) throw new SecurityException(NOT_AUTHORIZED_USERRESOURCE);
83
84        if (resource.OwnerUserId != currentUserId
85            && !RoleVerifier.IsInRole(HiveRoles.Administrator)) {
86          throw new SecurityException(NOT_AUTHORIZED_USERRESOURCE);
87        }
88      });
89    }
90
91    // authorize if user is admin, project owner or owner of a parent project
92    public void AuthorizeForProjectAdministration(Guid projectId) {
93      if (projectId == null) return;
94      var currentUserId = UserManager.CurrentUserId;
95      var pm = PersistenceManager;
96      var projectDao = pm.ProjectDao;
97      pm.UseTransaction(() => {
98        // check if project exists (not necessary)
99        var project = projectDao.GetById(projectId);
100        if (project == null) throw new SecurityException(NOT_AUTHORIZED_USERPROJECT);
101
102        var projectBranch = projectDao.GetCurrentAndParentProjectsById(projectId);
103        if(!projectBranch.Select(x => x.OwnerUserId).Contains(currentUserId)
104            && !RoleVerifier.IsInRole(HiveRoles.Administrator)) {
105          throw new SecurityException(NOT_AUTHORIZED_USERPROJECT);
106        }
107      });
108    }
109
110    // authorize if user is admin, or owner of a parent project, for which the resources are assigned to
111    public void AuthorizeForProjectResourceAdministration(Guid projectId, IEnumerable<Guid> resourceIds) {
112      if (projectId == null) return;
113      var currentUserId = UserManager.CurrentUserId;
114      var pm = PersistenceManager;
115      var projectDao = pm.ProjectDao;
116      var resourceDao = pm.ResourceDao;
117      var assignedProjectResourceDao = pm.AssignedProjectResourceDao;
118      pm.UseTransaction(() => {
119        // check if project exists (not necessary)
120        var project = projectDao.GetById(projectId);
121        if (project == null) throw new SecurityException(NOT_AUTHORIZED_USERRESOURCE);
122
123        // check if resourceIds exist
124        if (resourceIds != null && resourceIds.Any() && !resourceDao.CheckExistence(resourceIds))
125          throw new SecurityException(NOT_AUTHORIZED_USERRESOURCE);
126
127        // check if user is admin
128        if (RoleVerifier.IsInRole(HiveRoles.Administrator)) return;
129
130        // check if user is owner of a parent project
131        var projectBranch = projectDao.GetParentProjectsById(projectId);
132        if (!projectBranch.Select(x => x.OwnerUserId).Contains(currentUserId)
133            && !RoleVerifier.IsInRole(HiveRoles.Administrator)) {
134          throw new SecurityException(NOT_AUTHORIZED_USERPROJECT);
135        }
136
137        // check if the all argument resourceIds are among the assigned resources of the owned projects
138        var grantedResourceIds = assignedProjectResourceDao.GetAllGrantedResourceIdsOfOwnedParentProjects(projectId, currentUserId);
139        if(resourceIds.Except(grantedResourceIds).Any()) {
140          throw new SecurityException(NOT_AUTHORIZED_USERRESOURCE);
141        }
142      });
143    }
144
145    // Check if a project is authorized to use a list of resources
146    public void AuthorizeProjectForResourcesUse(Guid projectId, IEnumerable<Guid> resourceIds) {
147      if (projectId == null || resourceIds == null || !resourceIds.Any()) return;
148      var pm = PersistenceManager;
149      var assignedProjectResourceDao = pm.AssignedProjectResourceDao;
150      if (!assignedProjectResourceDao.CheckProjectGrantedForResources(projectId, resourceIds))
151        throw new SecurityException(NOT_AUTHORIZED_PROJECTRESOURCE);
152    }
153
154    // Check if current user is authorized to use an explicit project (e.g. in order to add a job)
155    // note: administrators and project owner are NOT automatically granted
156    public void AuthorizeUserForProjectUse(Guid userId, Guid projectId) {
157      if (userId == null || projectId == null) return;
158      var pm = PersistenceManager;
159      // collect current and group membership Ids
160      var userAndGroupIds = new List<Guid>() { userId };
161      userAndGroupIds.AddRange(UserManager.GetUserGroupIdsOfUser(userId));
162      // perform the actual check
163      var projectPermissionDao = pm.ProjectPermissionDao;
164      if (!projectPermissionDao.CheckUserGrantedForProject(projectId, userAndGroupIds)) {
165        throw new SecurityException(NOT_AUTHORIZED_USERPROJECT);
166      }
167    }
168
169    private DA.Permission GetPermissionForJob(IPersistenceManager pm, Guid jobId, Guid userId) {
170      var jobDao = pm.JobDao;
171      var jobPermissionDao = pm.JobPermissionDao;
172      var job = jobDao.GetById(jobId);
173      if (job == null) return DA.Permission.NotAllowed;
174      if (job.OwnerUserId == userId) return DA.Permission.Full;
175      var jobPermission = jobPermissionDao.GetByJobAndUserId(jobId, userId);
176      if (jobPermission == null) return DA.Permission.NotAllowed;
177      return jobPermission.Permission;
178    }
179
180    private void AuthorizeJob(IPersistenceManager pm, Guid jobId, DT.Permission requiredPermission) {
181      var currentUserId = UserManager.CurrentUserId;
182      var requiredPermissionEntity = requiredPermission.ToEntity();
183      DA.Permission permission = GetPermissionForJob(pm, jobId, currentUserId);
184      if (permission == Permission.NotAllowed
185          || ((permission != requiredPermissionEntity) && requiredPermissionEntity == Permission.Full)) {
186        throw new SecurityException(NOT_AUTHORIZED_USERJOB);
187      }
188    }
189  }
190}
Note: See TracBrowser for help on using the repository browser.