Beispiel #1
0
        public async Task <bool> DeleteUserAsync(Guid id)
        {
            var curUser = await userContext.GetCurrentUserAsync();

            var isAdmin = await dbContext.UserRoles
                          .AnyAsync(x => x.UserId == curUser.Id && x.RoleId == Users.Admin);

            if (!isAdmin || id == curUser.Id)
            {
                return(false);
            }

            var files = await dbContext.Files
                        .Where(x => x.UserId == id)
                        .ToListAsync();

            foreach (var file in files)
            {
                await objectStorage.RemoveAsync(file.Address);
            }

            dbContext.Files.RemoveRange(files);

            var user = await dbContext.Users
                       .FirstOrDefaultAsync(x => x.Id == id);

            await userManager.DeleteAsync(user);

            await dbContext.SaveChangesAsync();

            return(true);
        }
        public async Task <DeleteFileByIdCommandResponse> DeleteFromBucket(List <Guid> ids)
        {
            ids ??= new List <Guid>();
            var user = await userContext.GetCurrentUserAsync();

            var isAdmin = await dbContext.UserRoles
                          .AnyAsync(x => x.UserId == user.Id && x.RoleId == Users.Admin);

            if (!isAdmin)
            {
                var filesToDelete = await dbContext.Files
                                    .Where(x => x.IsDeleted)
                                    .Where(x => x.UserId == user.Id)
                                    .Where(x => !ids.Any() || ids.Contains(x.Id))
                                    .ToListAsync();

                foreach (var file in filesToDelete)
                {
                    await objectStorage.RemoveAsync(file.Address);
                }
                dbContext.Files.RemoveRange(filesToDelete);
                await dbContext.SaveChangesAsync();
            }
            else
            {
                var filesToDelete = await dbContext.Files
                                    .Where(x => x.IsDeleted)
                                    .Where(x => !ids.Any() || ids.Contains(x.Id)).ToListAsync();

                foreach (var file in filesToDelete)
                {
                    await objectStorage.RemoveAsync(file.Address);
                }
                dbContext.Files.RemoveRange(filesToDelete);
                await dbContext.SaveChangesAsync();
            }
            return(new DeleteFileByIdCommandResponse {
                Success = true
            });
        }