Ejemplo n.º 1
0
        public ActionResult Edit(int id)
        {
            User user;

            using (IUnitOfWork unitOfWork = new TskDataDataContext(DbUtil.ConnectionString))
            {
                IUserRep userRep = new UserRep(unitOfWork);
                user = userRep.FindById(id);
                if (user != null)
                {
                    if (Brilliantech.Tsk.Manage.WebApp.Util.CustomMembershipProvider.CanEdit(user.Name))
                    {
                        ViewData["Role"] = new SelectList(UserRoleModel.UserRoleList(), "Key", "Name", user.Role);
                        return(View(user));
                    }
                    else
                    {
                        TempData["Message"] = "初始管理员,不可以编辑";
                        return(RedirectToAction("Index"));
                    }
                }
                else
                {
                    return(RedirectToAction("Index"));
                }
            }
        }
 public TopicsController(YeniBlogDbContext context)
 {
     _context        = context;
     topicRepository = new TopicRep(context);
     articleRep      = new ArticleRep(context);
     userRep         = new UserRep(context);
 }
        public void FromAuthenticationTicket(HttpCookie cookie)
        {
            FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(cookie.Value);

            if (authTicket == null)
            {
                return;
            }
            var userGid        = authTicket.UserData;
            var serializeModel = Cache.Get <SysUserModel>(userGid);

            if (serializeModel == null)
            {
                var user = UserRep.GetByGid(userGid).Result;
                if (user == null)
                {
                    Cache.Remove(userGid);
                    FormsAuthentication.SignOut();
                    return;
                }
                serializeModel = GetModel(user);

                Cache.Add(user.GId.ToString(), serializeModel, CacheTime);
            }

            var userPrincipal = new SysUser(serializeModel);

            HttpContext.Current.User = userPrincipal;
            Thread.CurrentPrincipal  = userPrincipal;
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Saas后台管理登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public async Task <(string Error, User User)> SaasLoginAsync(string account, string password, string ip = "")
        {
            User user = await UserRep.FindSingleAsync(o => o.Account == account);

            if (user == null)
            {
                return($"帐号不存在", null);
            }
            if (user.State == 0)
            {
                return($"帐号禁止登录", null);
            }
            if (user.Password != password)
            {
                return($"密码不正确", null);
            }
            CurrentUser curUser = new CurrentUser
            {
                Id             = user.Id,
                RealName       = user.Account,
                LoginIPAddress = ip
            };
            await OperateLogApp.CustomLogAsync(curUser, "用户登录", user.RealName + "进行了登录操作");

            await UserRep.UpdateAsync(o => o.Id == user.Id, o => new User()
            {
                LastLoginTime = DateTime.Now
            });

            return(string.Empty, user);
        }
Ejemplo n.º 5
0
 public static bool IsAdmin(string username)
 {
     using (IUnitOfWork unitOfWork = new TskDataDataContext(DbUtil.ConnectionString))
     {
         IUserRep userRep = new UserRep(unitOfWork);
         return(userRep.Find(username).Role == "admin" ? true : false);
     }
 }
Ejemplo n.º 6
0
 protected override byte[] GetItem(byte[] id)
 {
     using (var rep = new UserRep())
     {
         var conv = new ModelsForWpf.Converter <ModelsForWpf.User>();
         var us   = new User(conv.ByteArrayToObject(id));
         return(conv.ObjectToByteArray(Converter.ToUser(rep.GetItem(us.Id))));
     }
 }
Ejemplo n.º 7
0
 protected override async Task Delete(byte[] id)
 {
     using (var rep = new UserRep())
     {
         var conv = new ModelsForWpf.Converter <ModelsForWpf.User>();
         var us   = new User(conv.ByteArrayToObject(id));
         await rep.Delete(us.Id);
     }
 }
Ejemplo n.º 8
0
        protected override async Task Update(byte[] item)
        {
            using (var rep = new UserRep())
            {
                var conv = new ModelsForWpf.Converter <ModelsForWpf.User>();
                var us   = new User(conv.ByteArrayToObject(item));
                await rep.Update(us);

                await rep.Save();
            }
        }
Ejemplo n.º 9
0
 public async Task DeleteUserFromDBTest()
 {
     try
     {
         using (var ur = new UserRep(cs))
             await ur.Delete(5);
     }
     catch (Exception ex)
     {
         Assert.Fail(ex.Message);
     }
 }
Ejemplo n.º 10
0
        public ActionResult Index(int?page)
        {
            IPagedList <User> users = null;

            using (IUnitOfWork unitOfWork = new TskDataDataContext(DbUtil.ConnectionString))
            {
                int      currentPageIndex = page.HasValue ? (page.Value <= 0 ? 0 : page.Value - 1) : 0;
                IUserRep userRep          = new UserRep(unitOfWork);
                users = userRep.Queryable().ToPagedList(currentPageIndex, int.Parse(Resources.PageSize));
            }
            return(View(users));
        }
Ejemplo n.º 11
0
 public IActionResult OnPostRegister()
 {
     if (ModelState.IsValid)
     {
         UserRep.Adduser(newUser);
         return(RedirectToPage("/Paginas/Login"));
     }
     else
     {
         return(Page());
     }
 }
Ejemplo n.º 12
0
        public void LogOut(IPrincipal user)
        {
            var cookieName = GetCookieName();

            CookieUtils.RemoveCookie(cookieName);
            var sysUser = user as ISysUser;

            if (user != null)
            {
                UserRep.RemoveUserCache(sysUser.UserModel.GId);
            }
            FormsAuthentication.SignOut();
        }
Ejemplo n.º 13
0
 public List <User> GetItems()
 {
     using (var userRep = new UserRep())
     {
         var         l   = userRep.GetItems();
         List <User> ret = new List <User>();
         foreach (var u in l)
         {
             ret.Add(Converter.ToUser(u));
         }
         return(ret);
     }
 }
Ejemplo n.º 14
0
        /// <summary>
        /// 添加(判断Account是否已存在)
        /// </summary>
        /// <param name="entity"></param>
        /// <returns>-1: 失败 , 0:已存在 ,1:成功</returns>
        public async Task <(string Error, User user)> InsertAsync(User entity)
        {
            //已存在
            int count = await UserRep.GetCountAsync(o => o.Account == entity.Account);

            if (count > 0)
            {
                return("帐号已存在", null);
            }
            await UserRep.AddAsync(entity);

            return(string.Empty, null);
        }
Ejemplo n.º 15
0
 public void GetItemsUserFromDBTest()
 {
     try
     {
         List <User> c;
         using (var ur = new UserRep(cs))
             c = new List <User>(ur.GetItems());
         Assert.AreEqual(c.Count, 9);
     }
     catch (Exception ex)
     {
         Assert.Fail(ex.Message);
     }
 }
Ejemplo n.º 16
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="pageNumber"></param>
        /// <param name="rowsPrePage"></param>
        /// <param name="filter"></param>
        /// <returns></returns>
        public async Task <PageResult <UserSunpleDto> > GetPageAsync(int pageNumber, int rowsPrePage, UserOption filter)
        {
            List <UserSunpleDto>       data = new List <UserSunpleDto>();
            PageResult <UserSunpleDto> list = new PageResult <UserSunpleDto>();
            string orderby   = " id desc";
            var    predicate = PredicateBuilder.True <User>();

            predicate = predicate.And(o => o.DeleteMark == null);
            if (!string.IsNullOrWhiteSpace(filter.Account))
            {
                predicate = predicate.And(o => o.Account == filter.Account);
            }
            if (!string.IsNullOrWhiteSpace(filter.RealName))
            {
                predicate = predicate.And(o => o.RealName == filter.RealName);
            }
            var tlist = await UserRep.Find(pageNumber, rowsPrePage, orderby, predicate).ToListAsync() ?? new List <User>();

            data = MapperHelper.MapList <User, UserSunpleDto>(tlist);
            List <long> roleIds = tlist.Select(o => o.RoleId).Distinct().ToList();

            if (roleIds.Count() > 0)
            {
                var roles = await RoleRep.Find(o => roleIds.Contains(o.Id)).ToListAsync();

                foreach (var d in data)
                {
                    var r = roles.FirstOrDefault(o => o.Id == d.RoleId);
                    d.RoleName = r?.Name;
                }
            }
            List <long?> DepartmentIds = tlist.Select(o => o.DepartmentId).Distinct().ToList();

            DepartmentIds.Remove(null);
            if (DepartmentIds.Count() > 0)
            {
                var Departments = await DepartmentRep.Find(o => DepartmentIds.Contains(o.Id)).ToListAsync();

                foreach (var d in data)
                {
                    var r = Departments.FirstOrDefault(o => o.Id == d.DepartmentId);
                    d.deptname = r?.Name;
                }
            }
            list.Data = data.ToList();
            int total = await UserRep.GetCountAsync(predicate);

            list.ItemCount = total;
            return(list);
        }
Ejemplo n.º 17
0
        protected override byte[] GetItems()
        {
            using (var rep = new UserRep())
            {
                var conv = new ModelsForWpf.Converter <List <ModelsForWpf.User> >();
                var lst  = new List <ModelsForWpf.User>();
                foreach (var u in rep.GetItems())
                {
                    lst.Add(Converter.ToUser(u));
                }

                return(conv.ObjectToByteArray(lst));
            }
        }
Ejemplo n.º 18
0
        public async Task UpdateUserFromDBTest()
        {
            User test = new User(13, "qwq", "qqq", -1337154236, "123", false);

            try
            {
                using (var ur = new UserRep(cs))
                    await ur.Update(test);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
        }
Ejemplo n.º 19
0
        /// <summary>
        /// 删除(假删除)
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="agentId"></param>
        /// <param name="optId"></param>
        /// <returns>-1: 失败 , 0:不存在 ,1:成功</returns>
        public async Task <(string Error, User User)> DeleteAsync(long Id, long agentId, long optId)
        {
            User user = await UserRep.FindSingleAsync(o => o.Id == Id);

            if (user == null || user.DeleteMark == true)
            {
                return("帐号不存在", null);
            }
            user.DeleteMark   = true;
            user.DeleteTime   = DateTime.Now;
            user.DeleteUserId = optId;
            await UserRep.UpdateAsync(user);

            return(string.Empty, user);
        }
Ejemplo n.º 20
0
        public void CreateAuthenticationTicket(Users user, HttpResponseBase response, HttpContextBase httpContextBase, bool remember)
        {
            var serializeModel = GetModel(user);
            var userGroup      = user.UserGroup;

            UserRep.AddUserCache(serializeModel);
            var    expiration = remember ? DateTime.Now.AddYears(1) : DateTime.Now.AddMinutes(FormsAuthentication.Timeout.TotalMinutes);
            string userData   = $"{user.GId}|{UserRep.GetIP()}|{userGroup}";
            var    authTicket = new FormsAuthenticationTicket(
                1, user.GId.ToString(), DateTime.Now, expiration, remember, userData);
            string encTicket = FormsAuthentication.Encrypt(authTicket);

            CookieUtils.AddCookie(GetCookieName(), encTicket, expiration);
            setPrinciple(serializeModel, userGroup);
        }
Ejemplo n.º 21
0
        public async Task UserRepCreateTest()
        {
            User test = new User(0, "testUser", "TestLogin", 111111, "test", false);

            try
            {
                using (var ur = new UserRep(cs))
                {
                    await ur.Create(test);
                }
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
        }
Ejemplo n.º 22
0
        /// <summary>
        /// 重置密码
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="newPassword"></param>
        /// <param name="agentId"></param>
        /// <returns></returns>
        public async Task <R> ResetPasswordAsync(long Id, string password, long agentId, CurrentUser curUser)
        {
            User user = await UserRep.FindSingleAsync(o => o.Id == Id);

            if (user == null)
            {
                return(R.Err("1001", $"帐号({Id})不存在"));
            }
            password = MD5Encrypt.MD5(password);
            await UserRep.UpdateAsync(o => o.Id == Id, o => new User()
            {
                Password = password
            });

            return(R.Suc());
        }
Ejemplo n.º 23
0
 public bool ValidateUser(string username, string password = null)
 {
     using (IUnitOfWork unitOfWork = new TskDataDataContext(DbUtil.ConnectionString))
     {
         IUserRep userRep = new UserRep(unitOfWork);
         User     user    = userRep.Find(username, password);
         if (user == null)
         {
             return(false);
         }
         else
         {
             user.LastLoginTime = DateTime.Now;
             unitOfWork.Submit();
             return(true);
         }
     }
 }
Ejemplo n.º 24
0
        /// <summary>
        ///  根据账号模糊查询获取列表
        /// </summary>
        /// <param name="q"></param>
        /// <returns></returns>
        public async Task <List <IdAccountDto> > SelectDataAsync(string q)
        {
            if (string.IsNullOrWhiteSpace(q))
            {
                return(new List <IdAccountDto>());
            }

            var result = UserRep.Find(o => o.Account.Contains(q.Trim()));

            if (result == null || result.Count() == 0)
            {
                return(new List <IdAccountDto>());
            }
            return(result.Select(o => new IdAccountDto()
            {
                Id = o.Id, Account = o.Account
            }).ToList());
        }
Ejemplo n.º 25
0
        public void FromAuthenticationTicket(HttpCookie cookie)
        {
            FormsAuthenticationTicket authTicket = FormsAuthentication.Decrypt(cookie.Value);

            if (authTicket == null)
            {
                return;
            }
            var userData    = authTicket.UserData;
            var userDataArr = userData.Split('|');

            if (userDataArr.Length != 3 ||
                UserRep.GetIP() != userDataArr[1])
            {
                FormsAuthentication.SignOut();
                return;
            }
            if (FormsAuthentication.SlidingExpiration)
            {
                var expiration = DateTime.Now.AddMinutes(FormsAuthentication.Timeout.TotalMinutes);
                cookie.Expires  = expiration;
                cookie.HttpOnly = true;
                HttpContext.Current.Response.Cookies.Set(cookie);
            }
            var           userGid        = userDataArr[0];
            var           userGroup      = (UserGroup)Enum.Parse(typeof(UserGroup), userDataArr[2]);
            ISysUserModel serializeModel = UserRep.GetByCache(userGid);

            if (serializeModel == null)
            {
                var user = UserRep.GetByGid(userGid);
                if (user == null)
                {
                    UserRep.RemoveUserCache(userGid);
                    FormsAuthentication.SignOut();
                    return;
                }
                serializeModel = GetModel(user);
                UserRep.AddUserCache(serializeModel);
            }

            setPrinciple(serializeModel, userGroup);
        }
Ejemplo n.º 26
0
        public bool ChangePassword(string username, string oldPassword, string newPassword)
        {
            using (IUnitOfWork unitOfWork = new TskDataDataContext(DbUtil.ConnectionString))
            {
                IUserRep userRep = new UserRep(unitOfWork);
                User     user    = userRep.Find(username, oldPassword);
                if (user != null)
                {
                    user.Password = newPassword;

                    unitOfWork.Submit();
                    return(true);
                }
                else
                {
                    return(false);
                }
            }
        }
Ejemplo n.º 27
0
        /// <summary>
        /// 修改登录状态
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="agentId"></param>
        /// <param name="curUser"></param>
        /// <returns></returns>
        public async Task <R> updatestatus(long Id, CurrentUser curUser)
        {
            var entry = await GetAsync(Id);

            if (entry == null)
            {
                return(R.Err(msg: "该用户不存在"));
            }
            int s = 1;

            if (entry.State == 1)
            {
                s = 0;
            }
            await UserRep.UpdateAsync(u => u.Id == Id, u => new User {
                State = s
            });

            return(R.Suc());
        }
Ejemplo n.º 28
0
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="password">旧密码</param>
        /// <param name="newPassword">新密码</param>
        /// <returns></returns>
        public async Task <R> ChangePasswordAsync(long Id, string password, string newPassword, long agentId, CurrentUser curUser)
        {
            User user = await UserRep.FindSingleAsync(o => o.Id == Id);

            if (user == null || user.DeleteMark == true)
            {
                return(R.Err("1001", $"帐号({Id})不存在"));
            }
            if (user.Password != MD5Encrypt.MD5(password))
            {
                return(R.Err("1003", $"原密码不正确"));
            }
            newPassword = MD5Encrypt.MD5(newPassword);
            await UserRep.UpdateAsync(o => o.Id == Id, o => new User()
            {
                Password = newPassword
            });

            return(R.Suc());
        }
Ejemplo n.º 29
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    //add the userprofile
                    UserProfile userProfile = new UserProfile
                    {
                        Fname         = model.FName,
                        Lname         = model.SName,
                        Dob           = model.dob,
                        PhoneNumber   = model.number,
                        Gender        = model.gender,
                        Address       = model.address,
                        MemebershipID = user.Id
                    };

                    UserRep.Add(userProfile);

                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");

                    return(RedirectToAction("Index", "Profile"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Ejemplo n.º 30
0
        public ActionResult Delete(int id, FormCollection collection)
        {
            User user = null;

            try
            {
                using (IUnitOfWork unitOfWork = new TskDataDataContext(DbUtil.ConnectionString))
                {
                    IUserRep userRep = new UserRep(unitOfWork);
                    user = userRep.FindById(id);
                    userRep.Delete(user);
                    unitOfWork.Submit();
                }

                return(RedirectToAction("Index"));
            }
            catch
            {
                return(View(user));
            }
        }