[Widget] Menu based on group permission


(Jean Christophe “Jice” Lavocat) #1

I have a navigation menu built with the api.decorateWidget method.

Here is an example from my template, file:

<script type="text/discourse-plugin" version="0.2">
api.decorateWidget('home-logo:after', helper => {
    const showExtraInfo = helper.attrs.minimized;
        if(!showExtraInfo) {
            return helper.h('ul.nav-link-container', [
                helper.h('li.hidden-for-mobile', [
                    helper.h('a.nav-link', {
                        href:'https://myforum.com/publicCategory',
                        text:'My Public Category',
                        target:'_self'
                    })
                ]),
                helper.h('li.hidden-for-mobile', [
                    helper.h('a.nav-link', {
                        href:'https://myforum.com/privateCategory',
                        text:'My Private Category',
                        target:'_self'
                    })
                ])
           ]);
        }
});
</script>

It works fine, and allows connected users to visit my public and private categories.

Now, I would like to hide the second link based on group permissions.
It seems very complicated to me since I didn’t look into Discourse widgets documentation, and I fear that this behavior is not possible.

Can anyone tell me if I could change/hide the link based on the current user groups? If yes, in what direction should I start looking?


(Rafael dos Santos Silva) #2
<script type="text/discourse-plugin" version="0.8">

    var u = api.getCurrentUser();

    if (u !== null) {
        u.findDetails().then(function (u) {
            if (u.groups.some(function (g) {
                return g.name === 'MyGroupName';
            })) {
                // User is member of the group
            }
        });
    }
</script>

This will incur in an extra network request for .findDetails but works.