Exemplo n.º 1
0
        /// <summary>
        /// 获取前台按钮权限
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public List <RoleButtonPermissionDto> GetButtonPermissionList(long roleId)
        {
            List <RoleButtonPermissionDto> permissionList = PermissionCache.GetFrontOperatesPermission(roleId);

            if (permissionList != null)
            {
                return(permissionList);
            }

            permissionList = new List <RoleButtonPermissionDto>();

            //菜单权限
            var menus = Sqldb.Select <sys_menu, sys_role_authorize>()
                        .InnerJoin((m, r) => m.id == r.menu_id)
                        .Where((m, r) => r.role_id == roleId && m.role_tag != "#")
                        .ToList((m, r) => m);

            List <string> roleTags = new List <string>();

            if (menus.Count > 0)
            {
                //按钮权限
                var buttonRoleTags = Sqldb.Select <sys_operate, sys_role_authorize>()
                                     .InnerJoin((o, r) => o.id == r.menu_id)
                                     .Where((o, r) => r.role_id == roleId)
                                     .ToList((o, r) => o);

                foreach (sys_menu menu in menus)
                {
                    //菜单权限标识
                    roleTags.Add(menu.role_tag.ToLower());

                    RoleButtonPermissionDto model = new RoleButtonPermissionDto();
                    model.menuId = CommHelper.GetMenuTabId(menu.menu_url.ToLower());
                    List <sys_operate> buttons = buttonRoleTags.Where(s => s.menu_id == menu.id).ToList();
                    foreach (sys_operate button in buttons)
                    {
                        model.add(button.role_tag.ToLower());
                        //操作权限标识
                        roleTags.Add(button.role_tag.ToLower());
                    }
                    permissionList.Add(model);
                }
            }

            //添加缓存
            if (permissionList.Count > 0)
            {
                PermissionCache.AddFrontOperatesPermission(roleId, permissionList);
            }
            if (roleTags.Count > 0)
            {
                PermissionCache.AddOperatePermission(roleId, roleTags);
            }
            return(permissionList);
        }
Exemplo n.º 2
0
        /// <summary>
        /// 获取菜单和操作权限字符串
        /// </summary>
        /// <param name="roleId"></param>
        public bool  HasPermission(long roleId, string permissionTags)
        {
            if (roleId == 0)
            {
                return(false);
            }

            string[] permissionTagsArray = permissionTags.ToLower().Split(',');
            var      permissionList      = PermissionCache.GetOperatePermission(roleId);

            if (permissionList != null)
            {
                var tag = permissionList.Where(s => permissionTagsArray.Contains(s)).FirstOrDefault();
                if (tag == null)
                {
                    return(false);
                }
                return(true);
            }

            //没缓存的化直接取数据
            var menus = Sqldb.Select <sys_menu, sys_role_authorize>()
                        .InnerJoin((m, r) => m.id == r.menu_id)
                        .Where((m, r) => r.role_id == roleId)
                        .ToList((m, r) => m.role_tag);

            var buttonRoleTags = Sqldb.Select <sys_operate, sys_role_authorize>()
                                 .InnerJoin((o, r) => o.id == r.menu_id)
                                 .Where((o, r) => r.role_id == roleId)
                                 .ToList((o, r) => o.role_tag);

            List <string> roleTags = new List <string>();

            if (menus.Count > 0)
            {
                foreach (var menu in menus)
                {
                    if (menu.IsEmpty() || menu == "#")
                    {
                        continue;
                    }
                    roleTags.Add(menu);
                }
            }
            if (buttonRoleTags.Count > 0)
            {
                foreach (var btag in buttonRoleTags)
                {
                    if (btag.IsEmpty() || btag == "#")
                    {
                        continue;
                    }
                    roleTags.Add(btag);
                }
            }

            if (roleTags.Count > 0)
            {
                PermissionCache.AddOperatePermission(roleId, roleTags);

                var tag = roleTags.Where(s => permissionTagsArray.Contains(s)).FirstOrDefault();
                if (tag == null)
                {
                    return(false);
                }
                return(true);
            }

            return(false);
        }