private async Task <User> ValidateUser(Guid subClaim, string nameClaim, bool update) { var user = await _context.Users .Where(u => u.Id == subClaim) .FirstOrDefaultAsync(); var anyUsers = await _context.Users.AnyAsync(); if (update) { if (user == null) { user = new User { Id = subClaim, Name = nameClaim ?? "Anonymous" }; // First user is default SystemAdmin if (!anyUsers) { var systemAdminPermission = await _context.Permissions.Where(p => p.Key == nameof(CasterClaimTypes.SystemAdmin)).FirstOrDefaultAsync(); if (systemAdminPermission != null) { user.UserPermissions.Add(new UserPermission(user.Id, systemAdminPermission.Id)); } } _context.Users.Add(user); await _context.SaveChangesAsync(); } else { if (nameClaim != null && user.Name != nameClaim) { user.Name = nameClaim; _context.Update(user); await _context.SaveChangesAsync(); } } } return(user); }
private async Task <User> ValidateUser(Guid subClaim, string nameClaim, bool update) { var userQuery = _context.Users.Where(u => u.Id == subClaim).Future(); var anyUsers = _context.Users.DeferredAny().FutureValue(); var user = (await userQuery.ToListAsync()).SingleOrDefault(); if (update) { if (user == null) { user = new User { Id = subClaim, Name = nameClaim ?? "Anonymous" }; // First user is default SystemAdmin if (!(await anyUsers.ValueAsync())) { var systemAdminPermission = await _context.Permissions.Where(p => p.Key == CasterClaimTypes.SystemAdmin.ToString()).FirstOrDefaultAsync(); if (systemAdminPermission != null) { user.UserPermissions.Add(new UserPermission(user.Id, systemAdminPermission.Id)); } } _context.Users.Add(user); await _context.SaveChangesAsync(); } else { if (nameClaim != null && user.Name != nameClaim) { user.Name = nameClaim; _context.Update(user); await _context.SaveChangesAsync(); } } } return(user); }