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 |
|
---|
22 | using System;
|
---|
23 | using System.Security;
|
---|
24 | using HeuristicLab.Services.Access;
|
---|
25 | using HeuristicLab.Services.Hive.DataAccess;
|
---|
26 | using HeuristicLab.Services.Hive.DataAccess.Interfaces;
|
---|
27 | using DA = HeuristicLab.Services.Hive.DataAccess;
|
---|
28 | using DT = HeuristicLab.Services.Hive.DataTransfer;
|
---|
29 | using System.Collections.Generic;
|
---|
30 | using System.Linq;
|
---|
31 |
|
---|
32 | namespace 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 | private const string USER_NOT_IDENTIFIED = "User could not be identified";
|
---|
40 | private const string TASK_NOT_EXISTENT = "Queried task could not be found";
|
---|
41 |
|
---|
42 | private IPersistenceManager PersistenceManager {
|
---|
43 | get { return ServiceLocator.Instance.PersistenceManager; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | private IUserManager UserManager {
|
---|
47 | get { return ServiceLocator.Instance.UserManager; }
|
---|
48 | }
|
---|
49 |
|
---|
50 | private IRoleVerifier RoleVerifier {
|
---|
51 | get { return ServiceLocator.Instance.RoleVerifier; }
|
---|
52 | }
|
---|
53 |
|
---|
54 | public void Authorize(Guid userId) {
|
---|
55 | if (userId != ServiceLocator.Instance.UserManager.CurrentUserId)
|
---|
56 | throw new SecurityException(NOT_AUTHORIZED_USERRESOURCE);
|
---|
57 | }
|
---|
58 |
|
---|
59 | public void AuthorizeForTask(Guid taskId, DT.Permission requiredPermission) {
|
---|
60 | if (ServiceLocator.Instance.RoleVerifier.IsInRole(HiveRoles.Slave)) return; // slave-users can access all tasks
|
---|
61 | var pm = PersistenceManager;
|
---|
62 | var taskDao = pm.TaskDao;
|
---|
63 | pm.UseTransaction(() => {
|
---|
64 | var task = taskDao.GetById(taskId);
|
---|
65 | if (task == null) throw new SecurityException(TASK_NOT_EXISTENT);
|
---|
66 | AuthorizeJob(pm, task.JobId, requiredPermission);
|
---|
67 | });
|
---|
68 | }
|
---|
69 |
|
---|
70 | public void AuthorizeForJob(Guid jobId, DT.Permission requiredPermission) {
|
---|
71 | var pm = PersistenceManager;
|
---|
72 | pm.UseTransaction(() => {
|
---|
73 | AuthorizeJob(pm, jobId, requiredPermission);
|
---|
74 | });
|
---|
75 | }
|
---|
76 |
|
---|
77 | // authorize if user is admin or resource owner
|
---|
78 | public void AuthorizeForResourceAdministration(Guid resourceId) {
|
---|
79 | var currentUserId = UserManager.CurrentUserId;
|
---|
80 | var pm = PersistenceManager;
|
---|
81 | var resourceDao = pm.ResourceDao;
|
---|
82 | pm.UseTransaction(() => {
|
---|
83 | var resource = resourceDao.GetById(resourceId);
|
---|
84 | if (resource == null) throw new SecurityException(NOT_AUTHORIZED_USERRESOURCE);
|
---|
85 |
|
---|
86 | if (resource.OwnerUserId != currentUserId
|
---|
87 | && !RoleVerifier.IsInRole(HiveRoles.Administrator)) {
|
---|
88 | throw new SecurityException(NOT_AUTHORIZED_USERRESOURCE);
|
---|
89 | }
|
---|
90 | });
|
---|
91 | }
|
---|
92 |
|
---|
93 | // authorize if user is admin, project owner or owner of a parent project
|
---|
94 | public void AuthorizeForProjectAdministration(Guid projectId) {
|
---|
95 | if (projectId == null) return;
|
---|
96 | var currentUserId = UserManager.CurrentUserId;
|
---|
97 | var pm = PersistenceManager;
|
---|
98 | var projectDao = pm.ProjectDao;
|
---|
99 | pm.UseTransaction(() => {
|
---|
100 | var projectBranch = projectDao.GetCurrentAndParentProjectsById(projectId).ToList();
|
---|
101 | if(!projectBranch.Select(x => x.OwnerUserId).Contains(currentUserId)
|
---|
102 | && !RoleVerifier.IsInRole(HiveRoles.Administrator)) {
|
---|
103 | throw new SecurityException(NOT_AUTHORIZED_USERPROJECT);
|
---|
104 | }
|
---|
105 | });
|
---|
106 | }
|
---|
107 |
|
---|
108 | // authorize if user is admin, or owner of a parent project, for which the resources are assigned to
|
---|
109 | public void AuthorizeForProjectResourceAdministration(Guid projectId, IEnumerable<Guid> resourceIds) {
|
---|
110 | if (projectId == null) return;
|
---|
111 | var currentUserId = UserManager.CurrentUserId;
|
---|
112 | var pm = PersistenceManager;
|
---|
113 | var projectDao = pm.ProjectDao;
|
---|
114 | var resourceDao = pm.ResourceDao;
|
---|
115 | var assignedProjectResourceDao = pm.AssignedProjectResourceDao;
|
---|
116 | pm.UseTransaction(() => {
|
---|
117 | // check if project exists (not necessary)
|
---|
118 | var project = projectDao.GetById(projectId);
|
---|
119 | if (project == null) throw new SecurityException(NOT_AUTHORIZED_USERRESOURCE);
|
---|
120 |
|
---|
121 | // check if resourceIds exist
|
---|
122 | if (resourceIds != null && resourceIds.Any() && !resourceDao.CheckExistence(resourceIds))
|
---|
123 | throw new SecurityException(NOT_AUTHORIZED_USERRESOURCE);
|
---|
124 |
|
---|
125 | // check if user is admin
|
---|
126 | if (RoleVerifier.IsInRole(HiveRoles.Administrator)) return;
|
---|
127 |
|
---|
128 | // check if user is owner of a parent project
|
---|
129 | var projectBranch = projectDao.GetParentProjectsById(projectId).ToList();
|
---|
130 | if (!projectBranch.Select(x => x.OwnerUserId).Contains(currentUserId)
|
---|
131 | && !RoleVerifier.IsInRole(HiveRoles.Administrator)) {
|
---|
132 | throw new SecurityException(NOT_AUTHORIZED_USERPROJECT);
|
---|
133 | }
|
---|
134 |
|
---|
135 | // check if the all argument resourceIds are among the assigned resources of the owned projects
|
---|
136 | var grantedResourceIds = assignedProjectResourceDao.GetAllGrantedResourceIdsOfOwnedParentProjects(projectId, currentUserId).ToList();
|
---|
137 | if(resourceIds.Except(grantedResourceIds).Any()) {
|
---|
138 | throw new SecurityException(NOT_AUTHORIZED_USERRESOURCE);
|
---|
139 | }
|
---|
140 | });
|
---|
141 | }
|
---|
142 |
|
---|
143 | // Check if a project is authorized to use a list of resources
|
---|
144 | public void AuthorizeProjectForResourcesUse(Guid projectId, IEnumerable<Guid> resourceIds) {
|
---|
145 | if (projectId == null || resourceIds == null || !resourceIds.Any()) return;
|
---|
146 | var pm = PersistenceManager;
|
---|
147 | var assignedProjectResourceDao = pm.AssignedProjectResourceDao;
|
---|
148 | if (!assignedProjectResourceDao.CheckProjectGrantedForResources(projectId, resourceIds))
|
---|
149 | throw new SecurityException(NOT_AUTHORIZED_PROJECTRESOURCE);
|
---|
150 | }
|
---|
151 |
|
---|
152 | // Check if current user is authorized to use an explicit project (e.g. in order to add a job)
|
---|
153 | // note: administrators and project owner are NOT automatically granted
|
---|
154 | public void AuthorizeUserForProjectUse(Guid userId, Guid projectId) {
|
---|
155 | if(userId == null || userId == Guid.Empty) {
|
---|
156 | throw new SecurityException(USER_NOT_IDENTIFIED);
|
---|
157 | }
|
---|
158 | if(projectId == null) return;
|
---|
159 |
|
---|
160 | var pm = PersistenceManager;
|
---|
161 | // collect current and group membership Ids
|
---|
162 | var userAndGroupIds = new List<Guid>() { userId };
|
---|
163 | userAndGroupIds.AddRange(UserManager.GetUserGroupIdsOfUser(userId));
|
---|
164 | // perform the actual check
|
---|
165 | var projectPermissionDao = pm.ProjectPermissionDao;
|
---|
166 | if (!projectPermissionDao.CheckUserGrantedForProject(projectId, userAndGroupIds)) {
|
---|
167 | throw new SecurityException(NOT_AUTHORIZED_USERPROJECT);
|
---|
168 | }
|
---|
169 | }
|
---|
170 |
|
---|
171 | private DA.Permission GetPermissionForJob(IPersistenceManager pm, Guid jobId, Guid userId) {
|
---|
172 | var jobDao = pm.JobDao;
|
---|
173 | var jobPermissionDao = pm.JobPermissionDao;
|
---|
174 | var job = jobDao.GetById(jobId);
|
---|
175 | if (job == null) return DA.Permission.NotAllowed;
|
---|
176 | if (job.OwnerUserId == userId) return DA.Permission.Full;
|
---|
177 | var jobPermission = jobPermissionDao.GetByJobAndUserId(jobId, userId);
|
---|
178 | if (jobPermission == null) return DA.Permission.NotAllowed;
|
---|
179 | return jobPermission.Permission;
|
---|
180 | }
|
---|
181 |
|
---|
182 | private void AuthorizeJob(IPersistenceManager pm, Guid jobId, DT.Permission requiredPermission) {
|
---|
183 | var currentUserId = UserManager.CurrentUserId;
|
---|
184 | var requiredPermissionEntity = requiredPermission.ToEntity();
|
---|
185 | DA.Permission permission = GetPermissionForJob(pm, jobId, currentUserId);
|
---|
186 | if (permission == Permission.NotAllowed
|
---|
187 | || ((permission != requiredPermissionEntity) && requiredPermissionEntity == Permission.Full)) {
|
---|
188 | throw new SecurityException(NOT_AUTHORIZED_USERJOB);
|
---|
189 | }
|
---|
190 | }
|
---|
191 | }
|
---|
192 | }
|
---|