public bool HasPermission(long adminUserId, string permissionName) { //using (ZSZContext zsz = new ZSZContext()) //{ // BaseService<AdminUserEntity> bsAdmin = new BaseService<AdminUserEntity>(zsz); // var admin = bsAdmin.GetById(adminUserId); // if (admin == null) // { // throw new Exception("没有id为" + adminUserId + "的用户"); // } // var permissions = admin.Roles.Select(m=>m.Permissions).ToList(); //} using (ZSZContext ctx = new ZSZContext()) { BaseService <AdminUserEntity> bs = new BaseService <AdminUserEntity>(ctx); var user = bs.GetAll().Include(u => u.Roles) .AsNoTracking().SingleOrDefault(u => u.Id == adminUserId); //var user = bs.GetById(adminUserId); if (user == null) { throw new ArgumentException("找不到id=" + adminUserId + "的用户"); } //每个Role都有一个Permissions属性 //Roles.SelectMany(r => r.Permissions)就是遍历Roles的每一个Role //然后把每个Role的Permissions放到一个集合中 //IEnumerable<PermissionEntity> return(user.Roles.SelectMany(r => r.Permissions) .Any(p => p.Name == permissionName)); } }
public RoleDTO GetById(long id) { using (ZSZContext context = new ZSZContext()) { BaseService <RoleEntity> roleService = new BaseService <RoleEntity>(context); return(ToDTO(roleService.GetById(id))); } }
public RoleDTO[] GetAllRoles() { using (ZSZContext context = new ZSZContext()) { BaseService <RoleEntity> roleService = new BaseService <RoleEntity>(context); return(roleService.GetAll().AsNoTracking().ToList().Select(m => ToDTO(m)).ToArray()); } }
public bool DeleRole(long id) { using (ZSZContext context = new ZSZContext()) { BaseService <RoleEntity> roleService = new BaseService <RoleEntity>(context); return(roleService.MarkDeleted(id)); } }
public bool DeleteAdmin(long id) { using (ZSZContext context = new ZSZContext()) { BaseService <AdminUserEntity> adbs = new BaseService <AdminUserEntity>(context); return(adbs.MarkDeleted(id)); } }
public RoleDTO[] GetByAdminUserId(long adminUserId) { using (ZSZContext context = new ZSZContext()) { BaseService <AdminUserEntity> bs = new BaseService <AdminUserEntity>(context); var admin = bs.GetById(adminUserId); if (admin == null) { throw new Exception("不存在id为" + adminUserId + "的用户"); } return(admin.Roles.Select(m => ToDTO(m)).ToArray()); } }
public long Add(string name) { using (ZSZContext context = new ZSZContext()) { RoleEntity r = new RoleEntity() { Name = name }; context.Roles.Add(r); context.SaveChanges(); return(r.Id); } }
public AdminUserDTO GetAdminInfo(long adminId) { using (ZSZContext context = new ZSZContext()) { BaseService <AdminUserEntity> adbs = new BaseService <AdminUserEntity>(context); var admin = adbs.GetById(adminId); if (admin == null) { return(null); } return(ToDTO(admin)); } }
public long GetTotalCount(string keyWords) { using (ZSZContext zsz = new ZSZContext()) { BaseService <AdminUserEntity> bsAdmin = new BaseService <AdminUserEntity>(zsz); if (!string.IsNullOrEmpty(keyWords)) { var list = bsAdmin.GetAll().Where(e => e.Name.Contains(keyWords) || e.PhoneNum.Contains(keyWords)); return(list.LongCount()); } return(bsAdmin.GetTotalCount()); } }
public AdminUserDTO[] GetAllAdmin(int pageSize, int currentIndex, string keyWords) { using (ZSZContext context = new ZSZContext()) { BaseService <AdminUserEntity> adbs = new BaseService <AdminUserEntity>(context); var items = adbs.GetAll(); if (!string.IsNullOrEmpty(keyWords)) { items = items.Where(e => e.Name.Contains(keyWords) || e.PhoneNum.Contains(keyWords)); } return(items.AsNoTracking().OrderByDescending(m => m.CreateDateTime).Skip(currentIndex).Take(pageSize).ToList().Select(u => ToDTO(u)).ToArray()); } }
public bool CheckLogin(string phoneNum, string password) { using (ZSZContext zsx = new ZSZContext()) { BaseService <AdminUserEntity> bs = new BaseService <AdminUserEntity>(zsx); var admin = bs.GetAll().SingleOrDefault(m => m.PhoneNum == phoneNum); if (admin == null) { return(false); } var salt = admin.PasswordSalt; var pwd = CommonHelper.CalcMD5(password + salt);//md5(盐+密码) return(admin.PasswordHash == pwd); } }
public bool CheckLogin(string tele, string pwd) { using (ZSZContext db = new ZSZContext()) { BaseService <AdminUserEntity> admin = new BaseService <AdminUserEntity>(db); var adminEnty = admin.GetAll().SingleOrDefault(m => m.PhoneNum == tele); if (adminEnty == null) { return(false); } string salt = adminEnty.PasswordSalt; //密码盐 //将用户穿过来的密码和盐在一块加密,然后跟数据库的密码做对比,如果一致,就说明登录成功 var pwdJM = CommonHelper.CalcMD5(pwd + salt); //md5(密码+盐) return(pwdJM == adminEnty.PasswordHash); } }
public AdminUserDTO GetById(long id) { using (ZSZContext ctx = new ZSZContext()) { BaseService <AdminUserEntity> bs = new BaseService <AdminUserEntity>(ctx); //这里不能用bs.GetById(id);因为无法Include、AsNoTracking()等 var user = bs.GetAll() .AsNoTracking().SingleOrDefault(u => u.Id == id); //.AsNoTracking().Where(u=>u.Id==id).SingleOrDefault(); //var user = bs.GetById(id); 用include就不能用GetById if (user == null) { return(null); } return(ToDTO(user)); } }
public long AddAdminUser(string name, string phoneNum, string password) { AdminUserEntity adminEf = new AdminUserEntity(); adminEf.Name = name; adminEf.PhoneNum = phoneNum; string salt = CommonHelper.CreateVerifyCode(5);//盐 adminEf.PasswordSalt = salt; //Md5(盐+用户密码) string pwdHash = CommonHelper.CalcMD5(password + salt);//处理后的密码 adminEf.PasswordHash = pwdHash; using (ZSZContext context = new ZSZContext()) { context.AdminUsers.Add(adminEf); context.SaveChanges(); return(adminEf.Id); } }
public void UpdateAdminUser(long id, string name, string phoneNum, string password) { using (ZSZContext ct = new ZSZContext()) { BaseService <AdminUserEntity> bs = new BaseService <AdminUserEntity>(ct); //先查询出来,再做更新 var admin = bs.GetById(id); if (admin == null) { throw new Exception("不存在id为" + id + "的管理员"); } admin.Name = name; admin.PhoneNum = phoneNum; if (!string.IsNullOrEmpty(password)) { admin.PasswordHash = CommonHelper.CalcMD5(password + admin.PasswordSalt); } ct.SaveChanges(); } }
public void AddRoleIds(long adminUserId, long[] roleIds) { //1:通过adminUSerID获取管理员信息 using (ZSZContext context = new ZSZContext()) { BaseService <AdminUserEntity> adminService = new BaseService <AdminUserEntity>(context); var adminModel = adminService.GetById(adminUserId); if (adminModel == null) { throw new Exception("该用户不存在!!"); } //2:添加对应的角色信息 BaseService <RoleEntity> roleService = new BaseService <RoleEntity>(context); var roles = roleService.GetAll().Where(m => roleIds.Contains(m.Id)).ToArray();//获取选中的角色信息 for (int i = 0; i < roles.Length; i++) { adminModel.Roles.Add(roles[i]); } context.SaveChanges(); } }
public AdminUserDTO GetAdminByPhone(string phone) { using (ZSZContext zsz = new ZSZContext()) { BaseService <AdminUserEntity> bsAdmin = new BaseService <AdminUserEntity>(zsz); //bsAdmin.GetAll().AsNoTracking().Where(m => m.PhoneNum == phone).ToList().Select(m => ToDTO(m)); var users = bsAdmin.GetAll().AsNoTracking().Where(m => m.PhoneNum == phone); int count = users.Count(); if (count <= 0) { return(null); } else if (count == 1) { return(ToDTO(users.Single())); } else { throw new ApplicationException("找到多个手机号为" + phone + "的管理员"); } } }
public AdminUserDTO IsExitTelePhone(string telPhone) { using (ZSZContext ctx = new ZSZContext()) { BaseService <AdminUserEntity> bs = new BaseService <AdminUserEntity>(ctx); var users = bs.GetAll() .AsNoTracking().Where(u => u.PhoneNum == telPhone); int count = users.Count(); if (count <= 0) { return(null); } else if (count == 1) { return(ToDTO(users.Single())); } else { throw new ApplicationException("找到多个手机号为" + telPhone + "的管理员"); } } }
public CommonService(ZSZContext zszModel) { this.zsz = zszModel; }
public BaseService(ZSZContext zsz) { this.zsz = zsz; }