Exemplo n.º 1
0
        public ActionResult UserEdit(int id,string loginname, string name, string password, int state, int role1, int role2, int role3, string email,  FormCollection collection)
        {
            if (!UserInfo.CurUser.HasRight("系统管理-用户管理")) return Redirect("~/content/AccessDeny.htm");
            SystemUser r = db.SystemUsers.Find(id);
            if (r == null) r = new SystemUser();

            if (loginname.Length == 0)
            {
                ModelState.AddModelError("LoginName", "用户姓名不能为空");
            }
            if (name.Length == 0)
            {
                ModelState.AddModelError("Name", "用户姓名不能为空");
            }
            if (role1 == 0 && role2 == 0 && role3 == 0)
            {
                ModelState.AddModelError("Role", "请选择至少一个角色");
            }if (id == 0 && string.IsNullOrEmpty(password))
            {
                ModelState.AddModelError("Password", "请输入密码");
            }else  if (name != r.LoginName && string.IsNullOrEmpty(password))
            {
                ModelState.AddModelError("Password", "修改用户名时,需要重置密码");
            }
            string checkuser =
                (from o in db.SystemUsers where o.Id != id && o.LoginName == loginname select name).FirstOrDefault();
            if (checkuser != null)
            {
                ModelState.AddModelError("Name", "该用户已存在");
            }

            if (ModelState.IsValid == false)
            {
                ViewBag.Role1 = role1;
                ViewBag.Role2 = role2;
                ViewBag.Role3 = role3;
                r.Password = null;
                return View(r);
            }
            else
            {

                SystemUser vo = new SystemUser();
                vo.LoginName = loginname;
                vo.Password = password;
                    vo.State = state;
                    vo.Name = name;
                    vo.Email = email;
                r.Save(db, vo);

                var query = (from o in db.RoleUsers where o.UserId == r.Id select o);
                foreach (RoleUser ru in query) db.RoleUsers.Remove(ru);
                if (role1 != 0) db.RoleUsers.Add(new RoleUser { UserId = r.Id, RoleId = role1 });
                if (role2 != 0 && role2 != role1) db.RoleUsers.Add(new RoleUser { UserId = r.Id, RoleId = role2 });
                if (role3 != 0 && role3 != role2 && role3 != role1) db.RoleUsers.Add(new RoleUser { UserId = r.Id, RoleId = role3 });
                db.SaveChanges();
                UserBLL.UpdateUsers();
                return Redirect("../UserView/" + r.Id);

            }
        }
Exemplo n.º 2
0
 public ActionResult UserView(int id)
 {
     if (!UserInfo.CurUser.HasRight("系统管理-用户管理")) return Redirect("~/content/AccessDeny.htm");
     if (!UserInfo.CurUser.HasRight("系统管理-用户管理")) return Redirect("~/content/AccessDeny.htm");
     SystemUser r = (from o in db.SystemUsers where o.Id == id select o).FirstOrDefault();
     ViewBag.Role1 = ""; ViewBag.Role2 = ""; ViewBag.Role3 = "";
     if (r == null)
     {
         r = new SystemUser();
     }
     else
     {
         var roles = (from o in db.RoleUsers from p in db.Roles where o.UserId == id && p.Id == o.RoleId select p.Name).ToList();
         if (roles.Count > 0) ViewBag.Role1 = roles[0];
         if (roles.Count > 1) ViewBag.Role2 = roles[1];
         if (roles.Count > 2) ViewBag.Role3 = roles[2];
     }
     return View(r);
 }
Exemplo n.º 3
0
 public ActionResult UserEdit(int id, string name, string password, int state, int role1, int role2, int role3, string email, string workno, FormCollection collection)
 {
     if (!UserInfo.CurUser.HasRight("系统管理-用户管理")) return Redirect("~/content/AccessDeny.htm");
     SystemUser r = (from o in db.SystemUsers where o.Id == id select o).FirstOrDefault();
     if (r == null)
     {
         r = new SystemUser(); db.SystemUsers.Add(r);
     }
     if (name.Length == 0)
     {
         ModelState.AddModelError("Name", "用户姓名不能为空");
     }
     if (role1 == 0 && role2 == 0 && role3 == 0)
     {
         ModelState.AddModelError("Role", "请选择至少一个角色");
     }
     if (name != r.Name && password == "")
     {
         ModelState.AddModelError("Password", "修改用户名时,需要重置密码");
     }
     r.Name = name;
     r.State = state;
     r.WorkNO = workno;
     r.Email = email;
     if (ModelState.IsValid == false)
     {
         ViewBag.Role1 = role1;
         ViewBag.Role2 = role2;
         ViewBag.Role3 = role3;
         return View(r);
     }
     else
     {
         //if (r.Password == null) r.Password = "******";
         if (r.Name != name || password != "")
         {
             r.Password = password;
             r.Save(db);
         }
         else
         {
             if (r.Id == 0) db.SystemUsers.Add(r);
             db.SaveChanges();
         }
         var query = (from o in db.RoleUsers where o.UserId == r.Id select o);
         foreach (RoleUser ru in query) db.RoleUsers.Remove(ru);
         if (role1 != 0) db.RoleUsers.Add(new RoleUser { UserId = r.Id, RoleId = role1 });
         if (role2 != 0 && role2 != role1) db.RoleUsers.Add(new RoleUser { UserId = r.Id, RoleId = role2 });
         if (role3 != 0 && role3 != role2 && role3 != role1) db.RoleUsers.Add(new RoleUser { UserId = r.Id, RoleId = role3 });
         db.SaveChanges();
         UserBLL.UpdateUsers();
         if (id == 0)
         {
             return Redirect("../UserView/" + r.Id);
         }
         else
         {
             return Redirect("~/content/close.htm");
         }
     }
 }
Exemplo n.º 4
0
        public void Fun()
        {
            string filename = "人员名单.xlsx";
            FileInfo fi = new FileInfo(filename);
            StringBuilder errorinfo = new StringBuilder();
            using (ExcelPackage package = new ExcelPackage(fi))
            {
                ExcelWorksheet worksheet = package.Workbook.Worksheets["Sheet1"];
                int i = 1;
                using (Context db = new Context())
                {
                    //db.Configuration.AutoDetectChangesEnabled = false;
                    //using (TransactionScope tran = new TransactionScope())
                    //{
                        while (i < 100)
                        {
                            i++;
                            string name = ExcelHelper.ReadString(worksheet.Cells[i, 4]);
                            if (string.IsNullOrEmpty(name)) continue;
                            if ((from o in db.SystemUsers where o.Name == name select o).FirstOrDefault() != null)
                            {
                                Console.WriteLine(name + " 已存在");
                                continue;
                            }
                            string rolename = ExcelHelper.ReadString(worksheet.Cells[i, 2]);
                            int roleid = (from o in db.Roles where o.Name == rolename select o.Id).FirstOrDefault();
                            if (roleid == 0)
                            {
                               roleid= (from o in db.Roles where o.Name == "销售员" select o.Id).First();
                            }
                            SystemUser user = new SystemUser
                                              {
                                                  Name = name,
                                                  Password = "******",
                                                  LoginName = name,
                                                  Email = ExcelHelper.ReadString(worksheet.Cells[i, 5])
                                              };
                            SystemUser u = new SystemUser();
                            db.SystemUsers.Add(u);
                            u.Save(db, user);
                            RoleUser ru = new RoleUser {UserId = u.Id, RoleId = (int) roleid};
                            db.RoleUsers.Add(ru);
                            string projname = ExcelHelper.ReadString(worksheet.Cells[i, 1]);
                            int project =
                                (from o in db.Departments
                                    where o.Name == projname
                                    select o.Id).First();
                            DepartmentUser du = new DepartmentUser {UserId = u.Id};
                            string d3 = ExcelHelper.ReadString(worksheet.Cells[i, 3]);
                            if (string.IsNullOrEmpty(d3))
                            {
                                d3 = "公共客户";
                            }
                            else
                            {
                                d3 = rolename;
                            }
                            Department dept =
                                (from o in db.Departments where o.PId == project && o.Name == d3 select o).First();
                            du.DepartmentId = dept.Id;
                            db.DepartmentUsers.Add(du);
                            db.SaveChanges();

                           // tran.Complete();
                      //  }

                    }
                }
            }
        }
Exemplo n.º 5
0
 public static UserCheckResult CheckUser(string name, string password, out SystemUser user)
 {
     if (logonFailedList.ContainsKey(name))
     {
         if (logonFailedList[name] > 4)
         {
             user = null;
             return UserCheckResult.用户登录锁定;
         }
     }
     OUContext db = new OUContext();
     user = (from o in db.SystemUsers where o.State == (int)UserState.Enabled && (name == o.Name ) select o).FirstOrDefault();
     if (user != null)
     {
         if (user.CheckPassword(password))
         {
             return UserCheckResult.验证通过;
         }
         else
         {
             if (logonFailedList.ContainsKey(name))
             {
                 logonFailedList[name]++;
             }
             else
             {
                 logonFailedList.Add(name, 1);
             }
             user = new SystemUser { Id = logonFailedList[name] };
             return UserCheckResult.用户密码错误;
         }
     }
     else
     {
         return UserCheckResult.用户不存在;
     }
 }
Exemplo n.º 6
0
        /// <summary>
        /// 修改密码或用户名时候调用。调用时password应该是加密前password
        /// </summary>
        /// <param name="db"></param>
        /// <returns></returns>
        public void Save(Context db,SystemUser newValue)
        {
            if (Id == 0)
            {
                db.SystemUsers.Add(this);
            }
            newValue.LoginName = newValue.LoginName.Trim();
            if (LoginName != newValue.LoginName || string.IsNullOrEmpty(newValue.Password) == false)
            {
                LoginName = newValue.LoginName;
                Password = EncryptPassword(LoginName.ToLower(),newValue.Password);
            }

            Name = newValue.Name;
            Email = newValue.Email;
            State = newValue.State;
            db.SaveChanges();
        }
Exemplo n.º 7
0
        static public UserCheckResult CheckUser(string name, string password, out SystemUser user, bool isDomain)
        {
            if (logonFailedList.ContainsKey(name))
            {
                if (logonFailedList[name] > 4)
                {
                    user = null;
                    return(UserCheckResult.用户登录锁定);
                }
            }
            Context db = new Context();

            user =
                (from o in db.SystemUsers where o.State == (int)UserState.Enabled && (name == o.LoginName) select o)
                .FirstOrDefault();
            if (user != null)
            {
                if (isDomain)
                {
                    bool   domainchecked = false;
                    string domain        = System.Configuration.ConfigurationManager.AppSettings["Domain"];
                    string aaa           = System.Environment.UserDomainName;
                    string bbb           = System.Environment.UserName;
                    WindowsImpersonationContext impersonationContext;
                    WindowsIdentity             tempWindowsIdentity;
                    IntPtr token          = IntPtr.Zero;
                    IntPtr tokenDuplicate = IntPtr.Zero;
                    if (LogonUser(name, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref token) != 0)
                    {
                        if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
                        {
                            tempWindowsIdentity  = new WindowsIdentity(tokenDuplicate);
                            impersonationContext = tempWindowsIdentity.Impersonate();
                            if (impersonationContext != null)
                            {
                                domainchecked = true;
                                return(UserCheckResult.验证通过);
                            }
                        }
                    }
                    return(UserCheckResult.用户密码错误);
                }
                else
                {
                    if (user.CheckPassword(password))
                    {
                        return(UserCheckResult.验证通过);
                    }
                    else
                    {
                        if (logonFailedList.ContainsKey(name))
                        {
                            logonFailedList[name]++;
                        }
                        else
                        {
                            logonFailedList.Add(name, 1);
                        }
                        user = new SystemUser {
                            Id = logonFailedList[name]
                        };
                        return(UserCheckResult.用户密码错误);
                    }
                }
            }
            else
            {
                return(UserCheckResult.用户不存在);
            }
        }