Пример #1
0
        public JsonResult roleByUser()
        {
            string area       = "system";
            string controller = "auth";
            string action     = "user";

            string userID = Request.Params["userID"];

            using (MyDB mydb = new MyDB())
            {
                EntityObjectLib.User user = mydb.Users.Find(HttpContext.User.Identity.Name);

                EntityObjectLib.RolePrivilege p = user.Roles.SelectMany(r => r.RolePrivileges)
                                                  .First(rp => rp.Privilege.privilegeCode.ToLower().Equals(action.ToLower()) &&
                                                         rp.Privilege.resource.resourceCode.ToLower().Equals(controller.ToLower()) &&
                                                         rp.Privilege.resource.module.moduleCode.ToLower().Equals(area.ToLower())
                                                         );

                string param = p.Parameters;

                if (string.IsNullOrEmpty(param))
                {
                    object[] userRoles =
                        mydb.Roles.GroupJoin(
                            mydb.Users.Find(userID).Roles.Select(r => r.ID)
                            , r => r.ID
                            , ru => ru
                            , (r, ru) => new { r.ID, r.roleCode, r.roleName, r.roleDescription, @checked = ru.Count() > 0, userID = userID }
                            ).ToArray();

                    // 下面报错
                    //    Unable to create a constant value of type 'EntityLib.Role '. Only primitive types ('such as Int32, String, and Guid') are supported in this context.
                    //object[] userRoles =
                    //    mydb.Roles.GroupJoin(
                    //        mydb.Users.Find(userID).Roles //是这句上的问题,对比PrivilegeModel的rolePrivilege
                    //        , r => r.ID
                    //        , ru => ru.ID
                    //        , (r, ru) => new { r.ID, r.roleCode, r.roleName, r.roleDescription, @checked = ru.Count() > 0, userID = userID }
                    //    ).ToArray();

                    return(Json(userRoles, JsonRequestBehavior.AllowGet));
                }
                else if (param.Equals("本部门"))
                {
                    IEnumerable <EntityObjectLib.Role> orgRoles = user.Organization.Users.SelectMany(u => u.Roles);
                    object[] userRoles =
                        orgRoles.GroupJoin(
                            mydb.Users.Find(userID).Roles
                            //.Where(r => orgRoles.Contains(r))
                            .Select(r => r.ID)
                            , r => r.ID
                            , ru => ru
                            , (r, ru) => new { r.ID, r.roleCode, r.roleName, r.roleDescription, @checked = ru.Count() > 0, userID = userID }
                            ).ToArray();

                    return(Json(userRoles, JsonRequestBehavior.AllowGet));
                }
                return(null);
            }
        }
Пример #2
0
        public ActionResult updateRolePrivilegeParam()
        {
            string id         = Request.Form["ID"];
            string Parameters = Request.Form["Parameters"];

            using (MyDB mydb = new MyDB())
            {
                EntityObjectLib.RolePrivilege rp = mydb.RolePrivileges.Find(id);
                rp.Parameters = Parameters;
                mydb.SaveChanges();
            }
            return(Json(new { success = true }));
        }
Пример #3
0
        public ActionResult updateRolePrivivlege()
        {
            IEnumerable <string> Ids = Request.Params["IDs"].Split(",".ToArray()).AsEnumerable();
            string roleID            = Request.Params["roleID"];

            using (MyDB mydb = new MyDB())
            {
                IQueryable <string> OriPrivilegeIDs =
                    mydb.RolePrivileges
                    .Where(rp => rp.Role.ID.Equals(roleID))
                    .Select(rp => rp.Privilege.ID);

                OriPrivilegeIDs.Load();

                foreach (string s in OriPrivilegeIDs)
                {
                    if (!Ids.Contains(s))
                    {
                        EntityObjectLib.RolePrivilege p = mydb.RolePrivileges
                                                          .FirstOrDefault(rp => rp.Role.ID.Equals(roleID) && rp.Privilege.ID.Equals(s));
                        mydb.RolePrivileges.Remove(p);
                    }
                }

                IEnumerable <string> preAppendPrivilegeIDs =
                    Ids.Except(
                        mydb.RolePrivileges
                        .Where(rp => rp.Role.ID.Equals(roleID))
                        .Select(rp => rp.Privilege.ID).ToArray()
                        );

                EntityObjectLib.Role role = mydb.Roles.Find(roleID);
                foreach (string s in preAppendPrivilegeIDs)
                {
                    EntityObjectLib.RolePrivilege rp = new EntityObjectLib.RolePrivilege();
                    rp.ID        = Guid.NewGuid().ToString();
                    rp.Role      = role;
                    rp.Privilege = mydb.Privileges.Find(s);
                    mydb.RolePrivileges.Add(rp);
                }
                mydb.SaveChanges();
            }
            return(Json(new { success = true }));
        }
Пример #4
0
        public JsonResult user()
        {
            // 查看当前用户获得该权限的角色来源
            // 取角色权限参数
            // 如果空,则取全部用户
            // 如果是"本部门",只取本部门用户

            string area       = "system";
            string controller = "auth";
            string action     = "user";

            //////////////////////////

            using (MyDB mydb = new MyDB())
            {
                EntityObjectLib.User user = mydb.Users.Find(HttpContext.User.Identity.Name);
                //EntityObjectLib.RolePrivilege p = mydb.RolePrivileges
                //    .Where(rp => rp.Privilege.privilegeCode.ToLower().Equals(action.ToLower())
                //        && rp.Privilege.resource.resourceCode.ToLower().Equals(controller.ToLower())
                //        && rp.Privilege.resource.module.moduleCode.ToLower().Equals(area.ToLower())
                //        ).First(rp => rp.Role.Subjects.OfType<EntityObjectLib.User>().Contains(user));
                //&& rp.Role.Subjects.OfType<EntityObjectLib.User>().Contains(user));
                //&& mydb.Users.Find(HttpContext.User.Identity.Name).Roles.Contains(rp.Role));

                EntityObjectLib.RolePrivilege p = user.Roles.SelectMany(r => r.RolePrivileges)
                                                  .First(rp => rp.Privilege.privilegeCode.ToLower().Equals(action.ToLower()) &&
                                                         rp.Privilege.resource.resourceCode.ToLower().Equals(controller.ToLower()) &&
                                                         rp.Privilege.resource.module.moduleCode.ToLower().Equals(area.ToLower())
                                                         );

                string param = p.Parameters;

                if (string.IsNullOrEmpty(param))
                {
                    return
                        (Json(mydb.Users
                              .OrderBy(u => u.Code)
                              .Select(u =>
                                      new
                    {
                        u.ID,
                        u.Code,
                        u.Name,
                        u.Password,
                        OrgID = u.Organization.ID,
                        Organization = u.Organization.Name
                    }).ToArray()
                              , JsonRequestBehavior.AllowGet
                              ));
                }
                else if (param.Equals("本部门"))
                {
                    return(Json(
                               mydb.Users
                               .Where(u => u.Organization.ID.Equals(user.Organization.ID))
                               .OrderBy(u => u.Code)
                               .Select(u => new
                    {
                        u.ID,
                        u.Code,
                        u.Name,
                        u.Password,
                        OrgID = u.Organization.ID,
                        Organization = u.Organization.Name
                    }).ToArray()
                               , JsonRequestBehavior.AllowGet));
                }

                return(null);
            }
        }
Пример #5
0
        public ActionResult updateRolePrivivlege()
        {
            IEnumerable<string> Ids = Request.Params["IDs"].Split(",".ToArray()).AsEnumerable();
            string roleID = Request.Params["roleID"];
            using (MyDB mydb = new MyDB())
            {
                IQueryable<string> OriPrivilegeIDs =
                    mydb.RolePrivileges
                    .Where(rp => rp.Role.ID.Equals(roleID))
                    .Select(rp => rp.Privilege.ID);

                OriPrivilegeIDs.Load();

                foreach (string s in OriPrivilegeIDs)
                {
                    if (!Ids.Contains(s))
                    {
                        EntityObjectLib.RolePrivilege p = mydb.RolePrivileges
                            .FirstOrDefault(rp => rp.Role.ID.Equals(roleID) && rp.Privilege.ID.Equals(s));
                        mydb.RolePrivileges.Remove(p);
                    }
                }

                IEnumerable<string> preAppendPrivilegeIDs =
                    Ids.Except(
                    mydb.RolePrivileges
                    .Where(rp => rp.Role.ID.Equals(roleID))
                    .Select(rp => rp.Privilege.ID).ToArray()
                    );

                EntityObjectLib.Role role = mydb.Roles.Find(roleID);
                foreach (string s in preAppendPrivilegeIDs)
                {
                    EntityObjectLib.RolePrivilege rp = new EntityObjectLib.RolePrivilege();
                    rp.ID = Guid.NewGuid().ToString();
                    rp.Role = role;
                    rp.Privilege = mydb.Privileges.Find(s);
                    mydb.RolePrivileges.Add(rp);
                }
                mydb.SaveChanges();
            }
            return Json(new { success = true });
        }