public ActionResult PostCompanyRoleAction(string id, int roleID)
 {
     string[] str = id.Split(','); //Chk_CityController_Create_False,
     for (int i = 0; i < str.Length; i++)
     {
         string[]    info           = str[i].Split('_');
         string      controllerName = info[1];
         string      actionName     = info[2];
         bool        isAssigned     = Convert.ToBoolean(info[3]);
         role_action roleAction     = db.role_action.Where(ra => ra.controller_name == controllerName && ra.action_name == actionName && ra.role_id == roleID).SingleOrDefault();
         if (roleAction == null) // new
         {
             roleAction                 = new role_action();
             roleAction.role_id         = roleID;
             roleAction.controller_name = controllerName;
             roleAction.action_name     = actionName;
             db.role_action.Add(roleAction);
         }
         else
         {
             db.role_action.Remove(roleAction);
         }
         db.SaveChanges();
     }
     return(RedirectToAction("Index"));
 }
示例#2
0
        /// <summary>
        /// 根据角色id删除角色及该角色对应的动作和资源
        /// </summary>
        /// <param name="id">用户id</param>
        /// <returns></returns>
        public bool deleteroleAR(int id)
        {
            roleActionEx    rae             = new roleActionEx();
            roleResourceEx  rre             = new roleResourceEx();
            List <action>   curactionList   = rae.getRoleAllAction(id, "");
            List <resource> curresourceList = rre.getRoleAllResource(id, "");
            role            r     = getRole(id);
            bool            rflag = db.Delete <role>(r);

            if (curactionList.Count == 0 && curresourceList.Count == 0)
            {
                return(rflag);
            }
            else
            {
                bool raflag = true;
                bool rrflag = true;
                foreach (var item in curactionList)
                {
                    role_action ra = rae.getRoleAction(id, item.id);
                    raflag = db.Delete <role_action>(ra);
                }
                foreach (var item in curresourceList)
                {
                    role_resource rr = rre.getRoleResourceById(id, item.id);
                    rrflag = db.Delete <role_resource>(rr);
                }
                return(rflag & raflag & rrflag);
            }
        }
示例#3
0
 public void InsertRole_action(role_action role_action)
 {
     if ((role_action.EntityState != EntityState.Detached))
     {
         this.ObjectContext.ObjectStateManager.ChangeObjectState(role_action, EntityState.Added);
     }
     else
     {
         this.ObjectContext.role_action.AddObject(role_action);
     }
 }
示例#4
0
 public void DeleteRole_action(role_action role_action)
 {
     if ((role_action.EntityState != EntityState.Detached))
     {
         this.ObjectContext.ObjectStateManager.ChangeObjectState(role_action, EntityState.Deleted);
     }
     else
     {
         this.ObjectContext.role_action.Attach(role_action);
         this.ObjectContext.role_action.DeleteObject(role_action);
     }
 }
 public ActionResult PostAssignViewAction(string id, int companyID)
 {
     result = new Result();
     try
     {
         string[] str = id.Split(','); //Chk_CityController_Create_False,
         for (int i = 0; i < str.Length; i++)
         {
             if (!String.IsNullOrEmpty(str[i]))
             {
                 string[] info = str[i].Split('_');
                 if (!String.IsNullOrEmpty(info[1]))
                 {
                     string roleName       = info[1];
                     string controllerName = info[2];
                     bool   isAssigned     = Convert.ToBoolean(info[3]);
                     var    roleID         = db.roles.AsEnumerable().Where(x => x.role_name == roleName && x.company_id == companyID).Select(x => x.role_id).FirstOrDefault();
                     var    listAllAction  = STUtil.GetListAllActionByController(controllerName);
                     //var listController = STUtil.GetListController();
                     var roleaction = db.role_action.Where(ra => ra.controller_name == controllerName && ra.role_id == roleID).ToList();
                     if (roleaction.Count == 0) // new
                     {
                         foreach (var item in listAllAction)
                         {
                             role_action roleAction = new role_action();
                             roleAction.role_id         = roleID;
                             roleAction.controller_name = controllerName;
                             roleAction.action_name     = item.Value;
                             db.role_action.Add(roleAction);
                         }
                     }
                     else
                     {
                         db.role_action.RemoveRange(roleaction);
                     }
                     db.SaveChanges();
                     Thread.Sleep(4000);
                 }
             }
         }
     }
     catch (Exception ex)
     {
         result.MessageType = MessageType.Error;
         result.Message     = ex.Message;
     }
     return(RedirectToAction("Index", result));
 }
示例#6
0
        /// <summary>
        /// 保存角色的动作授权
        /// </summary>
        /// <param name="roleid">角色id</param>
        /// <param name="permissionIds">动作授权</param>
        public void SaveRoleActionMap(int roleid, string permissionActionIds)
        {
            string resIdLists = permissionActionIds;

            string[] strids  = resIdLists.Split(',');//资源ID集合
            bool     flag    = false;
            int      intflag = 0;
            string   msg     = string.Empty;

            try
            {
                List <action> oldres     = IdalCommon.IroleActionEx.getRoleAllAction(roleid); //角色的所有动作对象
                role_action   newr_r     = new role_action();
                List <action> oldbtnres  = oldres.Where(p => p.actiontype == 3).ToList();     //获取角色的原来的所有btn动作对象
                List <action> oldmenures = oldres.Where(p => p.actiontype == 1).ToList();     //获取角色的原来的所有菜单动作对象
                foreach (action r in oldres)                                                  //删除-原来有现在没有权限的动作
                {
                    if (!resIdLists.Contains(r.id.ToString()))                                //如果新获取的id不包含原来roles集合的id,则删除
                    {
                        IdalCommon.IroleActionEx.delete(roleid, r.id);
                    }
                }
                for (int i = 0; i < strids.Length; i++)//循环选中的动作
                {
                    if (int.Parse(strids[i]) != 0)
                    {
                        newr_r.roleid   = roleid;
                        newr_r.actionid = int.Parse(strids[i]);
                        intflag         = IdalCommon.IroleActionEx.insert(newr_r);//在关联表中添加btn或者menu关系
                        if (-1 == intflag)
                        {
                            flag = true;
                        }
                        else
                        {
                            flag = false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                //  WriteBackHtml(msg, false);
            }
        }
示例#7
0
 public void UpdateRole_action(role_action currentrole_action)
 {
     this.ObjectContext.role_action.AttachAsModified(currentrole_action, this.ChangeSet.GetOriginal(currentrole_action));
 }
示例#8
0
        public void saveOfRoles()//保存角色的资源授权关系
        {
            string resIdLists = ctx.Request["indexs"];

            string[] strids  = ctx.Request["indexs"].Split(',');//资源ID集合
            bool     flag    = false;
            int      intflag = 0;
            string   msg     = string.Empty;

            try
            {
                int roleid = int.Parse(ctx.Request["roleid"]);
                if (ctx.Request["type"] == "resource")                                                  //保存资源角色关联
                {
                    List <resource> oldres     = IdalCommon.IroleResourceEx.getRoleAllResource(roleid); //角色的所有资源对象
                    role_resource   newr_r     = new role_resource();
                    List <resource> oldbtnres  = oldres.Where(p => p.resourcetype == 3).ToList();       //获取角色的原来的所有btn资源对象
                    List <resource> oldmenures = oldres.Where(p => p.resourcetype == 1).ToList();       //获取角色的原来的所有菜单资源对象
                    string          newBtnParentID;                                                     //添加btn的上级
                    foreach (resource r in oldres)                                                      //删除-原来有现在没有权限的资源
                    {
                        if (!resIdLists.Contains(r.id.ToString()))                                      //如果新获取的id不包含原来roles集合的id,则删除
                        {
                            IdalCommon.IroleResourceEx.delete(roleid, r.id);
                        }
                    }
                    for (int i = 0; i < strids.Length; i++)//循环选中的资源
                    {
                        if (int.Parse(strids[i]) != 0 && int.Parse(strids[i]) != 1)
                        {
                            newr_r.roleid      = roleid;
                            newr_r.resoureceid = int.Parse(strids[i]);
                            intflag            = IdalCommon.IroleResourceEx.insert(newr_r);                              //在关联表中添加btn或者menu关系
                            newBtnParentID     = IdalCommon.IresourceEx.getResource(int.Parse(strids[i])).resourceowner; //1
                            if (newBtnParentID != null)
                            {
                                //if (oldmenures.Where(p => p.id == newBtnParentID).Count() == 0)//判断添加的btn的上级菜单是否在关系表中,如果没有,则添加
                                //{
                                newr_r.resoureceid = int.Parse(newBtnParentID);                 //btn上级的menu的ID
                                intflag            = IdalCommon.IroleResourceEx.insert(newr_r); //在关联表中添加关系
                                // }
                            }
                            if (-1 == intflag)
                            {
                                flag = true;
                            }
                            else
                            {
                                flag = false;
                            }
                        }
                    }
                }
                else//保存按钮角色关联
                {
                    List <action> oldres     = IdalCommon.IroleActionEx.getRoleAllAction(roleid);//角色的所有资源对象
                    role_action   newr_r     = new role_action();
                    List <action> oldbtnres  = oldres.Where(p => p.actiontype == 3).ToList(); //获取角色的原来的所有btn资源对象
                    List <action> oldmenures = oldres.Where(p => p.actiontype == 1).ToList(); //获取角色的原来的所有菜单资源对象
                    string        newBtnParentID;                                             //添加btn的上级
                    foreach (action r in oldres)                                              //删除-原来有现在没有权限的资源
                    {
                        if (!resIdLists.Contains(r.id.ToString()))                            //如果新获取的id不包含原来roles集合的id,则删除
                        {
                            IdalCommon.IroleActionEx.delete(roleid, r.id);
                        }
                    }
                    for (int i = 0; i < strids.Length; i++)//循环选中的资源
                    {
                        if (int.Parse(strids[i]) != 0)
                        {
                            newr_r.roleid   = roleid;
                            newr_r.actionid = int.Parse(strids[i]);
                            intflag         = IdalCommon.IroleActionEx.insert(newr_r);                          //在关联表中添加btn或者menu关系
                            newBtnParentID  = IdalCommon.IactionEx.getAction(int.Parse(strids[i])).actionowner; //将int?转为int
                            if (newBtnParentID != null)
                            {
                                if (oldmenures.Where(p => p.id == int.Parse(newBtnParentID)).Count() == 0) //判断添加的btn的上级菜单是否在关系表中,如果没有,则添加
                                {
                                    newr_r.actionid = int.Parse(newBtnParentID);                           //btn上级的menu的ID
                                    intflag         = IdalCommon.IroleActionEx.insert(newr_r);             //在关联表中添加关系
                                }
                            }
                            if (-1 == intflag)
                            {
                                flag = true;
                            }
                            else
                            {
                                flag = false;
                            }
                        }
                    }
                }

                if (flag)//关联成功
                {
                    msg = "关联成功!";
                    ////获取当前用户的所有角色列表名称//这里之所以不直接用accctx.Roles的原因:可以添加为当前用户添加了新的角色
                    //List<role> currentRoles = IuserRoleEx.getRole(accctx.currentuser.username).ToList();
                    //if (currentRoles.Where(p => p.id == roleid).Count() > 0)//操作的角色如果是属于当前用户的角色,需要重新加载用户信息
                    //{
                    //    //accctx.ReloadAccounts();//重新加载当前账户信息
                    //    //ctx.User = accctx;
                    //    //ctx.Session["accctx"] = accctx;//数据重新加载
                    //}
                }

                else
                {
                    msg = "关联失败!";
                }
                WriteBackHtml(msg, flag);
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                WriteBackHtml(msg, false);
            }
        }
示例#9
0
 public RoleActionModel()
 {
     RoleAction             = new role_action();
     ListRoleActionAssigned = new List <role_action>();
 }