public async Task <bool> SavePermissions(IEnumerable <PermissionDto> dto) { foreach (var data in dto) { foreach (var permission in data.Permissions) { var x = new UserEntity_assoc { Id = permission.Id, EntityId = permission.EntityId, UserId = permission.UserId, HasAccess = permission.HasAccess }; _dbContext.Update(x); } } try { await _dbContext.SaveChangesAsync(); return(true); } catch { return(false); } }
public async Task <User> Register(User user, string password) { byte[] passwordHash, passwordSalt; CreatePaaswordHash(password, out passwordHash, out passwordSalt); user.PasswordHash = passwordHash; user.PasswordSalt = passwordSalt; await _dbContext.AppUsers.AddAsync(user); await _dbContext.SaveChangesAsync(); if (_dbContext.AppUsers.Count() == 1) { user = await _dbContext.AppUsers .Include(_ => _.Permissions) .FirstOrDefaultAsync(x => x.Username == user.Username); for (var i = 1; i <= 3; i++) { var permissions = new UserEntity_assoc { UserId = user.Id, EntityId = i, HasAccess = true }; user.Permissions.Add(permissions); } } else if (_dbContext.AppUsers.Count() > 1) { user = await _dbContext.AppUsers .Include(_ => _.Permissions) .FirstOrDefaultAsync(x => x.Username == user.Username); for (var i = 1; i <= 3; i++) { var permissions = new UserEntity_assoc { UserId = user.Id, EntityId = i, HasAccess = false }; user.Permissions.Add(permissions); } } try { _dbContext.UserEntity_Assocs.AddRange(user.Permissions); _dbContext.SaveChanges(); } catch (Exception ex) { return(user); throw; } user = await _dbContext.AppUsers .Include(_ => _.Permissions).ThenInclude(_ => _.Entities) .FirstOrDefaultAsync(x => x.Username == user.Username); return(user); }