serviceproviderdal
This commit is contained in:
parent
7388a5879a
commit
03bc3aa976
|
|
@ -8,7 +8,7 @@ using AyCode.Database.DataLayers.Users;
|
||||||
using AyCode.Utils.Extensions;
|
using AyCode.Utils.Extensions;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using TIAM.Database.DataLayers.Admins;
|
using TIAM.Database.DataLayers.Admins;
|
||||||
using TIAM.Database.DataLayers.ServiceProviders;
|
//using TIAM.Database.DataLayers.ServiceProviders;
|
||||||
using TIAM.Database.DataLayers.Users;
|
using TIAM.Database.DataLayers.Users;
|
||||||
using TIAM.Database.DbContexts.Admins;
|
using TIAM.Database.DbContexts.Admins;
|
||||||
using TIAM.Database.DbContexts.ServiceProviders;
|
using TIAM.Database.DbContexts.ServiceProviders;
|
||||||
|
|
|
||||||
|
|
@ -1,12 +1,15 @@
|
||||||
using AyCode.Database.DbSets.Users;
|
using AyCode.Database.DbSets.Users;
|
||||||
|
using AyCode.Models.Enums;
|
||||||
using Microsoft.EntityFrameworkCore;
|
using Microsoft.EntityFrameworkCore;
|
||||||
using TIAM.Database.DataLayers.ServiceProviders;
|
//using TIAM.Database.DataLayers.ServiceProviders;
|
||||||
using TIAM.Database.DbContexts.Admins;
|
using TIAM.Database.DbContexts.Admins;
|
||||||
using TIAM.Database.DbSets.Permissions;
|
using TIAM.Database.DbSets.Permissions;
|
||||||
using TIAM.Database.DbSets.Products;
|
using TIAM.Database.DbSets.Products;
|
||||||
using TIAM.Database.DbSets.Users;
|
using TIAM.Database.DbSets.Users;
|
||||||
using TIAM.Entities.Permissions;
|
using TIAM.Entities.Permissions;
|
||||||
using TIAM.Entities.Products;
|
using TIAM.Entities.Products;
|
||||||
|
using TIAM.Entities.Products.DTOs;
|
||||||
|
using TIAM.Entities.ServiceProviders;
|
||||||
using TIAM.Entities.TransferDestinations;
|
using TIAM.Entities.TransferDestinations;
|
||||||
using TIAM.Entities.Users;
|
using TIAM.Entities.Users;
|
||||||
using TIAM.Models.Dtos.Users;
|
using TIAM.Models.Dtos.Users;
|
||||||
|
|
@ -20,6 +23,7 @@ namespace TIAM.Database.DataLayers.Admins
|
||||||
}
|
}
|
||||||
|
|
||||||
public TransferDestination? GetTransferDestinationById(Guid transferDestinationId, bool autoInclude = false) => Session(ctx=>ctx.TransferDestinations.FirstOrDefault(x=>x.Id == transferDestinationId));
|
public TransferDestination? GetTransferDestinationById(Guid transferDestinationId, bool autoInclude = false) => Session(ctx=>ctx.TransferDestinations.FirstOrDefault(x=>x.Id == transferDestinationId));
|
||||||
|
public string? GetTransferDestinationJsonById(Guid transferDestinationId) => Session(ctx => ctx.TransferDestinations.FirstOrDefault(x => x.Id == transferDestinationId)?.ToJson());
|
||||||
|
|
||||||
public User? GetUserById(Guid userId, bool autoInclude = false) => Session(x => x.GetUserById(userId, autoInclude));
|
public User? GetUserById(Guid userId, bool autoInclude = false) => Session(x => x.GetUserById(userId, autoInclude));
|
||||||
public User? GetUserByEmail(string email, bool autoInclude = false) => Session(x => x.GetUserByEmail(email, autoInclude));
|
public User? GetUserByEmail(string email, bool autoInclude = false) => Session(x => x.GetUserByEmail(email, autoInclude));
|
||||||
|
|
@ -32,6 +36,7 @@ namespace TIAM.Database.DataLayers.Admins
|
||||||
public string GetUsersJson() => Session(ctx => ctx.Users.ToJson());
|
public string GetUsersJson() => Session(ctx => ctx.Users.ToJson());
|
||||||
|
|
||||||
public Product? GetProductById(Guid contextId) => Session(x => x.GetProductById(contextId));
|
public Product? GetProductById(Guid contextId) => Session(x => x.GetProductById(contextId));
|
||||||
|
public Task<bool> AddProduct(Product product) => TransactionAsync(ctx => ctx.AddProduct(product));
|
||||||
|
|
||||||
public UserProductMapping? GetUserProductMappingById(Guid userProductMappingId, bool autoInclude = true) => Session(x => x.GetUserProductMappingById(userProductMappingId, autoInclude));
|
public UserProductMapping? GetUserProductMappingById(Guid userProductMappingId, bool autoInclude = true) => Session(x => x.GetUserProductMappingById(userProductMappingId, autoInclude));
|
||||||
|
|
||||||
|
|
@ -47,41 +52,438 @@ namespace TIAM.Database.DataLayers.Admins
|
||||||
public Task<List<PermissionContextMapping>> GetPermissionContextsViewByContextIdAsync(Guid contextId)
|
public Task<List<PermissionContextMapping>> GetPermissionContextsViewByContextIdAsync(Guid contextId)
|
||||||
=> SessionAsync(x => x.GetPermissionContextsViewByContextId(contextId).ToList());
|
=> SessionAsync(x => x.GetPermissionContextsViewByContextId(contextId).ToList());
|
||||||
|
|
||||||
//public Task<List<User>> GetUsersAsync()
|
//15. (IServiceProviderDataService) Create service provider
|
||||||
//{
|
public Task<bool> CreateServiceProviderAsync(TiamServiceProvider serviceProvider)
|
||||||
// return Context.Users.ToListAsync();
|
{
|
||||||
//}
|
Context.CreateServiceProvider(serviceProvider);
|
||||||
|
return Context.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
||||||
|
}
|
||||||
|
|
||||||
//public Task<User?> GetUserByEmailAsync(string email)
|
public bool CreateProductAsync(Product product)
|
||||||
//{
|
{
|
||||||
// Console.WriteLine($"Getting user from db {email}");
|
Context.CreateProduct(product);
|
||||||
// var emailLower = email.ToLower();
|
Console.WriteLine($"Saving product to db {product.Id}, {product.Name}, {product.ServiceProviderId}");
|
||||||
// return Context.Users.SingleOrDefaultAsync(x=>x.EmailAddress.ToLower() == emailLower);
|
var _result = Context.SaveChangesAsync();
|
||||||
//}
|
return _result.Result > 0;
|
||||||
|
}
|
||||||
|
|
||||||
//public Task<bool> CreateUserAsync(User user)
|
public Task<List<TiamServiceProvider>> GetServiceProvidersAsync()
|
||||||
//{
|
{
|
||||||
// user.Created = DateTime.UtcNow;
|
return Context.ServiceProviders.ToListAsync();
|
||||||
// user.Modified = DateTime.UtcNow;
|
}
|
||||||
// Context.Users.Add(user);
|
|
||||||
// Console.WriteLine($"Saving user to db {user.Id}, {user.EmailAddress}, {user.PhoneNumber}, {user.Password}");
|
public virtual Task<TiamServiceProvider?> GetServiceProviderByIdAsync(Guid id)
|
||||||
// return Context.SaveChangesAsync().ContinueWith(x=>x.Result > 0);
|
{
|
||||||
//}
|
Console.WriteLine($"Getting serviceProvider from db {id}");
|
||||||
|
return Context.ServiceProviders.SingleOrDefaultAsync(x => x.Id == id);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task<UserProductMapping> CreateUserProductMappingAsync(UserProductMapping userProductMapping)
|
||||||
|
{
|
||||||
|
Context.UserProductMappings.Add(userProductMapping);
|
||||||
|
Console.WriteLine($"Saving userProductMapping to db {userProductMapping.Id}, {userProductMapping.ProductId}, {userProductMapping.UserId}");
|
||||||
|
return Context.SaveChangesAsync().ContinueWith(x => userProductMapping);
|
||||||
|
}
|
||||||
|
|
||||||
|
#region ServiceProviders
|
||||||
|
|
||||||
|
//14. (IserviceProviderDataService) Update service provider
|
||||||
|
public Task<bool> UpdateServiceProviderAsync(TiamServiceProvider serviceProvider)
|
||||||
|
{
|
||||||
|
var dbServiceProvider = Context.ServiceProviders.FirstOrDefault(u => u.Id == serviceProvider.Id);
|
||||||
|
if (dbServiceProvider != null)
|
||||||
|
{
|
||||||
|
dbServiceProvider = serviceProvider;
|
||||||
|
Context.ServiceProviders.Update(dbServiceProvider);
|
||||||
|
return Context.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
throw new Exception("ServiceProvider not found");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//13. (IserviceProviderDataService) delete service provider
|
||||||
|
public Task<bool> DeleteServiceProviderAsync(Guid id)
|
||||||
|
{
|
||||||
|
using (var transaction = Context.Database.BeginTransaction())
|
||||||
|
{
|
||||||
|
var dbServiceProvider = Context.ServiceProviders.FirstOrDefault(u => u.Id == id);
|
||||||
|
if (dbServiceProvider != null)
|
||||||
|
{
|
||||||
|
//get products for this provider
|
||||||
|
var products = Context.Products.Where(x => x.ServiceProviderId == id).ToList();
|
||||||
|
|
||||||
|
/*foreach (var productItem in products)
|
||||||
|
{
|
||||||
|
//delete products
|
||||||
|
var permissionContextMappings = Context.PermissionContextMappings.Where(x => x.ContextId == productItem.Id).ToList();
|
||||||
|
//iterate through every row
|
||||||
|
foreach (var item in permissionContextMappings)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (item.SubjectType == (int)PermissionContextMappingSubjectType.Group)
|
||||||
|
{
|
||||||
|
//get users in the permissiongroup
|
||||||
|
var permissionGroupUserMapping = Context.PermissionGroupUserMappings.Where(x => x.PermissionContextMappingId == item.Id).ToList();
|
||||||
|
//remove every row (users) from permissiongroup
|
||||||
|
foreach (var user in permissionGroupUserMapping)
|
||||||
|
{
|
||||||
|
Context.PermissionGroupUserMappings.Remove(user);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//remove permissioncontextmappings
|
||||||
|
Context.PermissionContextMappings.RemoveRange(permissionContextMappings);
|
||||||
|
}*/
|
||||||
|
Context.Products.RemoveRange(products);
|
||||||
|
Context.ServiceProviders.Remove(dbServiceProvider);
|
||||||
|
return Context.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return Task.FromResult(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//17. (IServiceProviderDataService) get service provider by ownerId
|
||||||
|
public Task<List<TiamServiceProvider>> GetServiceProvidersByOwnerIdAsync()
|
||||||
|
{
|
||||||
|
throw new NotImplementedException();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region PermissionTypes
|
||||||
|
|
||||||
|
|
||||||
|
//10. (IPermissionService) create permission type
|
||||||
|
public Task<bool> CreatePermissionsTypeAsync(PermissionsType permissionsType)
|
||||||
|
{
|
||||||
|
bool result = false;
|
||||||
|
|
||||||
|
using (var transaction = Context.Database.BeginTransaction())
|
||||||
|
{
|
||||||
|
var existingPermission = Context.PermissionsTypes
|
||||||
|
.FirstOrDefault(x => x.PermissionName == permissionsType.PermissionName)?.PermissionName;
|
||||||
|
|
||||||
|
if (existingPermission == null)
|
||||||
|
{
|
||||||
|
//get all the permissiontypes for this context
|
||||||
|
var permissionTypes = new List<PermissionsType>();
|
||||||
|
var nextBitValue = 0.0;
|
||||||
|
permissionTypes = Context.PermissionsTypes
|
||||||
|
.Where(x => x.ContextId == permissionsType.ContextId)
|
||||||
|
.ToList();
|
||||||
|
|
||||||
|
//get the max value of the permissiontypes
|
||||||
|
if (permissionTypes != null)
|
||||||
|
{
|
||||||
|
//next bit value is the power of two of the count of the permissiontypes
|
||||||
|
nextBitValue = Math.Pow(2, permissionTypes.Count);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
nextBitValue = Math.Pow(2, 0);
|
||||||
|
}
|
||||||
|
permissionsType.PermissionBit = (int)nextBitValue;
|
||||||
|
Context.PermissionsTypes.Add(permissionsType);
|
||||||
|
Context.SaveChanges();
|
||||||
|
transaction.Commit();
|
||||||
|
result = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
result = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
return Task.FromResult(result);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
//11. (IPermissionService) get permission types for context
|
||||||
|
public Task<List<PermissionsType>>? GetPermissionTypesByContextIdAsync(Guid contextId)
|
||||||
|
{
|
||||||
|
return Context.PermissionsTypes.Where(x => x.ContextId == contextId).ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task<int> GetPermissionFromPermissionType(PermissionsType pType)
|
||||||
|
{
|
||||||
|
if (Context.PermissionsTypes.FirstOrDefault(x => x.Id == pType.Id) != null)
|
||||||
|
{
|
||||||
|
return Task.FromResult(pType.PermissionBit);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return Task.FromResult(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region PermissionMappings
|
||||||
|
|
||||||
|
public Task<List<AssignedPermissionModel>> GetPermissionsOfUserProductMappingsAndGroupsAsyncByContextId(Guid contextId)
|
||||||
|
{
|
||||||
|
List<AssignedPermissionModel> result = new List<AssignedPermissionModel>();
|
||||||
|
|
||||||
|
var UserProductMappings = Context.UserProductMappings.Where(x => x.ProductId == contextId).ToListAsync();
|
||||||
|
|
||||||
|
if (UserProductMappings.Result != null)
|
||||||
|
{
|
||||||
|
foreach (var item in UserProductMappings.Result)
|
||||||
|
{
|
||||||
|
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
|
||||||
|
|
||||||
|
}
|
||||||
|
else if (mappingRow.Result.Count > 1)
|
||||||
|
{
|
||||||
|
//user has been assigned more than onece to same context
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
foreach (var mapping in mappingRow.Result)
|
||||||
|
{
|
||||||
|
result.Add(new AssignedPermissionModel(item.ProductId, item.Id, mapping.SubjectType, item.UserId.ToString(), mapping.Permissions));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var AssingedGroups = Context.PermissionGroups.Where(x => x.OwnerId == contextId).ToListAsync();
|
||||||
|
|
||||||
|
if (AssingedGroups.Result != null)
|
||||||
|
{
|
||||||
|
foreach (var group in AssingedGroups.Result)
|
||||||
|
{
|
||||||
|
var mappingRow = Context.PermissionContextMappings.Where(x => x.SubjectId == group.Id).ToListAsync();
|
||||||
|
if (mappingRow.Result == null)
|
||||||
|
{
|
||||||
|
//group has no permission but is assigned...
|
||||||
|
|
||||||
|
}
|
||||||
|
else if (mappingRow.Result.Count > 1)
|
||||||
|
{
|
||||||
|
//group has been assigned more than onece to same context
|
||||||
|
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
foreach (var mapping in mappingRow.Result)
|
||||||
|
{
|
||||||
|
result.Add(new AssignedPermissionModel(group.OwnerId, group.Id, mapping.SubjectType, group.GroupName, mapping.Permissions));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
foreach (var row in result)
|
||||||
|
{
|
||||||
|
Console.WriteLine($"GetPermissionsOfUserProductMappingsAndGroupsAsyncByContextId: {row.ContextId}, {row.SubjectId}, {row.SubjectType}, {row.Name}, {row.PermissionsValue}");
|
||||||
|
}
|
||||||
|
return Task.FromResult(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//12. (IPermissionService) get permission groups for context
|
||||||
|
public Task<List<PermissionContextMapping>> GetPermissionsForContextByContextIdAsync(Guid contextId)
|
||||||
|
{
|
||||||
|
List<PermissionContextMapping> permissionContextMappings = new List<PermissionContextMapping>();
|
||||||
|
//get all Groups where the contextId is the same
|
||||||
|
var groups = Context.PermissionGroups.Where(x => x.OwnerId == 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);
|
||||||
|
}
|
||||||
|
|
||||||
|
//9. (IPermissionService) add user to permission group
|
||||||
|
public Task<bool> AddUserToPermissionGroupAsync(Guid permissionGroupId, Guid userId)
|
||||||
|
{
|
||||||
|
bool result = false;
|
||||||
|
using (var transaction = Context.Database.BeginTransaction())
|
||||||
|
{
|
||||||
|
//do we need to check if PermissionContextMappingId exists?
|
||||||
|
var permissionGroupUserMapping = new PermissionGroupUserMapping(userId, permissionGroupId);
|
||||||
|
Context.PermissionGroupUserMappings.Add(permissionGroupUserMapping);
|
||||||
|
Context.SaveChanges();
|
||||||
|
transaction.Commit();
|
||||||
|
result = true;
|
||||||
|
}
|
||||||
|
return Task.FromResult(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
//8. (IPermissionService) create permission group
|
||||||
|
public Task<bool> CreatePermissionGroupAsync(PermissionGroup permissionGroup, TiamServiceProvider serviceProvider)
|
||||||
|
{
|
||||||
|
bool result = false;
|
||||||
|
using (var transaction = Context.Database.BeginTransaction())
|
||||||
|
{
|
||||||
|
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");
|
||||||
|
Context.CreatePermissionsType(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, PermissionContextMappingSubjectType.Group, 1, true);
|
||||||
|
Context.CreatePermissionContextMapping(permissionContextMapping);
|
||||||
|
Context.CreatePermissionGroup(permissionGroup);
|
||||||
|
Context.SaveChanges();
|
||||||
|
transaction.Commit();
|
||||||
|
result = true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//group with same name already exists
|
||||||
|
result = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return Task.FromResult(result);
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<UserProductMapping> GetUserProductMappingsInPermissionGroupByGroupId(Guid groupId)
|
||||||
|
{
|
||||||
|
return Context.GetUserProductMappingsByPermissionGroupId(groupId).ToList();
|
||||||
|
//List<UserProductMapping> userProductMappings = new List<UserProductMapping>();
|
||||||
|
|
||||||
|
////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 = Context.PermissionGroupUserMappings.Where(x => x.PermissionContextMappingId == pCmId).ToList();
|
||||||
|
//if (pGum.Count > 0)
|
||||||
|
//{
|
||||||
|
// foreach (var group in pGum)
|
||||||
|
// {
|
||||||
|
// userProductMappings.Add(Context.UserProductMappings.FirstOrDefault(x => x.Id == group.UserProductMappingId));
|
||||||
|
// }
|
||||||
|
|
||||||
|
//}
|
||||||
|
|
||||||
|
//return Task.FromResult(userProductMappings);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
#region Products
|
||||||
|
|
||||||
|
//* 20. (IServiceProviderDataService) Update product
|
||||||
|
public Product UpdateProduct(Product product)
|
||||||
|
{
|
||||||
|
|
||||||
|
var prod = Context.UpdateProduct(product);
|
||||||
|
Console.WriteLine($"Saving product to db {product.Id}, {product.Name}, {product.ServiceProviderId}");
|
||||||
|
Context.SaveChanges();
|
||||||
|
return prod;
|
||||||
|
}
|
||||||
|
|
||||||
|
//* 21. (IServiceProviderDataService) delete product
|
||||||
|
public Task<bool> DeleteProductByIdAsync(Guid productId)
|
||||||
|
{
|
||||||
|
return TransactionAsync(ctx =>
|
||||||
|
{
|
||||||
|
ctx.DeleteProductById(productId);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
//4. (IPermissionService) AssignPermissionToUserForContextAsync
|
||||||
|
public Task<bool> AssignPermissionToUserForContextAsync(UserProductMapping userProductMapping, PermissionsType permission)
|
||||||
|
{
|
||||||
|
var _assIgnedUser = Context.UserProductMappings.FirstOrDefault(x => x.Id == userProductMapping.Id);
|
||||||
|
|
||||||
|
if (_assIgnedUser != null)
|
||||||
|
{
|
||||||
|
//user exists
|
||||||
|
var _permissionInt = GetPermissionFromPermissionType(permission);
|
||||||
|
|
||||||
|
var permissionContextMapping = Context.PermissionContextMappings.FirstOrDefault(x => x.SubjectId == userProductMapping.Id);
|
||||||
|
var currentPermissions = permissionContextMapping.Permissions;
|
||||||
|
var newPermissions = currentPermissions + _permissionInt.Result;
|
||||||
|
permissionContextMapping.Permissions = newPermissions;
|
||||||
|
return Context.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//user does not exist, let's create it
|
||||||
|
return Task.FromResult(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
|
#region UserProductMappings
|
||||||
|
|
||||||
|
//23. (IServiceProviderDataService) Get Assigned Users By ProductId
|
||||||
|
public Task<List<UserProductMapping>> GetUserProductMappingsByProductIdAsync(Guid productId)
|
||||||
|
{
|
||||||
|
return Context.UserProductMappings.Where(x => x.ProductId == productId).ToListAsync();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
//24 . (IServiceProviderDataService) Remove Assigned Users By Product Id
|
||||||
|
public Task RemoveUserProductMappingsByContextId(Guid productId)
|
||||||
|
{
|
||||||
|
using (var transaction = Context.Database.BeginTransaction())
|
||||||
|
{
|
||||||
|
var userProductMappings = Context.UserProductMappings.Where(x => x.ProductId == productId).ToList();
|
||||||
|
//remove userProductMappings
|
||||||
|
|
||||||
|
return Context.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//25. (IServiceProviderDataService) Remove Assigned from product by userProductMappingId
|
||||||
|
public Task<bool> RemoveUserProductMappingAsync(UserProductMapping userProductMapping, bool removeFromGroups)
|
||||||
|
{
|
||||||
|
return TransactionAsync(ctx =>
|
||||||
|
{
|
||||||
|
var result = false;
|
||||||
|
var userProductMappingToRemove = ctx.UserProductMappings.FirstOrDefault(x => x.Id == userProductMapping.Id);
|
||||||
|
|
||||||
|
//remove userProductMappings
|
||||||
|
if (userProductMappingToRemove == null) return false;
|
||||||
|
|
||||||
|
|
||||||
|
if (removeFromGroups)
|
||||||
|
{
|
||||||
|
//remove permissiongroupusermappings
|
||||||
|
ctx.RemoveAssingedUserFromPermissionGroups(userProductMappingToRemove.Id);
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.UserProductMappings.Remove(userProductMappingToRemove);
|
||||||
|
|
||||||
|
|
||||||
|
return result;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endregion
|
||||||
|
|
||||||
//public Task<bool> UpdateUserAsync(User user)
|
|
||||||
//{
|
|
||||||
// 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;
|
|
||||||
// Context.Users.Update(existingUser);
|
|
||||||
// return Context.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
|
||||||
// }
|
|
||||||
// else
|
|
||||||
// {
|
|
||||||
// throw new Exception("User not found");
|
|
||||||
// }
|
|
||||||
//}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,202 @@
|
||||||
|
using AyCode.Interfaces.Entities;
|
||||||
|
using Newtonsoft.Json;
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using TIAM.Database.DbContexts.Admins;
|
||||||
|
using TIAM.Database.DbContexts.ServiceProviders;
|
||||||
|
using TIAM.Database.DbSets.Permissions;
|
||||||
|
using TIAM.Database.DbSets.Products;
|
||||||
|
using TIAM.Database.DbSets.Users;
|
||||||
|
using TIAM.Entities.Permissions;
|
||||||
|
using TIAM.Entities.Products;
|
||||||
|
using TIAM.Entities.ServiceProviders;
|
||||||
|
using TIAM.Entities.Users;
|
||||||
|
|
||||||
|
namespace TIAM.Database.DataLayers.Admins
|
||||||
|
{
|
||||||
|
public static class AdminDalDbContextExtension
|
||||||
|
{
|
||||||
|
public static string ToJson<T>(this T source) where T : class, IEntity
|
||||||
|
{
|
||||||
|
JsonSerializerSettings options = new()
|
||||||
|
{
|
||||||
|
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
|
||||||
|
NullValueHandling = NullValueHandling.Ignore
|
||||||
|
};
|
||||||
|
|
||||||
|
return JsonConvert.SerializeObject(source, options);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static string ToJson<T>(this IQueryable<T> source) where T : class, IEntity
|
||||||
|
{
|
||||||
|
JsonSerializerSettings options = new()
|
||||||
|
{
|
||||||
|
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
|
||||||
|
NullValueHandling = NullValueHandling.Ignore
|
||||||
|
};
|
||||||
|
|
||||||
|
return JsonConvert.SerializeObject(source, options);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IQueryable<UserProductMapping> GetUserProductMappingsByPermissionGroupId(this IAdminDbContext ctx, Guid permissionGroupId)
|
||||||
|
{
|
||||||
|
return ctx.UserProductMappings
|
||||||
|
.Where(user => ctx.PermissionGroupUserMappings
|
||||||
|
.Where(x => x.PermissionGroupId == permissionGroupId)
|
||||||
|
.Select(x => x.SubjectId)
|
||||||
|
.Contains(user.Id));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void CleanUpAndRemoveUserProductMappings(this IAdminDbContext ctx, IEnumerable<UserProductMapping> userProductMappings)
|
||||||
|
{
|
||||||
|
foreach (var userProductMapping in userProductMappings)
|
||||||
|
{
|
||||||
|
ctx.CleanUpAndRemoveAssignedUser(userProductMapping);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void CleanUpAndRemoveAssignedUser(this IAdminDbContext ctx, UserProductMapping userProductMapping)
|
||||||
|
{
|
||||||
|
ctx.RemoveContextMappingBySubjectId(userProductMapping.Id);
|
||||||
|
ctx.RemoveAssingedUserFromPermissionGroups(userProductMapping.Id);
|
||||||
|
|
||||||
|
ctx.UserProductMappings.Remove(userProductMapping);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool CleanUpAndRemoveUserProductMappings(this IAdminDbContext ctx, Guid userProductMappingId)
|
||||||
|
{
|
||||||
|
var userProductMapping = ctx.GetUserProductMappingById(userProductMappingId);
|
||||||
|
|
||||||
|
if (userProductMapping == null) return false;
|
||||||
|
|
||||||
|
ctx.CleanUpAndRemoveAssignedUser(userProductMapping);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static UserProductMapping UpdateUserProductMapping(this IAdminDbContext context, UserProductMapping userProductMapping)
|
||||||
|
{
|
||||||
|
if (userProductMapping == null) return null;
|
||||||
|
var existingUserProductMapping = context.UserProductMappings.FirstOrDefault(u => u.Id == userProductMapping.Id);
|
||||||
|
if (existingUserProductMapping == null) return null;
|
||||||
|
existingUserProductMapping.Id = userProductMapping.Id;
|
||||||
|
existingUserProductMapping.UserId = userProductMapping.UserId;
|
||||||
|
existingUserProductMapping.ProductId = userProductMapping.ProductId;
|
||||||
|
|
||||||
|
return existingUserProductMapping;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Product UpdateProduct(this IAdminDbContext ctx, Product product)
|
||||||
|
{
|
||||||
|
if (product == null) return null;
|
||||||
|
|
||||||
|
var existingProduct = ctx.Products.FirstOrDefault(u => u.Id == product.Id);
|
||||||
|
if (existingProduct == null) return null;
|
||||||
|
|
||||||
|
existingProduct.Name = product.Name;
|
||||||
|
existingProduct.ServiceProviderId = product.ServiceProviderId;
|
||||||
|
existingProduct.Description = product.Description;
|
||||||
|
existingProduct.Price = product.Price;
|
||||||
|
existingProduct.JsonDetails = product.JsonDetails;
|
||||||
|
//existingProduct.UserMediaId = product.UserMediaId;
|
||||||
|
existingProduct.ProductType = product.ProductType;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
return existingProduct;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void DeleteProductById(this IAdminDbContext ctx, Guid productId)
|
||||||
|
{
|
||||||
|
var product = ctx.Products.FirstOrDefault(u => u.Id == productId);
|
||||||
|
if (product == null) return;
|
||||||
|
|
||||||
|
ctx.CleanUpAndRemoveUserProductMappings(ctx.GetUserProductMappingsByProductId(productId));
|
||||||
|
ctx.Products.Remove(product);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool CreatePermissionGroup(this IAdminDbContext ctx, PermissionGroup permissionGroup)
|
||||||
|
{
|
||||||
|
if (permissionGroup == null) return false;
|
||||||
|
|
||||||
|
ctx.PermissionGroups.Add(permissionGroup);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool CreatePermissionContextMapping(this IAdminDbContext ctx, PermissionContextMapping permissionContextMapping)
|
||||||
|
{
|
||||||
|
if (permissionContextMapping == null) return false;
|
||||||
|
|
||||||
|
ctx.PermissionContextMappings.Add(permissionContextMapping);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool CreatePermissionGroupUserMapping(this IAdminDbContext ctx, PermissionGroupUserMapping permissionGroupUserMapping)
|
||||||
|
{
|
||||||
|
if (permissionGroupUserMapping == null) return false;
|
||||||
|
|
||||||
|
ctx.PermissionGroupUserMappings.Add(permissionGroupUserMapping);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool CreatePermissionsType(this IAdminDbContext ctx, PermissionsType permissionType)
|
||||||
|
{
|
||||||
|
if (permissionType == null) return false;
|
||||||
|
|
||||||
|
ctx.PermissionsTypes.Add(permissionType);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool CreateProduct(this IAdminDbContext ctx, Product myproduct)
|
||||||
|
{
|
||||||
|
if (myproduct == null) return false;
|
||||||
|
//Automatically add assigneduser for owner
|
||||||
|
TiamServiceProvider? productOwner = ctx.ServiceProviders.FirstOrDefault(x => x.Id == myproduct.ServiceProviderId);
|
||||||
|
if (productOwner == null) return false;
|
||||||
|
var userProductMapping = new UserProductMapping(myproduct.Id, productOwner.OwnerId);
|
||||||
|
ctx.CreateAssignedUser(userProductMapping);
|
||||||
|
ctx.AddProduct(myproduct);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static bool CreateAssignedUser(this IAdminDbContext ctx, UserProductMapping userProductMapping)
|
||||||
|
{
|
||||||
|
if (userProductMapping == null) return false;
|
||||||
|
|
||||||
|
ctx.UserProductMappings.Add(userProductMapping);
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static TiamServiceProvider CreateServiceProvider(this IAdminDbContext ctx, TiamServiceProvider serviceProvider)
|
||||||
|
{
|
||||||
|
if (serviceProvider == null) return null;
|
||||||
|
|
||||||
|
ctx.ServiceProviders.Add(serviceProvider);
|
||||||
|
var userProductMapping = new UserProductMapping(serviceProvider.Id, serviceProvider.OwnerId);
|
||||||
|
ctx.CreateAssignedUser(userProductMapping);
|
||||||
|
return serviceProvider;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static TiamServiceProvider UpdateServiceProvider(this IAdminDbContext ctx, TiamServiceProvider serviceProvider)
|
||||||
|
{
|
||||||
|
if (serviceProvider == null) return null;
|
||||||
|
|
||||||
|
var existingServiceProvider = ctx.ServiceProviders.FirstOrDefault(u => u.Id == serviceProvider.Id);
|
||||||
|
if (existingServiceProvider == null) return null;
|
||||||
|
|
||||||
|
existingServiceProvider.Name = serviceProvider.Name;
|
||||||
|
existingServiceProvider.OwnerId = serviceProvider.OwnerId;
|
||||||
|
return existingServiceProvider;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -23,538 +23,416 @@ using Newtonsoft.Json;
|
||||||
|
|
||||||
namespace TIAM.Database.DataLayers.ServiceProviders
|
namespace TIAM.Database.DataLayers.ServiceProviders
|
||||||
{
|
{
|
||||||
public class ServiceProviderDal : DalBase<ServiceProviderDbContext>
|
//public class ServiceProviderDal : DalBase<ServiceProviderDbContext>
|
||||||
{
|
|
||||||
|
|
||||||
public ServiceProviderDal() : base()
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
public ServiceProviderDal(ServiceProviderDbContext _object)
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
//public TransferDestination? GetTransferDestinationById(Guid transferDestinationId, bool autoInclude = false) => Session(ctx=>ctx.TransferDestinations.FirstOrDefault(x=>x.Id == transferDestinationId));
|
|
||||||
|
|
||||||
//public User? GetUserById(Guid userId, bool autoInclude = false) => Session(x => x.GetUserById(userId, autoInclude));
|
|
||||||
//public User? GetUserByEmail(string email, bool autoInclude = false) => Session(x => x.GetUserByEmail(email, autoInclude));
|
|
||||||
|
|
||||||
//public UserModelDto? GetUserModelDtoById(Guid userId) => Session(x => x.GetUserModelDtoById(userId));
|
|
||||||
//public Task<UserModelDto?> GetUserModelDtoByIdAsync(Guid userId) => SessionAsync(x => x.GetUserModelDtoById(userId));
|
|
||||||
//public UserModelDto? GetUserModelDtoByEmail(string email) => Session(x => x.GetUserModelDtoByEmail(email));
|
|
||||||
|
|
||||||
//public string? GetUserJsonById(Guid userId) => Session(ctx => ctx.GetUserById(userId)?.ToJson());
|
|
||||||
//public string GetUsersJson() => Session(ctx => ctx.Users.ToJson());
|
|
||||||
|
|
||||||
|
|
||||||
//public UserProductMapping? GetUserProductMappingById(Guid userProductMappingId, bool autoInclude = true) => Session(x => x.GetUserProductMappingById(userProductMappingId, autoInclude));
|
|
||||||
|
|
||||||
#region ServiceProviders
|
|
||||||
|
|
||||||
//16. (IServiceProviderDataService) get all service providers
|
|
||||||
public Task<List<TiamServiceProvider>> GetServiceProvidersAsync()
|
|
||||||
{
|
|
||||||
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 Context.ServiceProviders.SingleOrDefaultAsync(x => x.Id == id);
|
|
||||||
}
|
|
||||||
|
|
||||||
//15. (IServiceProviderDataService) Create service provider
|
|
||||||
public Task<bool> CreateServiceProviderAsync(TiamServiceProvider serviceProvider)
|
|
||||||
{
|
|
||||||
|
|
||||||
Context.CreateServiceProvider(serviceProvider);
|
|
||||||
return Context.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
//14. (IserviceProviderDataService) Update service provider
|
|
||||||
public Task<bool> UpdateServiceProviderAsync(TiamServiceProvider serviceProvider)
|
|
||||||
{
|
|
||||||
var dbServiceProvider = Context.ServiceProviders.FirstOrDefault(u => u.Id == serviceProvider.Id);
|
|
||||||
if (dbServiceProvider != null)
|
|
||||||
{
|
|
||||||
dbServiceProvider = serviceProvider;
|
|
||||||
Context.ServiceProviders.Update(dbServiceProvider);
|
|
||||||
return Context.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
throw new Exception("ServiceProvider not found");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//13. (IserviceProviderDataService) delete service provider
|
|
||||||
public Task<bool> DeleteServiceProviderAsync(Guid id)
|
|
||||||
{
|
|
||||||
using (var transaction = Context.Database.BeginTransaction())
|
|
||||||
{
|
|
||||||
var dbServiceProvider = Context.ServiceProviders.FirstOrDefault(u => u.Id == id);
|
|
||||||
if (dbServiceProvider != null)
|
|
||||||
{
|
|
||||||
//get products for this provider
|
|
||||||
var products = Context.Products.Where(x => x.ServiceProviderId == id).ToList();
|
|
||||||
|
|
||||||
/*foreach (var productItem in products)
|
|
||||||
{
|
|
||||||
//delete products
|
|
||||||
var permissionContextMappings = Context.PermissionContextMappings.Where(x => x.ContextId == productItem.Id).ToList();
|
|
||||||
//iterate through every row
|
|
||||||
foreach (var item in permissionContextMappings)
|
|
||||||
{
|
|
||||||
|
|
||||||
if (item.SubjectType == (int)PermissionContextMappingSubjectType.Group)
|
|
||||||
{
|
|
||||||
//get users in the permissiongroup
|
|
||||||
var permissionGroupUserMapping = Context.PermissionGroupUserMappings.Where(x => x.PermissionContextMappingId == item.Id).ToList();
|
|
||||||
//remove every row (users) from permissiongroup
|
|
||||||
foreach (var user in permissionGroupUserMapping)
|
|
||||||
{
|
|
||||||
Context.PermissionGroupUserMappings.Remove(user);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//remove permissioncontextmappings
|
|
||||||
Context.PermissionContextMappings.RemoveRange(permissionContextMappings);
|
|
||||||
}*/
|
|
||||||
Context.Products.RemoveRange(products);
|
|
||||||
Context.ServiceProviders.Remove(dbServiceProvider);
|
|
||||||
return Context.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return Task.FromResult(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//17. (IServiceProviderDataService) get service provider by ownerId
|
|
||||||
public Task<List<TiamServiceProvider>> GetServiceProvidersByOwnerIdAsync()
|
|
||||||
{
|
|
||||||
throw new NotImplementedException();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region PermissionTypes
|
|
||||||
|
|
||||||
|
|
||||||
//10. (IPermissionService) create permission type
|
|
||||||
public Task<bool> CreatePermissionsTypeAsync(PermissionsType permissionsType)
|
|
||||||
{
|
|
||||||
bool result = false;
|
|
||||||
|
|
||||||
using (var transaction = Context.Database.BeginTransaction())
|
|
||||||
{
|
|
||||||
var existingPermission = Context.PermissionsTypes
|
|
||||||
.FirstOrDefault(x => x.PermissionName == permissionsType.PermissionName)?.PermissionName;
|
|
||||||
|
|
||||||
if (existingPermission == null)
|
|
||||||
{
|
|
||||||
//get all the permissiontypes for this context
|
|
||||||
var permissionTypes = new List<PermissionsType>();
|
|
||||||
var nextBitValue = 0.0;
|
|
||||||
permissionTypes = Context.PermissionsTypes
|
|
||||||
.Where(x => x.ContextId == permissionsType.ContextId)
|
|
||||||
.ToList();
|
|
||||||
|
|
||||||
//get the max value of the permissiontypes
|
|
||||||
if (permissionTypes != null)
|
|
||||||
{
|
|
||||||
//next bit value is the power of two of the count of the permissiontypes
|
|
||||||
nextBitValue = Math.Pow(2, permissionTypes.Count);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
nextBitValue = Math.Pow(2, 0);
|
|
||||||
}
|
|
||||||
permissionsType.PermissionBit = (int)nextBitValue;
|
|
||||||
Context.PermissionsTypes.Add(permissionsType);
|
|
||||||
Context.SaveChanges();
|
|
||||||
transaction.Commit();
|
|
||||||
result = true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
result = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
return Task.FromResult(result);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
//11. (IPermissionService) get permission types for context
|
|
||||||
public Task<List<PermissionsType>>? GetPermissionTypesByContextIdAsync(Guid contextId)
|
|
||||||
{
|
|
||||||
return Context.PermissionsTypes.Where(x => x.ContextId == contextId).ToListAsync();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Task<int> GetPermissionFromPermissionType(PermissionsType pType)
|
|
||||||
{
|
|
||||||
if (Context.PermissionsTypes.FirstOrDefault(x => x.Id == pType.Id) != null)
|
|
||||||
{
|
|
||||||
return Task.FromResult(pType.PermissionBit);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return Task.FromResult(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#endregion
|
|
||||||
|
|
||||||
#region PermissionMappings
|
|
||||||
|
|
||||||
//public List<PermissionContextMapping> GetPermissionContextsView(Guid subjectId, Guid contextId)
|
|
||||||
// => Session(x => x.GetPermissionContextsView(subjectId, contextId).ToList());
|
|
||||||
|
|
||||||
//public List<PermissionContextMapping> GetPermissionContextsViewBySubjectId(Guid contextId)
|
|
||||||
// => Session(x => x.GetPermissionContextsViewBySubjectId(contextId).ToList());
|
|
||||||
|
|
||||||
//public List<PermissionContextMapping> GetPermissionContextsViewByContextId(Guid contextId)
|
|
||||||
// => Session(x => x.GetPermissionContextsViewByContextId(contextId).ToList());
|
|
||||||
|
|
||||||
//public Task<List<PermissionContextMapping>> GetPermissionContextsViewByContextIdAsync(Guid contextId)
|
|
||||||
// => SessionAsync(x => x.GetPermissionContextsViewByContextId(contextId).ToList());
|
|
||||||
|
|
||||||
//3. (IPermissionService) get permissions of assigned users and groups
|
|
||||||
public Task<List<AssignedPermissionModel>> GetPermissionsOfUserProductMappingsAndGroupsAsyncByContextId(Guid contextId)
|
|
||||||
{
|
|
||||||
List<AssignedPermissionModel> result = new List<AssignedPermissionModel>();
|
|
||||||
|
|
||||||
var UserProductMappings = Context.UserProductMappings.Where(x => x.ProductId == contextId).ToListAsync();
|
|
||||||
|
|
||||||
if (UserProductMappings.Result != null)
|
|
||||||
{
|
|
||||||
foreach (var item in UserProductMappings.Result)
|
|
||||||
{
|
|
||||||
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
|
|
||||||
|
|
||||||
}
|
|
||||||
else if (mappingRow.Result.Count > 1)
|
|
||||||
{
|
|
||||||
//user has been assigned more than onece to same context
|
|
||||||
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
foreach (var mapping in mappingRow.Result)
|
|
||||||
{
|
|
||||||
result.Add(new AssignedPermissionModel(item.ProductId, item.Id, mapping.SubjectType, item.UserId.ToString(), mapping.Permissions));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var AssingedGroups = Context.PermissionGroups.Where(x => x.OwnerId == contextId).ToListAsync();
|
|
||||||
|
|
||||||
if (AssingedGroups.Result != null)
|
|
||||||
{
|
|
||||||
foreach (var group in AssingedGroups.Result)
|
|
||||||
{
|
|
||||||
var mappingRow = Context.PermissionContextMappings.Where(x => x.SubjectId == group.Id).ToListAsync();
|
|
||||||
if (mappingRow.Result == null)
|
|
||||||
{
|
|
||||||
//group has no permission but is assigned...
|
|
||||||
|
|
||||||
}
|
|
||||||
else if (mappingRow.Result.Count > 1)
|
|
||||||
{
|
|
||||||
//group has been assigned more than onece to same context
|
|
||||||
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
foreach (var mapping in mappingRow.Result)
|
|
||||||
{
|
|
||||||
result.Add(new AssignedPermissionModel(group.OwnerId, group.Id, mapping.SubjectType, group.GroupName, mapping.Permissions));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
foreach (var row in result)
|
|
||||||
{
|
|
||||||
Console.WriteLine($"GetPermissionsOfUserProductMappingsAndGroupsAsyncByContextId: {row.ContextId}, {row.SubjectId}, {row.SubjectType}, {row.Name}, {row.PermissionsValue}");
|
|
||||||
}
|
|
||||||
return Task.FromResult(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//12. (IPermissionService) get permission groups for context
|
|
||||||
public Task<List<PermissionContextMapping>> GetPermissionsForContextByContextIdAsync(Guid contextId)
|
|
||||||
{
|
|
||||||
List<PermissionContextMapping> permissionContextMappings = new List<PermissionContextMapping>();
|
|
||||||
//get all Groups where the contextId is the same
|
|
||||||
var groups = Context.PermissionGroups.Where(x => x.OwnerId == 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);
|
|
||||||
}
|
|
||||||
|
|
||||||
//9. (IPermissionService) add user to permission group
|
// public ServiceProviderDal() : base()
|
||||||
public Task<bool> AddUserToPermissionGroupAsync(Guid permissionGroupId, Guid userId)
|
|
||||||
{
|
|
||||||
bool result = false;
|
|
||||||
using (var transaction = Context.Database.BeginTransaction())
|
|
||||||
{
|
|
||||||
//do we need to check if PermissionContextMappingId exists?
|
|
||||||
var permissionGroupUserMapping = new PermissionGroupUserMapping(userId, permissionGroupId);
|
|
||||||
Context.PermissionGroupUserMappings.Add(permissionGroupUserMapping);
|
|
||||||
Context.SaveChanges();
|
|
||||||
transaction.Commit();
|
|
||||||
result = true;
|
|
||||||
}
|
|
||||||
return Task.FromResult(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
//8. (IPermissionService) create permission group
|
|
||||||
public Task<bool> CreatePermissionGroupAsync(PermissionGroup permissionGroup, TiamServiceProvider serviceProvider)
|
|
||||||
{
|
|
||||||
bool result = false;
|
|
||||||
using (var transaction = Context.Database.BeginTransaction())
|
|
||||||
{
|
|
||||||
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");
|
|
||||||
Context.CreatePermissionsType(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, PermissionContextMappingSubjectType.Group, 1, true);
|
|
||||||
Context.CreatePermissionContextMapping(permissionContextMapping);
|
|
||||||
Context.CreatePermissionGroup(permissionGroup);
|
|
||||||
Context.SaveChanges();
|
|
||||||
transaction.Commit();
|
|
||||||
result = true;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
//group with same name already exists
|
|
||||||
result = false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return Task.FromResult(result);
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<UserProductMapping> GetUserProductMappingsInPermissionGroupByGroupId(Guid groupId)
|
|
||||||
{
|
|
||||||
return Context.GetUserProductMappingsByPermissionGroupId(groupId).ToList();
|
|
||||||
//List<UserProductMapping> userProductMappings = new List<UserProductMapping>();
|
|
||||||
|
|
||||||
////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 = Context.PermissionGroupUserMappings.Where(x => x.PermissionContextMappingId == pCmId).ToList();
|
|
||||||
//if (pGum.Count > 0)
|
|
||||||
// {
|
// {
|
||||||
// foreach (var group in pGum)
|
|
||||||
// {
|
|
||||||
// userProductMappings.Add(Context.UserProductMappings.FirstOrDefault(x => x.Id == group.UserProductMappingId));
|
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
// public ServiceProviderDal(ServiceProviderDbContext _object)
|
||||||
|
// {
|
||||||
// }
|
// }
|
||||||
|
|
||||||
//return Task.FromResult(userProductMappings);
|
//// #region ServiceProviders
|
||||||
}
|
|
||||||
|
//// //14. (IserviceProviderDataService) Update service provider
|
||||||
#endregion
|
//// public Task<bool> UpdateServiceProviderAsync(TiamServiceProvider serviceProvider)
|
||||||
|
//// {
|
||||||
#region Products
|
//// var dbServiceProvider = Context.ServiceProviders.FirstOrDefault(u => u.Id == serviceProvider.Id);
|
||||||
|
//// if (dbServiceProvider != null)
|
||||||
//public Product? GetProductById(Guid contextId) => Session(x => x.GetProductById(contextId));
|
//// {
|
||||||
|
//// dbServiceProvider = serviceProvider;
|
||||||
//* 19. (IServiceProviderDataService) Create product
|
//// Context.ServiceProviders.Update(dbServiceProvider);
|
||||||
public bool CreateProductAsync(Product product)
|
//// return Context.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
||||||
{
|
//// }
|
||||||
Context.CreateProduct(product);
|
//// else
|
||||||
Console.WriteLine($"Saving product to db {product.Id}, {product.Name}, {product.ServiceProviderId}");
|
//// {
|
||||||
var _result = Context.SaveChangesAsync();
|
//// throw new Exception("ServiceProvider not found");
|
||||||
return _result.Result > 0;
|
//// }
|
||||||
}
|
//// }
|
||||||
|
|
||||||
//* 20. (IServiceProviderDataService) Update product
|
//// //13. (IserviceProviderDataService) delete service provider
|
||||||
public Product UpdateProduct(Product product)
|
//// public Task<bool> DeleteServiceProviderAsync(Guid id)
|
||||||
{
|
//// {
|
||||||
|
//// using (var transaction = Context.Database.BeginTransaction())
|
||||||
var prod = Context.UpdateProduct(product);
|
//// {
|
||||||
Console.WriteLine($"Saving product to db {product.Id}, {product.Name}, {product.ServiceProviderId}");
|
//// var dbServiceProvider = Context.ServiceProviders.FirstOrDefault(u => u.Id == id);
|
||||||
Context.SaveChanges();
|
//// if (dbServiceProvider != null)
|
||||||
return prod;
|
//// {
|
||||||
}
|
//// //get products for this provider
|
||||||
|
//// var products = Context.Products.Where(x => x.ServiceProviderId == id).ToList();
|
||||||
//* 21. (IServiceProviderDataService) delete product
|
|
||||||
public Task<bool> DeleteProductByIdAsync(Guid productId)
|
//// /*foreach (var productItem in products)
|
||||||
{
|
//// {
|
||||||
return TransactionAsync(ctx =>
|
//// //delete products
|
||||||
{
|
//// var permissionContextMappings = Context.PermissionContextMappings.Where(x => x.ContextId == productItem.Id).ToList();
|
||||||
ctx.DeleteProductById(productId);
|
//// //iterate through every row
|
||||||
|
//// foreach (var item in permissionContextMappings)
|
||||||
return true;
|
//// {
|
||||||
});
|
|
||||||
}
|
//// if (item.SubjectType == (int)PermissionContextMappingSubjectType.Group)
|
||||||
|
//// {
|
||||||
//4. (IPermissionService) AssignPermissionToUserForContextAsync
|
//// //get users in the permissiongroup
|
||||||
public Task<bool> AssignPermissionToUserForContextAsync(UserProductMapping userProductMapping, PermissionsType permission)
|
//// var permissionGroupUserMapping = Context.PermissionGroupUserMappings.Where(x => x.PermissionContextMappingId == item.Id).ToList();
|
||||||
{
|
//// //remove every row (users) from permissiongroup
|
||||||
var _assIgnedUser = Context.UserProductMappings.FirstOrDefault(x => x.Id == userProductMapping.Id);
|
//// foreach (var user in permissionGroupUserMapping)
|
||||||
|
//// {
|
||||||
if (_assIgnedUser != null)
|
//// Context.PermissionGroupUserMappings.Remove(user);
|
||||||
{
|
//// }
|
||||||
//user exists
|
//// }
|
||||||
var _permissionInt = GetPermissionFromPermissionType(permission);
|
//// }
|
||||||
|
//// //remove permissioncontextmappings
|
||||||
var permissionContextMapping = Context.PermissionContextMappings.FirstOrDefault(x => x.SubjectId == userProductMapping.Id);
|
//// Context.PermissionContextMappings.RemoveRange(permissionContextMappings);
|
||||||
var currentPermissions = permissionContextMapping.Permissions;
|
//// }*/
|
||||||
var newPermissions = currentPermissions + _permissionInt.Result;
|
//// Context.Products.RemoveRange(products);
|
||||||
permissionContextMapping.Permissions = newPermissions;
|
//// Context.ServiceProviders.Remove(dbServiceProvider);
|
||||||
return Context.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
//// return Context.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
||||||
}
|
//// }
|
||||||
else
|
//// else
|
||||||
{
|
//// {
|
||||||
//user does not exist, let's create it
|
//// return Task.FromResult(false);
|
||||||
return Task.FromResult(false);
|
//// }
|
||||||
}
|
//// }
|
||||||
}
|
//// }
|
||||||
|
|
||||||
#endregion
|
//// //17. (IServiceProviderDataService) get service provider by ownerId
|
||||||
|
//// public Task<List<TiamServiceProvider>> GetServiceProvidersByOwnerIdAsync()
|
||||||
|
//// {
|
||||||
#region UserProductMappings
|
//// throw new NotImplementedException();
|
||||||
|
|
||||||
//22. (IServiceProviderDataService) Create userProductMapping
|
//// }
|
||||||
public Task<UserProductMapping> CreateUserProductMappingAsync(UserProductMapping userProductMapping)
|
|
||||||
{
|
//// #endregion
|
||||||
Context.UserProductMappings.Add(userProductMapping);
|
|
||||||
Console.WriteLine($"Saving userProductMapping to db {userProductMapping.Id}, {userProductMapping.ProductId}, {userProductMapping.UserId}");
|
//// #region PermissionTypes
|
||||||
return Context.SaveChangesAsync().ContinueWith(x => userProductMapping);
|
|
||||||
}
|
|
||||||
|
//// //10. (IPermissionService) create permission type
|
||||||
//23. (IServiceProviderDataService) Get Assigned Users By ProductId
|
//// public Task<bool> CreatePermissionsTypeAsync(PermissionsType permissionsType)
|
||||||
public Task<List<UserProductMapping>> GetUserProductMappingsByProductIdAsync(Guid productId)
|
//// {
|
||||||
{
|
//// bool result = false;
|
||||||
return Context.UserProductMappings.Where(x => x.ProductId == productId).ToListAsync();
|
|
||||||
}
|
//// using (var transaction = Context.Database.BeginTransaction())
|
||||||
|
//// {
|
||||||
|
//// var existingPermission = Context.PermissionsTypes
|
||||||
//24 . (IServiceProviderDataService) Remove Assigned Users By Product Id
|
//// .FirstOrDefault(x => x.PermissionName == permissionsType.PermissionName)?.PermissionName;
|
||||||
public Task RemoveUserProductMappingsByContextId(Guid productId)
|
|
||||||
{
|
//// if (existingPermission == null)
|
||||||
using (var transaction = Context.Database.BeginTransaction())
|
//// {
|
||||||
{
|
//// //get all the permissiontypes for this context
|
||||||
var userProductMappings = Context.UserProductMappings.Where(x => x.ProductId == productId).ToList();
|
//// var permissionTypes = new List<PermissionsType>();
|
||||||
//remove userProductMappings
|
//// var nextBitValue = 0.0;
|
||||||
|
//// permissionTypes = Context.PermissionsTypes
|
||||||
return Context.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
//// .Where(x => x.ContextId == permissionsType.ContextId)
|
||||||
|
//// .ToList();
|
||||||
}
|
|
||||||
}
|
//// //get the max value of the permissiontypes
|
||||||
|
//// if (permissionTypes != null)
|
||||||
//25. (IServiceProviderDataService) Remove Assigned from product by userProductMappingId
|
//// {
|
||||||
public Task<bool> RemoveUserProductMappingAsync(UserProductMapping userProductMapping, bool removeFromGroups)
|
//// //next bit value is the power of two of the count of the permissiontypes
|
||||||
{
|
//// nextBitValue = Math.Pow(2, permissionTypes.Count);
|
||||||
return TransactionAsync(ctx =>
|
//// }
|
||||||
{
|
//// else
|
||||||
var result = false;
|
//// {
|
||||||
var userProductMappingToRemove = ctx.UserProductMappings.FirstOrDefault(x => x.Id == userProductMapping.Id);
|
//// nextBitValue = Math.Pow(2, 0);
|
||||||
|
//// }
|
||||||
//remove userProductMappings
|
//// permissionsType.PermissionBit = (int)nextBitValue;
|
||||||
if (userProductMappingToRemove == null) return false;
|
//// Context.PermissionsTypes.Add(permissionsType);
|
||||||
|
//// Context.SaveChanges();
|
||||||
|
//// transaction.Commit();
|
||||||
if (removeFromGroups)
|
//// result = true;
|
||||||
{
|
//// }
|
||||||
//remove permissiongroupusermappings
|
//// else
|
||||||
ctx.RemoveAssingedUserFromPermissionGroups(userProductMappingToRemove.Id);
|
//// {
|
||||||
}
|
//// result = false;
|
||||||
|
//// }
|
||||||
ctx.UserProductMappings.Remove(userProductMappingToRemove);
|
|
||||||
|
//// }
|
||||||
|
//// return Task.FromResult(result);
|
||||||
return result;
|
|
||||||
});
|
//// }
|
||||||
}
|
|
||||||
|
//// //11. (IPermissionService) get permission types for context
|
||||||
//public Task RemoveUserProductMappingByUserIdAsync(Guid userProductMappingId)
|
//// public Task<List<PermissionsType>>? GetPermissionTypesByContextIdAsync(Guid contextId)
|
||||||
//{
|
//// {
|
||||||
// return TransactionAsync(ctx =>
|
//// return Context.PermissionsTypes.Where(x => x.ContextId == contextId).ToListAsync();
|
||||||
// {
|
//// }
|
||||||
// var userProductMapping = ctx.UserProductMappings.FirstOrDefault(x => x.Id == userProductMappingId);
|
|
||||||
// //remove userProductMappings
|
//// public Task<int> GetPermissionFromPermissionType(PermissionsType pType)
|
||||||
// if (userProductMapping == null) return false;
|
//// {
|
||||||
|
//// if (Context.PermissionsTypes.FirstOrDefault(x => x.Id == pType.Id) != null)
|
||||||
// //CleanUp
|
//// {
|
||||||
// //remove permissioncontextmappings
|
//// return Task.FromResult(pType.PermissionBit);
|
||||||
// ctx.RemoveUserProductMappingContextMappingBySubjectId(userProductMappingId);
|
//// }
|
||||||
// //remove permissiongroupusermappings
|
//// else
|
||||||
// ctx.RemoveAssingedUserFromAllProductPermissionGroups(userProductMappingId);
|
//// {
|
||||||
|
//// return Task.FromResult(0);
|
||||||
// return true;
|
//// }
|
||||||
// });
|
//// }
|
||||||
//}
|
|
||||||
|
//// #endregion
|
||||||
//public Task RemoveUserProductMappingContextMappingByUserProductMappingId(Guid userProductMappingId)
|
|
||||||
//{
|
//// #region PermissionMappings
|
||||||
// using (var transaction = Context.Database.BeginTransaction())
|
|
||||||
// {
|
//// public Task<List<AssignedPermissionModel>> GetPermissionsOfUserProductMappingsAndGroupsAsyncByContextId(Guid contextId)
|
||||||
// PermissionContextMapping? contextMapping = Context.PermissionContextMappings.FirstOrDefault(x => x.SubjectId == userProductMappingId);
|
//// {
|
||||||
// //remove userProductMappings
|
//// List<AssignedPermissionModel> result = new List<AssignedPermissionModel>();
|
||||||
// if(contextMapping != null)
|
|
||||||
// {
|
//// var UserProductMappings = Context.UserProductMappings.Where(x => x.ProductId == contextId).ToListAsync();
|
||||||
// Context.PermissionContextMappings.Remove(contextMapping);
|
|
||||||
// }
|
//// if (UserProductMappings.Result != null)
|
||||||
// return Context.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
//// {
|
||||||
|
//// foreach (var item in UserProductMappings.Result)
|
||||||
// }
|
//// {
|
||||||
//}
|
//// var mappingRow = Context.PermissionContextMappings.Where(x => x.SubjectId == item.Id).ToListAsync();
|
||||||
|
//// if (mappingRow.Result == null)
|
||||||
|
//// {
|
||||||
//public Task RemoveAssingedUserFromAllProductPermissionGroups(Guid userProductMappingId)
|
//// //user has no permission but is assigned... must be banned
|
||||||
//{
|
|
||||||
// using (var transaction = Context.Database.BeginTransaction())
|
//// }
|
||||||
// {
|
//// else if (mappingRow.Result.Count > 1)
|
||||||
// var permissionGroupUserMapping = Context.PermissionGroupUserMappings.Where(x => x.UserProductMappingId == userProductMappingId);
|
//// {
|
||||||
// //remove userProductMappings
|
//// //user has been assigned more than onece to same context
|
||||||
|
|
||||||
// if (permissionGroupUserMapping != null)
|
//// }
|
||||||
// {
|
//// else
|
||||||
// foreach (var item in permissionGroupUserMapping)
|
//// {
|
||||||
// {
|
//// foreach (var mapping in mappingRow.Result)
|
||||||
// Context.PermissionGroupUserMappings.Remove(item);
|
//// {
|
||||||
// }
|
//// result.Add(new AssignedPermissionModel(item.ProductId, item.Id, mapping.SubjectType, item.UserId.ToString(), mapping.Permissions));
|
||||||
// }
|
//// }
|
||||||
// return Context.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
//// }
|
||||||
|
|
||||||
// }
|
//// }
|
||||||
//}
|
//// }
|
||||||
|
|
||||||
|
//// var AssingedGroups = Context.PermissionGroups.Where(x => x.OwnerId == contextId).ToListAsync();
|
||||||
#endregion
|
|
||||||
|
//// if (AssingedGroups.Result != null)
|
||||||
}
|
//// {
|
||||||
|
//// foreach (var group in AssingedGroups.Result)
|
||||||
|
//// {
|
||||||
|
//// var mappingRow = Context.PermissionContextMappings.Where(x => x.SubjectId == group.Id).ToListAsync();
|
||||||
|
//// if (mappingRow.Result == null)
|
||||||
|
//// {
|
||||||
|
//// //group has no permission but is assigned...
|
||||||
|
|
||||||
|
//// }
|
||||||
|
//// else if (mappingRow.Result.Count > 1)
|
||||||
|
//// {
|
||||||
|
//// //group has been assigned more than onece to same context
|
||||||
|
|
||||||
|
//// }
|
||||||
|
//// else
|
||||||
|
//// {
|
||||||
|
//// foreach (var mapping in mappingRow.Result)
|
||||||
|
//// {
|
||||||
|
//// result.Add(new AssignedPermissionModel(group.OwnerId, group.Id, mapping.SubjectType, group.GroupName, mapping.Permissions));
|
||||||
|
//// }
|
||||||
|
//// }
|
||||||
|
|
||||||
|
//// }
|
||||||
|
//// }
|
||||||
|
//// foreach (var row in result)
|
||||||
|
//// {
|
||||||
|
//// Console.WriteLine($"GetPermissionsOfUserProductMappingsAndGroupsAsyncByContextId: {row.ContextId}, {row.SubjectId}, {row.SubjectType}, {row.Name}, {row.PermissionsValue}");
|
||||||
|
//// }
|
||||||
|
//// return Task.FromResult(result);
|
||||||
|
//// }
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
//// //12. (IPermissionService) get permission groups for context
|
||||||
|
//// public Task<List<PermissionContextMapping>> GetPermissionsForContextByContextIdAsync(Guid contextId)
|
||||||
|
//// {
|
||||||
|
//// List<PermissionContextMapping> permissionContextMappings = new List<PermissionContextMapping>();
|
||||||
|
//// //get all Groups where the contextId is the same
|
||||||
|
//// var groups = Context.PermissionGroups.Where(x => x.OwnerId == 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);
|
||||||
|
//// }
|
||||||
|
|
||||||
|
//// //9. (IPermissionService) add user to permission group
|
||||||
|
//// public Task<bool> AddUserToPermissionGroupAsync(Guid permissionGroupId, Guid userId)
|
||||||
|
//// {
|
||||||
|
//// bool result = false;
|
||||||
|
//// using (var transaction = Context.Database.BeginTransaction())
|
||||||
|
//// {
|
||||||
|
//// //do we need to check if PermissionContextMappingId exists?
|
||||||
|
//// var permissionGroupUserMapping = new PermissionGroupUserMapping(userId, permissionGroupId);
|
||||||
|
//// Context.PermissionGroupUserMappings.Add(permissionGroupUserMapping);
|
||||||
|
//// Context.SaveChanges();
|
||||||
|
//// transaction.Commit();
|
||||||
|
//// result = true;
|
||||||
|
//// }
|
||||||
|
//// return Task.FromResult(result);
|
||||||
|
//// }
|
||||||
|
|
||||||
|
//// //8. (IPermissionService) create permission group
|
||||||
|
//// public Task<bool> CreatePermissionGroupAsync(PermissionGroup permissionGroup, TiamServiceProvider serviceProvider)
|
||||||
|
//// {
|
||||||
|
//// bool result = false;
|
||||||
|
//// using (var transaction = Context.Database.BeginTransaction())
|
||||||
|
//// {
|
||||||
|
//// 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");
|
||||||
|
//// Context.CreatePermissionsType(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, PermissionContextMappingSubjectType.Group, 1, true);
|
||||||
|
//// Context.CreatePermissionContextMapping(permissionContextMapping);
|
||||||
|
//// Context.CreatePermissionGroup(permissionGroup);
|
||||||
|
//// Context.SaveChanges();
|
||||||
|
//// transaction.Commit();
|
||||||
|
//// result = true;
|
||||||
|
//// }
|
||||||
|
//// else
|
||||||
|
//// {
|
||||||
|
//// //group with same name already exists
|
||||||
|
//// result = false;
|
||||||
|
//// }
|
||||||
|
//// }
|
||||||
|
//// return Task.FromResult(result);
|
||||||
|
//// }
|
||||||
|
|
||||||
|
//// public List<UserProductMapping> GetUserProductMappingsInPermissionGroupByGroupId(Guid groupId)
|
||||||
|
//// {
|
||||||
|
//// return Context.GetUserProductMappingsByPermissionGroupId(groupId).ToList();
|
||||||
|
//// //List<UserProductMapping> userProductMappings = new List<UserProductMapping>();
|
||||||
|
|
||||||
|
//// ////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 = Context.PermissionGroupUserMappings.Where(x => x.PermissionContextMappingId == pCmId).ToList();
|
||||||
|
//// //if (pGum.Count > 0)
|
||||||
|
//// //{
|
||||||
|
//// // foreach (var group in pGum)
|
||||||
|
//// // {
|
||||||
|
//// // userProductMappings.Add(Context.UserProductMappings.FirstOrDefault(x => x.Id == group.UserProductMappingId));
|
||||||
|
//// // }
|
||||||
|
|
||||||
|
//// //}
|
||||||
|
|
||||||
|
//// //return Task.FromResult(userProductMappings);
|
||||||
|
//// }
|
||||||
|
|
||||||
|
//// #endregion
|
||||||
|
|
||||||
|
//// #region Products
|
||||||
|
|
||||||
|
//// //* 20. (IServiceProviderDataService) Update product
|
||||||
|
//// public Product UpdateProduct(Product product)
|
||||||
|
//// {
|
||||||
|
|
||||||
|
//// var prod = Context.UpdateProduct(product);
|
||||||
|
//// Console.WriteLine($"Saving product to db {product.Id}, {product.Name}, {product.ServiceProviderId}");
|
||||||
|
//// Context.SaveChanges();
|
||||||
|
//// return prod;
|
||||||
|
//// }
|
||||||
|
|
||||||
|
//// //* 21. (IServiceProviderDataService) delete product
|
||||||
|
//// public Task<bool> DeleteProductByIdAsync(Guid productId)
|
||||||
|
//// {
|
||||||
|
//// return TransactionAsync(ctx =>
|
||||||
|
//// {
|
||||||
|
//// ctx.DeleteProductById(productId);
|
||||||
|
|
||||||
|
//// return true;
|
||||||
|
//// });
|
||||||
|
//// }
|
||||||
|
|
||||||
|
//// //4. (IPermissionService) AssignPermissionToUserForContextAsync
|
||||||
|
//// public Task<bool> AssignPermissionToUserForContextAsync(UserProductMapping userProductMapping, PermissionsType permission)
|
||||||
|
//// {
|
||||||
|
//// var _assIgnedUser = Context.UserProductMappings.FirstOrDefault(x => x.Id == userProductMapping.Id);
|
||||||
|
|
||||||
|
//// if (_assIgnedUser != null)
|
||||||
|
//// {
|
||||||
|
//// //user exists
|
||||||
|
//// var _permissionInt = GetPermissionFromPermissionType(permission);
|
||||||
|
|
||||||
|
//// var permissionContextMapping = Context.PermissionContextMappings.FirstOrDefault(x => x.SubjectId == userProductMapping.Id);
|
||||||
|
//// var currentPermissions = permissionContextMapping.Permissions;
|
||||||
|
//// var newPermissions = currentPermissions + _permissionInt.Result;
|
||||||
|
//// permissionContextMapping.Permissions = newPermissions;
|
||||||
|
//// return Context.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
||||||
|
//// }
|
||||||
|
//// else
|
||||||
|
//// {
|
||||||
|
//// //user does not exist, let's create it
|
||||||
|
//// return Task.FromResult(false);
|
||||||
|
//// }
|
||||||
|
//// }
|
||||||
|
|
||||||
|
//// #endregion
|
||||||
|
|
||||||
|
|
||||||
|
//// #region UserProductMappings
|
||||||
|
|
||||||
|
//// //23. (IServiceProviderDataService) Get Assigned Users By ProductId
|
||||||
|
//// public Task<List<UserProductMapping>> GetUserProductMappingsByProductIdAsync(Guid productId)
|
||||||
|
//// {
|
||||||
|
//// return Context.UserProductMappings.Where(x => x.ProductId == productId).ToListAsync();
|
||||||
|
//// }
|
||||||
|
|
||||||
|
|
||||||
|
//// //24 . (IServiceProviderDataService) Remove Assigned Users By Product Id
|
||||||
|
//// public Task RemoveUserProductMappingsByContextId(Guid productId)
|
||||||
|
//// {
|
||||||
|
//// using (var transaction = Context.Database.BeginTransaction())
|
||||||
|
//// {
|
||||||
|
//// var userProductMappings = Context.UserProductMappings.Where(x => x.ProductId == productId).ToList();
|
||||||
|
//// //remove userProductMappings
|
||||||
|
|
||||||
|
//// return Context.SaveChangesAsync().ContinueWith(x => x.Result > 0);
|
||||||
|
|
||||||
|
//// }
|
||||||
|
//// }
|
||||||
|
|
||||||
|
//// //25. (IServiceProviderDataService) Remove Assigned from product by userProductMappingId
|
||||||
|
//// public Task<bool> RemoveUserProductMappingAsync(UserProductMapping userProductMapping, bool removeFromGroups)
|
||||||
|
//// {
|
||||||
|
//// return TransactionAsync(ctx =>
|
||||||
|
//// {
|
||||||
|
//// var result = false;
|
||||||
|
//// var userProductMappingToRemove = ctx.UserProductMappings.FirstOrDefault(x => x.Id == userProductMapping.Id);
|
||||||
|
|
||||||
|
//// //remove userProductMappings
|
||||||
|
//// if (userProductMappingToRemove == null) return false;
|
||||||
|
|
||||||
|
|
||||||
|
//// if (removeFromGroups)
|
||||||
|
//// {
|
||||||
|
//// //remove permissiongroupusermappings
|
||||||
|
//// ctx.RemoveAssingedUserFromPermissionGroups(userProductMappingToRemove.Id);
|
||||||
|
//// }
|
||||||
|
|
||||||
|
//// ctx.UserProductMappings.Remove(userProductMappingToRemove);
|
||||||
|
|
||||||
|
|
||||||
|
//// return result;
|
||||||
|
//// });
|
||||||
|
//// }
|
||||||
|
|
||||||
|
|
||||||
|
//// #endregion
|
||||||
|
|
||||||
|
////}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,189 +15,143 @@ using TIAM.Entities.Products;
|
||||||
using TIAM.Entities.ServiceProviders;
|
using TIAM.Entities.ServiceProviders;
|
||||||
using TIAM.Entities.Users;
|
using TIAM.Entities.Users;
|
||||||
|
|
||||||
namespace TIAM.Database.DataLayers.ServiceProviders;
|
//namespace TIAM.Database.DataLayers.ServiceProviders;
|
||||||
|
|
||||||
public static class ServiceProviderDalExtension
|
//public static class ServiceProviderDalExtension
|
||||||
{
|
//{
|
||||||
public static string ToJson<T>(this T source) where T : class, IEntity
|
// //public static string ToJson<T>(this T source) where T : class, IEntity
|
||||||
{
|
// //{
|
||||||
JsonSerializerSettings options = new()
|
// // JsonSerializerSettings options = new()
|
||||||
{
|
// // {
|
||||||
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
|
// // ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
|
||||||
NullValueHandling = NullValueHandling.Ignore
|
// // NullValueHandling = NullValueHandling.Ignore
|
||||||
};
|
// // };
|
||||||
|
|
||||||
return JsonConvert.SerializeObject(source, options);
|
// // return JsonConvert.SerializeObject(source, options);
|
||||||
}
|
// //}
|
||||||
|
|
||||||
public static string ToJson<T>(this IQueryable<T> source) where T : class, IEntity
|
// //public static string ToJson<T>(this IQueryable<T> source) where T : class, IEntity
|
||||||
{
|
// //{
|
||||||
JsonSerializerSettings options = new()
|
// // JsonSerializerSettings options = new()
|
||||||
{
|
// // {
|
||||||
ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
|
// // ReferenceLoopHandling = ReferenceLoopHandling.Ignore,
|
||||||
NullValueHandling = NullValueHandling.Ignore
|
// // NullValueHandling = NullValueHandling.Ignore
|
||||||
};
|
// // };
|
||||||
|
|
||||||
return JsonConvert.SerializeObject(source, options);
|
// // return JsonConvert.SerializeObject(source, options);
|
||||||
}
|
// //}
|
||||||
|
|
||||||
public static IQueryable<UserProductMapping> GetUserProductMappingsByPermissionGroupId(this IServiceProviderDbContext ctx, Guid permissionGroupId)
|
// //public static IQueryable<UserProductMapping> GetUserProductMappingsByPermissionGroupId(this IServiceProviderDbContext ctx, Guid permissionGroupId)
|
||||||
{
|
// //{
|
||||||
return ctx.UserProductMappings
|
// // return ctx.UserProductMappings
|
||||||
.Where(user => ctx.PermissionGroupUserMappings
|
// // .Where(user => ctx.PermissionGroupUserMappings
|
||||||
.Where(x => x.PermissionGroupId == permissionGroupId)
|
// // .Where(x => x.PermissionGroupId == permissionGroupId)
|
||||||
.Select(x => x.SubjectId)
|
// // .Select(x => x.SubjectId)
|
||||||
.Contains(user.Id));
|
// // .Contains(user.Id));
|
||||||
}
|
// //}
|
||||||
|
|
||||||
public static void CleanUpAndRemoveUserProductMappings(this IServiceProviderDbContext ctx, IEnumerable<UserProductMapping> userProductMappings)
|
// //public static void CleanUpAndRemoveUserProductMappings(this IServiceProviderDbContext ctx, IEnumerable<UserProductMapping> userProductMappings)
|
||||||
{
|
// //{
|
||||||
foreach (var userProductMapping in userProductMappings)
|
// // foreach (var userProductMapping in userProductMappings)
|
||||||
{
|
// // {
|
||||||
ctx.CleanUpAndRemoveAssignedUser(userProductMapping);
|
// // ctx.CleanUpAndRemoveAssignedUser(userProductMapping);
|
||||||
}
|
// // }
|
||||||
}
|
// //}
|
||||||
|
|
||||||
public static void CleanUpAndRemoveAssignedUser(this IServiceProviderDbContext ctx, UserProductMapping userProductMapping)
|
// //public static void CleanUpAndRemoveAssignedUser(this IServiceProviderDbContext ctx, UserProductMapping userProductMapping)
|
||||||
{
|
// //{
|
||||||
ctx.RemoveContextMappingBySubjectId(userProductMapping.Id);
|
// // ctx.RemoveContextMappingBySubjectId(userProductMapping.Id);
|
||||||
ctx.RemoveAssingedUserFromPermissionGroups(userProductMapping.Id);
|
// // ctx.RemoveAssingedUserFromPermissionGroups(userProductMapping.Id);
|
||||||
|
|
||||||
ctx.UserProductMappings.Remove(userProductMapping);
|
// // ctx.UserProductMappings.Remove(userProductMapping);
|
||||||
}
|
// //}
|
||||||
|
|
||||||
public static bool CleanUpAndRemoveUserProductMappings(this IServiceProviderDbContext ctx, Guid userProductMappingId)
|
// //public static bool CleanUpAndRemoveUserProductMappings(this IServiceProviderDbContext ctx, Guid userProductMappingId)
|
||||||
{
|
// //{
|
||||||
var userProductMapping = ctx.GetUserProductMappingById(userProductMappingId);
|
// // var userProductMapping = ctx.GetUserProductMappingById(userProductMappingId);
|
||||||
|
|
||||||
if (userProductMapping == null) return false;
|
// // if (userProductMapping == null) return false;
|
||||||
|
|
||||||
ctx.CleanUpAndRemoveAssignedUser(userProductMapping);
|
// // ctx.CleanUpAndRemoveAssignedUser(userProductMapping);
|
||||||
|
|
||||||
return true;
|
// // return true;
|
||||||
}
|
// //}
|
||||||
|
|
||||||
public static bool CreateAssignedUser(this IServiceProviderDbContext ctx, UserProductMapping userProductMapping)
|
// //public static UserProductMapping UpdateUserProductMapping(this IServiceProviderDbContext context, UserProductMapping userProductMapping)
|
||||||
{
|
// //{
|
||||||
if (userProductMapping == null) return false;
|
// // if(userProductMapping == null) return null;
|
||||||
|
// // var existingUserProductMapping = context.UserProductMappings.FirstOrDefault(u => u.Id == userProductMapping.Id);
|
||||||
|
// // if (existingUserProductMapping == null) return null;
|
||||||
|
// // existingUserProductMapping.Id = userProductMapping.Id;
|
||||||
|
// // existingUserProductMapping.UserId = userProductMapping.UserId;
|
||||||
|
// // existingUserProductMapping.ProductId = userProductMapping.ProductId;
|
||||||
|
|
||||||
ctx.UserProductMappings.Add(userProductMapping);
|
// // return existingUserProductMapping;
|
||||||
|
|
||||||
return true;
|
// //}
|
||||||
}
|
|
||||||
|
|
||||||
public static UserProductMapping UpdateUserProductMapping(this IServiceProviderDbContext context, UserProductMapping userProductMapping)
|
// //public static Product UpdateProduct(this IServiceProviderDbContext ctx, Product product)
|
||||||
{
|
// //{
|
||||||
if(userProductMapping == null) return null;
|
// // if (product == null) return null;
|
||||||
var existingUserProductMapping = context.UserProductMappings.FirstOrDefault(u => u.Id == userProductMapping.Id);
|
|
||||||
if (existingUserProductMapping == null) return null;
|
|
||||||
existingUserProductMapping.Id = userProductMapping.Id;
|
|
||||||
existingUserProductMapping.UserId = userProductMapping.UserId;
|
|
||||||
existingUserProductMapping.ProductId = userProductMapping.ProductId;
|
|
||||||
|
|
||||||
return existingUserProductMapping;
|
// // var existingProduct = ctx.Products.FirstOrDefault(u => u.Id == product.Id);
|
||||||
|
// // if (existingProduct == null) return null;
|
||||||
|
|
||||||
}
|
// // existingProduct.Name = product.Name;
|
||||||
|
// // existingProduct.ServiceProviderId = product.ServiceProviderId;
|
||||||
public static bool CreateProduct(this IServiceProviderDbContext ctx, Product myproduct)
|
// // existingProduct.Description = product.Description;
|
||||||
{
|
// // existingProduct.Price = product.Price;
|
||||||
if (myproduct == null) return false;
|
// // existingProduct.JsonDetails = product.JsonDetails;
|
||||||
//Automatically add assigneduser for owner
|
// // //existingProduct.UserMediaId = product.UserMediaId;
|
||||||
TiamServiceProvider? productOwner = ctx.ServiceProviders.FirstOrDefault(x => x.Id == myproduct.ServiceProviderId);
|
// // existingProduct.ProductType = product.ProductType;
|
||||||
if(productOwner == null) return false;
|
|
||||||
var userProductMapping = new UserProductMapping(myproduct.Id, productOwner.OwnerId);
|
|
||||||
ctx.CreateAssignedUser(userProductMapping);
|
|
||||||
ctx.AddProduct(myproduct);
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static Product UpdateProduct(this IServiceProviderDbContext ctx, Product product)
|
|
||||||
{
|
|
||||||
if (product == null) return null;
|
|
||||||
|
|
||||||
var existingProduct = ctx.Products.FirstOrDefault(u => u.Id == product.Id);
|
|
||||||
if (existingProduct == null) return null;
|
|
||||||
|
|
||||||
existingProduct.Name = product.Name;
|
|
||||||
existingProduct.ServiceProviderId = product.ServiceProviderId;
|
|
||||||
existingProduct.Description = product.Description;
|
|
||||||
existingProduct.Price = product.Price;
|
|
||||||
existingProduct.JsonDetails = product.JsonDetails;
|
|
||||||
//existingProduct.UserMediaId = product.UserMediaId;
|
|
||||||
existingProduct.ProductType = product.ProductType;
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return existingProduct;
|
// // return existingProduct;
|
||||||
}
|
// //}
|
||||||
|
|
||||||
public static void DeleteProductById(this IServiceProviderDbContext ctx, Guid productId)
|
// //public static void DeleteProductById(this IServiceProviderDbContext ctx, Guid productId)
|
||||||
{
|
// //{
|
||||||
var product = ctx.Products.FirstOrDefault(u => u.Id == productId);
|
// // var product = ctx.Products.FirstOrDefault(u => u.Id == productId);
|
||||||
if (product == null) return;
|
// // if (product == null) return;
|
||||||
|
|
||||||
ctx.CleanUpAndRemoveUserProductMappings(ctx.GetUserProductMappingsByProductId(productId));
|
// // ctx.CleanUpAndRemoveUserProductMappings(ctx.GetUserProductMappingsByProductId(productId));
|
||||||
ctx.Products.Remove(product);
|
// // ctx.Products.Remove(product);
|
||||||
}
|
// //}
|
||||||
|
|
||||||
public static bool CreatePermissionGroup(this IServiceProviderDbContext ctx, PermissionGroup permissionGroup)
|
// //public static bool CreatePermissionGroup(this IServiceProviderDbContext ctx, PermissionGroup permissionGroup)
|
||||||
{
|
// //{
|
||||||
if (permissionGroup == null) return false;
|
// // if (permissionGroup == null) return false;
|
||||||
|
|
||||||
ctx.PermissionGroups.Add(permissionGroup);
|
// // ctx.PermissionGroups.Add(permissionGroup);
|
||||||
|
|
||||||
return true;
|
// // return true;
|
||||||
}
|
// //}
|
||||||
|
|
||||||
public static bool CreatePermissionContextMapping(this IServiceProviderDbContext ctx, PermissionContextMapping permissionContextMapping)
|
// //public static bool CreatePermissionContextMapping(this IServiceProviderDbContext ctx, PermissionContextMapping permissionContextMapping)
|
||||||
{
|
// //{
|
||||||
if (permissionContextMapping == null) return false;
|
// // if (permissionContextMapping == null) return false;
|
||||||
|
|
||||||
ctx.PermissionContextMappings.Add(permissionContextMapping);
|
// // ctx.PermissionContextMappings.Add(permissionContextMapping);
|
||||||
|
|
||||||
return true;
|
// // return true;
|
||||||
}
|
// //}
|
||||||
|
|
||||||
public static bool CreatePermissionGroupUserMapping(this IServiceProviderDbContext ctx, PermissionGroupUserMapping permissionGroupUserMapping)
|
// //public static bool CreatePermissionGroupUserMapping(this IServiceProviderDbContext ctx, PermissionGroupUserMapping permissionGroupUserMapping)
|
||||||
{
|
// //{
|
||||||
if (permissionGroupUserMapping == null) return false;
|
// // if (permissionGroupUserMapping == null) return false;
|
||||||
|
|
||||||
ctx.PermissionGroupUserMappings.Add(permissionGroupUserMapping);
|
// // ctx.PermissionGroupUserMappings.Add(permissionGroupUserMapping);
|
||||||
|
|
||||||
return true;
|
// // return true;
|
||||||
}
|
// //}
|
||||||
|
|
||||||
public static bool CreatePermissionsType(this IServiceProviderDbContext ctx, PermissionsType permissionType)
|
// //public static bool CreatePermissionsType(this IServiceProviderDbContext ctx, PermissionsType permissionType)
|
||||||
{
|
// //{
|
||||||
if (permissionType == null) return false;
|
// // if (permissionType == null) return false;
|
||||||
|
|
||||||
ctx.PermissionsTypes.Add(permissionType);
|
// // ctx.PermissionsTypes.Add(permissionType);
|
||||||
|
|
||||||
return true;
|
// // return true;
|
||||||
}
|
// //}
|
||||||
|
//}
|
||||||
|
|
||||||
|
|
||||||
public static TiamServiceProvider CreateServiceProvider(this IServiceProviderDbContext ctx, TiamServiceProvider serviceProvider)
|
|
||||||
{
|
|
||||||
if (serviceProvider == null) return null;
|
|
||||||
|
|
||||||
ctx.ServiceProviders.Add(serviceProvider);
|
|
||||||
var userProductMapping = new UserProductMapping(serviceProvider.Id, serviceProvider.OwnerId);
|
|
||||||
ctx.CreateAssignedUser(userProductMapping);
|
|
||||||
return serviceProvider;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static TiamServiceProvider UpdateServiceProvider(this IServiceProviderDbContext ctx, TiamServiceProvider serviceProvider)
|
|
||||||
{
|
|
||||||
if (serviceProvider == null) return null;
|
|
||||||
|
|
||||||
var existingServiceProvider = ctx.ServiceProviders.FirstOrDefault(u => u.Id == serviceProvider.Id);
|
|
||||||
if (existingServiceProvider == null) return null;
|
|
||||||
|
|
||||||
existingServiceProvider.Name = serviceProvider.Name;
|
|
||||||
existingServiceProvider.OwnerId = serviceProvider.OwnerId;
|
|
||||||
return existingServiceProvider;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -8,7 +8,8 @@ using SkiaSharp.Views.Desktop;
|
||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Drawing.Imaging;
|
using System.Drawing.Imaging;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using TIAM.Database.DataLayers.ServiceProviders;
|
using TIAM.Database.DataLayers.Admins;
|
||||||
|
//using TIAM.Database.DataLayers.ServiceProviders;
|
||||||
using TIAM.Entities.Permissions;
|
using TIAM.Entities.Permissions;
|
||||||
using TIAM.Entities.Products;
|
using TIAM.Entities.Products;
|
||||||
using TIAM.Entities.ServiceProviders;
|
using TIAM.Entities.ServiceProviders;
|
||||||
|
|
@ -23,15 +24,15 @@ namespace TIAMWebApp.Server.Controllers
|
||||||
[Route("api/[controller]")]
|
[Route("api/[controller]")]
|
||||||
public class ServiceProviderAPIController : ControllerBase
|
public class ServiceProviderAPIController : ControllerBase
|
||||||
{
|
{
|
||||||
private ServiceProviderDal _serviceProviderDal;
|
private AdminDal _adminDal;
|
||||||
|
|
||||||
private readonly ILogger<ServiceProviderAPIController> _logger;
|
private readonly ILogger<ServiceProviderAPIController> _logger;
|
||||||
|
|
||||||
public ServiceProviderAPIController(ILogger<ServiceProviderAPIController> logger, ServiceProviderDal serviceProviderDal)
|
public ServiceProviderAPIController(ILogger<ServiceProviderAPIController> logger, AdminDal adminDal)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
|
|
||||||
_serviceProviderDal = serviceProviderDal;
|
_adminDal = adminDal;
|
||||||
}
|
}
|
||||||
|
|
||||||
//15.
|
//15.
|
||||||
|
|
@ -73,7 +74,7 @@ namespace TIAMWebApp.Server.Controllers
|
||||||
|
|
||||||
Console.WriteLine($"ServiceProvider to be created: {id}, {name}, {ownerId}");
|
Console.WriteLine($"ServiceProvider to be created: {id}, {name}, {ownerId}");
|
||||||
|
|
||||||
await _serviceProviderDal.CreateServiceProviderAsync(new TiamServiceProvider(id, name, ownerId));
|
await _adminDal.CreateServiceProviderAsync(new TiamServiceProvider(id, name, ownerId));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return Ok("yes");
|
return Ok("yes");
|
||||||
|
|
@ -92,7 +93,7 @@ namespace TIAMWebApp.Server.Controllers
|
||||||
{
|
{
|
||||||
//var users = await _serviceProviderDal.Ctx.Users.ToListAsync();//.GetUsersAsync();
|
//var users = await _serviceProviderDal.Ctx.Users.ToListAsync();//.GetUsersAsync();
|
||||||
//return users;
|
//return users;
|
||||||
return _serviceProviderDal.GetServiceProvidersAsync();
|
return _adminDal.GetServiceProvidersAsync();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -104,7 +105,7 @@ namespace TIAMWebApp.Server.Controllers
|
||||||
{
|
{
|
||||||
Console.WriteLine($"GetServiceProviderById called with id: {id}");
|
Console.WriteLine($"GetServiceProviderById called with id: {id}");
|
||||||
|
|
||||||
return await _serviceProviderDal.GetServiceProviderByIdAsync(id);
|
return await _adminDal.GetServiceProviderByIdAsync(id);
|
||||||
}
|
}
|
||||||
|
|
||||||
//17.
|
//17.
|
||||||
|
|
@ -117,7 +118,7 @@ namespace TIAMWebApp.Server.Controllers
|
||||||
{
|
{
|
||||||
Console.WriteLine($"GetServiceProvidersByOwnerId called with ownerId: {ownerId}");
|
Console.WriteLine($"GetServiceProvidersByOwnerId called with ownerId: {ownerId}");
|
||||||
|
|
||||||
var serviceProviders = await _serviceProviderDal.GetServiceProvidersAsync();
|
var serviceProviders = await _adminDal.GetServiceProvidersAsync();
|
||||||
|
|
||||||
//return serviceProviders.Where(x => x.OwnerId == ownerId).ToList();
|
//return serviceProviders.Where(x => x.OwnerId == ownerId).ToList();
|
||||||
var myServiceproviders = serviceProviders.Where(x => x.OwnerId == ownerId).ToDictionary(x => x.Id, x => x.Name);
|
var myServiceproviders = serviceProviders.Where(x => x.OwnerId == ownerId).ToDictionary(x => x.Id, x => x.Name);
|
||||||
|
|
@ -144,7 +145,7 @@ namespace TIAMWebApp.Server.Controllers
|
||||||
|
|
||||||
UserProductMapping userProductMapping = new UserProductMapping(createUserProductMappingModel.ContextId, createUserProductMappingModel.ContextId);
|
UserProductMapping userProductMapping = new UserProductMapping(createUserProductMappingModel.ContextId, createUserProductMappingModel.ContextId);
|
||||||
|
|
||||||
var result = await _serviceProviderDal.CreateUserProductMappingAsync(userProductMapping);
|
var result = await _adminDal.CreateUserProductMappingAsync(userProductMapping);
|
||||||
|
|
||||||
|
|
||||||
return Ok(result);
|
return Ok(result);
|
||||||
|
|
@ -162,7 +163,7 @@ namespace TIAMWebApp.Server.Controllers
|
||||||
|
|
||||||
Dictionary<Guid, string> userProductMappingDictionary = new Dictionary<Guid, string>();
|
Dictionary<Guid, string> userProductMappingDictionary = new Dictionary<Guid, string>();
|
||||||
|
|
||||||
var serviceProviders = await _serviceProviderDal.GetServiceProvidersAsync();
|
var serviceProviders = await _adminDal.GetServiceProvidersAsync();
|
||||||
|
|
||||||
var myServiceproviders = serviceProviders.Where(x => x.Id == serviceProviderId).ToDictionary(x => x.Id, x => x.Name);
|
var myServiceproviders = serviceProviders.Where(x => x.Id == serviceProviderId).ToDictionary(x => x.Id, x => x.Name);
|
||||||
//put serviceprovider id and name into a dictionary
|
//put serviceprovider id and name into a dictionary
|
||||||
|
|
@ -171,18 +172,18 @@ namespace TIAMWebApp.Server.Controllers
|
||||||
}
|
}
|
||||||
|
|
||||||
[HttpPost]
|
[HttpPost]
|
||||||
[Route("CreateProduct")]
|
[Route(APIUrls.AddProductRouteName)]
|
||||||
[Tags("In-Progress", "Product")]
|
[Tags("In-Progress", "Product")]
|
||||||
public async Task<IActionResult> CreateProduct([FromBody] Product product)
|
public async Task<IActionResult> AddProduct([FromBody] Product product)
|
||||||
{
|
{
|
||||||
Console.WriteLine("CreateProduct called");
|
Console.WriteLine("AddProduct called");
|
||||||
if (product == null)
|
if (product == null)
|
||||||
{
|
{
|
||||||
return BadRequest("Product is required");
|
return BadRequest("Product is required");
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var result = _serviceProviderDal.CreateProductAsync(product);
|
var result = _adminDal.AddProduct(product);
|
||||||
return Ok(result);
|
return Ok(result);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,8 @@ using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
using TIAM.Database.DataLayers.ServiceProviders;
|
using TIAM.Database.DataLayers.Admins;
|
||||||
|
//using TIAM.Database.DataLayers.ServiceProviders;
|
||||||
using TIAM.Database.DataLayers.Users;
|
using TIAM.Database.DataLayers.Users;
|
||||||
using TIAM.Entities.Permissions;
|
using TIAM.Entities.Permissions;
|
||||||
using TIAM.Entities.Products;
|
using TIAM.Entities.Products;
|
||||||
|
|
@ -18,15 +19,15 @@ namespace TIAMWebApp.Server.Controllers
|
||||||
[Route("api/[controller]")]
|
[Route("api/[controller]")]
|
||||||
public class UserPermissionAPIController : ControllerBase
|
public class UserPermissionAPIController : ControllerBase
|
||||||
{
|
{
|
||||||
private ServiceProviderDal _serviceProviderDal;
|
private AdminDal _adminDal;
|
||||||
|
|
||||||
private readonly ILogger<UserPermissionAPIController> _logger;
|
private readonly ILogger<UserPermissionAPIController> _logger;
|
||||||
|
|
||||||
public UserPermissionAPIController(ILogger<UserPermissionAPIController> logger, ServiceProviderDal serviceProviderDal)
|
public UserPermissionAPIController(ILogger<UserPermissionAPIController> logger, AdminDal adminDal)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
|
|
||||||
_serviceProviderDal = serviceProviderDal;
|
_adminDal = adminDal;
|
||||||
}
|
}
|
||||||
|
|
||||||
//1. create a permission to be assigned to users to access a context
|
//1. create a permission to be assigned to users to access a context
|
||||||
|
|
@ -69,7 +70,7 @@ namespace TIAMWebApp.Server.Controllers
|
||||||
|
|
||||||
Console.WriteLine($"GetPermissionsForContextByContextId called with contextId: {contextId}");
|
Console.WriteLine($"GetPermissionsForContextByContextId called with contextId: {contextId}");
|
||||||
Dictionary<Guid, int> permissionsDictionary = new Dictionary<Guid, int>();
|
Dictionary<Guid, int> permissionsDictionary = new Dictionary<Guid, int>();
|
||||||
var permissions = await _serviceProviderDal.GetPermissionsForContextByContextIdAsync(contextId);
|
var permissions = await _adminDal.GetPermissionsForContextByContextIdAsync(contextId);
|
||||||
return Ok(permissions);
|
return Ok(permissions);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -88,7 +89,7 @@ namespace TIAMWebApp.Server.Controllers
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
var response = await _serviceProviderDal.AssignPermissionToUserForContextAsync(assignPermissionModel.UserProductMapping, assignPermissionModel.PermissionsType);
|
var response = await _adminDal.AssignPermissionToUserForContextAsync(assignPermissionModel.UserProductMapping, assignPermissionModel.PermissionsType);
|
||||||
return Ok(response);
|
return Ok(response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -123,7 +124,7 @@ namespace TIAMWebApp.Server.Controllers
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
PermissionsType permissionType = new PermissionsType(contextId, name);
|
PermissionsType permissionType = new PermissionsType(contextId, name);
|
||||||
var response = await _serviceProviderDal.CreatePermissionsTypeAsync(permissionType);
|
var response = await _adminDal.CreatePermissionsTypeAsync(permissionType);
|
||||||
return Ok(response);
|
return Ok(response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ using AyCode.Interfaces.Messages;
|
||||||
using TIAM.Database.DataLayers.Admins;
|
using TIAM.Database.DataLayers.Admins;
|
||||||
using TIAM.Database.DataLayers.Auctions;
|
using TIAM.Database.DataLayers.Auctions;
|
||||||
using TIAMWebApp.Server.Services;
|
using TIAMWebApp.Server.Services;
|
||||||
using TIAM.Database.DataLayers.ServiceProviders;
|
//using TIAM.Database.DataLayers.ServiceProviders;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
|
|
@ -29,7 +29,7 @@ builder.Services.AddRazorPages();
|
||||||
builder.Services.AddScoped<UserDal>();
|
builder.Services.AddScoped<UserDal>();
|
||||||
builder.Services.AddScoped<AdminDal>();
|
builder.Services.AddScoped<AdminDal>();
|
||||||
builder.Services.AddScoped<AuctionDal>();
|
builder.Services.AddScoped<AuctionDal>();
|
||||||
builder.Services.AddScoped<ServiceProviderDal>();
|
//builder.Services.AddScoped<ServiceProviderDal>();
|
||||||
builder.Services.AddScoped<TransferDestinationDal>();
|
builder.Services.AddScoped<TransferDestinationDal>();
|
||||||
|
|
||||||
builder.Services.AddSwaggerGen(swagger =>
|
builder.Services.AddSwaggerGen(swagger =>
|
||||||
|
|
|
||||||
|
|
@ -8,13 +8,19 @@ namespace TIAMWebApp.Shared.Application.Models
|
||||||
{
|
{
|
||||||
public class APIUrls
|
public class APIUrls
|
||||||
{
|
{
|
||||||
public const string UserTest = "api/UserAPI/test1";
|
public const string BaseUrl = "api";
|
||||||
public const string GetUserByEmail = "api/UserAPI/GetUserByEmail";
|
public const string BaseUrlWithSlash = BaseUrl + "/";
|
||||||
public const string GetUserById = "api/UserAPI/GetUserById";
|
public const string BaseUrlWithSlashAndVersion = BaseUrlWithSlash + "v1/";
|
||||||
public const string GetUsers = "api/UserAPI/GetUsers";
|
|
||||||
public const string AuthenticateUser = "api/UserAPI/AuthenticateUser";
|
public const string UserAPI = BaseUrlWithSlash+"UserAPI";
|
||||||
public const string CreateUser = "api/UserAPI/CreateUser";
|
public const string UserTest = UserAPI + "/test1";
|
||||||
public const string RefreshToken = "/api/UserAPI/RefreshToken";
|
public const string GetUserByEmail = UserAPI + "/GetUserByEmail";
|
||||||
|
public const string GetUserById = UserAPI + "/GetUserById";
|
||||||
|
public const string GetUsers = UserAPI + "/GetUsers";
|
||||||
|
public const string AuthenticateUser = UserAPI + "/AuthenticateUser";
|
||||||
|
public const string CreateUser = UserAPI + "/CreateUser";
|
||||||
|
public const string RefreshToken = UserAPI + "/RefreshToken";
|
||||||
|
|
||||||
|
|
||||||
public const string WeatherForecast = "api/WeatherForecastAPI";
|
public const string WeatherForecast = "api/WeatherForecastAPI";
|
||||||
|
|
||||||
|
|
@ -27,6 +33,8 @@ namespace TIAMWebApp.Shared.Application.Models
|
||||||
|
|
||||||
public const string GetServiceProvidersByOwnerId = "api/ServiceProviderAPI/GetServiceProvidersByOwnerId";
|
public const string GetServiceProvidersByOwnerId = "api/ServiceProviderAPI/GetServiceProvidersByOwnerId";
|
||||||
public const string GetQRCodeByProductId = "api/ServiceProviderAPI/GetQRCodeByProductId";
|
public const string GetQRCodeByProductId = "api/ServiceProviderAPI/GetQRCodeByProductId";
|
||||||
|
public const string AddProductRouteName = "AddProduct";
|
||||||
|
public const string AddProductRouteUrl = "api/ServiceProviderAPI/"+AddProductRouteName;
|
||||||
|
|
||||||
//AssingedUsers
|
//AssingedUsers
|
||||||
public const string CreateAssignedUser = "api/ServiceProviderAPI/CreateAssignedUser";
|
public const string CreateAssignedUser = "api/ServiceProviderAPI/CreateAssignedUser";
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue