How to determine whether a user is logged in or has specific permissions to display private content in AnQiCMS templates?

As an experienced website operations expert, I am well aware of the importance of implementing fine-grained content display in content management systems.Especially for systems like AnQiCMS that focus on enterprise-level applications and user experience, dynamically presenting content based on the user's login status or their permission group is a core strategy for enhancing website personalization, achieving content monetization, and building a membership system.Today, let's delve into how to skillfully use the powerful functions of the AnQiCMS template to achieve this goal.

AnQiCMS with its high-performance architecture based on the Go language and flexible permission control mechanism provides a solid foundation for content operations.In its project advantages, we clearly see the two core functions of "user group management and VIP system" and "flexible permission control mechanism".This means that the system inherently has the ability to group users, set permission levels, and support paid content.On the template level, AnQiCMS uses a syntax similar to the Django template engine, which provides us with a直观且强大的tool for conditional judgments and data display.

The core syntax of AnQiCMS template judgment logic

In AnQiCMS template files, all logical judgments and data calls follow a specific syntax. Variable output uses double curly braces{{ 变量名 }}While conditional judgments, loop controls, and other logical tags use single curly braces and percent signs{% 标签名 %}This means that we can write simple programs likeifstatements to control the display of content in templates.

determine if the user is logged in

Firstly, the most basic requirement is to determine whether the user is already logged in. In the template rendering environment of AnQiCMS, after the user successfully logs in, the system usually injects a template with the current user information (such asuserThe global variable of an object. We can determine this by checkinguserwhether the object exists or its key properties (such as user ID) are valid to determine the login status.

Suppose when a user logs in, a template will have oneuserobject, and the object has oneIdattribute. Then, we can judge like this:

{% if user.Id %}
    <!-- 用户已登录时显示的内容 -->
    <p>欢迎回来,{{ user.UserName }}!</p>
    <a href="/user/profile">查看个人中心</a>
{% else %}
    <!-- 用户未登录时显示的内容 -->
    <p>您尚未登录,请先登录或注册。</p>
    <a href="/user/login">登录</a> | <a href="/user/register">注册</a>
{% endif %}

In this piece of code, we use{% if user.Id %}To checkuser.IdDoes it exist and is not empty. If it exists, it means the user is logged in, and the welcome message and personal center link are displayed;On the contrary, it displays the login/register prompt. This method is concise and clear, and is a common means of judging the login status.

If you wish to obtain more detailed information about the currently logged-in user, such as the user's avatar, the user group ID, etc., you can combineuserDetailtags to obtain. It should be noted that,userDetailTags usually require a user ID as a parameter. We can pass it:user.Idto it:

{% if user.Id %}
    {% userDetail currentUser with id=user.Id %}
    <p>欢迎回来,尊贵的 {{ currentUser.UserName }}!</p>
    <img src="{{ currentUser.AvatarURL }}" alt="{{ currentUser.UserName }} 的头像">
    <!-- 其他用户信息 -->
{% else %}
    <p>您尚未登录。</p>
{% endif %}

By this means,currentUserThe variable contains the detailed information of the currently logged-in user, and we can flexibly display it on the page.

Determine if the user has a specific permission or user group

It is usually not enough to just judge the login status, often we need to display different content based on the user group the user belongs to or their VIP level.AnQiCMS has built-in 'User Group Management and VIP System', which means each user will have a user group ID associated with them.

First, we need to go throughuserDetailTag to obtain the user group ID of the currently logged-in user (GroupId)。Then, we can use thisGroupIdDirectly make a judgment or useuserGroupDetailLabel to obtain user group details (such asLevelorTitle), and then make a more flexible judgment.

Example one: Directly judge based on the user group ID

Assuming we have configured the user group ID in the background as2is a VIP user group.

{% if user.Id %}
    {% userDetail currentUser with id=user.Id %}
    {% if currentUser.GroupId == 2 %}
        <!-- VIP用户专属内容 -->
        <p>这是VIP会员才能看到的独家内容!</p>
        <button>下载VIP资料</button>
    {% else %}
        <!-- 普通用户或非VIP会员看到的内容 -->
        <p>升级为VIP会员,解锁更多精彩内容!</p>
        <a href="/vip/upgrade">立即升级</a>
    {% endif %}
{% else %}
    <p>请先登录查看内容。</p>
{% endif %}

Example two: Judgment based on user group level

If your VIP system is tiered, for example, tier5Represents a VIP. We can first get the user group details, and then check itsLevelProperty.

{% if user.Id %}
    {% userDetail currentUser with id=user.Id %}
    {% if currentUser.GroupId %}
        {% userGroupDetail currentGroup with id=currentUser.GroupId %}
        {% if currentGroup.Level >= 5 %}
            <!-- 高级VIP专属内容 -->
            <p>恭喜您,已是高级VIP会员,尊享所有特权!</p>
        {% elif currentGroup.Level >= 1 %}
            <!-- 普通VIP专属内容 -->
            <p>您是普通VIP会员,可访问部分特权内容。</p>
        {% else %}
            <!-- 非VIP用户 -->
            <p>成为VIP会员,享受专属服务!</p>
        {% endif %}
    {% else %}
        <p>您未分配用户组,请联系管理员。</p>
    {% endif %}
{% else %}
    <p>登录后可查看您的会员状态。</p>
{% endif %}

In this hierarchical judgment method, we can implement multi-level content access control based on the user group ID or a more flexible level system.

Application scenarios and **practice

  1. Member exclusive content area:The website can set up a "Member Center" or "VIP Zone", where all articles, videos, and download resources are only open to a specific user group.On the list page or detail page, decide through the above judgment logic whether to display the content summary and 'Login Visible/VIP Visible' prompts, or to display the full content directly.
  2. Preview of paid content:For content that needs to be monetized, part of the content can be displayed on the detail page as a preview, and then the remaining full content can be displayed based on the user group, and an entry for purchasing or upgrading VIP can be provided.
  3. Personalized recommendations and ad display:According to the user's VIP level, the priority of recommended content can be adjusted, or more ads can be shown to non-VIP users, while ad-free or higher-quality customized content can be shown to VIP users.

When using these judgment logic, there are several practices that need to be remembered:

  • Front-end display and back-end security are parallel:Judgments in the template are mainly used forControl of front-end content display, rather thanThe fundamental security of data access.Real data access permissions (such as API interfaces, file downloads) must be strictly verified on the backend to prevent users from bypassing the frontend judgment and directly accessing private resources.
  • Clear prompt information:Whether the user meets the conditions or not,