Ho aggiunto alcuni pulsanti tramite script nella parte <head>...</head> di questa pagina:
/admin/customize/themes/2/common/head_tag/edit
in questo modo:
<script type="text/discourse-plugin" version="0.4">
api.onToolbarCreate(toolbar => {
toolbar.addButton({
id: "times-circle",
});
});
</script>
Come posso fare in modo che questo pulsante sia visibile solo agli utenti dello staff (moderatori, amministratori) e agli utenti appartenenti al gruppo chiamato “vip-group”?
zcuric
(Zdravko)
28 Aprile 2020, 7:43am
2
Ciao! Semplice come questo
<script type="text/discourse-plugin" version="0.8">
const user = api.getCurrentUser();
// Verifica se l'utente è nel gruppo vip-group
const isInVipGroup = user.groups.some(({name}) => name === "vip-group");
if (user.staff || isInVipGroup) {
api.onToolbarCreate(toolbar => {
toolbar.addButton({
id: "times-circle",
});
});
}
</script>
Ottimo! Sta funzionando!
Ho anche trovato questi argomenti, che potrebbero essere utili a qualcuno.
Discourse adds a staff class to the body tag when a staff user is viewing the site. If you are ok with the button being visible to admins and moderators, you could hide the button with CSS and then make it visible when the staff class is present.
If only having the button visible to admins is a requirement, you will need to use some javascript to determine that the user is an admin. The easiest approach to that would be to add an admin class to the body and then make the button visible when the…
<div id="after-header">Stuff</div>
<script type="text/discourse-plugin" version="0.8">
api.onPageChange(() => {
let currentUser = api.getCurrentUser();
var groups = currentUser.groups;
const isInGroup1 = groups.some(g => g.name === 'group1');
const isInGroup2 = groups.some(g => g.name === 'group2');
const isInGroup3 = groups.some(g => g.name === 'group3');
if (isInGroup1) {
$("#after-header").addClass("group1");
}
…
zcuric
(Zdravko)
28 Aprile 2020, 7:51am
4
@Ed_Bobkov Ho modificato lo snippet per funzionare con il tuo specifico tipo di gruppo. Non so se l’abbia già visto, è solo un promemoria.