In website operations, providing differentiated content access permissions and display effects for different users is crucial for improving user experience, realizing content monetization, and even internal management efficiency.The powerful user group management function of AnQiCMS was born to solve this pain point, allowing us to flexibly divide user groups and finely control the content they can see and operate.

Understanding AnQiCMS user group management

In AnQiCMS, user groups can be understood as tags that classify website visitors, each tag is associated with a series of preset permissions.For example, you can create different user groups such as 'General Visitor', 'Registered Member', 'VIP Member', 'Internal Staff', and so on.The purpose of this grouping is to better manage and distinguish user identities, thereby providing customized services.

Usually, you can find the "User Group Management" entry in the "Administrator Management" or similar user settings area of the AnQiCMS backend.Here, you can create new user groups, specify a unique name for each user group, set permission levels (which is very useful for controlling content access later, such as VIP member levels being higher than regular members), and even define some additional user group settings.After creating a user group, you can manually assign users to the corresponding group, or automatically assign users to specific groups through the registration process, paid subscriptions, and other methods.

How to control access to specific content for different users

The core of AnQiCMS implementing content access control lies in the combination of front-end template judgment logic and background user group configuration.This means that when a user visits the page, the website will dynamically decide what content to display based on their login status and user group.

AnQiCMS has provided a series of flexible template tags, allowing you to easily implement these judgments in templates. Among them,userDetailThe tag can help you get detailed information about the current user, including their group ID;userGroupDetailThe tag can retrieve the detailed settings of the user group based on the user group ID or level, such as the permission level. CombinedifLogic judgment tags, we can build a powerful content access control system.

Scenario one: VIP exclusive content

Assuming you have some high-quality articles or exclusive download resources on your website, you only want VIP members to have full access.You can set a 'reading level' or 'access level' for this content.

When publishing or editing content, AnQiCMS usually allows you to set aReadLevel(reading level). For example, a VIP exclusive article'sReadLevelMay be set to “2” (assuming the ordinary member level is 1, and the VIP member level is 2).

You can design it like this in the front-end template (such as the article detail page):

  1. First, throughuserDetailTag to obtain the user group ID of the currently logged-in user.
  2. Then, useuserGroupDetailTag, to further obtain the user group of the user group ID obtained.Level(Permission level).
  3. Finally, useifTags will link user permission levels with contentReadLevel.

If the user's permission level is lower than the content reading level, only the summary of the content will be displayed, and prompt the user to upgrade to VIP; if the user's permission level meets the requirements, the full content will be displayed.

A simplified but practical template logic might look something like this:

{% userDetail currentUser with name="Id" %} {# 获取当前用户的ID,如果未登录则为0 #}
{% set userLevel = 0 %} {# 默认访客等级为0 #}

{% if currentUser > 0 %} {# 如果用户已登录 #}
    {% userDetail currentGroupId with name="GroupId" id=currentUser %} {# 获取已登录用户的用户组ID #}
    {% if currentGroupId > 0 %} {# 确保用户组ID有效 #}
        {% userGroupDetail currentGroupLevel with name="Level" id=currentGroupId %} {# 获取用户组的等级 #}
        {% set userLevel = currentGroupLevel %}
    {% endif %}
{% endif %}

{% archiveDetail currentArchive with name="Id" %} {# 获取当前文档的ID #}
{% if currentArchive > 0 %}
    {% archiveDetail archiveReadLevel with name="ReadLevel" id=currentArchive %} {# 获取当前文档的阅读等级 #}

    {% if userLevel >= archiveReadLevel %} {# 如果用户等级高于或等于内容阅读等级 #}
        <h3>{{ archive.Title }}</h3>
        <div>
            {# 显示完整文章内容 #}
            {% archiveDetail with name="Content" %}|safe
        </div>
    {% else %}
        <h3>{{ archive.Title }}</h3>
        <div>
            {# 显示文章摘要 #}
            {% archiveDetail with name="Description" %}
            <p>此为VIP专属内容,请<a href="/login">登录</a>或<a href="/vip-upgrade">升级VIP</a>查看完整版。</p>
        </div>
    {% endif %}
{% else %}
    <p>抱歉,未找到相关内容。</p>
{% endif %}

In this way, AnQiCMS allows you to easily control the visibility of content at the template level without writing complex backend code.

Scene two: Control the display range of content

You can also control the display range of content based on user groups, in addition to completely restricting access.For example, on some article list pages, you can allow ordinary users to see the title and summary, while registered members can see more details, such as thumbnails or publication time.

This can also be done inarchiveListorcategoryListthe loop of list tags, combined with the above user level judgment to achieve.

{% userDetail currentUser with name="Id" %} {# 获取当前用户的ID #}
{% set userLevel = 0 %}
{% if currentUser > 0 %}
    {% userDetail currentGroupId with name="GroupId" id=currentUser %}
    {% if currentGroupId > 0 %}
        {% userGroupDetail currentGroupLevel with name="Level" id=currentGroupId %}
        {% set userLevel = currentGroupLevel %}
    {% endif %}
{% endif %}

{% archiveList archives with type="page" limit="10" %}
    {% for item in archives %}
        <li>
            <a href="{{item.Link}}">
                <h5>{{item.Title}}</h5>
                <div>{{item.Description}}</div>
                {% if userLevel >= 1 %} {# 假设注册会员等级为1 #}
                    <div>
                        <span>{{stampToDate(item.CreatedTime, "2006-01-02")}}</span>
                        <span>{{item.Views}} 阅读</span>
                    </div>
                {% endif %}
            </a>
        </li>
    {% endfor %}
{% endarchiveList %}

Scene three: Custom navigation menu or function

User group permissions can control the visibility of content and can also be used to customize the navigation menu or function buttons of the website. For example, you may want to internally