public void AddUserAccount(UserSignUpView user) { using (DemoDBEntities db = new DemoDBEntities()) { SYSUSER SU = new SYSUSER(); SU.LoginName = user.LoginName; SU.PasswordEncryptedText = user.Password; SU.RowCreatedSYSUserID = user.SYSUserID > 0 ? user.SYSUserID : 1; SU.RowModifiedSYSUserID = user.SYSUserID > 0 ? user.SYSUserID : 1; SU.RowCreatedDateTime = DateTime.Now; SU.RowModifiedDateTime = DateTime.Now; db.SYSUSER.Add(SU); db.SaveChanges(); SYSUserProfile SUP = new SYSUserProfile(); SUP.SYSUserID = SU.SYSUserID; SUP.FirstName = user.FirstName; SUP.LastName = user.LastName; SUP.Gender = user.Gender; SUP.RowCreatedSYSUserID = user.SYSUserID > 0 ? user.SYSUserID : 1; SUP.RowModifiedSYSUserID = user.SYSUserID > 0 ? user.SYSUserID : 1; SUP.RowCreatedDateTime = DateTime.Now; SUP.RowModifiedDateTime = DateTime.Now; db.SYSUserProfile.Add(SUP); db.SaveChanges(); if (user.LOOKUPRoleID > 0) { SYSUserRole SUR = new SYSUserRole(); SUR.LOOKUPRoleID = user.LOOKUPRoleID; SUR.SYSUserID = user.SYSUserID; SUR.IsActive = true; SUR.RowCreatedSYSUserID = user.SYSUserID > 0 ? user.SYSUserID : 1; SUR.RowModifiedSYSUserID = user.SYSUserID > 0 ? user.SYSUserID : 1; SUR.RowCreatedDateTime = DateTime.Now; SUR.RowModifiedDateTime = DateTime.Now; db.SYSUserRole.Add(SUR); db.SaveChanges(); } } }
/// <summary> /// 根据UserId获取行政区划树,行政区划树中的UseIn标识了该用户是否位于该行政区划 /// </summary> /// <param name="userId"></param> /// <returns></returns> public static List <District> GetDistrictTree(string userId) { using (var db = SystemUtils.NewEFDbContext) { var dpts = db.DISTRICT.Where(d => d.PARENTID == null).ToList(); SYSUSER user = null; List <DISTRICT> userDists = null; if (!string.IsNullOrEmpty(userId)) { user = db.SYSUSER.Where(u => u.USERID == userId).FirstOrDefault(); if (user != null) { userDists = user.DISTRICTs; } } return(GetDistrictTree(dpts, userDists)); } }
public bool IsUserInRole(string loginName, string roleName) { using (DemoDBEntities db = new DemoDBEntities()) { SYSUSER SU = db.SYSUSER.FirstOrDefault(o => o.LoginName.ToLower().Equals(loginName)); if (SU != null) { var roles = from q in db.SYSUserRole join r in db.LOOKUPRole on q.LOOKUPRoleID equals r.LOOKUPRoleID where r.RoleName.Equals(roleName) && q.SYSUserID.Equals(SU.SYSUserID) select r.RoleName; if (roles != null) { return(roles.Any()); } } return(false); } }
public void UpdateUserAccount(UserProfileView user) { using (DemoDBEntities db = new DemoDBEntities()) { using (var dbContextTransaction = db.Database.BeginTransaction()) { try { SYSUSER SU = db.SYSUSER.Find(user.SYSUserID); SU.LoginName = user.LoginName; SU.PasswordEncryptedText = user.Password; SU.RowCreatedSYSUserID = user.SYSUserID; SU.RowModifiedSYSUserID = user.SYSUserID; SU.RowCreatedDateTime = DateTime.Now; SU.RowModifiedDateTime = DateTime.Now; db.SaveChanges(); var userProfile = db.SYSUserProfile.Where(o => o.SYSUserID == user.SYSUserID); if (userProfile.Any()) { SYSUserProfile SUP = userProfile.FirstOrDefault(); SUP.SYSUserID = SU.SYSUserID; SUP.FirstName = user.FirstName; SUP.LastName = user.LastName; SUP.Gender = user.Gender; SUP.RowCreatedSYSUserID = user.SYSUserID; SUP.RowModifiedSYSUserID = user.SYSUserID; SUP.RowCreatedDateTime = DateTime.Now; SUP.RowModifiedDateTime = DateTime.Now; db.SaveChanges(); } if (user.LOOKUPRoleID > 0) { var userRole = db.SYSUserRole.Where(o => o.SYSUserID == user.SYSUserID); SYSUserRole SUR = null; if (userRole.Any()) { SUR = userRole.FirstOrDefault(); SUR.LOOKUPRoleID = user.LOOKUPRoleID; SUR.SYSUserID = user.SYSUserID; SUR.IsActive = true; SUR.RowCreatedSYSUserID = user.SYSUserID; SUR.RowModifiedSYSUserID = user.SYSUserID; SUR.RowCreatedDateTime = DateTime.Now; SUR.RowModifiedDateTime = DateTime.Now; } else { SUR = new SYSUserRole(); SUR.LOOKUPRoleID = user.LOOKUPRoleID; SUR.SYSUserID = user.SYSUserID; SUR.IsActive = true; SUR.RowCreatedSYSUserID = user.SYSUserID; SUR.RowModifiedSYSUserID = user.SYSUserID; SUR.RowCreatedDateTime = DateTime.Now; SUR.RowModifiedDateTime = DateTime.Now; db.SYSUserRole.Add(SUR); } db.SaveChanges(); } dbContextTransaction.Commit(); } catch { dbContextTransaction.Rollback(); } } } }