larecipe icon indicating copy to clipboard operation
larecipe copied to clipboard

Hide side menu options depending on the user role

Open fabiomlferreira opened this issue 3 years ago • 3 comments

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.

fabiomlferreira avatar Oct 26 '21 16:10 fabiomlferreira

I need this feature too

indra-yana avatar Apr 28 '22 03:04 indra-yana

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 avatar Mar 13 '23 12:03 rderks88

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');
    }

rderks88 avatar Mar 13 '23 13:03 rderks88