Example #1
0
        public ActionResult GetRoleValues(string roleId)
        {
            //var result = ResModelFactory.ResDefaultData<SystemRoleValueViewModel>();
            //角色信息
            SystemRoleService service = new SystemRoleService();
            SystemRole        role    = service.GetEntitys(p => p.ID == roleId).FirstOrDefault();

            //角色权限信息
            SystemRoleValueService       rvalservice = new SystemRoleValueService();
            IQueryable <SystemRoleValue> rolevalues  = rvalservice.GetEntitys(p => p.RoleId == roleId);

            //所有系统导航信息
            IQueryable <SystemMenu> navigations = new SystemMenuService().GetEntitys(p => true);

            RoleAndRloeValues model = new RoleAndRloeValues();

            model.Id          = role.ID;
            model.RoleName    = role.RoleName;
            model.Description = role.Description;

            var parentsnav = navigations.Where(p => string.IsNullOrEmpty(p.ParentID)).OrderBy(o => o.Sort);
            List <TreeDataViewModel> tree = new List <TreeDataViewModel>();

            foreach (var p in parentsnav)
            {
                //如果有菜单权限 选中并展开
                TreeDataViewModel roleval = new TreeDataViewModel();
                roleval.id    = p.ID;
                roleval.title = p.MenuName;


                var parents = navigations.Where(n => n.ParentID == p.ID);
                if (parents.Count() > 0)
                {
                    roleval.spread   = true;
                    roleval.children = initRoleValTree(navigations, parents, rolevalues);
                }
                else
                {
                    if (rolevalues.Where(q => q.MenuId == p.ID).Count() > 0)
                    {
                        roleval.@checked = true;
                    }
                    roleval.children = null;
                }
                tree.Add(roleval);
            }
            model.TreeData = tree;
            return(View("AddRole", model));
        }
Example #2
0
        public ActionResult GetAllRoles()
        {
            var result = ResModelFactory.ResDefaultData <SystemRole>();

            try
            {
                SystemRoleService service = new SystemRoleService();
                var list = service.GetEntitys(p => true);
                result.data = list.ToArray();
            }
            catch (Exception ex)
            {
                result.code = "999";
                result.msg  = ex.Message;
            }
            return(Json(result, JsonRequestBehavior.AllowGet));;
        }
Example #3
0
        public ActionResult EditRole(RoleAndRloeValues rolevalModel)
        {
            var result = ResModelFactory.ResDefault();
            //
            SystemRoleService service = new SystemRoleService();
            SystemRole        oldRole = service.GetEntitys(p => p.ID == rolevalModel.Id).FirstOrDefault();

            oldRole.RoleName    = rolevalModel.RoleName;
            oldRole.Description = rolevalModel.Description;
            //传过来的 角色权限
            List <SystemRoleValue> rolevalues = new List <SystemRoleValue>();

            if (rolevalModel.TreeData != null)
            {
                foreach (var rval in rolevalModel.TreeData)
                {
                    SystemRoleValue roleval = new SystemRoleValue();
                    roleval.ID     = Guid.NewGuid().ToString();
                    roleval.RoleId = rolevalModel.Id;
                    roleval.MenuId = rval.id;
                    roleval.Action = "View,Show,Add,Modify,Delete";
                    rolevalues.Add(roleval);
                    InitRoleVal(rolevalues, rval.children ?? new List <TreeDataViewModel>().ToArray(), rolevalModel.Id);
                }
            }
            //角色是增加还是减少
            SystemRoleValueService       rservice   = new SystemRoleValueService();
            IQueryable <SystemRoleValue> oldroleval = rservice.GetEntitys(p => p.RoleId == rolevalModel.Id);

            List <string> oldmids = oldroleval.Select(s => s.MenuId).ToList();
            List <string> newmids = rolevalues.Select(t => t.MenuId).ToList();

            //得到增加的menuid
            List <string> add = new List <string>();
            //得到减少的menuid
            List <string> decrease = new List <string>();

            if (oldmids.Count() > 0 && newmids.Count() > 0)
            {
                decrease = oldmids.Except(newmids).ToList();
                add      = newmids.Except(oldmids).ToList();
            }
            else if (oldmids.Count() > 0 && newmids.Count() <= 0)
            {
                decrease = oldmids;
            }
            else if (oldmids.Count() <= 0 && newmids.Count() > 0)
            {
                add = newmids;
            }

            //删除减少的权限
            object[] obj   = new object[decrease.Count() + 1];
            string   parms = string.Empty;
            string   sql   = string.Empty;

            if (decrease.Count > 0)
            {
                parms = SqlParameterHelper.GetParameters(decrease.ToArray(), ref obj);
                sql   = $"delete from SystemRoleValue where roleid=@roleid and menuid in({parms})";
                obj[decrease.Count()] = new SqlParameter("@roleid", rolevalModel.Id);
            }
            //var add = new List<SystemRoleValue>();
            //if (add.Count > 0)
            //{
            //    add = rolevalues.Where(p => add.Contains(p.MenuId)).ToList();
            //}
            bool isok = service.EditRole(oldRole, rolevalues.Where(p => add.Contains(p.MenuId)).ToList(), sql, obj);

            return(Json(result, JsonRequestBehavior.AllowGet));
        }