Exemple #1
0
        internal bool Login(string username, string password)
        {
            string passwordHash = utils.HashPassword(password);

            userName         = username;
            userPasswordHash = passwordHash;
            Damocles2Entities de = new Damocles2Entities();
            User user            = de.Users.Where(u => u.Username == username && u.UserPassword == passwordHash).FirstOrDefault();

            if (user == null)
            {
                return(false);
            }

            CurrentUser.UserId = user.Id;

            //TODO: This is not functioning correctly - simply want the User's current rank.
            UserRank ur = de.UserRanks.Where(usr => usr.UserId == user.Id).FirstOrDefault();

            user.IsOnline = true;
            UserRank      = ur.Rank.RankNameEnglish;

            UserJurisdiction uj = de.UserJurisdictions.Where(usrj => usrj.UserId == user.Id).FirstOrDefault();

            if (uj != null)
            {
                if (uj.Jurisidction.Country == uj.Jurisidction.State)
                {
                    UJurisdiction = uj.Jurisidction.Country;
                }
                else
                {
                    UJurisdiction = uj.Jurisidction.State.Trim() + " in " + uj.Jurisidction.Country.Trim();
                }
            }
            else
            {
                UJurisdiction = "Unknown";
            }


            var aus = de.UsersSessions.Where(auss => auss.id == user.Id);

            foreach (UsersSession userS in aus)
            {
                CurrentUser.SessionSecondsTotal += userS.SessionSeconds;
            }

            UsersSession us = new UsersSession();

            us.LoggedOnAt = DateTime.UtcNow;
            us.id         = user.Id;
            de.UsersSessions.Add(us);

            de.SaveChanges();
            return(true);
        }
Exemple #2
0
        public void Create(FormCollection collection)
        {
            var msg = new Msg();

            try
            {
                var Db    = new Users().Db;
                var Utils = new Utils.Utils();
                // 初始化对象
                Entity.T_User user = new Entity.T_User()
                {
                    User_nickname   = collection["nickname"],
                    User_note       = collection["note"],
                    User_phone      = collection["phone"],
                    User_role       = Convert.ToInt32(collection["role"]),
                    User_dorm_id    = Convert.ToInt32(collection["pid"]),
                    User_login_name = collection["login_name"],
                    User_pwd        = Utils.HashPassword(((string)Utils.GetAppSetting("DefaultPassword", typeof(string)))), // 设置默认密码
                };
                if (user.User_login_name.Trim().Length < 3)
                {
                    throw new Exception("用户名不能少于3个字符长度");
                }

                if (user.User_dorm_id == 0 && user.User_role < 3)
                {
                    throw new Exception("非系统管理员请选择所属园区");
                }
                if ((int)Session["role"] < 3 && (int)Session["role"] < user.User_role + 1)
                {
                    // 判断权限
                    throw new Exception("权限不足");
                }
                /// 检查用户名是否已存在

                if (Db.Queryable <Entity.T_User>().Count(x => x.User_login_name == user.User_login_name) > 0)
                {
                    // 用户名已存在
                    throw new Exception("用户名已存在!如果列表不显示可能是未实际从数据库中删除。");
                }
                if (Db.Insertable(user).ExecuteCommand() > 0)
                {
                    msg.Message = "添加成功!";
                }
                else
                {
                    throw new Exception("发生未知错误,添加失败!");
                }
            }
            catch (Exception ex)
            {
                msg.Message = ex.Message;
                msg.Code    = -1;
            }
            Response.Write(msg.ToJson());
            Response.End();
        }
Exemple #3
0
        public void Reset(int id, FormCollection collection)
        {
            var msg = new Msg();

            try
            {
                var User = new Users();
                // 初始化对象

                var user = User.FindById(id);
                if (user == null)
                {
                    throw new Exception("该用户不存在!");
                }
                if ((int)Session["role"] < 3 && (int)Session["role"] < user.User_role + 1)
                {
                    // 判断权限
                    throw new Exception("权限不足");
                }
                else
                {
                    var Util = new Utils.Utils();
                    var pwd  = (string)Util.GetAppSetting("DefaultPassword", typeof(string));
                    user.User_pwd = Util.HashPassword(pwd); // 设置默认密码
                    if (User.Update(user))
                    {
                        msg.Message = "重置默认密码成功,该角色的密码已设置为'" + pwd + "'";
                    }
                    else
                    {
                        throw new Exception("发生未知错误!");
                    }
                }
            }
            catch (Exception ex)
            {
                msg.Code    = -1;
                msg.Message = ex.Message;
            }
            Response.Write(msg.ToJson());
            Response.End();
        }
Exemple #4
0
        // GET: Tools
        public ActionResult Index()
        {
            var hash  = Request["hash"];
            var txt   = Request["txt"];
            var Utils = new Utils.Utils();

            if (hash != null && hash.Length > 20)
            {
                Response.Write(Utils.CheckPasswd(txt, hash) ? true : false);
                Response.End();
            }
            if (txt != null && txt.Length > 0)
            {
                var msg = new Msg();
                msg.Content = new
                {
                    text        = txt,
                    bcrypt_hash = Utils.HashPassword(txt),
                };
                Response.Write(msg.ToJson());
                Response.End();
            }
            return(View());
        }