In the era of content explosion on the Internet, how to accurately deliver content to different user groups and provide a personalized browsing experience has become the key to the success of website operation.AnQiCMS (AnQiCMS) is well-versed in this field, providing operators with a powerful weapon for fine-grained user permission control at the template level, thanks to its flexible template engine and powerful permission management features.Today, let's delve into how to巧妙组合multiple logical conditions in AnQiCMS templates to achieve fine-grained control over user permissions.
The foundation of permission control: users and user groups
In AnQiCMS, the foundation of user permission management is 'User Group Management and VIP System'.The system allows us to group users and define different permission levels for different user groups.When performing permission judgment in the template, we need to first obtain the relevant information of the current user.
AnQiCMS's template syntax is similar to Django, variables are passed through double curly braces{{变量}}Call, use single curly braces with a percentage sign for logical judgment{% if 条件 %}. To determine user permissions, we usually use the following key tags:
userDetailTags:To retrieve the detailed information of the current logged-in user or a specified user. For example,{% userDetail user with name="GroupId" %}Can obtain the current user's user group ID,{% userDetail user with name="ExpireTime" %}can possibly obtain VIP expiration time, etc. This information will be the basis for our logical judgment.userGroupDetailTags:Used to obtain detailed information of a specified user group, such as the level of the user group (Level) or the name (TitleThis is very useful when it is necessary to judge based on the "level" of the user group rather than a specific ID.ifLogical judgment tag:This is the core for conditional combination. Its basic syntax is{% if 条件 %} ... {% elif 其他条件 %} ... {% else %} ... {% endif %}.
Combine logical conditions: the core of fine control
AnQiCMS templateiftag supportand(Logical AND),or(Logical OR),not(Logical NOT) and other operators, as well as==(equals,)!=(not equal to,)>(greater than,)<(less than,)>=(greater than or equal to,)<=(Less than or equal to) comparison operator. By flexibly combining these operators, we can construct permission judgment logic that meets various complex scenarios.
1. Simple judgment based on user groups
The most common scenario is to display or hide content based on the user group to which the user belongs. 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 there is a validity period limit for VIP members, we need to judge both the user group and whether the VIP is expired at the same time. Assuminguser.ExpireTimeThe storage is a timestamp representing the expiration time of VIP.system.CurrentTimestampIt is the current timestamp of the system.
{%- 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 multiple conditions
Sometimes, content may be accessible to multiple user groups or by meeting any 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 -%}
PassorOperator, the content can be seen as long as the user meets one of the conditions.
We can also takeandandorCombined, for example: VIP users and VIPs not expired, or first-level 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, ensuringandoperations precedeoroperation execution.
4. Exclude specific user groups or conditions
When most users can access certain content but a few specific groups need to be excluded,notThe operator comes into play. For example, all users can comment on articles, but users in the banned 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:
auto
{%- if not (userGroup in english) %}