Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Services.Hive.DataAccess/3.3/Daos/ProjectPermissionDao.cs @ 16755

Last change on this file since 16755 was 16565, checked in by gkronber, 6 years ago

#2520: merged changes from PersistenceOverhaul branch (r16451:16564) into trunk

File size: 4.1 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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 ProjectPermissionDao : GenericDao<Guid, ProjectPermission> {
29    public ProjectPermissionDao(DataContext dataContext) : base(dataContext) { }
30
31    public override ProjectPermission GetById(Guid id) {
32      throw new NotImplementedException();
33    }
34
35    public IEnumerable<ProjectPermission> GetByProjectId(Guid id) {
36      return GetByProjectIdGetByIdQuery(DataContext, id);
37    }
38
39    public bool CheckUserGrantedForProject(Guid projectId, IEnumerable<Guid> userAndGroupIds) {
40      string paramUserAndGroupIds = string.Join(",", userAndGroupIds.ToList().Select(x => string.Format("'{0}'", x)));
41      if(!string.IsNullOrWhiteSpace(paramUserAndGroupIds)) {
42        string queryString = string.Format(CheckUserGrantedForProjectQueryString, projectId, paramUserAndGroupIds);
43        return DataContext.ExecuteQuery<int>(queryString).First() > 0;
44      }
45      return false;
46    }
47
48    public void DeleteByProjectIdAndGrantedUserIds(Guid projectId, IEnumerable<Guid> grantedUserIds) {
49      string paramIds = string.Join(",", grantedUserIds.ToList().Select(x => string.Format("'{0}'", x)));
50      if (!string.IsNullOrWhiteSpace(paramIds)) {
51        string query = string.Format(DeleteByProjectIdAndGrantedUserIdsQueryString, projectId, paramIds);
52        DataContext.ExecuteCommand(query);
53      }
54    }
55
56    public void DeleteByProjectIdsAndGrantedUserIds(IEnumerable<Guid> projectIds, IEnumerable<Guid> grantedUserIds) {
57      string paramProjectIds = string.Join(",", projectIds.ToList().Select(x => string.Format("'{0}'", x)));
58      string paramUserIds = string.Join(",", grantedUserIds.ToList().Select(x => string.Format("'{0}'", x)));
59      if (!string.IsNullOrWhiteSpace(paramProjectIds) && !string.IsNullOrWhiteSpace(paramUserIds)) {
60        string query = string.Format(DeleteByProjectIdsAndGrantedUserIdsQueryString, paramProjectIds, paramUserIds);
61        DataContext.ExecuteCommand(query);
62      }
63    }
64
65    #region Compiled queries
66    private static readonly Func<DataContext, Guid, IEnumerable<ProjectPermission>> GetByProjectIdGetByIdQuery =
67      CompiledQuery.Compile((DataContext db, Guid projectId) =>
68        from projectPermission in db.GetTable<ProjectPermission>()
69        where projectPermission.ProjectId == projectId
70        select projectPermission);
71    #endregion
72
73    #region String queries
74    private const string DeleteByProjectIdAndGrantedUserIdsQueryString = @"
75      DELETE FROM [ProjectPermission]
76      WHERE ProjectId = '{0}'
77      AND GrantedUserId IN ({1});
78    ";
79    private const string DeleteByProjectIdsAndGrantedUserIdsQueryString = @"
80      DELETE FROM [ProjectPermission]
81      WHERE ProjectId IN ({0})
82      AND GrantedUserId IN ({1});
83    ";
84    private const string CheckUserGrantedForProjectQueryString = @"
85      SELECT COUNT(pp.ProjectId)
86      FROM [ProjectPermission] pp
87      WHERE pp.ProjectId = '{0}'
88      AND pp.GrantedUserId IN ({1})
89    ";
90    private const string GetGrantedProjectsForUserQueryString = @"
91      SELECT DISTINCT p.*
92      FROM [ProjectPermission] pp, [Project] p
93      WHERE pp.GrantedUserId IN ({0})
94      AND  pp.ProjectId = p.ProjectId
95    ";
96    #endregion
97  }
98}
Note: See TracBrowser for help on using the repository browser.