public bool DeleteUser(string userId) { try { if (string.IsNullOrEmpty(userId)) { return(false); } using (var db = new BCBackContext()) { var temp = db.BackUsers.FirstOrDefault(obj => obj.UserID.Equals(userId)); IQueryable <ML.BC.BCBackData.Model.UserRole> userrolelist; if (null != temp) { userrolelist = db.UserRoles.Where(x => x.UserID == temp.UserID); } // db.BackUsers.Attach(temp); db.BackUsers.Remove(temp); return(0 < db.SaveChanges()); } } catch (Exception e) { throw e; } }
public string AddRoleFunction(int roleID, string functionID) { try { if (string.IsNullOrEmpty(functionID)) { throw new KnownException("参数无效!"); } using (var db = new BCBackContext()) { db.RFAAuthorizations.Add(new RFAAuthorization() { RoleID = roleID, FunctionID = functionID, UpdateTime = DateTime.Now }); if (1 == db.SaveChanges()) { return(functionID); } else { throw new KnownException(); } } } catch (Exception ex) { throw ex; } }
public bool UpdateUser(Account.Dtos.UserDto user) { try { using (var db = new BCBackContext()) { var temp = db.BackUsers.First(x => x.UserID == user.UserID); if (null == temp) { throw new KnownException("该对象不存在"); } temp.Closed = user.Closed; temp.Mobile = user.Mobile; if (!string.IsNullOrEmpty(user.Password)) { temp.Password = CryptoService.MD5Encrypt(user.Password); } temp.Name = user.Name; temp.UpdateTime = DateTime.Now; return(0 < db.SaveChanges()); } } catch (Exception e) { throw e; } }
public bool DeleteRole(int roleId) { try { if (0 > roleId) { throw new KnownException(); } using (var db = new BCBackContext()) { var fun = db.RFAAuthorizations.Where(x => x.RoleID == roleId && !x.Deleted); var role = db.UserRoles.FirstOrDefault(x => x.RoleID == roleId && !x.Deleted); if (role != null) { throw new KnownException("该角色被占用,无法删除"); } else { foreach (var f in fun) { f.Deleted = true; } role.Deleted = true; } //db.RFARoles.Attach(del); //db.RFARoles.Remove(del); return(0 < db.SaveChanges()); } } catch (Exception ex) { throw ex; } }
public bool DeleteRoleFunction(int roleID, string functionID) { try { if (string.IsNullOrEmpty(functionID)) { throw new KnownException("参数无效!"); } using (var db = new BCBackContext()) { RFAAuthorization au = new RFAAuthorization { RoleID = roleID, FunctionID = functionID }; db.RFAAuthorizations.Attach(au); db.RFAAuthorizations.Remove(au); if (1 == db.SaveChanges()) { return(true); } else { return(false); } } } catch (Exception ex) { throw ex; } }
public List <int> GetAllRolesByUserId(string userId) { try { if (string.IsNullOrEmpty(userId)) { throw new KnownException("用户id输入有误"); } List <int> list = new List <int>(); if (string.IsNullOrEmpty(userId)) { return(null); } using (var db = new BCBackContext()) { list = db.UserRoles.Where(obj => obj.UserID == userId && !obj.Deleted).Select(obj => obj.RoleID).ToList(); list = db.RFARoles.Where(obj => list.Contains(obj.RoleID) && obj.Available).Select(o => o.RoleID).ToList(); } return(list); } catch (Exception ex) { throw ex; } }
public bool DeleteUserRole(string userID, int roleID) { try { if (string.IsNullOrEmpty(userID)) { throw new KnownException("传递参数有误!"); } using (var db = new BCBackContext()) { var del = new UserRole() { UserID = userID, RoleID = roleID }; db.UserRoles.Attach(del); db.UserRoles.Remove(del); if (1 == db.SaveChanges()) { return(true); } else { return(false); } } } catch (Exception ex) { throw ex; } }
public string CreateUser(Account.Dtos.UserDto userDto) { using (var db = new BCBackContext()) { if (string.IsNullOrEmpty(userDto.Password)) { throw new KnownException("密码不允许为空"); } var uid = Ioc.GetService <ML.BC.Services.Common.IUniqeIdGenerator>().GeneratorBackUserID(); var user = new BackUser { UserID = uid, Name = userDto.Name, Password = CryptoService.MD5Encrypt(userDto.Password), Mobile = userDto.Mobile, RegistDate = DateTime.Now, Closed = userDto.Closed, UpdateTime = DateTime.Now }; db.BackUsers.Add(user); if (db.SaveChanges() > 0) { return(user.UserID); } else { return(null); } } }
/// <summary> /// 生成后台用户ID /// </summary> /// <param name="length">字符串长度默认6</param> /// <returns></returns> public virtual string GeneratorBackUserID(uint length = 6) { try { if (string.IsNullOrEmpty(_backUserID)) { using (var db = new BCBackContext()) { _backUserID = db.BackUsers.Max(x => x.UserID); if (!IsAvailableId(_backUserID)) { throw new KnownException("数据库Max值Id不合法,请检查数据库!"); } } // 当数据库不存在任何ID时候,自动生成最小字符串 if (string.IsNullOrEmpty(_backUserID)) { length = length <= 6 ? 6 : length; _backUserID = GetMinString(length); } } lock (_backUserID) { _backUserID = GetNextStringID(_backUserID); } return(_backUserID); } catch (Exception ex) { throw ex; } }
public int AddRole(RolesDto role) { try { if (null == role) { throw new KnownException(); } if (!string.IsNullOrEmpty(role.FunctionIDs)) { using (var db = new BCBackContext()) { // 事务处理RoleFunction表添加成功和RFARole表添加成功需要同时满足。 using (TransactionScope transaction = new TransactionScope()) { db.RFARoles.Add(role); db.SaveChanges(); var roleId = db.RFARoles.Where(obj => obj.Name == role.Name).Select(obj => obj.RoleID).ToList(); if (0 >= roleId.First()) { throw new KnownException("保存角色失败!"); } // db.SaveChange()已在SetFunctin函数执行 var result = SetFunction(roleId.First(), role.FunctionIDs); if (null == result) { throw new KnownException("保存角色功能失败!"); } else { transaction.Complete(); return(role.RoleID); } } } } using (var db = new BCBackContext()) { db.RFARoles.Add(role); if (1 == db.SaveChanges()) { return(role.RoleID); } else { throw new KnownException("角色保存失败!"); } } } catch (Exception ex) { throw ex; } }
public bool CanLogin(string userId, string password) { using (var db = new BCBackContext()) { var user = db.BackUsers.FirstOrDefault(n => n.UserID == userId); var encedpass = CryptoService.MD5Encrypt(password); return(user != null && user.Closed == false && user.Password == encedpass); } }
public bool UpdateUserLogin(string userId, string lastIP) { using (var db = new BCBackContext()) { var user = db.BackUsers.First(n => n.UserID == userId); user.LastDate = DateTime.Now; user.LastIP = lastIP; user.UpdateTime = DateTime.Now; db.Entry <BackUser>(user).State = System.Data.Entity.EntityState.Modified; return(db.SaveChanges() > 0); } }
private List <AuthorizationsDto> SetFunction(int roleID, string functionIDs) { try { if (0 > roleID || string.IsNullOrEmpty(functionIDs)) { throw new KnownException("参数无效!"); } // 返回设置完成后的角色功能信息 var list = new List <AuthorizationsDto>(); var strS = functionIDs.Split('|'); var listFuc = strS.ToList(); using (var db = new BCBackContext()) { foreach (var funId in listFuc) { // 更新或者添加功能 var au = new RFAAuthorization() { RoleID = roleID, FunctionID = funId, UpdateTime = DateTime.Now }; db.RFAAuthorizations.AddOrUpdate(au); } // 删除不再具有的功能 var query = db.RFAAuthorizations.Where(obj => obj.RoleID == roleID).Select(obj => obj.FunctionID).ToList(); foreach (var f in query) { if (!listFuc.Contains(f)) { var del = db.RFAAuthorizations.FirstOrDefault(obj => obj.RoleID == roleID && obj.FunctionID == f); db.RFAAuthorizations.Remove(del); } } if (0 < db.SaveChanges()) { foreach (var fun in listFuc) { list.Add(db.RFAAuthorizations.FirstOrDefault(obj => obj.RoleID == roleID && obj.FunctionID == fun)); } } } return(list); } catch (Exception ex) { throw ex; } }
public int GetAmount() { try { using (var db = new BCBackContext()) { return(db.RFAFunctions.Count()); } } catch (Exception e) { throw e; } }
public bool ExistFunction(string funcId) { try { using (var db = new BCBackContext()) { return(db.RFAFunctions.Where(obj => obj.FunctionID.Equals(funcId)).Count() == 1); } } catch (Exception e) { throw e; } }
public int GetAmount(System.Linq.Expressions.Expression <Func <BackUser, bool> > filter) { try { using (var db = new BCBackContext()) { return(db.BackUsers.Count(filter)); } } catch (Exception e) { throw e; } }
public List <Account.Dtos.UserDto> GetList(int pageNumber, int pageSize, out int amount) { try { using (var db = new BCBackContext()) { var list = db.BackUsers.Select(obj => new Account.Dtos.UserDto { UserID = obj.UserID, Name = obj.Name, Mobile = obj.Mobile, LastIP = obj.LastIP, LastDate = obj.LastDate, Password = obj.Password, UpdateTime = obj.UpdateTime, Closed = obj.Closed, RegistDate = obj.RegistDate }); int pagecount; amount = list.Count(); if (pageSize > 0) { // 获取总共页数 pagecount = (list.Count() + pageSize - 1) / pageSize; } else { pagecount = 0; } //页码判断,小于1则为1,大于最大页码则为最大页码 if (pageNumber > pagecount) { pageNumber = pagecount; } if (pageNumber < 1) { pageNumber = 1; } return(list.OrderBy(x => x.UserID).Skip(pageSize * (pageNumber - 1)).Take(pageSize).ToList()); } } catch (Exception e) { throw e; } }
//public int Delete(string FunctionID) //{ // using (var db = new BCBackContext()) // { // RFAFunction temp = db.RFAFunctions.FirstOrDefault(x => x.FunctionID == FunctionID); // if (null == temp) // return 0; // db.RFAFunctions.Remove(temp); // Action<string> deleteChildren = null; // deleteChildren = (parentId) => // { // List<RFAFunction> list = db.RFAFunctions.Where(x => x.ParentID == FunctionID).ToList<RFAFunction>(); // for (int i = 0; i < list.Count; i++) // { // var tempFunction = list[i]; // db.RFAFunctions.Remove(tempFunction); // deleteChildren(tempFunction.FunctionID); // } // }; // deleteChildren(temp.FunctionID); // return db.SaveChanges(); // } //} public int Add(FunctionDto obj) { using (var db = new BCBackContext()) { RFAFunction temp = new RFAFunction(); temp.FunctionID = obj.FunctionID; temp.MyID = obj.MyID; temp.Name = obj.Name; temp.ParentID = obj.ParentID; temp.Available = obj.Available; temp.Desription = obj.Desription; db.RFAFunctions.Add(temp); // db.Entry<RFAFunction>(temp).State = EntityState.Added; return(db.SaveChanges()); } }
//public List<RFAFunction> Select(Expression<Func<RFAFunction, bool>> foo) //{ // using (var db = new BCBackContext()) // { // List<RFAFunction> list = new List<RFAFunction>(); // list = db.RFAFunctions.Where(foo).ToList<RFAFunction>(); // return list; // } //} public int Update(FunctionDto obj) { using (var db = new BCBackContext()) { RFAFunction temp = db.RFAFunctions.Where(objx => objx.FunctionID.Equals(obj.FunctionID)).First(); if (temp == null) { return(0); } //temp.FunctionID = obj.FunctionID; temp.MyID = obj.MyID; temp.Name = obj.Name; //temp.ParentID = obj.ParentID; temp.Available = obj.Available; temp.Desription = obj.Desription; return(db.SaveChanges()); } }
public Account.Dtos.UserDto Get(string username) { using (var db = new BCBackContext()) { var user = db.BackUsers.Where(n => n.Name == username) .Select(n => new Account.Dtos.UserDto { UserID = n.UserID, Name = n.Name, Mobile = n.Mobile, RegistDate = n.RegistDate, LastDate = n.LastDate, LastIP = n.LastIP, Closed = n.Closed, UpdateTime = n.UpdateTime, }).FirstOrDefault(); return(user); } }
public bool UpdateRole(RolesDto role) { try { if (null == role || 0 > role.RoleID) { throw new KnownException("关键信息缺失,无法更新!"); } using (var db = new BCBackContext()) { using (TransactionScope transaction = new TransactionScope()) { var temp = db.RFARoles.FirstOrDefault(obj => obj.RoleID == role.RoleID); if (null == temp) { throw new KnownException("不存在该记录,无法更新"); } temp.Name = role.Name; temp.OwnerID = role.OwnerID; temp.Description = role.Description; temp.Available = role.Available; db.SaveChanges(); var auths = SetFunction(role.RoleID, role.FunctionIDs); if (auths == null || auths.Count <= 0) { throw new KnownException("角色功能信息更新失败!"); } transaction.Complete(); return(true); } } } catch (Exception ex) { throw ex; } }
public List <string> GetAllFunctionIDByRoleID(int roleID) { try { if (0 > roleID) { throw new KnownException("参数无效!"); } var list = new List <string>(); using (var db = new BCBackContext()) { list = db.RFAAuthorizations.Where(obj => obj.RoleID == roleID).Select(obj => obj.FunctionID).ToList(); return(list); } } catch (Exception ex) { throw ex; } }
public List <RolesDto> GetAllRoles() { try { using (var db = new BCBackContext()) { var list = db.RFARoles.Where(obj => null != obj && obj.Available).Select(obj => new RolesDto { RoleID = obj.RoleID, Name = obj.Name, Available = obj.Available, OwnerID = obj.OwnerID }).ToList(); return(list); } } catch (Exception ex) { throw ex; } }
public string AddUserRole(string userID, int roleID) { try { if (string.IsNullOrEmpty(userID)) { throw new KnownException("传递参数有误!"); } using (var db = new BCBackContext()) { if (!db.BackUsers.Any(obj => obj.UserID == userID)) { throw new KnownException("不存在的用户ID无法添加用户角色!"); } if (!db.RFARoles.Any(obj => obj.RoleID == roleID)) { throw new KnownException("不存在的角色ID无法添加用户角色!"); } db.UserRoles.Add(new UserRole() { UserID = userID, RoleID = roleID, UpdateTime = DateTime.Now }); var re = db.SaveChanges(); if (1 == re) { return(userID); } else { throw new KnownException(); } } } catch (Exception ex) { throw ex; } }
public bool Delete(string FunctionID) { using (var db = new BCBackContext()) { return(0 < DeleteFunctionsons(db, FunctionID)); //RFAFunction temp = db.RFAFunctions.FirstOrDefault(x => x.FunctionID == FunctionID); //if (null == temp) // return 0; //db.RFAFunctions.Remove(temp); //List<RFAFunction> list = db.RFAFunctions.Where(x => x.ParentID == FunctionID).ToList<RFAFunction>(); //for (int i = 0; i < list.Count; i++) //{ // if (0 == Delete(list[i].FunctionID)) // break; //} // return db.SaveChanges(); } }
public bool UpdateRoleFunction(int roleID, string functionID, bool available) { if (string.IsNullOrEmpty(functionID)) { throw new KnownException("更新信息不全!"); } using (var db = new BCBackContext()) { var q = db.RFAAuthorizations.FirstOrDefault(obj => obj.RoleID == roleID && obj.FunctionID == functionID); if (null == q) { throw new KnownException("该记录不存在!"); } db.RFAAuthorizations.AddOrUpdate(new RFAAuthorization() { RoleID = roleID, FunctionID = functionID, UpdateTime = DateTime.Now }); return(1 == db.SaveChanges()); } }
private int DeleteFunctionsons(BCBackContext db, string FunctionID) { RFAFunction temp = db.RFAFunctions.FirstOrDefault(x => x.FunctionID.Equals(FunctionID)); if (null == temp) { return(0); } db.RFAFunctions.Remove(temp); List <RFAFunction> list = db.RFAFunctions.Where(x => x.ParentID.Equals(FunctionID)).ToList <RFAFunction>(); for (int i = 0; i < list.Count; i++) { if (0 == DeleteFunctionsons(db, list[i].FunctionID)) { break; } } return(db.SaveChanges()); }
public List <FunctionDto> GetAllFunctions() { try { using (var db = new BCBackContext()) { var list = db.RFAFunctions.Select(n => new FunctionDto { FunctionID = n.FunctionID, MyID = n.MyID, ParentID = n.ParentID, Name = n.Name, Desription = n.Desription, Available = n.Available }).ToList(); return(list); } } catch (Exception e) { throw e; } }
public List <RolesDto> GetRoleList(RolesDto model, int pageSize, int pageIndex, out int count) { try { RolesDto m; // 为空时,查询所有结果 if (null == model) { m = new RolesDto() { RoleID = 0, Name = "", Description = "" }; model = m; } using (var db = new BCBackContext()) { var query = db.RFARoles.Where(r => (model.RoleID > 0 ? r.RoleID == model.RoleID : true) && (!string.IsNullOrEmpty(model.Name) ? r.Name.Contains(model.Name) : true) && (!string.IsNullOrEmpty(model.Description) ? r.Description.Contains(model.Description) : true)); count = query.Count(); int pageTotal; if (pageSize > 0) { pageTotal = (count + pageSize - 1) / pageSize; } else { pageSize = 10; pageTotal = (count + pageSize - 1) / pageSize; } if (pageIndex > pageTotal) { pageIndex = pageTotal; } if (pageIndex < 1) { pageIndex = 1; } var list = query .OrderBy(obj => obj.RoleID) .Skip(pageSize * (pageIndex - 1)) .Take(pageSize) .Select(obj => new RolesDto() { RoleID = obj.RoleID, Name = obj.Name, OwnerID = obj.OwnerID, Description = obj.Description, Available = obj.Available }).ToList(); return(list); #region 注释无用代码 // 转为List<RoleDto>并返回 //int len = list.Count(); //List<RolesDto> listDto = new List<RolesDto>(); // // //for (int i = 0; i < len; i++) //{ // // listDto[i] = list[i]; //} //return listDto; //List<RolesDto> listDto = new List<RolesDto>(); //foreach (var rfaRole in list) //{ // listDto[outCount] = list[outCount]; // outCount++; //} // //count = outCount; //return listDto; #endregion } } catch (Exception ex) { throw ex; } }
public List <FunctionDto> GetAllFunctions(string nameKeyword, int psize, int pageidx) { try { using (var db = new BCBackContext()) { if (psize <= 0 || pageidx <= 0) { return(new List <FunctionDto>()); } IEnumerable <RFAFunction> tResult; if (nameKeyword == null || nameKeyword.Equals("")) { tResult = db.RFAFunctions; } else { tResult = db.RFAFunctions.Where(obj => obj.Name.Contains(nameKeyword)); } //tResult.c int count = tResult.Count(); int pageCount = (count + psize - 1) / psize; if (pageCount < pageidx) { //if pageidx is overflow return the last page return(tResult.OrderBy(x => x.FunctionID).Skip((pageCount - 1) * psize).Select(n => new FunctionDto { FunctionID = n.FunctionID, MyID = n.MyID, ParentID = n.ParentID, Name = n.Name, Desription = n.Desription, Available = n.Available }).ToList());; } else { //return tResult. return(tResult.OrderBy(x => x.FunctionID).Skip((pageidx - 1) * psize).Take(psize).Select(n => new FunctionDto { FunctionID = n.FunctionID, MyID = n.MyID, ParentID = n.ParentID, Name = n.Name, Desription = n.Desription, Available = n.Available }).ToList());; } //var list = db.RFAFunctions.Where(lambda).AsEnumerable().Select(n => new FunctionDto //{ // FunctionID = n.FunctionID, // MyID = n.MyID, // ParentID = n.ParentID, // Name = n.Name, // Desription = n.Desription, // Available = n.Available //}).ToList(); } } catch (Exception e) { throw e; } }