Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HiveProjectManagement/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/ResourcePermissionDao.cs @ 15503

Last change on this file since 15503 was 15503, checked in by jzenisek, 6 years ago

#2839 added ResourcePermission handling (still in progress)

File size: 3.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Data.Linq;
25using System.Linq;
26
27namespace HeuristicLab.Services.Hive.DataAccess.Daos {
28  public class ResourcePermissionDao : GenericDao<Guid, ResourcePermission> {
29
30    public ResourcePermissionDao(DataContext dataContext) : base(dataContext) { }
31
32    public override ResourcePermission GetById(Guid id) {
33      throw new NotImplementedException();
34    }
35
36    public IEnumerable<Guid> GetByUserAndGroupIds(IEnumerable<Guid> userAndGroupIds) {
37      string paramIds = string.Join(",", userAndGroupIds.Select(x => string.Format("'{0}'", x)));
38      if (!string.IsNullOrWhiteSpace(paramIds)) {
39        string query = string.Format(GetGrantedResourcesQuery, paramIds);
40        return DataContext.ExecuteQuery<Guid>(query);
41      }
42      return Enumerable.Empty<Guid>();
43    }
44
45    public void DeleteByResourceIdAndGrantedUserId(Guid resourceId, IEnumerable<Guid> grantedUserId) {
46      string paramIds = string.Join(",", grantedUserId.Select(x => string.Format("'{0}'", x)));
47      if (!string.IsNullOrWhiteSpace(paramIds)) {
48        string query = string.Format(DeleteByGrantedUserQuery, resourceId, paramIds);
49        DataContext.ExecuteCommand(query);
50      }
51    }
52
53    #region Compiled queries
54    private static readonly Func<DataContext, Guid, IEnumerable<ResourcePermission>> GetByResourceIdQuery =
55      CompiledQuery.Compile((DataContext db, Guid resourceId) =>
56      from resourcePermission in db.GetTable<ResourcePermission>()
57      where resourcePermission.ResourceId == resourceId
58      select resourcePermission);
59    #endregion
60
61    #region String queries
62    private const string GetGrantedResourcesQuery = @"
63      WITH rtree AS
64      (
65        SELECT ResourceId, ParentResourceId
66        FROM [Resource]
67        UNION ALL
68        SELECT rt.ResourceId, r.ParentResourceId
69        FROM [Resource] r
70        JOIN rtree rt ON rt.ParentResourceId = r.ResourceId AND r.ParentResourceId <> r.ResourceId AND rt.ParentResourceId <> rt.ResourceId
71      )
72      SELECT DISTINCT rtree.ResourceId
73      FROM rtree, [ResourcePermission] rp
74      WHERE rtree.ParentResourceId = rp.ResourceId AND rp.GrantedUserId IN ({0})
75      UNION ALL
76      SELECT rp.ResourceId
77      FROM [ResourcePermission] rp
78      WHERE rp.GrantedUserId IN ({0})
79      ;";
80
81    private const string DeleteByGrantedUserQuery = @"
82      DELETE FROM [ResourcePermission]
83        WHERE ResourceId = '{0}'
84        AND GrantedUserId IN ({1})
85    ;";
86    #endregion
87  }
88}
Note: See TracBrowser for help on using the repository browser.