1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Security.Contracts.Interfaces;
|
---|
6 | using HeuristicLab.DataAccess;
|
---|
7 | using HeuristicLab.Security.Contracts.BusinessObjects;
|
---|
8 | using HeuristicLab.Hive.Contracts.BusinessObjects;
|
---|
9 | using HeuristicLab.Hive.Contracts.Interfaces;
|
---|
10 | using HeuristicLab.Hive.Contracts;
|
---|
11 | using HeuristicLab.Hive.Server.Core.InternalInterfaces;
|
---|
12 |
|
---|
13 | namespace HeuristicLab.Hive.Server.Core {
|
---|
14 | public class HivePermissionManager : IHivePermissionManager {
|
---|
15 |
|
---|
16 | private IPermissionManager permManager = ServiceLocator.GetPermissionManager();
|
---|
17 |
|
---|
18 | private IClientManager clientManager = ServiceLocator.GetClientManager();
|
---|
19 |
|
---|
20 | /// <summary>
|
---|
21 | /// Authenticates a user and returns a valid guid if success.
|
---|
22 | /// </summary>
|
---|
23 | /// <param name="username"></param>
|
---|
24 | /// <param name="password"></param>
|
---|
25 | /// <returns></returns>
|
---|
26 | public Guid Login(string username, string password) {
|
---|
27 | return permManager.Authenticate(username, password);
|
---|
28 | }
|
---|
29 |
|
---|
30 | /// <summary>
|
---|
31 | /// Checks if a user has permission for a specified action.
|
---|
32 | /// </summary>
|
---|
33 | /// <param name="sessionID">The current session.</param>
|
---|
34 | /// <param name="actionID">A pre-defined action that requires permission.</param>
|
---|
35 | /// <param name="entityID">A resource (Job,...)</param>
|
---|
36 | /// <returns></returns>
|
---|
37 | public bool CheckPermission(Guid sessionID, Guid actionID, Guid entityId) {
|
---|
38 | bool hasPerm = permManager.CheckPermission(sessionID, actionID, entityId);
|
---|
39 |
|
---|
40 | if (!hasPerm) {
|
---|
41 | if ((actionID == PermissiveSecurityAction.Add_Job) ||
|
---|
42 | (actionID == PermissiveSecurityAction.Remove_Job) ||
|
---|
43 | (actionID == PermissiveSecurityAction.Request_Snapshot)||
|
---|
44 | (actionID==PermissiveSecurityAction.Abort_Job))
|
---|
45 | if (CheckPermissionHelper(sessionID, actionID, entityId)) return true;
|
---|
46 | }
|
---|
47 |
|
---|
48 | return hasPerm;
|
---|
49 | }
|
---|
50 |
|
---|
51 | private bool CheckPermissionHelper(Guid sessionId, Guid actionId, Guid entityId) {
|
---|
52 | IList<ClientGroup> groups = clientManager.GetAllGroupsOfResource(entityId).Obj;
|
---|
53 | foreach (ClientGroup group in groups)
|
---|
54 | if (CheckPermission(sessionId, actionId, group.Id)) return true;
|
---|
55 | return false;
|
---|
56 | }
|
---|
57 | }
|
---|
58 | }
|
---|