/// <summary> /// 验证用户名和密码是否正确 /// </summary> /// <param name="userName">用户名</param> /// <param name="password">密码</param> /// <returns>登录成功后的用户信息</returns> public SysPerson ValidateUser(string userName, string password) { if (String.IsNullOrWhiteSpace(userName) || String.IsNullOrWhiteSpace(password)) return null; //获取用户信息,请确定web.config中的连接字符串正确 using (SysEntities db = new SysEntities()) { var person = (from p in db.SysPerson where p.Name == userName && p.Password == password && p.State == "开启" select p).FirstOrDefault(); if (person != null) {//登录成功 person.LastLogonTime = person.LogonTime; person.LastLogonIP = person.LogonIP; person.LogonNum++; person.LogonTime = DateTime.Now; person.LogonIP = Common.IP.GetIP(); db.SaveChanges(); return person; } } return null; }
/// <summary> /// 修改密码 /// </summary> /// <param name="personName">用户名</param> /// <param name="oldPassword">旧密码</param> /// <param name="newPassword">新密码</param> /// <returns>修改密码是否成功</returns> public bool ChangePassword(string personName, string oldPassword, string newPassword) { if (!string.IsNullOrWhiteSpace(personName) && !string.IsNullOrWhiteSpace(oldPassword) && !string.IsNullOrWhiteSpace(newPassword)) { try { using (SysEntities db = new SysEntities()) { var person = db.SysPerson.FirstOrDefault(p => (p.Name == personName) && (p.Password == oldPassword)); person.Password = newPassword; person.SurePassword = newPassword; db.SaveChanges(); return true; } } catch (Exception ex) { ExceptionsHander.WriteExceptions(ex); } } return false; }
/// <summary> /// 创建一个菜单 /// </summary> /// <param name="validationErrors">返回的错误信息</param> /// <param name="db">数据库上下文</param> /// <param name="entity">一个菜单</param> /// <returns></returns> public bool Create(ref ValidationErrors validationErrors, SysEntities db, SysMenu entity) { int count = 1; foreach (string item in entity.SysOperationId.GetIdSort()) { SysOperation sys = new SysOperation { Id = item }; db.SysOperation.Attach(sys); entity.SysOperation.Add(sys); count++; } repository.Create(db, entity); if (count == repository.Save(db)) { //创建后重置菜单编码 List<int> flags = new List<int>();//层级 GetMenus2(null, flags); db.SaveChanges(); return true; } else { validationErrors.Add("创建出错了"); } return false; }
/// <summary> /// 编辑一个菜单 /// </summary> /// <param name="validationErrors">返回的错误信息</param> /// <param name="db">数据上下文</param> /// <param name="entity">一个菜单</param> /// <returns>是否编辑成功</returns> public bool Edit(ref ValidationErrors validationErrors, SysEntities db, SysMenu entity) { /* * 不操作 原有 现有 * 增加 原没 现有 * 删除 原有 现没 */ if (entity == null) { return false; } int count = 1; SysMenu editEntity = repository.Edit(db, entity); List<string> addSysOperationId = new List<string>(); List<string> deleteSysOperationId = new List<string>(); DataOfDiffrent.GetDiffrent(entity.SysOperationId.GetIdSort(), entity.SysOperationIdOld.GetIdSort(), ref addSysOperationId, ref deleteSysOperationId); if (addSysOperationId != null && addSysOperationId.Count() > 0) { foreach (var item in addSysOperationId) { SysOperation sys = new SysOperation { Id = item }; db.SysOperation.Attach(sys); editEntity.SysOperation.Add(sys); count++; } } if (deleteSysOperationId != null && deleteSysOperationId.Count() > 0) { List<SysOperation> listEntity = new List<SysOperation>(); foreach (var item in deleteSysOperationId) { SysOperation sys = new SysOperation { Id = item }; listEntity.Add(sys); db.SysOperation.Attach(sys); } foreach (SysOperation item in listEntity) { editEntity.SysOperation.Remove(item);//查询数据库 count++; } } if (count == repository.Save(db)) { //修改后重置菜单编码 List<int> flags = new List<int>();//层级 GetMenus2(null, flags); db.SaveChanges(); return true; } else { validationErrors.Add("编辑菜单出错了"); } return false; }