Beispiel #1
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);
            }
        }
Beispiel #2
0
        /// <summary>
        ///保存角色的资源授权
        /// </summary>
        /// <param name="roleid">角色id</param>
        /// <param name="permissionIds">资源授权</param>
        public void SaveRoleResourceMap(int roleid, string permissionIds)
        {
            string resIdLists = permissionIds;

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

            try
            {
                //保存角色资源的权限关系
                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)
                    {
                        newr_r.roleid      = roleid;
                        newr_r.resoureceid = int.Parse(strids[i]);
                        intflag            = IdalCommon.IroleResourceEx.insert(newr_r);//在关联表中添加btn或者menu关系

                        if (-1 == intflag)
                        {
                            flag = true;
                        }
                        else
                        {
                            flag = false;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                msg = ex.Message;
                //  WriteBackHtml(msg, false);
            }
        }
Beispiel #3
0
 /// <summary>
 /// 资源授权
 /// </summary>
 /// <param name="resourcepermissionIds">资源id集合的字符串</param>
 /// <param name="keyValue"></param>
 public void resourceAuthority(string resourcepermissionIds, int keyValue)
 {
     if (!string.IsNullOrEmpty(resourcepermissionIds))
     {
         string[]        ids            = resourcepermissionIds.Split(',');
         IroleResourceEx roleresourceex = new roleResourceEx();
         for (int i = 0; i < ids.Length; i++)
         {
             role_resource ro = new role_resource();
             ro.roleid      = keyValue;
             ro.resoureceid = Convert.ToInt32(ids[i]);
             roleresourceex.insert(ro);
         }
     }
 }
Beispiel #4
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);
            }
        }