Free cookie consent management tool by TermsFeed Policy Generator

Changeset 1624


Ignore:
Timestamp:
04/22/09 18:20:46 (16 years ago)
Author:
asimon
Message:

SecurityManager revised and PermissionManager implemented (ticket #537)

Location:
trunk/sources
Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Security.Contracts/3.2/BusinessObjects/Permission.cs

    r1384 r1624  
    3232    public String Description { get; set; }
    3333
    34     [DataMember]
    35     public Guid EntityId { get; set; }
    3634  }
    3735}
  • trunk/sources/HeuristicLab.Security.Contracts/3.2/BusinessObjects/PermissionOwner.cs

    r1384 r1624  
    3232    public string Name { get; set; }
    3333
    34     [DataMember]
    35     public Guid EntityId { get; set; }
    36   }
     34 }
    3735}
  • trunk/sources/HeuristicLab.Security.Contracts/3.2/Interfaces/ISecurityManager.cs

    r1520 r1624  
    2020
    2121    [OperationContract]
     22    ICollection<User> GetAllUsers();
     23
     24    [OperationContract]
     25    User GetUserByName(string name);
     26
     27    [OperationContract]
    2228    UserGroup AddNewUserGroup(UserGroup group);
    2329
    2430    [OperationContract]
    25     bool RemoveUserGroup(UserGroup group);
     31    bool RemoveUserGroup(Guid userGroupId);
    2632
    2733    [OperationContract]
     
    2935
    3036    [OperationContract]
    31     bool AddPermissionOwnerToGroup(UserGroup userGroup, PermissionOwner permissionOwner);
     37    ICollection<UserGroup> GetAllUserGroups();
    3238
    3339    [OperationContract]
    34     bool RemovePermissionOwnerFromGroup(UserGroup userGroup, PermissionOwner permissionOwner);
     40    UserGroup GetUserGroupByName(string name);
    3541
    3642    [OperationContract]
    37     bool GrantPermission(PermissionOwner permissionOwner, Permission permission);
     43    PermissionOwner UpdatePermissionOwner(PermissionOwner permissionOwner);
    3844
    3945    [OperationContract]
    40     bool RevokePermission(PermissionOwner permissionOwner, Permission permission);
     46    bool AddPermissionOwnerToGroup(Guid userGroupId, Guid permissionOwnerId);
     47
     48    [OperationContract]
     49    bool RemovePermissionOwnerFromGroup(Guid userGroupId, Guid permissionOwnerId);
     50
     51    [OperationContract]
     52    bool GrantPermission(Guid permissionOwnerId, Guid permissionId, Guid entityId);
     53
     54    [OperationContract]
     55    Permission GetPermissionById(Guid permissionId);
     56
     57    [OperationContract]
     58    bool RevokePermission(Guid permissionOwnerId, Guid permissionId, Guid entityId);
    4159  }
    4260}
  • trunk/sources/HeuristicLab.Security.Core/3.2/HeuristicLab.Security.Core-3.2.csproj

    r1534 r1624  
    4646  </ItemGroup>
    4747  <ItemGroup>
     48    <Compile Include="PermissionManager.cs" />
    4849    <Compile Include="SecurityCorePlugin.cs" />
    4950    <Compile Include="Properties\AssemblyInfo.cs" />
     51    <Compile Include="SecurityManager.cs" />
    5052  </ItemGroup>
    5153  <ItemGroup>
     54    <ProjectReference Include="..\..\HeuristicLab.DataAccess\3.2\HeuristicLab.DataAccess-3.2.csproj">
     55      <Project>{9076697B-C151-46CD-95BC-1D059492B478}</Project>
     56      <Name>HeuristicLab.DataAccess-3.2</Name>
     57    </ProjectReference>
    5258    <ProjectReference Include="..\..\HeuristicLab.PluginInfrastructure\HeuristicLab.PluginInfrastructure.csproj">
    5359      <Project>{94186A6A-5176-4402-AE83-886557B53CCA}</Project>
  • trunk/sources/HeuristicLab.Security.Core/3.2/SecurityManager.cs

    r1530 r1624  
    77using HeuristicLab.Security.DataAccess;
    88using HeuristicLab.PluginInfrastructure;
     9using HeuristicLab.DataAccess.Interfaces;
    910
    1011namespace HeuristicLab.Security.Core {
    1112  public class SecurityManager : ISecurityManager {
    1213
    13     private static DiscoveryService discoveryService =
    14       new DiscoveryService();
    15 
    16     private static IUserAdapter userAdapter = discoveryService.GetInstances<IUserAdapter>()[0];
    17     private static IUserGroupAdapter userGroupAdapter = discoveryService.GetInstances<IUserGroupAdapter>()[0];
    18     private static IPermissionOwnerAdapter permOwnerAdapter = discoveryService.GetInstances<IPermissionOwnerAdapter>()[0];
    19     private static IPermissionAdapter permissionAdapter = discoveryService.GetInstances<IPermissionAdapter>()[0];
    20 
     14    private static ISessionFactory factory = (new DiscoveryService()).GetInstances<ISessionFactory>()[0];
     15
     16    private ISession session;
     17
     18   /// <summary>
     19    /// Add new user.
     20    /// </summary>
     21    /// <param name="user"></param>
     22    /// <returns></returns>
    2123    public User AddNewUser(User user) {
    22       userAdapter.Update(user);
    23       return user;
    24     }
    25 
     24      try {
     25        session = factory.GetSessionForCurrentThread();
     26
     27        IUserAdapter userAdapter = session.GetDataAdapter<User, IUserAdapter>();
     28        if (user != null)
     29          userAdapter.Update(user);
     30
     31        return user;
     32      }
     33      finally {
     34        if (session != null)
     35          session.EndSession();
     36      }
     37    }
     38
     39    /// <summary>
     40    /// Update user.
     41    /// </summary>
     42    /// <param name="user"></param>
     43    /// <returns></returns>
    2644    public User UpdateUser(User user) {
    27       userAdapter.Update(user);
    28       return user;
    29     }
    30 
     45      try {
     46        session = factory.GetSessionForCurrentThread();
     47        IUserAdapter userAdapter = session.GetDataAdapter<User, IUserAdapter>();
     48
     49        if (user != null)
     50          userAdapter.Update(user);
     51        return user;
     52      }
     53      finally {
     54        if (session != null)
     55          session.EndSession();
     56      }
     57    }
     58
     59   /// <summary>
     60    /// Remove user.
     61   /// </summary>
     62   /// <param name="userId"></param>
     63   /// <returns></returns>
    3164    public bool RemoveUser(Guid userId) {
    32       User user = userAdapter.GetById(userId);
    33       if ( user != null )       // do we check this ?
    34         return userAdapter.Delete(user);
    35       return false;
    36     }
    37 
    38     public UserGroup AddNewUserGroup(UserGroup group) {
    39       userGroupAdapter.Update(group);
    40       return group;
    41     }
    42 
    43     public UserGroup UpdateUserGroup(UserGroup group) {
    44       userGroupAdapter.Update(group);
    45       return group;
    46     }
    47 
    48     public bool RemoveUserGroup(Guid groupId) {
    49       UserGroup userGroup = userGroupAdapter.GetById(groupId);
    50       if (userGroup != null)
    51         return userGroupAdapter.Delete(userGroup);
    52       return false;
    53     }
    54 
    55     public bool AddPermissionOwnerToGroup(Guid groupId, Guid permissionOwnerId) {
    56       UserGroup userGroup = userGroupAdapter.GetById(groupId);
    57       PermissionOwner permissionOwner = permOwnerAdapter.GetById(permissionOwnerId);
    58       userGroup.Members.Add(permissionOwner);
    59       userGroupAdapter.Update(userGroup);
    60       return true;
    61     }
    62 
    63     public bool RemovePermissionOwnerFromGroup(Guid groupId, Guid permissionOwnerId) {
    64       UserGroup userGroup = userGroupAdapter.GetById(groupId);
    65       PermissionOwner permissionOwner = permOwnerAdapter.GetById(permissionOwnerId);
    66       userGroup.Members.Add(permissionOwner);
    67       userGroupAdapter.Delete(userGroup);
    68       return true;
    69     }
    70 
     65      try {
     66        session = factory.GetSessionForCurrentThread();
     67        IUserAdapter userAdapter = session.GetDataAdapter<User, IUserAdapter>();
     68        User user = userAdapter.GetById(userId);
     69
     70        if (user != null)
     71          return userAdapter.Delete(user);
     72        return false;
     73      }
     74      finally {
     75        if (session != null)
     76          session.EndSession();
     77      }
     78    }
     79
     80    /// <summary>
     81    /// Gets all Users.
     82    /// </summary>
     83    /// <returns></returns>
     84    public ICollection<User> GetAllUsers() {
     85      try {
     86        session = factory.GetSessionForCurrentThread();
     87        IUserAdapter userAdapter = session.GetDataAdapter<User, IUserAdapter>();
     88
     89        return userAdapter.GetAll();
     90      }
     91      finally {
     92        if (session != null)
     93          session.EndSession();
     94      }         
     95    }
     96
     97    /// <summary>
     98    /// Gets user by name.
     99    /// </summary>
     100    /// <param name="name"></param>
     101    /// <returns></returns>
     102    public User GetUserByName(string name) {
     103      try {
     104        session = factory.GetSessionForCurrentThread();
     105        IUserAdapter userAdapter = session.GetDataAdapter<User, IUserAdapter>();
     106
     107        return userAdapter.GetByName(name);
     108      }
     109      finally {
     110        if (session != null)
     111          session.EndSession();
     112      } 
     113    }
     114
     115    /// <summary>
     116    /// Add new user group.
     117    /// </summary>
     118    /// <param name="group"></param>
     119    /// <returns></returns>
     120    public UserGroup AddNewUserGroup(UserGroup userGroup) {
     121      try {
     122        session = factory.GetSessionForCurrentThread();
     123
     124        IUserGroupAdapter userGroupAdapter = session.GetDataAdapter<UserGroup, IUserGroupAdapter>();
     125        if (userGroup != null)
     126          userGroupAdapter.Update(userGroup);
     127
     128        return userGroup;
     129      }
     130      finally {
     131        if (session != null)
     132          session.EndSession();
     133      }
     134    }
     135
     136    /// <summary>
     137    /// Update user group.
     138    /// </summary>
     139    /// <param name="group"></param>
     140    /// <returns></returns>
     141    public UserGroup UpdateUserGroup(UserGroup userGroup) {
     142      try {
     143        session = factory.GetSessionForCurrentThread();
     144
     145        IUserGroupAdapter userGroupAdapter = session.GetDataAdapter<UserGroup, IUserGroupAdapter>();
     146        if (userGroup != null)
     147          userGroupAdapter.Update(userGroup);
     148
     149        return userGroup;
     150      }
     151      finally {
     152        if (session != null)
     153          session.EndSession();
     154      }
     155    }
     156
     157    /// <summary>
     158    /// Remove user group.
     159    /// </summary>
     160    /// <param name="groupId"></param>
     161    /// <returns></returns>
     162    public bool RemoveUserGroup(Guid userGroupId) {
     163      try {
     164        session = factory.GetSessionForCurrentThread();
     165
     166        IUserGroupAdapter userGroupAdapter = session.GetDataAdapter<UserGroup, IUserGroupAdapter>();
     167        UserGroup userGroup = userGroupAdapter.GetById(userGroupId);   
     168
     169        if (userGroup != null)
     170          return userGroupAdapter.Delete(userGroup);
     171        return false;
     172      }
     173      finally {
     174        if (session != null)
     175          session.EndSession();
     176      }
     177    }
     178
     179    /// <summary>
     180    /// Gets all UserGroups.
     181    /// </summary>
     182    /// <returns></returns>
     183    public ICollection<UserGroup> GetAllUserGroups() {
     184      try {
     185        session = factory.GetSessionForCurrentThread();
     186        IUserGroupAdapter userGroupAdapter = session.GetDataAdapter<UserGroup, IUserGroupAdapter>();
     187
     188        return userGroupAdapter.GetAll();
     189       }
     190      finally {
     191        if (session != null)
     192          session.EndSession();
     193      }
     194    }
     195
     196    /// <summary>
     197    /// Gets UserGroup by name.
     198    /// </summary>
     199    /// <param name="name"></param>
     200    /// <returns></returns>
     201    public UserGroup GetUserGroupByName(string name) {
     202      try {
     203        session = factory.GetSessionForCurrentThread();
     204        IUserGroupAdapter userGroupAdapter = session.GetDataAdapter<UserGroup, IUserGroupAdapter>();
     205
     206        return userGroupAdapter.GetByName(name);
     207      }
     208      finally {
     209        if (session != null)
     210          session.EndSession();
     211      }
     212    }
     213
     214    /// <summary>
     215    /// Updates a PermissionOwner.
     216    /// </summary>
     217    /// <param name="permissionOwner"></param>
     218    /// <returns></returns>
     219    public PermissionOwner UpdatePermissionOwner(PermissionOwner permissionOwner) {
     220      try {
     221        session = factory.GetSessionForCurrentThread();
     222
     223        IPermissionOwnerAdapter permOwnerAdapter = session.GetDataAdapter<PermissionOwner, IPermissionOwnerAdapter>();
     224        if (permissionOwner != null)
     225          permOwnerAdapter.Update(permissionOwner);
     226
     227        return permissionOwner;
     228      }
     229      finally {
     230        if (session != null)
     231          session.EndSession();
     232      }
     233    }
     234
     235    /// <summary>
     236    /// Add permission owner to group.
     237    /// </summary>
     238    /// <param name="userGroupId"></param>
     239    /// <param name="permissionOwnerId"></param>
     240    /// <returns></returns>
     241    public bool AddPermissionOwnerToGroup(Guid userGroupId, Guid permissionOwnerId) {
     242      try {
     243        session = factory.GetSessionForCurrentThread();
     244        ITransaction transaction = session.BeginTransaction();
     245
     246        IUserGroupAdapter userGroupAdapter = session.GetDataAdapter<UserGroup, IUserGroupAdapter>();
     247        UserGroup userGroup = userGroupAdapter.GetById(userGroupId);
     248
     249        IPermissionOwnerAdapter permOwnerAdapter = session.GetDataAdapter<PermissionOwner, IPermissionOwnerAdapter>();
     250        PermissionOwner permissionOwner = permOwnerAdapter.GetById(permissionOwnerId);
     251       
     252        if ((userGroup != null) && (permissionOwner != null)) {
     253          userGroup.Members.Add(permissionOwner);
     254          userGroupAdapter.Update(userGroup);
     255          transaction.Commit();
     256          return true;
     257        }
     258        return false;
     259      }
     260      finally {
     261        if (session != null)
     262          session.EndSession();
     263      }
     264    }
     265
     266
     267    /// <summary>
     268    /// Remove permission owner from group.
     269    /// </summary>
     270    /// <param name="groupId"></param>
     271    /// <param name="permissionOwnerId"></param>
     272    /// <returns></returns>
     273    public bool RemovePermissionOwnerFromGroup(Guid userGroupId, Guid permissionOwnerId) {
     274      try {
     275        session = factory.GetSessionForCurrentThread();
     276        ITransaction transaction = session.BeginTransaction();
     277
     278        IUserGroupAdapter userGroupAdapter = session.GetDataAdapter<UserGroup, IUserGroupAdapter>();
     279        UserGroup userGroup = userGroupAdapter.GetById(userGroupId);
     280
     281        IPermissionOwnerAdapter permOwnerAdapter = session.GetDataAdapter<PermissionOwner, IPermissionOwnerAdapter>();
     282        PermissionOwner permissionOwner = permOwnerAdapter.GetById(permissionOwnerId);
     283
     284        if ((userGroup != null) && (permissionOwner != null)) {
     285          userGroup.Members.Add(permissionOwner);
     286          userGroupAdapter.Delete(userGroup);
     287          transaction.Commit();
     288          return true;
     289        }
     290        return false;
     291      }
     292      finally {
     293        if (session != null)
     294          session.EndSession();
     295      }
     296    }
     297
     298    /// <summary>
     299    /// Grant permission.
     300    /// </summary>
     301    /// <param name="permissionOwnerId"></param>
     302    /// <param name="permissionId"></param>
     303    /// <param name="entityId"></param>
     304    /// <returns></returns>
    71305    public bool GrantPermission(Guid permissionOwnerId, Guid permissionId, Guid entityId) {
    72       return permissionAdapter.addPermission(permissionOwnerId, permissionId, entityId);
    73     }
    74 
     306      try {
     307        session = factory.GetSessionForCurrentThread();                         
     308        IPermissionAdapter permissionAdapter = session.GetDataAdapter<Permission, IPermissionAdapter>();
     309
     310        return permissionAdapter.grantPermission(permissionOwnerId, permissionId, entityId);
     311      }
     312      finally {
     313        if (session != null)
     314          session.EndSession();
     315      }
     316    }
     317
     318    /// <summary>
     319    /// Gets Permission by ID.
     320    /// </summary>
     321    /// <param name="permissionId"></param>
     322    /// <returns></returns>
     323    public Permission GetPermissionById(Guid permissionId) {
     324      try {
     325        session = factory.GetSessionForCurrentThread();
     326
     327        IPermissionAdapter permissionAdapter = session.GetDataAdapter<Permission, IPermissionAdapter>();
     328        return permissionAdapter.GetById(permissionId);
     329      }
     330      finally {
     331        if (session != null)
     332          session.EndSession();
     333      }
     334    }
     335
     336    /// <summary>
     337    /// Revoke permission.
     338    /// </summary>
     339    /// <param name="permissionOwnerId"></param>
     340    /// <param name="permissionId"></param>
     341    /// <param name="entityId"></param>
     342    /// <returns></returns>
    75343    public bool RevokePermission(Guid permissionOwnerId, Guid permissionId, Guid entityId) {
    76       return permissionAdapter.removePermission(permissionOwnerId, permissionId, entityId);
     344      try {
     345        session = factory.GetSessionForCurrentThread();     
     346        IPermissionAdapter permissionAdapter = session.GetDataAdapter<Permission, IPermissionAdapter>();
     347 
     348        return permissionAdapter.revokePermission(permissionOwnerId, permissionId, entityId);
     349      }
     350      finally {
     351        if (session != null)
     352          session.EndSession();
     353      }
    77354    }
    78355 
  • trunk/sources/HeuristicLab.Security.DataAccess/3.2/IPermissionAdapter.cs

    r1530 r1624  
    3838    /// <param name="entityId"></param>
    3939    /// <returns></returns>
    40     GrantedPermission getPermission(PermissionOwner permissionOwner, Permission permission);
     40    GrantedPermission getPermission(PermissionOwner permissionOwner, Permission permission, Guid entityId);
    4141
    4242    /// <summary>
     
    4747    /// <param name="entityId"></param>
    4848    /// <returns></returns>
    49     bool addPermission(PermissionOwner permissionOwner, Permission permission);
     49    bool grantPermission(Guid permissionOwnerId, Guid permissionId, Guid entityId);
    5050
    5151    /// <summary>
     
    5656    /// <param name="entityId"></param>
    5757    /// <returns></returns>
    58     bool removePermission(PermissionOwner permissionOwner, Permission permission);
     58    bool revokePermission(Guid permissionOwnerId, Guid permissionId, Guid entityId);
    5959  }
    6060}
Note: See TracChangeset for help on using the changeset viewer.