[1656] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using HeuristicLab.DataAccess.ADOHelper;
|
---|
| 6 | using HeuristicLab.Security.Contracts.BusinessObjects;
|
---|
| 7 | using HeuristicLab.Security.DataAccess;
|
---|
| 8 | using HeuristicLab.Security.ADODataAccess.TableAdapterWrapper;
|
---|
| 9 |
|
---|
| 10 | namespace 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 |
|
---|
[1720] | 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 |
|
---|
[1724] | 33 | private IUserGroupAdapter userGroupAdapter;
|
---|
| 34 |
|
---|
| 35 | private IUserGroupAdapter UserGroupAdapter {
|
---|
| 36 | get {
|
---|
| 37 | if (userGroupAdapter == null)
|
---|
[1729] | 38 | userGroupAdapter = this.Session.GetDataAdapter<UserGroup, IUserGroupAdapter>();
|
---|
[1724] | 39 |
|
---|
| 40 | return userGroupAdapter;
|
---|
| 41 | }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
[1656] | 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 |
|
---|
[1720] | 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 {
|
---|
[1724] | 102 | ICollection<UserGroup> groups =
|
---|
[1729] | 103 | UserGroupAdapter.MemberOf(permissionOwnerId);
|
---|
[1724] | 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;
|
---|
[1720] | 117 | }
|
---|
[1656] | 118 | }
|
---|
| 119 |
|
---|
[1720] | 120 | public bool grantPermission(Guid permissionOwnerId, Guid permissionId, Guid entityId) {
|
---|
[1729] | 121 | if (GrantedPermissionsAdapter.FindByPermissionPermissionOwnerEntityId(
|
---|
| 122 | permissionId, permissionOwnerId, entityId) == null) {
|
---|
[1720] | 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 | }
|
---|
[1656] | 132 | }
|
---|
| 133 |
|
---|
[1720] | 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 | }
|
---|
[1656] | 143 | }
|
---|
| 144 |
|
---|
| 145 | #endregion
|
---|
| 146 | }
|
---|
| 147 | }
|
---|