[15496] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Data.Linq;
|
---|
| 25 | using System.Linq;
|
---|
| 26 |
|
---|
| 27 | namespace 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 |
|
---|
[15500] | 36 | public IEnumerable<Guid> GetByUserAndGroupIds(IEnumerable<Guid> userAndGroupIds) {
|
---|
| 37 | string paramIds = string.Join(",", userAndGroupIds.Select(x => string.Format("'{0}'", x)));
|
---|
[15496] | 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
|
---|
[15497] | 58 | FROM [Resource]
|
---|
[15496] | 59 | UNION ALL
|
---|
| 60 | SELECT rt.ResourceId, r.ParentResourceId
|
---|
[15497] | 61 | FROM [Resource] r
|
---|
[15496] | 62 | JOIN rtree rt ON rt.ParentResourceId = r.ResourceId AND r.ParentResourceId <> r.ResourceId AND rt.ParentResourceId <> rt.ResourceId
|
---|
| 63 | )
|
---|
[15497] | 64 | SELECT DISTINCT rtree.ResourceId
|
---|
| 65 | FROM rtree, [ResourcePermission] rp
|
---|
| 66 | WHERE rtree.ParentResourceId = rp.ResourceId AND rp.GrantedUserId IN ({0})
|
---|
[15496] | 67 | UNION ALL
|
---|
| 68 | SELECT rp.ResourceId
|
---|
[15497] | 69 | FROM [ResourcePermission] rp
|
---|
[15496] | 70 | WHERE rp.GrantedUserId IN ({0})
|
---|
| 71 | ;";
|
---|
| 72 |
|
---|
| 73 | #endregion
|
---|
| 74 | }
|
---|
| 75 | }
|
---|