In website operations, displaying different content or functions based on the user's identity or permissions is a very common and important requirement.Whether it is to provide exclusive benefits for VIP members, hide internal materials, or customize the operation interface according to the user role, precise content control can significantly improve user experience and website operational efficiency.The AnQiCMS (AnQiCMS) can help us easily achieve this goal with its flexible user group management and powerful template engine.

Understanding the user group and permission system of Anqi CMS

The security CMS classifies users into different "user groups" and allows defining unique permission levels for each user group.This is like dividing your website visitors into different 'identity circles', such as ordinary visitors, registered members, VIP members, internal staff, and so on.Each circle member can have different content access rights or feature usage rights.

The built-in 'User Group Management and VIP System' is the foundation to achieve this goal.Through it, we can create, edit user groups, and set their levels (Level), which is a key basis for conditional judgments in the template.

Core idea: Condition judgment through template tags

To display or hide content on the website front-end based on user group permissions, we mainly rely on the template tag function of Anqi CMS. The template engine of Anqi CMS supports syntax similar to Django, and we can make use ofifEnglish label for logical judgment to check the current user's status and the attributes of the user group they belong to, thereby deciding which content should be displayed and which should be hidden.

This process can be summarized into the following steps:

  1. Get the information of the currently logged-in user.
  2. Get the detailed information of the user group to which the user belongs by user ID.
  3. UtilizeifLabel, judgment based on the ID or level of the user group.

How to get user and user group information

AutoCMS provides a series of convenient template tags for obtaining the current user and user group data on the front-end page.

Firstly, we need to retrieve the information of the currently logged-in user. This can be done byuserDetailTags:

{% userDetail currentUser %}
    {# currentUser变量现在包含了当前登录用户的详细信息 #}
{% enduserDetail %}

Here,currentUserit will become an object, if the user is logged in, it will contain the user'sId(UserID),UserName(Username),GroupId(User Group ID)等信息。If the user is not logged in,currentUser.Idit will be0.

Next, based on the user's group information obtained,GroupIdWe can further obtain the detailed information of the user group, such as the name and the most important "Level". This requires usinguserGroupDetailTags:

{% userDetail currentUser %}
    {% if currentUser.Id %} {# 确保用户已登录 #}
        {% userGroupDetail currentGroup with id=currentUser.GroupId %}
            {# currentGroup变量现在包含了当前用户所属用户组的详细信息 #}
        {% enduserGroupDetail %}
    {% endif %}
{% enduserDetail %}

currentGroupThe object will include the user group'sId/Title(Name),Description(Introduction) as well asLevel(Level).LevelIs a numerical value, usually the larger the number, the higher the authority, which is a very practical attribute for authority judgment.

Practice Session: Show/Hide Content Based on Permissions

With the data provided by the above tags, we can flexibly use them in the templateiffor logical judgment tags.

Scene one: Determine if the user is logged in

This is the most basic permission control, for example, allowing logged-in users to see the message 'Welcome back' and unlogged-in users to see the prompt 'Please log in/register'.

{% userDetail currentUser %}
    {% if currentUser.Id %}
        <p>欢迎回来,<strong>{{ currentUser.UserName }}</strong>!</p>
        <a href="/user/profile">个人中心</a> | <a href="/logout">退出登录</a>
    {% else %}
        <p>您尚未登录。请<a href="/login">登录</a>或<a href="/register">注册</a>。</p>
    {% endif %}
{% enduserDetail %}

Scene two: Display specific content or features based on user group ID

If your website has multiple distinct user groups (for example, the general member group ID is 1, the VIP member group ID is 2), you can directly useGroupIdto determine.

{% userDetail currentUser %}
    {% if currentUser.Id %}
        {% if currentUser.GroupId == 2 %} {# 假设用户组ID为2是VIP会员 #}
            <div class="vip-exclusive-content">
                <p>尊贵的VIP会员,这是您专属的最新报告!</p>
                <button>下载VIP报告</button>
            </div>
        {% elif currentUser.GroupId == 1 %} {# 假设用户组ID为1是普通会员 #}
            <div class="regular-member-content">
                <p>普通会员,您可以查看我们的公开内容。</p>
            </div>
        {% else %}
            {# 其他用户组或默认情况 #}
            <p>您的用户组无权查看此内容。</p>
        {% endif %}
    {% else %}
        <p>请登录并升级为VIP会员以查看此内容。</p>
    {% endif %}
{% enduserDetail %}

Scene three: Display or hide content based on user group level (Level)

UseLevelPerforming judgment is more flexible, especially when your user group system is more complex, or when you want different levels of users to unlock more features gradually.For example, we can set level 1 as a general member, and level 5 as a senior member.

{% userDetail currentUser %}
    {% if currentUser.Id %}
        {% userGroupDetail currentGroup with id=currentUser.GroupId %}
            {% if currentGroup.Level >= 5 %} {# 等级5及以上可查看 #}
                <div class="premium-features">
                    <h3>高级功能区</h3>
                    <p>作为高级会员,您拥有所有网站功能的完整权限。</p>
                    <a href="/dashboard/full-analytics">访问完整数据分析</a>
                </div>
            {% elif currentGroup.Level >= 2 %} {# 等级2及以上可查看 #}
                <div class="standard-features">
                    <h3>标准功能区</h3>
                    <p>作为注册会员,您可以访问大部分核心功能。</p>
                    <a href="/dashboard/basic-analytics">访问基础数据分析</a>
                </div>
            {% else %}
                <p>您的会员等级不足,请升级以解锁更多功能。</p>
            {% endif %}
        {% enduserGroupDetail %}
    {% else %}
        <p>请登录以查看会员专属功能。</p>
    {% endif %}
{% enduserDetail %}

This logic can be applied to any HTML element, whether it's an entire content block, a navigation menu item, or an operation button, it can achieve fine-grained display control.

Summary

The user group management function provided by Anqi CMS and the flexible template tag system lay a solid foundation for us to refine the operation of website content and features. By skillfully utilizinguserDetail/userGroupDetailandifTags such as these, you can provide highly personalized browsing experiences for different user groups, whether it's promoting VIP services, managing internal information, or simplifying the user interface, you can handle it with ease.This content distribution strategy not only enhances user satisfaction but also contributes to the commercial goals of the website.


Common Questions (FAQ)

Q1: If the user is not logged in,userDetailWhat label will be returned? How can I judge?A1: If the user is not logged in,userDetail