Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15500 was 15500, checked in by jzenisek, 5 years ago

#2839 worked on resource permission checks (v2 = accelerated verison)

File size: 2.8 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    #region Compiled queries
46    private static readonly Func<DataContext, Guid, IEnumerable<ResourcePermission>> GetByResourceIdQuery =
47      CompiledQuery.Compile((DataContext db, Guid resourceId) =>
48      from resourcePermission in db.GetTable<ResourcePermission>()
49      where resourcePermission.ResourceId == resourceId
50      select resourcePermission);
51    #endregion
52
53    #region String queries
54    private const string GetGrantedResourcesQuery = @"
55      WITH rtree AS
56      (
57        SELECT ResourceId, ParentResourceId
58        FROM [Resource]
59        UNION ALL
60        SELECT rt.ResourceId, r.ParentResourceId
61        FROM [Resource] r
62        JOIN rtree rt ON rt.ParentResourceId = r.ResourceId AND r.ParentResourceId <> r.ResourceId AND rt.ParentResourceId <> rt.ResourceId
63      )
64      SELECT DISTINCT rtree.ResourceId
65      FROM rtree, [ResourcePermission] rp
66      WHERE rtree.ParentResourceId = rp.ResourceId AND rp.GrantedUserId IN ({0})
67      UNION ALL
68      SELECT rp.ResourceId
69      FROM [ResourcePermission] rp
70      WHERE rp.GrantedUserId IN ({0})
71      ;";
72
73    #endregion
74  }
75}
Note: See TracBrowser for help on using the repository browser.