In the operation of daily websites, we often encounter such needs: some content is intended for everyone to see, while some is only open to specific members, or different information is displayed to users of different levels.AnQiCMS provides powerful user group permission management features, allowing us to flexibly control the visibility of website content and achieve 'thousand faces for a thousand people' in 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 feature, these contents cannot effectively differentiate the audience, either all being publicly available will devalue the paid content, or all being hidden will result in the loss of potential users.The user group management and VIP system of Anqi CMS is designed to address this pain point, helping us to realize content monetization, enhance user stickiness, and provide a personalized browsing experience.

How does AnQi CMS identify user and content permissions?

The AnQi CMS cleverly constructs a content permission system through the core concepts of 'user group levels' and 'content reading levels'.

  1. User group levelsIn the AnQi CMS backend, we can create different user groups, such as 'Visitor', 'Registered User', 'VIP Member', 'Senior VIP', and so on.Each user group can be assigned a specific 'level', usually a number, with higher levels representing greater permissions.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, the security CMS allows us to set a 'reading level' for this content.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 in full.

In this way, the system automatically compares the user's level with the content 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 backend management interface of AnQi CMS is a relatively intuitive process.

When creating or editing a document (whether an article or a product), you will find an option named "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, accessible only to VIP members; while an internal document may be set to level 10, accessible only to the highest-level users.

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

The frontend template is where users ultimately see the website content and is also the key to dynamically displaying content based on user group permissions. The template tag system of Anqi CMS, especiallyuserDetail/userGroupDetailandarchiveDetailThese tags make this process very flexible.

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

  1. Get the current logged-in user's levelFirstly, we need to know who the user is accessing the current website and what level of user group they belong to. In the template of Anqi CMS, we can achieve this by...userDetailTag retrieves the basic information of the current user, including the ID of the user group he belongs to. Then, it usesuserGroupDetailTags, retrieve the specific level of the user group according to the user group ID. If the user is not logged in, we can consider them as the default 'Visitor' level (e.g., 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 usearchiveDetailLabel to get the "ReadLevel" (Reading Level) set for the content.

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

    In the article detail page, suppose 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, in the content list page, although we may not be able to directly inarchiveListFilter out content that does not meet the permissions in the tags, but you can check the permissions for each article while iterating through each article, and then decide whether to display the article title and link, or show a "VIP Exclusive" prompt.

For example, in a list of articles: “`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>