-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathAccessControlService.cs
More file actions
189 lines (174 loc) · 8.4 KB
/
Copy pathAccessControlService.cs
File metadata and controls
189 lines (174 loc) · 8.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright (c) CGI France. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
namespace IoTHub.Portal.Application.Services
{
using System.Threading.Tasks;
using AutoMapper;
using IoTHub.Portal.Domain;
using IoTHub.Portal.Domain.Entities;
using IoTHub.Portal.Domain.Repositories;
using IoTHub.Portal.Shared.Models.v10;
using IoTHub.Portal.Shared.Models.v10.Filters;
using IoTHub.Portal.Domain.Exceptions;
using IoTHub.Portal.Crosscutting;
using System.Linq; // for Select
using System.Linq.Expressions; // for includes expressions
public class AccessControlService : IAccessControlManagementService
{
private readonly IAccessControlRepository accessControlRepository;
private readonly IUnitOfWork unitOfWork;
private readonly IMapper mapper;
private readonly IRoleRepository roleRepository;
private readonly IPrincipalRepository principalRepository;
public AccessControlService(IAccessControlRepository accessControlRepository, IUnitOfWork unitOfWork, IMapper mapper, IRoleRepository roleRepository, IPrincipalRepository principalRepository)
{
this.accessControlRepository = accessControlRepository;
this.unitOfWork = unitOfWork;
this.mapper = mapper;
this.roleRepository = roleRepository;
this.principalRepository = principalRepository;
}
public async Task<AccessControlModel> GetAccessControlAsync(string Id)
{
var acEntity = await this.accessControlRepository.GetByIdAsync(Id, ac => ac.Role);
if (acEntity is null)
{
throw new ResourceNotFoundException($"The AccessControl with the id {Id} doesn't exist");
}
var acModel = this.mapper.Map<AccessControlModel>(acEntity);
return acModel;
}
public async Task<PaginatedResult<AccessControlModel>> GetAccessControlPage(
string? searchKeyword = null,
int pageSize = 10,
int pageNumber = 0,
string[] orderBy = null,
string? principalId = null
)
{
var acFilter = new AccessControlFilter
{
Keyword = searchKeyword,
PageSize = pageSize,
PageNumber = pageNumber,
OrderBy = orderBy
};
var acPredicate = PredicateBuilder.True<AccessControl>();
if (!string.IsNullOrWhiteSpace(acFilter.Keyword))
{
acPredicate = acPredicate.And(ac => ac.Scope.ToLower().Contains(acFilter.Keyword.ToLower()) || ac.Role.Name.ToLower().Contains(acFilter.Keyword.ToLower()));
}
if (!string.IsNullOrEmpty(principalId))
{
acPredicate = acPredicate.And(ac => ac.PrincipalId == principalId);
}
// Include Role so that Role.Name is populated for UI display
var paginatedAc = await this.accessControlRepository.GetPaginatedListAsync(
pageNumber,
pageSize,
orderBy,
acPredicate,
includes: new Expression<System.Func<AccessControl, object>>[] { ac => ac.Role }
);
var paginatedAcDto = new PaginatedResult<AccessControlModel>
{
Data = paginatedAc.Data.Select(x => this.mapper.Map<AccessControlModel>(x)).ToList(),
TotalCount = paginatedAc.TotalCount,
CurrentPage = paginatedAc.CurrentPage,
PageSize = pageSize
};
return new PaginatedResult<AccessControlModel>(paginatedAcDto.Data, paginatedAcDto.TotalCount);
}
public async Task<AccessControlModel> CreateAccessControl(AccessControlModel accessControl)
{
if (accessControl is null)
{
throw new ArgumentNullException(nameof(accessControl));
}
var principal = await this.principalRepository.GetByIdAsync(accessControl.PrincipalId);
if (principal == null)
{
throw new ResourceNotFoundException($"The principal with the id {accessControl.PrincipalId} does'nt exist !");
}
var role = await this.roleRepository.GetByIdAsync(accessControl.Role.Id);
if (role == null)
{
throw new ResourceNotFoundException($"The role {accessControl.Role.Name} with the id {accessControl.Role.Id} does'nt exist !");
}
var acEntity = this.mapper.Map<AccessControl>(accessControl);
await this.accessControlRepository.InsertAsync(acEntity);
await this.unitOfWork.SaveAsync();
var createdAc = await this.accessControlRepository.GetByIdAsync(acEntity.Id, ac => ac.Role);
var createdModel = this.mapper.Map<AccessControlModel>(createdAc);
return createdModel;
}
public async Task<AccessControlModel?> UpdateAccessControl(string id, AccessControlModel accessControl)
{
if (accessControl is null)
{
throw new ArgumentNullException(nameof(accessControl));
}
var acEntity = await this.accessControlRepository.GetByIdAsync(id, ac => ac.Role);
if (acEntity is null)
{
throw new ResourceNotFoundException($"The AccessControl with the id {id} doesn't exist");
}
var principal = await this.principalRepository.GetByIdAsync(accessControl.PrincipalId);
if (principal is null)
{
throw new ResourceNotFoundException($"The principal with the id {accessControl.PrincipalId} not found !");
}
var role = await this.roleRepository.GetByIdAsync(accessControl.Role.Id);
if (role is null)
{
throw new ResourceNotFoundException($"Specified role with the id {accessControl.Role.Id} not found");
}
acEntity.PrincipalId = accessControl.PrincipalId;
acEntity.RoleId = accessControl.Role.Id;
acEntity.Scope = accessControl.Scope;
accessControlRepository.Update(acEntity);
await this.unitOfWork.SaveAsync();
var createdAc = await this.accessControlRepository.GetByIdAsync(id, ac => ac.Role);
return this.mapper.Map<AccessControlModel>(createdAc);
}
public async Task<bool> DeleteAccessControl(string id)
{
var acEntity = await this.accessControlRepository.GetByIdAsync(id);
if (acEntity is null)
{
throw new ResourceNotFoundException($"The AccessControl with the id {id} doesn't exist");
}
accessControlRepository.Delete(id);
await this.unitOfWork.SaveAsync();
return true;
}
/// <summary>
/// Verify if the principal (user) has the specified permission.
/// For this, we retrieve all access controls related to the principal including the role and its actions,
/// then we check if one of the role's actions matches (case insensitive) the requested permission.
/// </summary>
/// <param name="permission">Permission name (e.g. "group:read").</param>
/// <param name="principalId">Principal identifier of the user (trying to reach the request) </param>
/// <returns> True if the permission is found, else False </returns>
public async Task<bool> UserHasPermissionAsync(string principalId, string permission)
{
// We retrieve the access controls of the principal including the role and its list of actions.
var accessControls = await this.accessControlRepository.GetAllAsync(
ac => ac.PrincipalId == principalId,
CancellationToken.None,
ac => ac.Role,
ac => ac.Role.Actions
);
// For each access control, check if the associated role contains an action matching the requested permission.
foreach (var ac in accessControls)
{
if (ac.Role?.Actions != null &&
ac.Role.Actions.Any(a => string.Equals(a.Name, permission, StringComparison.OrdinalIgnoreCase)))
{
return true;
}
}
return false;
}
}
}