In today's era of content explosion on the internet, how to accurately deliver content to different user groups and provide personalized browsing experiences has become the key to the success of website operations.AnQiCMS is well-versed in this field, providing operators with a powerful weapon for fine-grained user permission control at the template level with its flexible template engine and powerful permission management functions.Today, let's delve into how to巧妙组合 multiple logical conditions in AnQiCMS templates to achieve fine-grained control over user permissions.
The basics of permission control: users and user groups
In AnQiCMS, the foundation of user permission management is the 'Group Management and VIP System'.The system allows us to group users and define different permission levels for different user groups.When making permission judgments in the template, we need to first obtain the relevant information of the current user.
AnQiCMS template syntax is similar to Django, variables are passed through double curly braces{{变量}}Invoke, use single curly braces with a percentage sign for logical judgments{% if 条件 %}To determine user permissions, we usually use the following key tags:
userDetailTags:Used to obtain detailed information about the current logged-in user or a specified user. For example,{% userDetail user with name="GroupId" %}Can retrieve the user group ID of the current user,{% userDetail user with name="ExpireTime" %}may obtain VIP expiration time, etc. This information will serve as the basis for our logical judgment.userGroupDetailTags:Used to obtain detailed information about a specified user group, such as the user group level (Level) or name (Title). This is very useful when you need to judge according to the 'level' of the user group rather than a specific ID.ifLogic judgment label:This is the core of conditional combination. Its basic syntax is{% if 条件 %} ... {% elif 其他条件 %} ... {% else %} ... {% endif %}.
Combined logical conditions: the core for fine control
AnQiCMS template'sifTag supportand(Logical AND),or(Logical OR),not(Logical NOT) and other operators, as well as==(equal to),!=(Not equal),>(greater than),<(Less than),>=(Greater than or equal),<=(Less than or equal to) comparison operator. By combining these operators flexibly, we can build permission judgment logic that meets various complex scenarios.
1. A simple judgment based on user groups
The most common scenario is to display or hide content based on the user group the user belongs to. For example, only VIP members can see some exclusive content:
{# 假设用户组ID为2代表VIP会员 #}
{%- userDetail currentUser with name="GroupId" -%}
{%- if currentUser == 2 -%}
<div class="vip-exclusive-content">
恭喜您,VIP会员尊享内容!
</div>
{%- else -%}
<div class="non-vip-prompt">
开通VIP,解锁更多精彩内容!
</div>
{%- endif -%}
2. Combine user groups with specific attributes (such as VIP expiration time)
If VIP members have an expiration date limit, we need to judge both the user group and whether the VIP is expired at the same time. Assuminguser.ExpireTimeIt stores a timestamp representing the expiration time of the VIP, andsystem.CurrentTimestampIt is the current system timestamp.
{%- userDetail user with name="GroupId" -%}
{%- userDetail expireTime with name="ExpireTime" -%}
{%- system currentTimestamp with name="CurrentTimestamp" -%} {# 假设后端提供了当前时间戳 #}
{%- if user == 2 and expireTime > currentTimestamp -%}
<div class="vip-active-content">
您的VIP服务正在有效期内,尽情体验!
</div>
{%- elif user == 2 and expireTime <= currentTimestamp -%}
<div class="vip-expired-prompt">
您的VIP已过期,请及时续费。
</div>
{%- else -%}
<div class="non-vip-prompt">
开通VIP,享受更多特权!
</div>
{%- endif -%}
here,andThe operator ensures that both conditions must be true for the content to be displayed.
3. Complex combination of multiple user groups or conditions
Sometimes, content may be open to multiple user groups or accessible by meeting any one of the conditions. For example, only VIP members or specific dealer users can view certain product information:
{%- userDetail userGroupId with name="GroupId" -%}
{%- userDetail isRetailer with name="IsRetailer" -%} {# 假设isRetailer是用户的一个布尔属性 #}
{# 假设VIP用户组ID为2,经销商用户组ID为3 #}
{%- if userGroupId == 2 or userGroupId == 3 or isRetailer -%}
<div class="privileged-access">
欢迎内部用户/经销商,这是专供资料。
</div>
{%- else -%}
<div class="public-access-prompt">
您没有权限访问此内容。
</div>
{%- endif -%}
ByorAn operator, the content can be viewed as long as the user meets one of the conditions.
We can also useandandorCombined, for example: VIP users and VIPs who have not expired, or level 1 administrators.
{%- userDetail userGroupId with name="GroupId" -%}
{%- userDetail expireTime with name="ExpireTime" -%}
{%- system currentTimestamp with name="CurrentTimestamp" -%} {# 假设后端提供了当前时间戳 #}
{# 假设VIP用户组ID为2,一级管理员用户组ID为1 #}
{%- if (userGroupId == 2 and expireTime > currentTimestamp) or userGroupId == 1 -%}
<div class="special-content">
这是VIP或一级管理员才能看到的内容。
</div>
{%- else -%}
<div class="no-privilege">
抱歉,您的权限不足。
</div>
{%- endif -%}
Note that parentheses are used here()to clarify the precedence of logical operations, ensureandoperations precedeoroperations are executed.
4. Exclude specific user groups or conditions
When most users can access certain content but need to exclude a few specific groups,notOperators come into play. For example, all users can comment on articles, but users in the blacklisted user group (assuming ID 4) are prohibited from commenting:
{%- userDetail userGroupId with name="GroupId" -%}
{%- if not (userGroupId == 4) -%}
<div class="comment-form">
{# 评论表单代码 #}
欢迎您发表评论!
</div>
{%- else -%}
<div class="comment-disabled-prompt">
您暂无权限发表评论。
</div>
{%- endif -%}
Or, if the name of the blacklist user group is "blacklist user", we can combineuserGroupDetailto make a judgment:
”`twig {%- userDetail userGroupId with name=“GroupId” -%} {%- userGroupDetail userGroupTitle with name=“Title” id=userGroupId -%}
{%- if not (userGroup