How to determine if 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 achieving refined content display in a content management system.Especially for systems like AnQiCMS, which pay attention to enterprise-level applications and user experience, dynamically presenting content based on the user's login status or their permission group is a core strategy to enhance website personalization, achieve content monetization, and build a member system.Today, let's delve into how to skillfully use the powerful functions in 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 operation in English.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 itself has the ability to group users, set permission levels, and support paid content.While at the template level, AnQiCMS uses syntax similar to Django's template engine, which provides us with an intuitive and powerful tool for conditional judgments and data display.

AnQiCMS template judgment logic: core syntax

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 templates in a way that is similar to writing simple programsifThe statement controls the display of content.

Determine if the user has logged in

First, the most basic requirement is to determine if the user is logged in. In the template rendering environment of AnQiCMS, after the user successfully logs in, the system usually injects a template containing the current user's information (for exampleuserThe global variable of the object. We can determine the login status by checking whether the object exists or if its key attributes (such as user ID) are valid.userThe login status can be judged by checking whether the object exists or if its key attributes (such as user ID) are valid.

Assuming when the user logs in, there will be auserobject, and this object has aIdproperty. 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 %}Check in.user.IdDoes it exist and is not empty.If present, it indicates that the user is logged in and displays welcome messages and personal center links; otherwise, it displays login/register prompts.This method is concise and clear, and is a commonly used means to judge the login status.

If you want to get more detailed information about the currently logged-in user, such as the user's avatar, user group ID, and so on, you can combineuserDetailtags to get. It should be noted that,userDetailThe label usually needs a user ID as a parameter. We can passuser.Idit:

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

In this way,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's group or their VIP level.AnQiCMS built-in “User Group Management and VIP System”, which means each user will have a user group ID.

Firstly, we need touserDetailLabel to get the user group ID of the currently logged-in userGroupId)。Then, we can directly judge thisGroupIdor useuserGroupDetailLabel to get user group details (such asLevelorTitle) to make the judgment more flexible.

Example 1: Directly judge according to the user group ID.

Suppose we have configured the user group ID 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: Determine user group level by level.

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

{% 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 %}

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

Actual application scenarios and **practice

  1. Members-only content area:The website can set up a "Member Center" or "VIP Zone", where all articles, videos, and download resources are open only to specific user groups.On the list page or detail page, use the above judgment logic to determine whether to display the content summary and 'Login Visible/VIP Visible' prompt or directly display the full content.
  2. Paid content preview: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 purchase 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 non-ad 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 parallel:Judgment in templates is mainly used forControl of front-end content display, rather thanThe fundamental security of data accessThe true 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,