public void SetUserPhoto(int tenant, Guid id, byte[] photo) { using var tr = UserDbContext.Database.BeginTransaction(); var userPhoto = UserDbContext.Photos.FirstOrDefault(r => r.UserId == id && r.Tenant == tenant); if (photo != null && photo.Length != 0) { if (userPhoto == null) { userPhoto = new UserPhoto { Tenant = tenant, UserId = id, Photo = photo }; } else { userPhoto.Photo = photo; } UserDbContext.AddOrUpdate(r => r.Photos, userPhoto); } else if (userPhoto != null) { UserDbContext.Photos.Remove(userPhoto); } UserDbContext.SaveChanges(); tr.Commit(); }
public UserGroupRef SaveUserGroupRef(int tenant, UserGroupRef r) { if (r == null) { throw new ArgumentNullException("userGroupRef"); } r.LastModified = DateTime.UtcNow; r.Tenant = tenant; using var tr = UserDbContext.Database.BeginTransaction(); UserDbContext.AddOrUpdate(r => r.UserGroups, FromUserGroupRefToUserGroup(r)); var user = UserDbContext.Users.FirstOrDefault(a => a.Tenant == tenant && a.Id == r.UserId); if (user != null) { user.LastModified = r.LastModified; } UserDbContext.SaveChanges(); tr.Commit(); return(r); }
public void SetUserPassword(int tenant, Guid id, string password) { var h1 = !string.IsNullOrEmpty(password) ? Hasher.Base64Hash(password, HashAlg.SHA256) : null; var h2 = !string.IsNullOrEmpty(password) ? Crypto.GetV(password, 1, true) : null; var us = new UserSecurity { UserId = id, PwdHash = h1, PwdHashSha512 = h2 }; UserDbContext.AddOrUpdate(r => r.UserSecurity, us); UserDbContext.SaveChanges(); }
public void SetUserPasswordHash(int tenant, Guid id, string passwordHash) { var h1 = GetPasswordHash(id, passwordHash); var us = new UserSecurity { Tenant = tenant, UserId = id, PwdHash = h1, PwdHashSha512 = null,//todo: remove LastModified = DateTime.UtcNow }; UserDbContext.AddOrUpdate(r => r.UserSecurity, us); UserDbContext.SaveChanges(); }
public UserInfo SaveUser(int tenant, UserInfo user) { if (user == null) { throw new ArgumentNullException("user"); } if (string.IsNullOrEmpty(user.UserName)) { throw new ArgumentOutOfRangeException("Empty username."); } if (user.ID == default) { user.ID = Guid.NewGuid(); } if (user.CreateDate == default) { user.CreateDate = DateTime.UtcNow; } user.LastModified = DateTime.UtcNow; user.Tenant = tenant; user.UserName = user.UserName.Trim(); user.Email = user.Email.Trim(); using var tx = UserDbContext.Database.BeginTransaction(); var count = UserDbContext.Users.Where(r => r.UserName == user.UserName && r.Id != user.ID && !r.Removed).Count(); if (count != 0) { throw new ArgumentOutOfRangeException("Duplicate username."); } count = UserDbContext.Users.Where(r => r.Email == user.Email && r.Id != user.ID && !r.Removed).Count(); if (count != 0) { throw new ArgumentOutOfRangeException("Duplicate email."); } UserDbContext.AddOrUpdate(r => r.Users, FromUserInfoToUser(user)); UserDbContext.SaveChanges(); tx.Commit(); return(user); }
public Group SaveGroup(int tenant, Group group) { if (group == null) { throw new ArgumentNullException("user"); } if (group.Id == default) { group.Id = Guid.NewGuid(); } group.LastModified = DateTime.UtcNow; group.Tenant = tenant; var dbGroup = FromGroupToDbGroup(group); UserDbContext.AddOrUpdate(r => r.Groups, dbGroup); UserDbContext.SaveChanges(); return(group); }