Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Security.Core/3.2/PermissionManager.cs @ 1624

Last change on this file since 1624 was 1624, checked in by asimon, 15 years ago

SecurityManager revised and PermissionManager implemented (ticket #537)

File size: 3.7 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Text;
4using System.Threading;
5using HeuristicLab.Security.Contracts.Interfaces;
6using HeuristicLab.Security.Contracts.BusinessObjects;
7using HeuristicLab.Security.DataAccess;
8using HeuristicLab.DataAccess.Interfaces;
9using HeuristicLab.PluginInfrastructure;
10
11namespace HeuristicLab.Security.Core {
12  public class PermissionManager : IPermissionManager{
13
14    private static ISessionFactory factory = (new DiscoveryService()).GetInstances<ISessionFactory>()[0];
15
16    private static ISession session;
17   
18    private static IDictionary<Guid,string> currentSessions = new Dictionary<Guid, string>();
19    Object locker = new Object();
20
21 
22   /// <summary>
23   /// If a session exists for this userName then it is returned, otherwise the given password
24   /// is checked and a new session is created.
25   /// </summary>
26   /// <param name="userName"></param>
27   /// <param name="password"></param>
28   /// <returns></returns>
29    public Guid Authenticate(String userName, String password) {
30      lock (locker)
31        if (currentSessions.Values.Contains(userName))
32          return GetGuid(userName);
33      try {
34        session = factory.GetSessionForCurrentThread();
35
36        IUserAdapter userAdapter = session.GetDataAdapter<User, IUserAdapter>();
37        User user = userAdapter.GetByName(userName);
38
39        if (user.Password.CompareTo(password) == 0) {
40          Guid newSessionId = Guid.NewGuid();
41          lock (locker)
42            currentSessions.Add(newSessionId, userName);
43          return newSessionId;
44        } else return Guid.Empty;
45      }
46      finally {
47        if (session != null)
48          session.EndSession();
49      }
50    }
51
52    /// <summary>
53    /// Checks if the owner of the given session has the given permission.
54    /// </summary>
55    /// <param name="sessionId"></param>
56    /// <param name="permissionId"></param>
57    /// <param name="entityId"></param>
58    /// <returns></returns>
59    public bool CheckPermission(Guid sessionId, Guid permissionId, Guid entityId) {
60      string userName;
61      bool existsSession;
62      lock (locker)
63        existsSession = currentSessions.TryGetValue(sessionId, out userName);
64      if (existsSession) {
65        try {
66          session = factory.GetSessionForCurrentThread();
67         
68          IPermissionOwnerAdapter permOwnerAdapter = session.GetDataAdapter<PermissionOwner, IPermissionOwnerAdapter>();
69          PermissionOwner permOwner = permOwnerAdapter.GetByName(userName);
70
71          IPermissionAdapter permissionAdapter = session.GetDataAdapter<Permission, IPermissionAdapter>();
72          Permission permission = permissionAdapter.GetById(permissionId);
73         
74          if ((permission != null) && (permOwner != null))
75            return (permissionAdapter.getPermission(permOwner, permission,entityId) != null);
76          else return false;
77        }
78        finally {
79          if (session != null)
80            session.EndSession();
81        }
82      } else return false;
83    }
84
85    /// <summary>
86    /// Removes the given session.
87    /// </summary>
88    /// <param name="sessionId"></param>
89    public void EndSession(Guid sessionId) {
90      lock (locker) {
91        if (currentSessions.Keys.Contains(sessionId))
92          currentSessions.Remove(sessionId);
93      }
94    }
95
96    /// <summary>
97    /// Gets the sessionId for a user.
98    /// </summary>
99    /// <param name="userName"></param>
100    /// <returns></returns>
101    public Guid GetGuid(string userName) {
102      foreach (Guid guid in currentSessions.Keys)
103        if (currentSessions[guid].CompareTo(userName) == 0)
104          return guid;
105      return Guid.Empty;
106    }
107  }
108}
Note: See TracBrowser for help on using the repository browser.