Exemple #1
0
        public LoginInfo GetLoginInfo(Guid token)
        {
            return CacheHelper.Get<LoginInfo>(string.Format(_LoginInfoKeyFormat, token), () =>
            {
                using (var dbContext = new SysManageDbContext())
                {
                    //如果有超时的,启动超时处理
                    var timeoutList = dbContext.FindAll<LoginInfo>(p => EntityFunctions.DiffMinutes(DateTime.Now, p.LastAccessTime) > _UserLoginTimeoutMinutes);
                    if (timeoutList.Count > 0)
                    {
                        foreach (var li in timeoutList)
                            dbContext.LoginInfos.Remove(li);
                    }

                    dbContext.SaveChanges();


                    var loginInfo = dbContext.FindAll<LoginInfo>(l => l.LoginToken == token).FirstOrDefault();
                    if (loginInfo != null)
                    {
                        loginInfo.LastAccessTime = DateTime.Now;
                        dbContext.Update<LoginInfo>(loginInfo);
                    }

                    return loginInfo;
                }
            });
        }
Exemple #2
0
        public LoginInfo Login(string loginName, string password)
        {
            LoginInfo loginInfo = null;

            password = Encrypt.MD5(password);
            loginName = loginName.Trim();

            using (var dbContext = new SysManageDbContext())
            {
                var user = dbContext.Users.Include("Roles").FirstOrDefault(u => u.LoginName == loginName && u.Password == password && u.IsActive);
                if (user != null)
                {
                    var ip = Fetch.UserIp;
                    loginInfo = dbContext.FindAll<LoginInfo>(p => p.LoginName == loginName && p.ClientIP == ip).FirstOrDefault();
                    if (loginInfo != null)
                    {
                        loginInfo.LastAccessTime = DateTime.Now;
                    }
                    else
                    {
                        loginInfo = new LoginInfo(user.ID, user.LoginName);
                        loginInfo.ClientIP = ip;
                        loginInfo.BusinessPermissionList = user.BusinessPermissionList;
                        dbContext.Insert<LoginInfo>(loginInfo);
                    }
                }
            }

            return loginInfo;
        }
Exemple #3
0
        public void Logout(Guid token)
        {
            using (var dbContext = new SysManageDbContext())
            {
                var loginInfo = dbContext.FindAll<LoginInfo>(l => l.LoginToken == token).FirstOrDefault();
                if (loginInfo != null)
                {
                    dbContext.Delete<LoginInfo>(loginInfo);
                }
            }

            CacheHelper.Remove(string.Format(_LoginInfoKeyFormat, token));
        }
Exemple #4
0
        public void ModifyPwd(User user)
        {
            user.Password = Encrypt.MD5(user.Password);

            using (var dbContext = new SysManageDbContext())
            {
                if (dbContext.Users.Any(l => l.ID == user.ID && user.Password == l.Password))
                {
                    if (!string.IsNullOrEmpty(user.NewPassword))
                        user.Password = Encrypt.MD5(user.NewPassword);

                    dbContext.Update<User>(user);
                }
                else
                {
                    throw new BusinessException("Password", "原密码不正确!");
                }
            }
        }
Exemple #5
0
        public bool CheckVerifyCode(string verifyCodeText, Guid guid)
        {
            using (var dbContext = new SysManageDbContext())
            {
                var verifyCode = dbContext.FindAll<VerifyCode>(v => v.Guid == guid && v.VerifyText == verifyCodeText).LastOrDefault();
                if (verifyCode != null)
                {
                    dbContext.VerifyCodes.Remove(verifyCode);
                    dbContext.SaveChanges();

                    //清除验证码大于2分钟还没请求的
                    var expiredTime = DateTime.Now.AddMinutes(-2);
                    dbContext.VerifyCodes.Where(v => v.CreateTime < expiredTime).Delete();

                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
Exemple #6
0
        public Guid SaveVerifyCode(string verifyCodeText)
        {
            if (string.IsNullOrWhiteSpace(verifyCodeText))
                throw new BusinessException("VerifyCode", "输入的验证码不能为空!");

            using (var dbContext = new SysManageDbContext())
            {
                var verifyCode = new VerifyCode() { VerifyText = verifyCodeText, Guid = Guid.NewGuid() };
                dbContext.Insert<VerifyCode>(verifyCode);
                return verifyCode.Guid;
            }
        }
Exemple #7
0
 public void DeleteRole(List<int> ids)
 {
     using (var dbContext = new SysManageDbContext())
     {
         dbContext.Roles.Include("Users").Where(u => ids.Contains(u.ID)).ToList().ForEach(a => { a.Users.Clear(); dbContext.Roles.Remove(a); });
         dbContext.SaveChanges();
     }
 }
Exemple #8
0
 public void SaveRole(Role role)
 {
     using (var dbContext = new SysManageDbContext())
     {
         if (role.ID > 0)
         {
             dbContext.Update<Role>(role);
         }
         else
         {
             dbContext.Insert<Role>(role);
         }
     }
 }
Exemple #9
0
        public IEnumerable<Role> GetRoleList(RoleRequest request = null)
        {
            request = request ?? new RoleRequest();
            using (var dbContext = new SysManageDbContext())
            {
                IQueryable<Role> roles = dbContext.Roles;

                if (!string.IsNullOrEmpty(request.RoleName))
                {
                    roles = roles.Where(u => u.Name.Contains(request.RoleName));
                }

                return roles.OrderByDescending(u => u.ID).ToPagedList(request.PageIndex, request.PageSize);
            }
        }
Exemple #10
0
 public Role GetRole(int id)
 {
     using (var dbContext = new SysManageDbContext())
     {
         return dbContext.Find<Role>(id);
     }
 }
Exemple #11
0
        public void SaveUser(User user)
        {
            using (var dbContext = new SysManageDbContext())
            {
                if (user.ID > 0)
                {
                    dbContext.Update<User>(user);

                    var roles = dbContext.Roles.Where(r => user.RoleIds.Contains(r.ID)).ToList();
                    user.Roles = roles;
                    dbContext.SaveChanges();
                }
                else
                {
                    var existUser = dbContext.FindAll<User>(u => u.LoginName == user.LoginName);
                    if (existUser.Count > 0)
                    {
                        throw new BusinessException("UserName", "此登录名已存在!");
                    }
                    else
                    {
                        dbContext.Insert<User>(user);

                        var roles = dbContext.Roles.Where(r => user.RoleIds.Contains(r.ID)).ToList();
                        user.Roles = roles;
                        dbContext.SaveChanges();
                    }
                }
            }
        }
Exemple #12
0
        public IEnumerable<User> GetUserList(UserRequest request = null)
        {
            request = request ?? new UserRequest();

            using (var dbContext = new SysManageDbContext())
            {
                IQueryable<User> users = dbContext.Users.Include("Roles");

                if (!string.IsNullOrEmpty(request.LoginName))
                    users = users.Where(u => u.LoginName.Contains(request.LoginName));

                if (!string.IsNullOrEmpty(request.Mobile))
                    users = users.Where(u => u.Mobile.Contains(request.Mobile));

                return users.OrderByDescending(u => u.ID).ToPagedList(request.PageIndex, request.PageSize);
            }
        }
Exemple #13
0
 public User GetUser(int id)
 {
     using (var dbContext = new SysManageDbContext())
     {
         return dbContext.Users.Include("Roles").SingleOrDefault(u => u.ID == id);
     }
 }