/// <summary> /// Get user permissions /// </summary> /// <param name="userId">User identifier</param> /// <returns>Return user permissions list</returns> public Task <UserPermissionDto> GetUserPermissionById(string userId) { //Validazione argomenti if (string.IsNullOrEmpty(userId)) { throw new ArgumentNullException(nameof(userId)); } var cached = _cache.GetValue <UserPermissionDto>($"perm:{userId}"); if (cached != null) { return(Task.FromResult(cached)); } // user group TODO to be implemented // var permissionGroupIds = _userPermissionGroupRepository.FetchWithProjection(x=>x.PermissionGroupId,x => x.UserId == userId); // user permission var userPermissions = _userPermissionRepository.Fetch(x => x.UserId == userId); // extract permission var permissionIds = userPermissions.Select(x => x.PermissionId).ToList(); // user roles var userRoles = _userRoleRepository.Fetch(x => x.UserId == userId); var rolesIds = userRoles.Select(x => x.RoleId).ToList(); var permissionRoles = _permissionRoleRepository.Fetch(x => rolesIds.Contains(x.RoleId)); permissionIds.AddRange(permissionRoles.Select(x => x.PermissionId)); // permission applied var permissions = _permissionRepository.Fetch(x => permissionIds.Contains(x.Id)); var genericPermissionsDto = new List <string>(); // split permission between entities and general // general // - roles var generalRoles = userRoles.Where(x => string.IsNullOrEmpty(x.EntityId)); var generalRolesId = generalRoles.Select(x => x.RoleId).ToList(); var permissionsFromRole = permissionRoles.Where(x => generalRolesId.Contains(x.RoleId)) .Select(x => x.PermissionId).ToList(); // - permissions var generalPermissionIds = userPermissions.Where(x => string.IsNullOrEmpty(x.EntityId)).Select(x => x.PermissionId).ToList(); permissionsFromRole.AddRange(generalPermissionIds); genericPermissionsDto.AddRange(permissions.Where(x => permissionsFromRole.Contains(x.Id)).Select(x => x.Name)); var genericPermissions = genericPermissionsDto .Distinct() .Select(x => x.ParseEnum <Permissions>()) .ToList(); // entities // group by entityId // entity type is not relevant because a the permission name is unique, an entityid associated to 'edit match' will be for sure related to match entity // - roles var targetingRoles = userRoles.Where(x => !string.IsNullOrEmpty(x.EntityId)); var entityPermissionList = targetingRoles.GroupBy(x => x.EntityId).Select(x => new EntityPermission { EntityId = x.Key, Permissions = permissionRoles.Where(pr => x.Select(r => r.RoleId).ToList().Contains(pr.RoleId)) .Select(pr => pr.PermissionId.ParseEnum <Permissions>()).ToList() }).ToList(); // - permissions var targetingPermissions = userPermissions.Where(x => !string.IsNullOrEmpty(x.EntityId)); var tp = targetingPermissions.GroupBy(x => x.EntityId).Select(x => new EntityPermissionTmp { EntityId = x.Key, Permissions = x.Select(c => c.PermissionId).ToList() }).ToList(); // merge lists foreach (var t in tp) { var tmp = entityPermissionList.FirstOrDefault(x => x.EntityId == t.EntityId); if (tmp == null) { entityPermissionList.Add(new EntityPermission() { EntityId = t.EntityId, Permissions = permissions.Where(x => t.Permissions.Contains(x.Id)) .Select(x => x.Name) .Distinct() .Select(x => x.ParseEnum <Permissions>()) .ToList() }); continue; } tmp.Permissions.AddRange(t.Permissions.Select(x => x.ParseEnum <Permissions>()).ToList()); } var result = new UserPermissionDto { GenericPermissions = genericPermissions, EntityPermissions = entityPermissionList }; _cache.SetValue(userId, result); //Recupero i dati, commit ed uscita return(Task.FromResult(result)); }