improvements
This commit is contained in:
parent
fbeb685aea
commit
2ce0890f12
|
|
@ -0,0 +1,40 @@
|
|||
using AyCode.Database.Tests;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using AyCode.Database.DataLayers.Users;
|
||||
using TIAM.Database.DataLayers.ServiceProviders;
|
||||
using TIAM.Database.DataLayers.Users;
|
||||
using TIAM.Database.DbContexts;
|
||||
|
||||
namespace TIAM.Database.Test
|
||||
{
|
||||
[TestClass]
|
||||
public class ServiceProviderDalTest : DatabaseTestModelBase<ServiceProviderDbContext>
|
||||
{
|
||||
private ServiceProviderDal _serviceProviderDal;
|
||||
|
||||
[TestInitialize]
|
||||
public void Setup()
|
||||
{
|
||||
_serviceProviderDal = new ServiceProviderDal();
|
||||
}
|
||||
|
||||
[TestCleanup]
|
||||
public void TearDown()
|
||||
{ }
|
||||
|
||||
[TestMethod]
|
||||
//[DataRow(Guid.Parse("540271F6-C604-4C16-8160-D5A7CAFEDF00")]
|
||||
public async Task GetPermissionContextMappingByContext_ReturnsPermissionContextMapping_WhenPermissionContextMappingExists()//(Guid contextId)
|
||||
{
|
||||
var contextId = Guid.Parse("540271F6-C604-4C16-8160-D5A7CAFEDF00");
|
||||
|
||||
var permMapping = await _serviceProviderDal.GetPermissionContextMappingByContextIdAsync(contextId);
|
||||
|
||||
Assert.IsNotNull(permMapping);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -39,13 +39,13 @@ namespace TIAM.Database.DataLayers.Users
|
|||
|
||||
public Task<bool> UpdateUserAsync(User user)
|
||||
{
|
||||
var existingUser = Ctx.Users.FirstOrDefault(u => u.Email == user.Email);
|
||||
var existingUser = Context.Users.FirstOrDefault(u => u.EmailAddress == user.EmailAddress);
|
||||
if (existingUser != null)
|
||||
{
|
||||
//user.Modified = DateTime.UtcNow; //ezt nem kell megadni, a háttérben ezt magától megcsinálja a DbContextBase - J.
|
||||
existingUser = user;
|
||||
Ctx.Users.Update(existingUser);
|
||||
return Ctx.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
||||
Context.Users.Update(existingUser);
|
||||
return Context.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
|||
|
|
@ -31,29 +31,29 @@ namespace TIAM.Database.DataLayers.ServiceProviders
|
|||
//16. (IServiceProviderDataService) get all service providers
|
||||
public Task<List<TiamServiceProvider>> GetServiceProvidersAsync()
|
||||
{
|
||||
return Ctx.ServiceProviders.ToListAsync();
|
||||
return Context.ServiceProviders.ToListAsync();
|
||||
}
|
||||
|
||||
//18. (IServiceProviderDataService) get serviceProvider by Id
|
||||
public virtual Task<TiamServiceProvider?> GetServiceProviderByIdAsync(Guid id)
|
||||
{
|
||||
Console.WriteLine($"Getting serviceProvider from db {id}");
|
||||
return Ctx.ServiceProviders.SingleOrDefaultAsync(x=>x.Id == id);
|
||||
return Context.ServiceProviders.SingleOrDefaultAsync(x=>x.Id == id);
|
||||
}
|
||||
|
||||
//15. (IServiceProviderDataService) Create service provider
|
||||
public Task<bool> CreateServiceProviderAsync(TiamServiceProvider serviceProvider)
|
||||
{
|
||||
if(serviceProvider.Name == Ctx.ServiceProviders.FirstOrDefault(x=>x.Name == serviceProvider.Name)?.Name)
|
||||
if(serviceProvider.Name == Context.ServiceProviders.FirstOrDefault(x=>x.Name == serviceProvider.Name)?.Name)
|
||||
{
|
||||
throw new Exception("ServiceProvider already exists");
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Ctx.ServiceProviders.Add(serviceProvider);
|
||||
Context.ServiceProviders.Add(serviceProvider);
|
||||
Console.WriteLine($"Saving serviceProvider to db {serviceProvider.Id}, {serviceProvider.Name}, {serviceProvider.OwnerId}");
|
||||
return Ctx.SaveChangesAsync().ContinueWith(x=>x.Result > 0);
|
||||
return Context.SaveChangesAsync().ContinueWith(x=>x.Result > 0);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -61,12 +61,12 @@ namespace TIAM.Database.DataLayers.ServiceProviders
|
|||
//14. (IserviceProviderDataService) Update service provider
|
||||
public Task<bool> UpdateServiceProviderAsync(TiamServiceProvider serviceProvider)
|
||||
{
|
||||
var dbServiceProvider = Ctx.ServiceProviders.FirstOrDefault(u => u.Id == serviceProvider.Id);
|
||||
var dbServiceProvider = Context.ServiceProviders.FirstOrDefault(u => u.Id == serviceProvider.Id);
|
||||
if (dbServiceProvider != null)
|
||||
{
|
||||
dbServiceProvider = serviceProvider;
|
||||
Ctx.ServiceProviders.Update(dbServiceProvider);
|
||||
return Ctx.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
||||
Context.ServiceProviders.Update(dbServiceProvider);
|
||||
return Context.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -77,18 +77,18 @@ namespace TIAM.Database.DataLayers.ServiceProviders
|
|||
//13. (IserviceProviderDataService) delete service provider
|
||||
public Task<bool> DeleteServiceProviderAsync(Guid id)
|
||||
{
|
||||
using (var transaction = Ctx.Database.BeginTransaction())
|
||||
using (var transaction = Context.Database.BeginTransaction())
|
||||
{
|
||||
var dbServiceProvider = Ctx.ServiceProviders.FirstOrDefault(u => u.Id == id);
|
||||
var dbServiceProvider = Context.ServiceProviders.FirstOrDefault(u => u.Id == id);
|
||||
if (dbServiceProvider != null)
|
||||
{
|
||||
//get products for this provider
|
||||
var products = Ctx.Products.Where(x => x.OwnerId == id).ToList();
|
||||
var products = Context.Products.Where(x => x.OwnerId == id).ToList();
|
||||
|
||||
/*foreach (var productItem in products)
|
||||
{
|
||||
//delete products
|
||||
var permissionContextMappings = Ctx.PermissionContextMappings.Where(x => x.ContextId == productItem.Id).ToList();
|
||||
var permissionContextMappings = Context.PermissionContextMappings.Where(x => x.ContextId == productItem.Id).ToList();
|
||||
//iterate through every row
|
||||
foreach (var item in permissionContextMappings)
|
||||
{
|
||||
|
|
@ -96,20 +96,20 @@ namespace TIAM.Database.DataLayers.ServiceProviders
|
|||
if (item.SubjectType == (int)PermissionContextMappingSubjectType.Group)
|
||||
{
|
||||
//get users in the permissiongroup
|
||||
var permissionGroupUserMapping = Ctx.PermissionGroupUserMappings.Where(x => x.PermissionContextMappingId == item.Id).ToList();
|
||||
var permissionGroupUserMapping = Context.PermissionGroupUserMappings.Where(x => x.PermissionContextMappingId == item.Id).ToList();
|
||||
//remove every row (users) from permissiongroup
|
||||
foreach (var user in permissionGroupUserMapping)
|
||||
{
|
||||
Ctx.PermissionGroupUserMappings.Remove(user);
|
||||
Context.PermissionGroupUserMappings.Remove(user);
|
||||
}
|
||||
}
|
||||
}
|
||||
//remove permissioncontextmappings
|
||||
Ctx.PermissionContextMappings.RemoveRange(permissionContextMappings);
|
||||
Context.PermissionContextMappings.RemoveRange(permissionContextMappings);
|
||||
}*/
|
||||
Ctx.Products.RemoveRange(products);
|
||||
Ctx.ServiceProviders.Remove(dbServiceProvider);
|
||||
return Ctx.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
||||
Context.Products.RemoveRange(products);
|
||||
Context.ServiceProviders.Remove(dbServiceProvider);
|
||||
return Context.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -135,9 +135,9 @@ namespace TIAM.Database.DataLayers.ServiceProviders
|
|||
{
|
||||
bool result = false;
|
||||
|
||||
using (var transaction = Ctx.Database.BeginTransaction())
|
||||
using (var transaction = Context.Database.BeginTransaction())
|
||||
{
|
||||
var existingPermission = Ctx.PermissionsTypes
|
||||
var existingPermission = Context.PermissionsTypes
|
||||
.FirstOrDefault(x => x.PermissionName == permissionsType.PermissionName)?.PermissionName;
|
||||
|
||||
if (existingPermission == null)
|
||||
|
|
@ -145,7 +145,7 @@ namespace TIAM.Database.DataLayers.ServiceProviders
|
|||
//get all the permissiontypes for this context
|
||||
var permissionTypes = new List<PermissionsType>();
|
||||
var nextBitValue = 0.0;
|
||||
permissionTypes = Ctx.PermissionsTypes
|
||||
permissionTypes = Context.PermissionsTypes
|
||||
.Where(x => x.ContextId == permissionsType.ContextId)
|
||||
.ToList();
|
||||
|
||||
|
|
@ -160,8 +160,8 @@ namespace TIAM.Database.DataLayers.ServiceProviders
|
|||
nextBitValue = Math.Pow(2,0);
|
||||
}
|
||||
permissionsType.PermissionBit = (int)nextBitValue;
|
||||
Ctx.PermissionsTypes.Add(permissionsType);
|
||||
Ctx.SaveChanges();
|
||||
Context.PermissionsTypes.Add(permissionsType);
|
||||
Context.SaveChanges();
|
||||
transaction.Commit();
|
||||
result = true;
|
||||
}
|
||||
|
|
@ -178,12 +178,12 @@ namespace TIAM.Database.DataLayers.ServiceProviders
|
|||
//11. (IPermissionService) get permission types for context
|
||||
public Task<List<PermissionsType>>? GetPermissionTypesByContextIdAsync(Guid contextId)
|
||||
{
|
||||
return Ctx.PermissionsTypes.Where(x => x.ContextId == contextId).ToListAsync();
|
||||
return Context.PermissionsTypes.Where(x => x.ContextId == contextId).ToListAsync();
|
||||
}
|
||||
|
||||
public Task<int> GetPermissionFromPermissionType(PermissionsType pType)
|
||||
{
|
||||
if(Ctx.PermissionsTypes.FirstOrDefault(x=>x.Id == pType.Id) != null)
|
||||
if(Context.PermissionsTypes.FirstOrDefault(x=>x.Id == pType.Id) != null)
|
||||
{
|
||||
return Task.FromResult(pType.PermissionBit);
|
||||
}
|
||||
|
|
@ -197,57 +197,60 @@ namespace TIAM.Database.DataLayers.ServiceProviders
|
|||
|
||||
#region PermissionMappings
|
||||
|
||||
public Task<List<PermissionContextMapping>> GetPermissionContextMappingByContextIdAsync(Guid contextId)
|
||||
=> SessionAsync(x => x.GetPermissionContextMappingByContextId(contextId).ToList());
|
||||
|
||||
//2. get the contexts where the user has permission
|
||||
public async Task<List<AssignedPermissionModel>> GetPermissionContextByUserIdAsync(Guid UserId)
|
||||
{
|
||||
List<AssignedPermissionModel> _permissions = new List<AssignedPermissionModel>();
|
||||
//get all assignedUsers
|
||||
List<AssignedUser> assignedUsers = await Ctx.AssignedUsers.Where(x => x.EmployeeUserId == UserId).ToListAsync();
|
||||
//List<PermissionContextMapping> _permissionContextMappings = new List<PermissionContextMapping>();
|
||||
List<PermissionGroupUserMapping> _permissionGroupUserMappings = new List<PermissionGroupUserMapping>();
|
||||
//get contexts where the user has permission
|
||||
foreach (var item in assignedUsers)
|
||||
{
|
||||
//get the product where the permissioncontextmapping is
|
||||
var contextMapping = await Ctx.PermissionContextMappings.FirstOrDefaultAsync(x => x.SubjectId == item.Id);
|
||||
if (contextMapping != null)
|
||||
{
|
||||
_permissions.Add(new AssignedPermissionModel(item.ContextId, item.Id, (short)PermissionContextMappingSubjectType.User, item.Id.ToString(), contextMapping.Permissions));
|
||||
}
|
||||
//get permissiongroupusermappings where the user is in the group
|
||||
_permissionGroupUserMappings = await Ctx.PermissionGroupUserMappings.Where(x => x.AssignedUserId == item.Id).ToListAsync();
|
||||
//public async Task<List<AssignedPermissionModel>> GetPermissionModelByUserIdAsync(Guid UserId)
|
||||
//{
|
||||
// List<AssignedPermissionModel> _permissions = new List<AssignedPermissionModel>();
|
||||
// //get all assignedUsers
|
||||
// List<AssignedUser> assignedUsers = await Context.AssignedUsers.Where(x => x.EmployeeUserId == UserId).ToListAsync();
|
||||
// //List<PermissionContextMapping> _permissionContextMappings = new List<PermissionContextMapping>();
|
||||
// List<PermissionGroupUserMapping> _permissionGroupUserMappings = new List<PermissionGroupUserMapping>();
|
||||
// //get contexts where the user has permission
|
||||
// foreach (var item in assignedUsers)
|
||||
// {
|
||||
// //get the product where the permissioncontextmapping is
|
||||
// var contextMapping = await Context.PermissionContextMappings.FirstOrDefaultAsync(x => x.SubjectId == item.Id);
|
||||
// if (contextMapping != null)
|
||||
// {
|
||||
// _permissions.Add(new AssignedPermissionModel(item.ContextId, item.Id, PermissionContextMappingSubjectType.User, item.Id.ToString(), contextMapping.Permissions));
|
||||
// }
|
||||
// //get permissiongroupusermappings where the user is in the group
|
||||
// _permissionGroupUserMappings = await Context.PermissionGroupUserMappings.Where(x => x.AssignedUserId == item.Id).ToListAsync();
|
||||
|
||||
foreach (var groupUserMapping in _permissionGroupUserMappings)
|
||||
{
|
||||
//get the permissioncontextmapping where the permissiongroup is
|
||||
var contextMapping2 = await Ctx.PermissionContextMappings.FirstOrDefaultAsync(x => x.Id == groupUserMapping.PermissionContextMappingId);
|
||||
if (contextMapping2 != null)
|
||||
{
|
||||
// foreach (var groupUserMapping in _permissionGroupUserMappings)
|
||||
// {
|
||||
// //get the permissioncontextmapping where the permissiongroup is
|
||||
// var contextMapping2 = await Context.PermissionContextMappings.FirstOrDefaultAsync(x => x.Id == groupUserMapping.PermissionContextMappingId);
|
||||
// if (contextMapping2 != null)
|
||||
// {
|
||||
|
||||
//get the group so we have the contextId
|
||||
var group = await Ctx.PermissionGroups.FirstOrDefaultAsync(x => x.Id == contextMapping2.SubjectId);
|
||||
// //get the group so we have the contextId
|
||||
// var group = await Context.PermissionGroups.FirstOrDefaultAsync(x => x.Id == contextMapping2.SubjectId);
|
||||
|
||||
_permissions.Add(new AssignedPermissionModel(group.ContextId, contextMapping2.SubjectId, (short)PermissionContextMappingSubjectType.Group, group.GroupName, contextMapping2.Permissions));
|
||||
}
|
||||
}
|
||||
// _permissions.Add(new AssignedPermissionModel(group.ContextId, contextMapping2.SubjectId, PermissionContextMappingSubjectType.Group, group.GroupName, contextMapping2.Permissions));
|
||||
// }
|
||||
// }
|
||||
|
||||
}
|
||||
// }
|
||||
|
||||
return _permissions;
|
||||
}
|
||||
// return _permissions;
|
||||
//}
|
||||
|
||||
//3. (IPermissionService) get permissions of assigned users and groups
|
||||
public Task<List<AssignedPermissionModel>> GetPermissionsOfAssignedUsersAndGroupsAsyncByContextId(Guid contextId)
|
||||
{
|
||||
List<AssignedPermissionModel> result = new List<AssignedPermissionModel>();
|
||||
|
||||
var AssignedUsers = Ctx.AssignedUsers.Where(x => x.ContextId == contextId).ToListAsync();
|
||||
var AssignedUsers = Context.AssignedUsers.Where(x => x.ContextId == contextId).ToListAsync();
|
||||
|
||||
if (AssignedUsers.Result != null)
|
||||
{
|
||||
foreach (var item in AssignedUsers.Result)
|
||||
{
|
||||
var mappingRow = Ctx.PermissionContextMappings.Where(x => x.SubjectId == item.Id).ToListAsync();
|
||||
var mappingRow = Context.PermissionContextMappings.Where(x => x.SubjectId == item.Id).ToListAsync();
|
||||
if (mappingRow.Result == null)
|
||||
{
|
||||
//user has no permission but is assigned... must be banned
|
||||
|
|
@ -269,13 +272,13 @@ namespace TIAM.Database.DataLayers.ServiceProviders
|
|||
}
|
||||
}
|
||||
|
||||
var AssingedGroups = Ctx.PermissionGroups.Where(x => x.ContextId == contextId).ToListAsync();
|
||||
var AssingedGroups = Context.PermissionGroups.Where(x => x.ContextId == contextId).ToListAsync();
|
||||
|
||||
if (AssingedGroups.Result != null)
|
||||
{
|
||||
foreach (var group in AssingedGroups.Result)
|
||||
{
|
||||
var mappingRow = Ctx.PermissionContextMappings.Where(x => x.SubjectId == group.Id).ToListAsync();
|
||||
var mappingRow = Context.PermissionContextMappings.Where(x => x.SubjectId == group.Id).ToListAsync();
|
||||
if (mappingRow.Result == null)
|
||||
{
|
||||
//group has no permission but is assigned...
|
||||
|
|
@ -310,13 +313,15 @@ namespace TIAM.Database.DataLayers.ServiceProviders
|
|||
{
|
||||
List<PermissionContextMapping> permissionContextMappings = new List<PermissionContextMapping>();
|
||||
//get all Groups where the contextId is the same
|
||||
var groups = Ctx.PermissionGroups.Where(x => x.ContextId == contextId).ToListAsync();
|
||||
foreach (var item in groups.Result)
|
||||
{
|
||||
//get permissioncontextmapping for the group if there is, so we know what permissions the group has
|
||||
var pCm = Ctx.PermissionContextMappings.FirstOrDefault(x => x.SubjectId == item.Id);
|
||||
permissionContextMappings.Add(pCm);
|
||||
}
|
||||
var groups = Context.PermissionGroups.Where(x => x.ContextId == contextId).Select(x=>x.Id).ToHashSet();
|
||||
permissionContextMappings = Context.PermissionContextMappings.Where(x => groups.Contains(x.SubjectId)).ToList();
|
||||
|
||||
//foreach (var item in groups.Result)
|
||||
//{
|
||||
// //get permissioncontextmapping for the group if there is, so we know what permissions the group has
|
||||
// var pCm = Context.PermissionContextMappings.FirstOrDefault(x => x.SubjectId == item.Id);
|
||||
// permissionContextMappings.Add(pCm);
|
||||
//}
|
||||
return Task.FromResult(permissionContextMappings);
|
||||
}
|
||||
|
||||
|
|
@ -324,12 +329,12 @@ namespace TIAM.Database.DataLayers.ServiceProviders
|
|||
public Task<bool> AddUserToPermissionGroupAsync(Guid permissionGroupId, Guid userId)
|
||||
{
|
||||
bool result = false;
|
||||
using (var transaction = Ctx.Database.BeginTransaction())
|
||||
using (var transaction = Context.Database.BeginTransaction())
|
||||
{
|
||||
//do we need to check if PermissionContextMappingId exists?
|
||||
var permissionGroupUserMapping = new PermissionGroupUserMapping(userId, permissionGroupId);
|
||||
Ctx.PermissionGroupUserMappings.Add(permissionGroupUserMapping);
|
||||
Ctx.SaveChanges();
|
||||
Context.PermissionGroupUserMappings.Add(permissionGroupUserMapping);
|
||||
Context.SaveChanges();
|
||||
transaction.Commit();
|
||||
result = true;
|
||||
}
|
||||
|
|
@ -340,24 +345,24 @@ namespace TIAM.Database.DataLayers.ServiceProviders
|
|||
public Task<bool> CreatePermissionGroupAsync(PermissionGroup permissionGroup, TiamServiceProvider serviceProvider)
|
||||
{
|
||||
bool result = false;
|
||||
using (var transaction = Ctx.Database.BeginTransaction())
|
||||
using (var transaction = Context.Database.BeginTransaction())
|
||||
{
|
||||
var existingPermissionGroup = Ctx.PermissionGroups.FirstOrDefault(x => x.GroupName == permissionGroup.GroupName)?.GroupName;
|
||||
var existingPermissionGroup = Context.PermissionGroups.FirstOrDefault(x => x.GroupName == permissionGroup.GroupName)?.GroupName;
|
||||
if (existingPermissionGroup == null)
|
||||
{
|
||||
//create permission type 1 for the group
|
||||
var permissionType = new PermissionsType(serviceProvider.Id, "View");
|
||||
Ctx.PermissionsTypes.Add(permissionType);
|
||||
Context.PermissionsTypes.Add(permissionType);
|
||||
|
||||
//Create PermissionContextMapping for the group
|
||||
|
||||
//create Id for the group
|
||||
Guid Id = Guid.NewGuid();
|
||||
permissionGroup.Id = Id;
|
||||
var permissionContextMapping = new PermissionContextMapping(serviceProvider.Id, Id, (short)PermissionContextMappingSubjectType.Group, 1, true);
|
||||
Ctx.PermissionContextMappings.Add(permissionContextMapping);
|
||||
Ctx.PermissionGroups.Add(permissionGroup);
|
||||
Ctx.SaveChanges();
|
||||
var permissionContextMapping = new PermissionContextMapping(serviceProvider.Id, Id, PermissionContextMappingSubjectType.Group, 1, true);
|
||||
Context.PermissionContextMappings.Add(permissionContextMapping);
|
||||
Context.PermissionGroups.Add(permissionGroup);
|
||||
Context.SaveChanges();
|
||||
transaction.Commit();
|
||||
result = true;
|
||||
}
|
||||
|
|
@ -370,26 +375,27 @@ namespace TIAM.Database.DataLayers.ServiceProviders
|
|||
return Task.FromResult(result);
|
||||
}
|
||||
|
||||
public Task<List<AssignedUser>> GetAssingedUsersInPermissionGroupByGroupId(Guid groupId)
|
||||
public List<AssignedUser> GetAssingedUsersInPermissionGroupByGroupId(Guid groupId)
|
||||
{
|
||||
List<AssignedUser> assignedUsers = new List<AssignedUser>();
|
||||
return Context.GetAssignedUsersByPermissionGroupId(groupId).ToList();
|
||||
//List<AssignedUser> assignedUsers = new List<AssignedUser>();
|
||||
|
||||
//let's get the permissioncontextmapping for the group
|
||||
var pCm = Ctx.PermissionContextMappings.FirstOrDefault(x => x.SubjectId == groupId);
|
||||
Guid pCmId = pCm.Id;
|
||||
////let's get the permissioncontextmapping for the group
|
||||
//var pCm = Context.PermissionContextMappings.FirstOrDefault(x => x.SubjectId == groupId);
|
||||
//Guid pCmId = pCm.Id;
|
||||
|
||||
//let's get the permissiongroupusermappings for the permissioncontextmapping
|
||||
var pGum = Ctx.PermissionGroupUserMappings.Where(x => x.PermissionContextMappingId == pCmId).ToList();
|
||||
if (pGum.Count > 0)
|
||||
{
|
||||
foreach (var group in pGum)
|
||||
{
|
||||
assignedUsers.Add(Ctx.AssignedUsers.FirstOrDefault(x => x.Id == group.AssignedUserId));
|
||||
}
|
||||
////let's get the permissiongroupusermappings for the permissioncontextmapping
|
||||
//var pGum = Context.PermissionGroupUserMappings.Where(x => x.PermissionContextMappingId == pCmId).ToList();
|
||||
//if (pGum.Count > 0)
|
||||
//{
|
||||
// foreach (var group in pGum)
|
||||
// {
|
||||
// assignedUsers.Add(Context.AssignedUsers.FirstOrDefault(x => x.Id == group.AssignedUserId));
|
||||
// }
|
||||
|
||||
}
|
||||
//}
|
||||
|
||||
return Task.FromResult(assignedUsers);
|
||||
//return Task.FromResult(assignedUsers);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
|
@ -402,21 +408,21 @@ namespace TIAM.Database.DataLayers.ServiceProviders
|
|||
|
||||
|
||||
|
||||
Ctx.Products.Add(product);
|
||||
Context.Products.Add(product);
|
||||
Console.WriteLine($"Saving product to db {product.Id}, {product.Name}, {product.OwnerId}");
|
||||
return Ctx.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
||||
return Context.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
||||
|
||||
}
|
||||
|
||||
//20. (IServiceProviderDataService) Update product
|
||||
public Task<bool> UpdateProductAsync(TiamProduct product)
|
||||
{
|
||||
var dbProduct = Ctx.Products.FirstOrDefault(u => u.Id == product.Id);
|
||||
var dbProduct = Context.Products.FirstOrDefault(u => u.Id == product.Id);
|
||||
if (dbProduct != null)
|
||||
{
|
||||
dbProduct = product;
|
||||
Ctx.Products.Update(dbProduct);
|
||||
return Ctx.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
||||
Context.Products.Update(dbProduct);
|
||||
return Context.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -425,44 +431,46 @@ namespace TIAM.Database.DataLayers.ServiceProviders
|
|||
}
|
||||
|
||||
//21. (IServiceProviderDataService) delete product
|
||||
public Task<bool> DeleteProductAsync(Guid id)
|
||||
public Task<bool> DeleteProductByIdAsync(Guid productId)
|
||||
{
|
||||
using (var transaction = Ctx.Database.BeginTransaction())
|
||||
return TransactionAsync(ctx =>
|
||||
{
|
||||
var dbProduct = Ctx.Products.FirstOrDefault(u => u.Id == id);
|
||||
if (dbProduct != null)
|
||||
{
|
||||
//get assignedUsers for this product
|
||||
var assignedUsers = Ctx.AssignedUsers.Where(x => x.ContextId == id).ToList();
|
||||
//remove assignedUsers
|
||||
foreach (var item in assignedUsers)
|
||||
{
|
||||
RemoveAssignedUserByUserId(item.Id);
|
||||
}
|
||||
return Ctx.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
return Task.FromResult(false);
|
||||
}
|
||||
}
|
||||
ctx.DeleteProductById(productId);
|
||||
|
||||
//var dbProduct = ctx.Products.FirstOrDefault(u => u.Id == id);
|
||||
//if (dbProduct != null)
|
||||
//{
|
||||
// ctx.CleanUpAndRemoveAssignedUser();
|
||||
// //get assignedUsers for this product
|
||||
// var assignedUsers = ctx.AssignedUsers.Where(x => x.ContextId == id).ToList();
|
||||
// //remove assignedUsers
|
||||
// foreach (var item in assignedUsers)
|
||||
// {
|
||||
// await RemoveAssignedUserByUserIdAsync(item.Id);
|
||||
// }
|
||||
|
||||
// return true;
|
||||
//}
|
||||
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
//4. (IPermissionService) AssignPermissionToUserForContextAsync
|
||||
public Task<bool> AssignPermissionToUserForContextAsync(AssignedUser assignedUser, PermissionsType permission)
|
||||
{
|
||||
var _assIgnedUser = Ctx.AssignedUsers.FirstOrDefault(x => x.Id == assignedUser.Id);
|
||||
var _assIgnedUser = Context.AssignedUsers.FirstOrDefault(x => x.Id == assignedUser.Id);
|
||||
|
||||
if(_assIgnedUser != null)
|
||||
{
|
||||
//user exists
|
||||
var _permissionInt = GetPermissionFromPermissionType(permission);
|
||||
|
||||
var permissionContextMapping = Ctx.PermissionContextMappings.FirstOrDefault(x => x.SubjectId == assignedUser.Id);
|
||||
var permissionContextMapping = Context.PermissionContextMappings.FirstOrDefault(x => x.SubjectId == assignedUser.Id);
|
||||
var currentPermissions = permissionContextMapping.Permissions;
|
||||
var newPermissions = currentPermissions + _permissionInt.Result;
|
||||
permissionContextMapping.Permissions = newPermissions;
|
||||
return Ctx.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
||||
return Context.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
@ -479,107 +487,108 @@ namespace TIAM.Database.DataLayers.ServiceProviders
|
|||
//22. (IServiceProviderDataService) Create assignedUser
|
||||
public Task<AssignedUser> CreateAssignedUserAsync(AssignedUser assignedUser)
|
||||
{
|
||||
Ctx.AssignedUsers.Add(assignedUser);
|
||||
Context.AssignedUsers.Add(assignedUser);
|
||||
Console.WriteLine($"Saving assignedUser to db {assignedUser.Id}, {assignedUser.ContextId}, {assignedUser.EmployeeUserId}, {assignedUser.UserRoles}");
|
||||
return Ctx.SaveChangesAsync().ContinueWith(x => assignedUser);
|
||||
return Context.SaveChangesAsync().ContinueWith(x => assignedUser);
|
||||
}
|
||||
|
||||
//23. (IServiceProviderDataService) Get Assigned Users By ProductId
|
||||
public Task<List<AssignedUser>> GetAssignedUsersByProductIdAsync(Guid productId)
|
||||
{
|
||||
return Ctx.AssignedUsers.Where(x => x.ContextId == productId).ToListAsync();
|
||||
return Context.AssignedUsers.Where(x => x.ContextId == productId).ToListAsync();
|
||||
}
|
||||
|
||||
|
||||
//24 . (IServiceProviderDataService) Remove Assigned Users By Product Id
|
||||
public Task RemoveAssignedUsersByContextId(Guid contextId)
|
||||
{
|
||||
using (var transaction = Ctx.Database.BeginTransaction())
|
||||
using (var transaction = Context.Database.BeginTransaction())
|
||||
{
|
||||
var assignedUsers = Ctx.AssignedUsers.Where(x => x.ContextId == contextId).ToList();
|
||||
var assignedUsers = Context.AssignedUsers.Where(x => x.ContextId == contextId).ToList();
|
||||
//remove assignedUsers
|
||||
|
||||
return Ctx.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
||||
return Context.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
//25. (IServiceProviderDataService) Remove Assigned from product by AssignedUserId
|
||||
public Task RemoveAssignedUser(AssignedUser assignedUser, bool removeFromGroups)
|
||||
//25. (IServiceProviderDataService) Remove Assigned from product by assignedUserId
|
||||
public Task<bool> RemoveAssignedUserAsync(AssignedUser assignedUser, bool removeFromGroups)
|
||||
{
|
||||
using (var transaction = Ctx.Database.BeginTransaction())
|
||||
return TransactionAsync(ctx =>
|
||||
{
|
||||
var assignedUserToRemove = Ctx.AssignedUsers.FirstOrDefault(x => x.Id == assignedUser.Id);
|
||||
//remove assignedUsers
|
||||
if (assignedUserToRemove != null)
|
||||
{
|
||||
if(removeFromGroups)
|
||||
{
|
||||
//remove permissiongroupusermappings
|
||||
RemoveAssingedUserFromAllProductPermissionGroups(assignedUserToRemove.Id);
|
||||
}
|
||||
Ctx.AssignedUsers.Remove(assignedUserToRemove);
|
||||
}
|
||||
return Ctx.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
||||
var result = false;
|
||||
var assignedUserToRemove = ctx.AssignedUsers.FirstOrDefault(x => x.Id == assignedUser.Id);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public Task RemoveAssignedUserByUserId(Guid assignedUserId)
|
||||
{
|
||||
|
||||
using (var transaction = Ctx.Database.BeginTransaction())
|
||||
{
|
||||
var assignedUser = Ctx.AssignedUsers.FirstOrDefault(x => x.Id == assignedUserId);
|
||||
//remove assignedUsers
|
||||
if (assignedUser != null)
|
||||
if (assignedUserToRemove == null) return false;
|
||||
|
||||
|
||||
if (removeFromGroups)
|
||||
{
|
||||
//CleanUp
|
||||
//remove permissioncontextmappings
|
||||
RemoveAssignedUserContextMappingByAssignedUserId(assignedUserId);
|
||||
//remove permissiongroupusermappings
|
||||
RemoveAssingedUserFromAllProductPermissionGroups(assignedUserId);
|
||||
|
||||
ctx.RemoveAssingedUserFromPermissionGroups(assignedUserToRemove.Id);
|
||||
}
|
||||
return Ctx.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
||||
|
||||
}
|
||||
ctx.AssignedUsers.Remove(assignedUserToRemove);
|
||||
|
||||
|
||||
return result;
|
||||
});
|
||||
}
|
||||
|
||||
public Task RemoveAssignedUserContextMappingByAssignedUserId(Guid AssignedUserId)
|
||||
{
|
||||
using (var transaction = Ctx.Database.BeginTransaction())
|
||||
{
|
||||
PermissionContextMapping? contextMapping = Ctx.PermissionContextMappings.FirstOrDefault(x => x.SubjectId == AssignedUserId);
|
||||
//remove assignedUsers
|
||||
if(contextMapping != null)
|
||||
{
|
||||
Ctx.PermissionContextMappings.Remove(contextMapping);
|
||||
}
|
||||
return Ctx.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
||||
//public Task RemoveAssignedUserByUserIdAsync(Guid assignedUserId)
|
||||
//{
|
||||
// return TransactionAsync(ctx =>
|
||||
// {
|
||||
// var assignedUser = ctx.AssignedUsers.FirstOrDefault(x => x.Id == assignedUserId);
|
||||
// //remove assignedUsers
|
||||
// if (assignedUser == null) return false;
|
||||
|
||||
}
|
||||
}
|
||||
// //CleanUp
|
||||
// //remove permissioncontextmappings
|
||||
// ctx.RemoveAssignedUserContextMappingBySubjectId(assignedUserId);
|
||||
// //remove permissiongroupusermappings
|
||||
// ctx.RemoveAssingedUserFromAllProductPermissionGroups(assignedUserId);
|
||||
|
||||
// return true;
|
||||
// });
|
||||
//}
|
||||
|
||||
//public Task RemoveAssignedUserContextMappingByAssignedUserId(Guid assignedUserId)
|
||||
//{
|
||||
// using (var transaction = Context.Database.BeginTransaction())
|
||||
// {
|
||||
// PermissionContextMapping? contextMapping = Context.PermissionContextMappings.FirstOrDefault(x => x.SubjectId == assignedUserId);
|
||||
// //remove assignedUsers
|
||||
// if(contextMapping != null)
|
||||
// {
|
||||
// Context.PermissionContextMappings.Remove(contextMapping);
|
||||
// }
|
||||
// return Context.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
||||
|
||||
// }
|
||||
//}
|
||||
|
||||
|
||||
public Task RemoveAssingedUserFromAllProductPermissionGroups(Guid assignedUserId)
|
||||
{
|
||||
using (var transaction = Ctx.Database.BeginTransaction())
|
||||
{
|
||||
var permissionGroupUserMapping = Ctx.PermissionGroupUserMappings.Where(x => x.AssignedUserId == assignedUserId);
|
||||
//remove assignedUsers
|
||||
//public Task RemoveAssingedUserFromAllProductPermissionGroups(Guid assignedUserId)
|
||||
//{
|
||||
// using (var transaction = Context.Database.BeginTransaction())
|
||||
// {
|
||||
// var permissionGroupUserMapping = Context.PermissionGroupUserMappings.Where(x => x.AssignedUserId == assignedUserId);
|
||||
// //remove assignedUsers
|
||||
|
||||
if (permissionGroupUserMapping != null)
|
||||
{
|
||||
foreach (var item in permissionGroupUserMapping)
|
||||
{
|
||||
Ctx.PermissionGroupUserMappings.Remove(item);
|
||||
}
|
||||
}
|
||||
return Ctx.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
||||
// if (permissionGroupUserMapping != null)
|
||||
// {
|
||||
// foreach (var item in permissionGroupUserMapping)
|
||||
// {
|
||||
// Context.PermissionGroupUserMappings.Remove(item);
|
||||
// }
|
||||
// }
|
||||
// return Context.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
||||
|
||||
}
|
||||
}
|
||||
// }
|
||||
//}
|
||||
|
||||
|
||||
#endregion
|
||||
|
|
|
|||
|
|
@ -0,0 +1,109 @@
|
|||
using AyCode.Database.DataLayers.Users;
|
||||
using AyCode.Models.Enums;
|
||||
using TIAM.Database.DbContexts;
|
||||
using TIAM.Entities.Permissions;
|
||||
using TIAM.Entities.Users;
|
||||
|
||||
namespace TIAM.Database.DataLayers.ServiceProviders;
|
||||
|
||||
public static class ServiceProviderDalExtension
|
||||
{
|
||||
public static IQueryable<AssignedUser> GetAssignedUsersByPermissionGroupId(this ServiceProviderDbContext ctx, Guid permissionGroupId)
|
||||
{
|
||||
return ctx.AssignedUsers
|
||||
.Where(user => ctx.PermissionGroupUserMappings
|
||||
.Where(x => x.PermissionGroupId == permissionGroupId)
|
||||
.Select(x => x.AssignedUserId)
|
||||
.Contains(user.Id));
|
||||
}
|
||||
|
||||
//public static IQueryable<PermissionGroup> GetPermissionGroupByContextMapping(this ServiceProviderDbContext ctx, PermissionContextMapping permissionContextMapping)
|
||||
//{
|
||||
// if (permissionContextMapping.SubjectType == PermissionContextMappingSubjectType.Group)
|
||||
// return ctx.PermissionGroups.Where(x => x.Id == permissionContextMapping.SubjectId);
|
||||
// else if (permissionContextMapping.SubjectType == PermissionContextMappingSubjectType.User)
|
||||
// return ctx.PermissionGroups.Where(x => x.Id == permissionContextMapping.SubjectId);
|
||||
//}
|
||||
|
||||
public static IQueryable<PermissionContextMapping> GetPermissionContextMappingByContextId(this ServiceProviderDbContext ctx, Guid contextId)
|
||||
{
|
||||
var subjectIds = ctx.GetAssignedUsersByContextId(contextId).Select(x => x.Id).
|
||||
Concat(ctx.PermissionGroups.Where(x => x.ContextId == contextId).Select(x => x.Id)).ToHashSet();
|
||||
|
||||
return ctx.GetPermissionContextMappingsBySubjectIds(subjectIds);
|
||||
}
|
||||
|
||||
//public static IQueryable<PermissionContextMapping> GetPermissionContextMappingByAssignedUserId(this ServiceProviderDbContext ctx, Guid assignedUserId)
|
||||
//{
|
||||
// var subjectIds = ctx.GetAssignedUsersByContextId(assignedUserId).Select(x => x.Id).
|
||||
// Concat(ctx.PermissionGroupUserMappings.Where(x => x.AssignedUserId == assignedUserId).Select(x => x.)).ToHashSet();
|
||||
|
||||
// return ctx.GetPermissionContextMappingsBySubjectIds(subjectIds);
|
||||
//}
|
||||
|
||||
public static IQueryable<PermissionContextMapping> GetPermissionContextMappingsBySubjectIds(this ServiceProviderDbContext ctx, IEnumerable<Guid> subjectIds)
|
||||
=> ctx.PermissionContextMappings.Where(x => subjectIds.Contains(x.SubjectId));
|
||||
|
||||
public static PermissionContextMapping? GetPermissionContextMappingBySubjectId(this ServiceProviderDbContext ctx, Guid subjectId)
|
||||
=> ctx.PermissionContextMappings.FirstOrDefault(x => x.SubjectId == subjectId);
|
||||
|
||||
public static void RemoveContextMappingBySubjectId(this ServiceProviderDbContext ctx, Guid subjectId)
|
||||
{
|
||||
var contextMapping = ctx.GetPermissionContextMappingBySubjectId(subjectId);
|
||||
|
||||
if (contextMapping == null) return;
|
||||
|
||||
ctx.PermissionContextMappings.Remove(contextMapping);
|
||||
}
|
||||
|
||||
public static IQueryable<PermissionGroupUserMapping> GetAllPermissionGroupsByAssignedUserId(this ServiceProviderDbContext ctx, Guid assignedUserId)
|
||||
=> ctx.PermissionGroupUserMappings.Where(x => x.AssignedUserId == assignedUserId);
|
||||
|
||||
public static void DeleteProductById(this ServiceProviderDbContext ctx, Guid productId)
|
||||
{
|
||||
var product = ctx.Products.FirstOrDefault(u => u.Id == productId);
|
||||
if (product == null) return;
|
||||
|
||||
ctx.RemoveAssignedUsers(ctx.GetAssignedUsersByContextId(productId));
|
||||
ctx.Products.Remove(product);
|
||||
}
|
||||
|
||||
public static void RemoveAssingedUserFromPermissionGroups(this ServiceProviderDbContext ctx, Guid assignedUserId)
|
||||
{
|
||||
ctx.PermissionGroupUserMappings.RemoveRange(ctx.GetAllPermissionGroupsByAssignedUserId(assignedUserId));
|
||||
}
|
||||
|
||||
public static void RemoveAssignedUsers(this ServiceProviderDbContext ctx, IEnumerable<AssignedUser> assignedUsers)
|
||||
{
|
||||
foreach (var assignedUser in assignedUsers)
|
||||
{
|
||||
ctx.CleanUpAndRemoveAssignedUser(assignedUser);
|
||||
}
|
||||
}
|
||||
|
||||
public static void CleanUpAndRemoveAssignedUser(this ServiceProviderDbContext ctx, AssignedUser assignedUser)
|
||||
{
|
||||
ctx.RemoveContextMappingBySubjectId(assignedUser.Id);
|
||||
ctx.RemoveAssingedUserFromPermissionGroups(assignedUser.Id);
|
||||
|
||||
ctx.AssignedUsers.Remove(assignedUser);
|
||||
}
|
||||
|
||||
public static bool RemoveAssignedUserById(this ServiceProviderDbContext ctx, Guid assignedUserId)
|
||||
{
|
||||
var assignedUser = ctx.GetAssignedUserById(assignedUserId);
|
||||
|
||||
if (assignedUser == null) return false;
|
||||
|
||||
ctx.CleanUpAndRemoveAssignedUser(assignedUser);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public static AssignedUser? GetAssignedUserById(this ServiceProviderDbContext ctx, Guid assignedUserId)
|
||||
=> ctx.AssignedUsers.FirstOrDefault(x => x.Id == assignedUserId);
|
||||
|
||||
public static IQueryable<AssignedUser> GetAssignedUsersByContextId(this ServiceProviderDbContext ctx, Guid contextId)
|
||||
=> ctx.AssignedUsers.Where(x => x.ContextId == contextId);
|
||||
|
||||
}
|
||||
|
|
@ -22,21 +22,21 @@ public class TransferDestinationDal : TiamDalBase<TransferDestinationDbContext>
|
|||
{
|
||||
//transferDestination.Created = DateTime.UtcNow;
|
||||
//transferDestination.Modified = DateTime.UtcNow;
|
||||
Ctx.TransferDestinations.Add(transferDestination);
|
||||
return Ctx.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
||||
Context.TransferDestinations.Add(transferDestination);
|
||||
return Context.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
||||
}
|
||||
|
||||
public Task<bool> UpdateTransferDestinationAsync(TransferDestination transferDestination)
|
||||
{
|
||||
//transferDestination.Modified = DateTime.UtcNow;
|
||||
Ctx.TransferDestinations.Update(transferDestination);
|
||||
return Ctx.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
||||
Context.TransferDestinations.Update(transferDestination);
|
||||
return Context.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
||||
}
|
||||
|
||||
public Task<List<TransferDestination>> GetTransferDestinations()
|
||||
{
|
||||
|
||||
return Ctx.TransferDestinations.ToListAsync();
|
||||
return Context.TransferDestinations.ToListAsync();
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -41,48 +41,56 @@ namespace TIAM.Database.DataLayers.Users
|
|||
{
|
||||
Console.WriteLine($"Getting user from db {phoneNumber}");
|
||||
var phoneNumberLower = phoneNumber.ToLower();
|
||||
return Context.Users.SingleOrDefaultAsync(x=>x.PhoneNumber.ToLower() == phoneNumberLower);
|
||||
return Context.Users.SingleOrDefaultAsync(x=>x.PhoneNumber.Equals(phoneNumberLower, StringComparison.CurrentCultureIgnoreCase));
|
||||
}
|
||||
|
||||
public Task<User?> GetUserByEmailOrPhoneNumberAsync(string emailOrPhoneNumber)
|
||||
{
|
||||
Console.WriteLine($"Getting user from db {emailOrPhoneNumber}");
|
||||
var emailOrPhoneNumberLower = emailOrPhoneNumber.ToLower();
|
||||
return Context.Users.SingleOrDefaultAsync(x=>x.EmailAddress.ToLower() == emailOrPhoneNumberLower || x.PhoneNumber.ToLower() == emailOrPhoneNumberLower);
|
||||
return Context.Users.SingleOrDefaultAsync(x=>x.EmailAddress.Equals(emailOrPhoneNumberLower, StringComparison.CurrentCultureIgnoreCase) || x.PhoneNumber.Equals(emailOrPhoneNumberLower, StringComparison.CurrentCultureIgnoreCase));
|
||||
}
|
||||
|
||||
//get user by Id
|
||||
public virtual Task<User?> GetUserByIdAsync(Guid id)
|
||||
{
|
||||
Console.WriteLine($"Getting user from db {id}");
|
||||
return Context.Users.SingleOrDefaultAsync(x=>x.Id == id);
|
||||
}
|
||||
////get user by Id
|
||||
//public Task<User?> GetUserByIdAsync(Guid id)
|
||||
//{
|
||||
// Console.WriteLine($"Getting user from db {id}");
|
||||
// return Context.Users.SingleOrDefaultAsync(x=>x.Id == id);
|
||||
//}
|
||||
|
||||
public Task<bool> CreateUserAsync(User user)
|
||||
public async Task<bool> CreateUserAsync(User user)
|
||||
{
|
||||
user.Created = DateTime.UtcNow;
|
||||
user.Modified = DateTime.UtcNow;
|
||||
Context.Users.Add(user);
|
||||
Console.WriteLine($"Saving user to db {user.Id}, {user.EmailAddress}, {user.PhoneNumber}, {user.Password}");
|
||||
return Context.SaveChangesAsync().ContinueWith(x=>x.Result > 0);
|
||||
return await Context.SaveChangesAsync() > 0;
|
||||
}
|
||||
|
||||
public Task<bool> UpdateUserAsyncOld(User user)
|
||||
{
|
||||
user.Modified = DateTime.UtcNow;
|
||||
Context.Users.Update(user);
|
||||
return Context.SaveChangesAsync().ContinueWith(x=>x.Result > 0);
|
||||
}
|
||||
|
||||
public Task<bool> UpdateUserAsync(User user)
|
||||
public async Task<bool> UpdateJwtRefreshTokenAsync(string email, string refreshToken)
|
||||
{
|
||||
var existingUser = Context.Users.FirstOrDefault(u => u.EmailAddress == user.EmailAddress);
|
||||
var existingUser = Context.Users.FirstOrDefault(u => u.EmailAddress == email);
|
||||
if (existingUser != null)
|
||||
{
|
||||
//user.Modified = DateTime.UtcNow; //ezt nem kell megadni, a háttérben ezt magától megcsinálja a DbContextBase - J.
|
||||
existingUser = user;
|
||||
existingUser.RefreshToken = refreshToken;
|
||||
|
||||
Context.Users.Update(existingUser);
|
||||
return Context.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
||||
return await Context.SaveChangesAsync() > 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new Exception("User not found");
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<bool> UpdateUserAsync(User user)
|
||||
{
|
||||
var existingUser = await Context.Users.CountAsync(u => u.EmailAddress == user.EmailAddress);
|
||||
if (existingUser == 1)
|
||||
{
|
||||
//user.Modified = DateTime.UtcNow; //ezt nem kell megadni, a háttérben ezt magától megcsinálja a DbContextBase - J.
|
||||
Context.Users.Update(user);
|
||||
return await Context.SaveChangesAsync() > 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
|||
|
|
@ -4,6 +4,8 @@ using System.Linq;
|
|||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using AyCode.Database.DbContexts;
|
||||
using AyCode.Database.DbContexts.Users;
|
||||
using AyCode.Entities.Users;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using TIAM.Entities.Permissions;
|
||||
using TIAM.Entities.Products;
|
||||
|
|
@ -12,8 +14,12 @@ using TIAM.Entities.Users;
|
|||
|
||||
namespace TIAM.Database.DbContexts
|
||||
{
|
||||
public class ServiceProviderDbContext : TiamDbContextBase
|
||||
public class ServiceProviderDbContext : TiamDbContextBase, IUserDbContextBase<User, UserTokenBase>
|
||||
{
|
||||
public DbSet<User> Users { get; set; }
|
||||
public DbSet<UserTokenBase> UserTokens { get; set; }
|
||||
|
||||
|
||||
public virtual DbSet<TiamServiceProvider> ServiceProviders { get; set; }
|
||||
public virtual DbSet<TiamProduct> Products { get; set; }
|
||||
public virtual DbSet<AssignedUser> AssignedUsers { get; set; }
|
||||
|
|
@ -45,6 +51,5 @@ namespace TIAM.Database.DbContexts
|
|||
optionsBuilder.EnableDetailedErrors(true);
|
||||
base.OnConfiguring(optionsBuilder);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
using System.ComponentModel.DataAnnotations.Schema;
|
||||
using AyCode.Interfaces.Entities;
|
||||
using AyCode.Interfaces.TimeStampInfo;
|
||||
using AyCode.Models.Enums;
|
||||
|
||||
namespace TIAM.Entities.Permissions;
|
||||
|
||||
|
|
@ -12,15 +13,15 @@ public class PermissionContextMapping : IEntityGuid, ITimeStampInfo
|
|||
public Guid Id { get; set; }
|
||||
public Guid SubjectId { get; set; } //group or user
|
||||
|
||||
public short SubjectType { get; set; } //1 for user, 2 for group
|
||||
public PermissionContextMappingSubjectType SubjectType { get; set; } //1 for user, 2 for group
|
||||
public int Permissions { get; set; }
|
||||
public bool IsBuiltin { get; set; }
|
||||
|
||||
public DateTime Created { get; set; }
|
||||
public DateTime Modified { get; set; }
|
||||
|
||||
public PermissionContextMapping(Guid subjectId, short subjectType, int permissions, bool isBuiltin) : this(Guid.NewGuid(), subjectId, subjectType, permissions, isBuiltin) { }
|
||||
public PermissionContextMapping(Guid id, Guid subjectId, short subjectType, int permissions, bool isBuiltin)
|
||||
public PermissionContextMapping(Guid subjectId, PermissionContextMappingSubjectType subjectType, int permissions, bool isBuiltin) : this(Guid.NewGuid(), subjectId, subjectType, permissions, isBuiltin) { }
|
||||
public PermissionContextMapping(Guid id, Guid subjectId, PermissionContextMappingSubjectType subjectType, int permissions, bool isBuiltin)
|
||||
{
|
||||
Id = id;
|
||||
SubjectId = subjectId;
|
||||
|
|
|
|||
|
|
@ -21,10 +21,8 @@ public class PermissionGroup : GroupBase
|
|||
IsBuiltin = isBuiltin;
|
||||
}
|
||||
|
||||
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
|
||||
public Guid Id { get; set; }
|
||||
|
||||
public Guid ContextId { get; set; }
|
||||
public bool IsPublic { get; set; }
|
||||
public string? GroupName { get; set; }
|
||||
public bool IsBuiltin { get; set; }
|
||||
}
|
||||
|
|
@ -11,7 +11,7 @@ public class PermissionGroupUserMapping : IEntityGuid, ITimeStampInfo
|
|||
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
|
||||
public Guid Id { get; set; }
|
||||
public Guid AssignedUserId { get; set; }
|
||||
public Guid PermissionContextMappingId { get; set; }
|
||||
public Guid PermissionGroupId { get; set; }
|
||||
|
||||
public DateTime Created { get; set; }
|
||||
public DateTime Modified { get; set; }
|
||||
|
|
@ -19,11 +19,11 @@ public class PermissionGroupUserMapping : IEntityGuid, ITimeStampInfo
|
|||
public PermissionGroupUserMapping(Guid assignedUserId, Guid permissionContextMappingId) : this (Guid.NewGuid(), assignedUserId, permissionContextMappingId)
|
||||
{ }
|
||||
|
||||
public PermissionGroupUserMapping(Guid id, Guid assignedUserId, Guid permissionContextMappingId)
|
||||
public PermissionGroupUserMapping(Guid id, Guid assignedUserId, Guid permissionGroupId)
|
||||
{
|
||||
Id = id;
|
||||
AssignedUserId = assignedUserId;
|
||||
PermissionContextMappingId = permissionContextMappingId;
|
||||
PermissionGroupId = permissionGroupId;
|
||||
Created = DateTime.UtcNow;
|
||||
Modified = DateTime.UtcNow;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
using System;
|
||||
using AyCode.Models.Enums;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Security.AccessControl;
|
||||
|
|
@ -11,11 +12,11 @@ namespace TIAM.Entities.Products.DTOs
|
|||
{
|
||||
public Guid ContextId { get; set; }
|
||||
public Guid SubjectId { get; set; } //user or group id
|
||||
public short SubjectType { get; set; } //user or group
|
||||
public PermissionContextMappingSubjectType SubjectType { get; set; } //user or group
|
||||
public string Name { get; set; } //user email or group name
|
||||
public int PermissionsValue { get; set; }
|
||||
|
||||
public AssignedPermissionModel(Guid contextId, Guid subjectId, short subjectType, string name, int permissionsValue)
|
||||
public AssignedPermissionModel(Guid contextId, Guid subjectId, PermissionContextMappingSubjectType subjectType, string name, int permissionsValue)
|
||||
{
|
||||
ContextId = contextId;
|
||||
SubjectId = subjectId;
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@
|
|||
using TIAMMobileApp.Services;
|
||||
using TIAMWebApp.Shared.Application.Interfaces;
|
||||
using DevExpress.Blazor;
|
||||
using TIAMMobilApp.Services;
|
||||
using System.Resources;
|
||||
using AyCode.Interfaces.StorageHandlers;
|
||||
|
||||
|
|
|
|||
|
|
@ -1,29 +1,28 @@
|
|||
using AyCode.Interfaces.StorageHandlers;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.JSInterop;
|
||||
using Newtonsoft.Json;
|
||||
using System.Net.Http.Json;
|
||||
using System.Net.Http.Json;
|
||||
using System.Text;
|
||||
using AyCode.Interfaces.StorageHandlers;
|
||||
using Newtonsoft.Json;
|
||||
using TIAM.Entities.Users;
|
||||
using TIAMWebApp.Shared.Application.Interfaces;
|
||||
using TIAMWebApp.Shared.Application.Models;
|
||||
using TIAMWebApp.Shared.Application.Models.ClientSide;
|
||||
using TIAMWebApp.Shared.Application.Models.PageModels;
|
||||
using TIAMWebApp.Shared.Application.Utility;
|
||||
|
||||
namespace TIAMMobilApp.Services
|
||||
namespace TIAMMobileApp.Services
|
||||
{
|
||||
public class UserDataServiceMobile : IUserDataService
|
||||
{
|
||||
private readonly HttpClient http;
|
||||
private readonly ISecureStorageHandler secureStorageHandler;
|
||||
|
||||
private readonly IServiceProviderDataService _serviceProviderDataService;
|
||||
|
||||
public Dictionary<int, string> userRoleTypes { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
|
||||
|
||||
public UserDataServiceMobile(HttpClient http, ISecureStorageHandler secureStorageHandler)
|
||||
public UserDataServiceMobile(HttpClient http, ISecureStorageHandler secureStorageHandler, IServiceProviderDataService serviceProviderDataService)
|
||||
{
|
||||
this.http = http;
|
||||
this.secureStorageHandler = secureStorageHandler;
|
||||
this.secureStorageHandler = secureStorageHandler;
|
||||
_serviceProviderDataService = serviceProviderDataService;
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -44,25 +43,23 @@ namespace TIAMMobilApp.Services
|
|||
|
||||
public async Task<UserSessionModel> IsLoggedInAsync(Guid id)
|
||||
{
|
||||
UserSessionModel User = null;
|
||||
|
||||
var dbUser = await GetUserByIdAsync(id);
|
||||
var dbUser = await GetUserByIdAsync(id);
|
||||
|
||||
if (dbUser != null)
|
||||
{
|
||||
|
||||
User = new UserSessionModel(dbUser.Id, UserType.User, dbUser.EmailAddress, 1);
|
||||
return User;
|
||||
|
||||
var hasProperties = await _serviceProviderDataService.GetPropertiesByOwnerIdAsync(dbUser.Id);
|
||||
var user = new UserSessionModel(dbUser.Id, UserType.User, dbUser.EmailAddress, hasProperties, 1);
|
||||
|
||||
return user;
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
public async Task<string> TestUserApi(int Param)
|
||||
{
|
||||
var url = APIUrls.UserTest;
|
||||
|
|
|
|||
|
|
@ -2,7 +2,7 @@
|
|||
<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<IsFirstTimeProjectOpen>False</IsFirstTimeProjectOpen>
|
||||
<ActiveDebugFramework>net7.0-windows10.0.19041.0</ActiveDebugFramework>
|
||||
<ActiveDebugFramework>net8.0-windows10.0.19041.0</ActiveDebugFramework>
|
||||
<ActiveDebugProfile>Windows Machine</ActiveDebugProfile>
|
||||
<SelectedPlatformGroup>PhysicalDevice</SelectedPlatformGroup>
|
||||
<DefaultDevice>pixel_5_-_api_31</DefaultDevice>
|
||||
|
|
|
|||
|
|
@ -60,7 +60,7 @@ namespace TIAMWebApp.Client.Services
|
|||
//get user's properties
|
||||
var hasProperties = await serviceProviderDataService.GetPropertiesByOwnerIdAsync(dbUser.Id);
|
||||
//create user session model
|
||||
User = new UserSessionModel(dbUser.Id, UserType.User, dbUser.Email, hasProperties, 1);
|
||||
User = new UserSessionModel(dbUser.Id, UserType.User, dbUser.EmailAddress, hasProperties, 1);
|
||||
return User;
|
||||
}
|
||||
else
|
||||
|
|
|
|||
|
|
@ -96,7 +96,7 @@ namespace TIAMWebApp.Server.Controllers
|
|||
var refreshToken = GenerateRefreshToken();
|
||||
dbUser.RefreshToken = refreshToken;
|
||||
//Update userModel with refreshToken!!
|
||||
await _userDal.UpdateUserAsync(dbUser);
|
||||
await _userDal.UpdateJwtRefreshTokenAsync(dbUser.EmailAddress, dbUser.RefreshToken);
|
||||
|
||||
var response = new MainResponse
|
||||
{
|
||||
|
|
@ -199,7 +199,7 @@ namespace TIAMWebApp.Server.Controllers
|
|||
//mocking - update userModel with new refreshToken
|
||||
dbUser.RefreshToken = refreshToken;
|
||||
//TODO await _userManager.UpdateAsync(userModel);
|
||||
await _userDal.UpdateUserAsync(dbUser);
|
||||
await _userDal.UpdateJwtRefreshTokenAsync(dbUser.EmailAddress, dbUser.RefreshToken);
|
||||
|
||||
response.IsSuccess = true;
|
||||
response.Content = new AuthenticationResponse
|
||||
|
|
|
|||
|
|
@ -47,8 +47,10 @@ namespace TIAMWebApp.Server.Controllers
|
|||
else
|
||||
{
|
||||
Console.WriteLine($"GetPermissionContextByUserId called with userId: {userId}");
|
||||
List<AssignedPermissionModel> response = await _serviceProviderDal.GetPermissionContextByUserIdAsync(userId);
|
||||
return Ok(response);
|
||||
//List<AssignedPermissionModel> response = await _serviceProviderDal.GetPermissionModelByUserIdAsync(userId);
|
||||
//return Ok(response);
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
|
|
@ -63,8 +63,8 @@ namespace TIAMWebApp.Server.Services
|
|||
Console.WriteLine($"Sender: {message.SenderId}");
|
||||
Console.WriteLine($"Message: {message.Message}");
|
||||
//resolve user!!!
|
||||
var senderUser = _userDal.Ctx.Users.FirstOrDefault(x => x.Id == message.SenderId);
|
||||
var receiverUser = _userDal.Ctx.Users.FirstOrDefault(x => x.Id == message.ReceiverId);
|
||||
var senderUser = _userDal.Context.Users.FirstOrDefault(x => x.Id == message.SenderId);
|
||||
var receiverUser = _userDal.Context.Users.FirstOrDefault(x => x.Id == message.ReceiverId);
|
||||
string apiKey = _configuration["SendGrid:Key"];
|
||||
var _client = new SendGridClient(apiKey);
|
||||
var _from = new EmailAddress("", "");
|
||||
|
|
@ -74,10 +74,10 @@ namespace TIAMWebApp.Server.Services
|
|||
}
|
||||
else
|
||||
{
|
||||
_from = new EmailAddress(senderUser.Email, senderUser.Email);
|
||||
_from = new EmailAddress(senderUser.EmailAddress, senderUser.EmailAddress);
|
||||
}
|
||||
var _subject = message.Subject;
|
||||
var _to = new EmailAddress(receiverUser.Email, receiverUser.Email);
|
||||
var _to = new EmailAddress(receiverUser.EmailAddress, receiverUser.EmailAddress);
|
||||
var _plainTextContent = message.Message;
|
||||
var _htmlContent = message.HtmlContent;
|
||||
var _msg = MailHelper.CreateSingleEmail(_from, _to, message.Subject, _plainTextContent, _htmlContent);
|
||||
|
|
|
|||
Loading…
Reference in New Issue