/// <summary>
 /// 为用户分配角色
 /// </summary>
 /// <param name="roleid">角色id</param>
 /// <param name="userid">用户id</param>
 /// <param name="type">类型:1添加;0删除</param>
 /// <returns></returns>
 public ActionResult SetSysUserRole(int roleid, int userid, int type)
 {
     using (EFContext db = new Repositories.EFContext())
     {
         if (type == 1)
         {
             Model.SysUserRole userRole = new Model.SysUserRole()
             {
                 UserID           = userid,
                 RoleID           = roleid,
                 UserRoleCreate   = DateTime.Now,
                 UserRoleModified = DateTime.Now
             };
             db.SysUserRole.Add(userRole);
             db.SaveChanges();
         }
         else
         {
             var userRole = db.SysUserRole.Where(m => m.UserID == userid && m.RoleID == roleid).FirstOrDefault();
             db.SysUserRole.Remove(userRole);
             db.SaveChanges();
         }
     }
     return(SuccessResult("修改成功"));
 }
        /// <summary>
        /// 编辑菜单功能
        /// </summary>
        /// <returns></returns>
        public ActionResult SysMenuBarsEdit()
        {
            string[] codes = Request.Form["MenuLimitCode"].Split(',');
            string[] names = Request.Form["MenuLimitName"].Split(',');
            string[] sorts = Request.Form["MenuLimitSort"].Split(',');
            string   id    = Request.Form["id"];

            if (codes.Length != names.Length)
            {
                return(FailResult(1, "功能代码和功能名称数量不匹配!"));
            }
            if (codes.Length != sorts.Length)
            {
                return(FailResult(1, "功能代码和排序数量不匹配!"));
            }
            int sort = 0;

            for (int i = 0; i < sorts.Length; i++)
            {
                if (!Int32.TryParse(sorts[i], out sort))
                {
                    return(FailResult(1, "排序自动只能是数字"));
                }
            }
            using (EFContext db = new Repositories.EFContext())
            {
                var dbContextTransaction = db.Database.BeginTransaction();
                try
                {
                    // 删除菜单权限
                    db.Database.ExecuteSqlCommand("delete SysRoleMenuLimit where MenuLimitID in (select MenuLimitID from SysMenuLimit where MenuLimitCode <> 'view' and MenuID = {0})", id);
                    db.Database.ExecuteSqlCommand("delete SysMenuLimit where MenuLimitCode <> 'view' and MenuID = {0}", id);
                    for (int i = 0; i < codes.Length; i++)
                    {
                        Model.SysMenuLimit mlModel = new Model.SysMenuLimit()
                        {
                            MenuID            = Int32.Parse(id),
                            MenuLimitModified = DateTime.Now,
                            MenuLimitCreate   = DateTime.Now,
                            MenuLimitCode     = codes[i],
                            MenuLimitName     = names[i],
                            MenuLimitSort     = Int32.Parse(sorts[i])
                        };
                        db.SysMenuLimit.Add(mlModel);
                    }
                    db.SaveChanges();
                    dbContextTransaction.Commit();
                }
                catch (Exception ex)
                {
                    dbContextTransaction.Rollback();
                    return(FailResult(1, ex.Message));
                }
                finally {
                    dbContextTransaction.Dispose();
                }
                return(SuccessResult("保存成功"));
            }
        }
 public ActionResult ChangePwd(int id, string npwd)
 {
     using (EFContext db = new Repositories.EFContext())
     {
         var user = db.SysUser.Where(m => m.UserID == id).FirstOrDefault();
         user.UserPwd = Cryptography.MD5Encrypt64(npwd);
         db.SaveChanges();
     }
     return(SuccessResult("密码修改成功"));
 }
        public ActionResult ChangThisPwd(string npwd, string oldpwd)
        {
            string usercode = new cuser().usercode;

            using (EFContext db = new Repositories.EFContext())
            {
                var user = db.SysUser.Where(m => m.UserCode == usercode).FirstOrDefault();
                if (user.UserPwd != Cryptography.MD5Encrypt64(oldpwd))
                {
                    return(FailResult(1, "原始密码输入错误"));
                }
                user.UserPwd = Cryptography.MD5Encrypt64(npwd);
                db.SaveChanges();
            }
            return(SuccessResult("密码修改成功,请重新登陆"));
        }
 /// <summary>
 /// 给菜单添加查看权限的bar
 /// 当新增一个菜单的时候,给菜单添加一个查询的功能。
 /// </summary>
 /// <param name="id"></param>
 protected void AddMenuViewBar(int id)
 {
     using (EFContext db = new Repositories.EFContext())
     {
         Model.SysMenuLimit limit = new Model.SysMenuLimit()
         {
             MenuID            = id,
             MenuLimitModified = DateTime.Now,
             MenuLimitCreate   = DateTime.Now,
             MenuLimitCode     = "view",
             MenuLimitName     = "查看",
             MenuLimitSort     = 0
         };
         db.SysMenuLimit.Add(limit);
         db.SaveChanges();
     }
 }