public async Task <int> CreateAsync(Account model) { EntityExtension.FlagForCreate(model, IdentityService.Username, UserAgent); EntityExtension.FlagForCreate(model.AccountProfile, IdentityService.Username, UserAgent); foreach (var item in model.AccountRoles) { item.Role = null; EntityExtension.FlagForCreate(item, IdentityService.Username, UserAgent); } model.Password = SHA1Encrypt.Hash(model.Password); DbSet.Add(model); return(await DbContext.SaveChangesAsync()); }
public async Task <int> UpdateAsync(int id, Account model) { var data = await ReadByIdAsync(id); data.Username = model.Username; if (!string.IsNullOrEmpty(model.Password)) { data.Password = SHA1Encrypt.Hash(model.Password); } data.IsLocked = model.IsLocked; data.AccountProfile.Dob = model.AccountProfile.Dob; data.AccountProfile.Email = model.AccountProfile.Email; data.AccountProfile.Firstname = model.AccountProfile.Firstname; data.AccountProfile.Gender = model.AccountProfile.Gender; data.AccountProfile.Lastname = model.AccountProfile.Lastname; var updatedRoles = model.AccountRoles.Where(x => data.AccountRoles.Any(y => y.RoleId == x.RoleId)); var addedRoles = model.AccountRoles.Where(x => !data.AccountRoles.Any(y => y.RoleId == x.RoleId)); var deletedRoles = data.AccountRoles.Where(x => !model.AccountRoles.Any(y => y.RoleId == x.RoleId)); foreach (var item in updatedRoles) { var role = data.AccountRoles.SingleOrDefault(x => x.RoleId == item.RoleId); EntityExtension.FlagForUpdate(role, IdentityService.Username, UserAgent); } foreach (var item in addedRoles) { item.AccountId = id; item.Role = null; EntityExtension.FlagForCreate(item, IdentityService.Username, UserAgent); data.AccountRoles.Add(item); } foreach (var item in deletedRoles) { EntityExtension.FlagForDelete(item, IdentityService.Username, UserAgent, true); } EntityExtension.FlagForUpdate(data, IdentityService.Username, UserAgent); DbSet.Update(data); return(await DbContext.SaveChangesAsync()); }
public async Task <Account> Authenticate(string username, string password) { if (string.IsNullOrWhiteSpace(username)) { throw new Exception("Username is required"); } else if (string.IsNullOrWhiteSpace(password)) { throw new Exception("Password is required"); } else { var user = await DbSet .Include(x => x.AccountProfile) .Include(x => x.AccountRoles) .ThenInclude(i => i.Role) .ThenInclude(y => y.Permissions) .SingleOrDefaultAsync(d => d.Username.Equals(username) && d.Password.Equals(SHA1Encrypt.Hash(password), StringComparison.OrdinalIgnoreCase) && !d.IsDeleted); return(user); } }