public async Task <bool> UpdateLogin(Login login) { if (login == null || string.IsNullOrEmpty(login.LogIn)) { throw new ArgumentException("Логин не задан"); } if (string.IsNullOrEmpty(login.Password)) { throw new ArgumentException("Пароль не задан"); } if (login.UserId == Guid.Empty) { throw new ArgumentException("Пользователь не задан"); } if (login.GroupId == Guid.Empty) { throw new ArgumentException("Группа не задана"); } var existed = await _dbContext.Logins.FirstOrDefaultAsync(x => x.LogIn.Equals(login.LogIn, StringComparison.InvariantCultureIgnoreCase)); if (existed == null) { throw new Exception($"Логин {login.LogIn} не существует"); } existed.Certificate = CertificateWorker.CreateCertificate(); existed.LogIn = login.LogIn; existed.Password = login.Password; existed.GroupId = login.GroupId; existed.UserId = login.UserId; var updated = await(_dbContext as DbContext).SaveChangesAsync(); return(updated > 0); }
public async Task <Login> AddLogin(Login newLogin) { if (newLogin == null || string.IsNullOrEmpty(newLogin.LogIn)) { throw new ArgumentException("Логин не задан"); } if (string.IsNullOrEmpty(newLogin.Password)) { throw new ArgumentException("Пароль не задан"); } if (newLogin.UserId == Guid.Empty) { throw new ArgumentException("Пользователь не задан"); } if (newLogin.GroupId == Guid.Empty) { throw new ArgumentException("Группа не заданa"); } var existed = await _dbContext.Logins.AsNoTracking().FirstOrDefaultAsync(x => x.LogIn.Equals(newLogin.LogIn, StringComparison.InvariantCultureIgnoreCase)); if (existed != null) { throw new Exception($"Логин {newLogin.LogIn} уже существует"); } newLogin.LoginId = Guid.NewGuid(); newLogin.Certificate = CertificateWorker.CreateCertificate(); var result = await _dbContext.Logins.AddAsync(newLogin); await(_dbContext as DbContext).SaveChangesAsync(); return(result.Entity); }
public static void InitDbContext(ModelBuilder modelBuilder, bool initializeDerivedContext) { if (initializeDerivedContext) { UserDbContext.InitDbContext(modelBuilder); modelBuilder.Entity <Login>().HasOne(x => x.LoginUser); GroupDbContext.InitDbContext(modelBuilder); modelBuilder.Entity <Login>().HasOne(x => x.LoginGroup); } else { modelBuilder.Ignore <Group>(); } modelBuilder.Entity <Login>().HasData(new Login { LoginId = ConstStore.AdminLoginIdGuid, GroupId = ConstStore.AdminGroupIdGuid, UserId = ConstStore.AdminUserIdGuid, LogIn = ConstStore.AdminLogIn, Certificate = CertificateWorker.CreateCertificate(), Password = Hasher.Hash(ConstStore.AdminSecure) }); }