source: trunk/sources/HeuristicLab.Security.ADODataAccess/3.2/PermissionAdapter.cs @ 1724

Last change on this file since 1724 was 1724, checked in by svonolfe, 12 years ago

Finished security server (#537)

File size: 4.2 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          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(permissionId);
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 (getPermission(permissionOwnerId, permissionId, entityId) == null) {
122        GrantedPermission perm = new GrantedPermission();
123        perm.PermissionId = permissionId;
124        perm.PermissionOwnerId = permissionOwnerId;
125        perm.EntityId = entityId;
126
127        return GrantedPermissionsAdapter.InsertNewRow(perm) != null;
128      } else {
129        return false;
130      }     
131    }
132
133    public bool revokePermission(Guid permissionOwnerId, Guid permissionId, Guid entityId) {
134      GrantedPermission perm =
135        getPermission(permissionOwnerId, permissionId, entityId);
136     
137      if (perm != null) {
138        return GrantedPermissionsAdapter.DeleteRow(perm);
139      } else {
140        return false;
141      }
142    }
143
144    #endregion
145  }
146}
Note: See TracBrowser for help on using the repository browser.