Override user profile template based on group

I’m trying to make a theme component that overrides the user profile template based on the viewed user’s group membership. I’d like profiles for anonymous users to not show the #user-content area at all unless the currentUser is staff. I’ve made a group that anonymous users are automatically added to, but I’m having trouble conditionally overriding the template.

Based on Overriding Discourse Templates from a Theme and Adding background videos to certain user profiles I’ve been trying to override user/summary.hbs like this:

<script type="text/discourse-plugin" version="0.8">
    const TARGET_GROUP = "anon"

  api.registerConnectorClass('above-user-summary-stats', 'user-summary', {
    setupComponent(args, component) {
      const inGroup = [...args.model.groups].filter(g => g.name === TARGET_GROUP)
      const isAnon = inGroup.length ? true : false;
      component.setProperties({isAnon})
    }
  });

</script>

<script type="text/x-handlebars" data-template-name="user/summary">
    <DSection @pageClass="user-summary" @tagName="">
    <div class="user-content" id="user-content">
      <PluginOutlet
        @name="above-user-summary-stats"
        @args={{hash model=this.model user=this.user}}
      />
      {{log isAnon}}
      
    </div>
</DSection>
</script>

This overrides the template for all profiles, but isAnon is undefined so I think I’m not understanding how to create the logic for handlebars to check when overriding instead of adding to a plugin outlet.

Is the registerConnectorClass correct to use for this? Is a template override necessary here? I’ve been through the plugin api and User.rb and I figured out how to check if the current user is in anonymous mode, but I didn’t find any easy access besides group to check if the viewed user is anonymous. I’m familiar with Rails, js, and handlebars but I’m new to Ember and Discourse.

1 Like

If someone else comes across this, what I ended up doing was overriding the templates for user, user/summary, and user/activity by adding {{#if (or viewingSelf currentUser.staff)}} around the sections that I didn’t want logged in users to see. This overrides the profile templates for all users rather than only users in a specified group, but I decided that was good enough for my use case, to give users a little more general privacy. The template that I worked out still allowed users to choose to feature a topic but hid all other summary stats, topics, and activity except when viewed by the user themselves and staff.

My use case is similar to this topic on anonymous mode for classroom use, in that it would be nice to have an easily accessible way to give users or admin more detailed control over what parts of the profile to show or hide even for logged in users. For example, in a health support group forum, it would be nice to have anonymous accounts in the anon group, then configure to not show the activity page or tab for the anon group.

1 Like