Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hide side menu options depending on the user role #287

Open
fabiomlferreira opened this issue Oct 26, 2021 · 3 comments
Open

Hide side menu options depending on the user role #287

fabiomlferreira opened this issue Oct 26, 2021 · 3 comments

Comments

@fabiomlferreira
Copy link

We have examples how to use a gate to enable/disable access to a specific page, but in for a good user experience the users that don't have access to some pages should now saw it on the side menu.

How can we hide some pages for some users on the sidebar?

In my case I want to create a documentation for admins and other for end users, the ideal approach was to have a different route for each type of documentation for example "/docs/..." for end users and "/admin-docs/..." for administrators, but I think this was not possible it should be possible to change the sidebar menu depending on the users role/permission.

@indra-yana
Copy link

I need this feature too

@rderks88
Copy link

Only way I can see this to be achieved is by converting the parsedContent within Documentation.php to an array, taking the url per item (if any) and then filtering/feeding that to the "viewLarecipe" gate.

Would that be the way to go?
It seams a bit bloated, but I really need this feature to.

@rderks88
Copy link

rderks88 commented Mar 13, 2023

I don't have the time to completely work it out right now. Maybe later. But if anyone feels to take a stab at it, I meant something along the lines of this. Again, there are probably way neater ways to do this, but it will work I guess. And when cached, it shouldn't we too much of a performance hit.



    /**
     * Get the documentation index page.
     *
     * @param  string  $version
     * @return string
     */
    public function getIndex($version)
    {
        return $this->cache->remember(function() use($version) {
            $path = base_path(config('larecipe.docs.path').'/'.$version.'/index.md');

            if ($this->files->exists($path)) {
                $parsedContent = $this->parse($this->files->get($path));

                // Load the HTML file
                $doc = new \DOMDocument();
                $doc->loadHtml($parsedContent);

                // Create an array to store the parsed data
                $data = [];

                // Traverse the HTML tree and extract the data
                foreach ($doc->getElementsByTagName('body')->item(0)->childNodes as $node) {
                    if ($node->nodeName == 'h1') {
                        // Add the h1 heading to the array
                        $data[] = $node;
                    } elseif ($node->nodeName == 'p') {
                        // Add the paragraph text to the array
                        $data[] = $node;
                    } elseif ($node->nodeName == 'ul') {
                        // Create an array to store the list items
                        $list = [];
                        // Traverse the list and add the items to the array
                        foreach ($node->getElementsByTagName('li') as $li) {
                            $list[] = $li;

                            foreach ($node->getElementsByTagName('a') as $a) {
                                /**
                                 * Get the href from this a node
                                 * Take everything after "{{route}}/{{version}}".
                                 * Continue here
                                 * Filter by sending this string to the LaRecipe gate and if falsy, we filter out the element
                                 * If the list is empty, we do not add the element back onto data.
                                 * If list not empty, continue, and at the end convert data back to html.
                                 */
                                $href = $a->getAttribute('href');
                            }
                        }
                        // Add the list to the main array
                        $data['list'] = $list;
                    }
                }

                // Print the multidimensional array
                dd($data);

                return $this->replaceLinks($version, $parsedContent);
            }

            return null;
        }, 'larecipe.docs.'.$version.'.index');
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants