In the operation of daily websites, we often encounter such needs: some content is intended to be seen by everyone, while some is only open to specific members or displays different information for users of different levels.AnQiCMS provides powerful user group permission management functions, allowing us to flexibly control the visibility of website content and achieve the 'thousand faces of content'.

Why is it necessary to display or hide content based on user groups?

Imagine that you are running a website offering a variety of services, including free basic articles, paid in-depth reports, and exclusive tutorials available only to premium members.If there is no user group permission function, these contents cannot effectively differentiate the audience, either all public will devalue the paid content, or all hidden will lose potential users.The user group management and VIP system of AnQi CMS is designed to solve this pain point, help us realize content monetization, enhance user stickiness, and provide a personalized browsing experience.

How does AnQi CMS identify user and content permissions?

AnQi CMS cleverly constructs a content permission system through the two core concepts of "user group level" and "content reading level".

  1. User group levelIn the AnQi CMS backend, we can create different user groups, such as "Visitors", "Registered Users", "VIP Members", "Senior VIP" and so on.Each user group can be assigned a specific 'level', which is usually a number, with higher levels representing greater privileges.For example, visitors may be level 0, registered users level 1, VIP members level 5, and senior VIPs level 10.

  2. Content Reading Level (ReadLevel)When we publish or edit articles, products and other content, Anqie CMS allows us to set a 'reading level' for these contents.This level determines which user groups have permission to view this content.If an article's reading level is set to 5, only users with a level of 5 or higher can read it completely.

In this way, the system automatically compares the user's level and the content's reading level when the user accesses the content, thereby deciding whether to display the full content or prompt the user that they do not have sufficient permissions.

Set content reading permissions in the background

Setting content reading permissions in the Anqi CMS backend management interface is a relatively intuitive process

When creating or editing a document (whether an article or a product), you will find an option called "Reading Level" in the "Other Parameters" section of the editing page.Here, we can assign a reading level to the current content based on its importance, value, and target audience.For example, a regular news article can be set to level 0, indicating that it is visible to everyone;A deep research report can be set to level 5, available only to VIP members;And an internal document may be set to level 10, only the highest-level users can access it.

Implement the display and hide of content in the front-end template

The front-end template is where users finally see the website content, and it is also the key to dynamically displaying content based on user group permissions. The template tag system of Anqi CMS, especiallyuserDetail/userGroupDetailandarchiveDetailThese labels make this process very flexible.

Generally, we will take the following steps to judge and display content:

  1. Get the level of the currently logged-in userFirstly, we need to know who the current visitor to the website is and what user group level they belong to. In the Anqicms template, we can do this byuserDetailLabel to get the basic information of the current user, including the ID of the user group he belongs to. Then, make use ofuserGroupDetailLabel, obtain the specific level of the user group according to the user group ID. If the user is not logged in, we can consider it as the default 'visitor' level (for example, level 0).'

    A rough logic might look like this:

    {% userDetail currentUser with name="Id" %}
    {% set userIsLoggedIn = (currentUser > 0) %}
    {% set currentUserId = currentUser %}
    
    
    {% set currentUserLevel = 0 %} {# 默认访客等级为0 #}
    {% if userIsLoggedIn %}
        {% userDetail currentGroupId with name="GroupId" id=currentUserId %}
        {% userGroupDetail userGroupInfo with name="Level" id=currentGroupId %}
        {% set currentUserLevel = userGroupInfo %} {# 获取实际用户等级 #}
    {% endif %}
    
  2. Get the reading level of the current content: For the specific content being accessed (such as an article detail page), we can usearchiveDetailTag to get the "ReadLevel" (reading level) set for the content.

    {% archiveDetail contentReadLevel with name="ReadLevel" %}
    {# contentReadLevel 现在存储着当前内容的阅读等级 #}
    
  3. According to the level comparison, condition display is implemented.Now, we have the current user's level and the reading level of the content, so we can useiflogical judgment tags to decide how to display the content.

    On the article detail page, we need to determine whether the user has the right to view the full content of the article:

    {% if currentUserLevel >= contentReadLevel %}
        {# 用户等级满足,显示完整内容 #}
        {% archiveDetail articleContent with name="Content" %}
        {{ articleContent|safe }}
    {% else %}
        {# 用户等级不足,显示提示信息或部分内容 #}
        <div class="restricted-content-message">
            <p>此内容为付费或会员专属,您的当前等级不足,请<a href="/login">登录</a>或<a href="/upgrade-vip">升级VIP</a>查看。</p>
            {# 也可以显示一些简介或摘要 #}
            {% archiveDetail articleDescription with name="Description" %}
            <p>{{ articleDescription }}</p>
        </div>
    {% endif %}
    

Similarly, on the content list page, although we may not be able to directly inarchiveListFilter out content that does not meet the permissions, but you can iterate over each article and make permission judgments for each article, then decide whether to display the article title and link or display a 'VIP Exclusive' prompt.

For example, in an article list: “`twig {% archiveList archives with type=“page” limit=“10” %}

{% for item in archives %}
    <li>
        {% if currentUserLevel >= item.ReadLevel %}
            {# 用户等级满足,显示文章标题和链接 #}
            <a href="{{item.Link}}">{{item.Title}}</a>
            <p>{{item.Description}}</p>