Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.Security.ADODataAccess/3.2/PermissionAdapter.cs @ 4593

Last change on this file since 4593 was 1729, checked in by svonolfe, 16 years ago

Fixed some bugs in the Security DAL (#597)

File size: 4.3 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using HeuristicLab.DataAccess.ADOHelper;
6using HeuristicLab.Security.Contracts.BusinessObjects;
7using HeuristicLab.Security.DataAccess;
8using HeuristicLab.Security.ADODataAccess.TableAdapterWrapper;
9
10namespace HeuristicLab.Security.ADODataAccess {
11  class PermissionAdapter: DataAdapterBase<
12      dsSecurityTableAdapters.PermissionTableAdapter,
13      Permission,
14      dsSecurity.PermissionRow>,
15      IPermissionAdapter {
16    public PermissionAdapter() :
17      base(new PermissionAdapterWrapper()) {
18    }
19
20    private GrantedPermissionsAdapterWrapper grantedPermissionsAdapter;
21
22    private GrantedPermissionsAdapterWrapper GrantedPermissionsAdapter {
23      get {
24        if (grantedPermissionsAdapter == null)
25          grantedPermissionsAdapter = new GrantedPermissionsAdapterWrapper();
26
27        grantedPermissionsAdapter.Session = Session as Session;
28
29        return grantedPermissionsAdapter;
30      }
31    }
32
33    private IUserGroupAdapter userGroupAdapter;
34
35    private IUserGroupAdapter UserGroupAdapter {
36      get {
37        if (userGroupAdapter == null)
38          userGroupAdapter = this.Session.GetDataAdapter<UserGroup, IUserGroupAdapter>();
39
40        return userGroupAdapter;
41      }
42    }
43
44    protected override dsSecurity.PermissionRow ConvertObj(Permission perm,
45      dsSecurity.PermissionRow row) {
46      if (row != null && perm != null) {
47        row.PermissionId = perm.Id;
48        row.Name = perm.Name;
49        row.Description = perm.Description;
50        row.Plugin = perm.Plugin;
51
52        return row;
53      } else {
54        return null;
55      }
56    }
57
58    protected override Permission ConvertRow(dsSecurity.PermissionRow row,
59      Permission perm) {
60      if (row != null && perm != null) {
61        perm.Id = row.PermissionId;
62        if (!row.IsNameNull())
63          perm.Name = row.Name;
64        else
65          perm.Name = String.Empty;
66
67        if (!row.IsDescriptionNull())
68          perm.Description = row.Description;
69        else
70          perm.Description = String.Empty;
71
72        if (!row.IsPluginNull())
73          perm.Plugin = row.Plugin;
74        else
75          perm.Plugin = String.Empty;
76
77        return perm;
78      } else {
79        return null;
80      }
81    }
82
83    #region IPermissionAdapter Members
84
85    #endregion
86
87    #region IPermissionAdapter Members
88
89    public GrantedPermission getPermission(Guid permissionOwnerId, Guid permissionId, Guid entityId) {
90      dsSecurity.GrantedPermissionsRow row =
91          GrantedPermissionsAdapter.FindByPermissionPermissionOwnerEntityId(
92        permissionId, permissionOwnerId, entityId);
93
94      if (row != null) {
95        GrantedPermission perm = new GrantedPermission();
96        perm.PermissionId = row.PermissionId;
97        perm.PermissionOwnerId = row.PermissionOwnerId;
98        perm.EntityId = row.EntityId;
99
100        return perm;
101      } else {
102        ICollection<UserGroup> groups =
103          UserGroupAdapter.MemberOf(permissionOwnerId);
104
105        GrantedPermission perm = null;
106
107        if (groups != null) {
108          foreach(UserGroup group in groups) {
109            perm = getPermission(group.Id, permissionId, entityId);
110
111            if (perm != null)
112              break;
113          }
114        }
115
116        return perm;
117      }
118    }
119
120    public bool grantPermission(Guid permissionOwnerId, Guid permissionId, Guid entityId) {
121      if (GrantedPermissionsAdapter.FindByPermissionPermissionOwnerEntityId(
122        permissionId, permissionOwnerId, entityId) == null) {
123        GrantedPermission perm = new GrantedPermission();
124        perm.PermissionId = permissionId;
125        perm.PermissionOwnerId = permissionOwnerId;
126        perm.EntityId = entityId;
127
128        return GrantedPermissionsAdapter.InsertNewRow(perm) != null;
129      } else {
130        return false;
131      }     
132    }
133
134    public bool revokePermission(Guid permissionOwnerId, Guid permissionId, Guid entityId) {
135      GrantedPermission perm =
136        getPermission(permissionOwnerId, permissionId, entityId);
137     
138      if (perm != null) {
139        return GrantedPermissionsAdapter.DeleteRow(perm);
140      } else {
141        return false;
142      }
143    }
144
145    #endregion
146  }
147}
Note: See TracBrowser for help on using the repository browser.