In website operation, displaying or hiding specific content based on user identity is a common strategy, which can help us meet various business needs such as exclusive membership, paid content, and internal information distribution.AnQiCMS provides flexible user group management functions, combined with its powerful template engine, it can easily implement content control based on user group permissions.
Understand the user group mechanism of AnQiCMS
Core Mechanism: Condition judgment in the template
Implement content display or hide based on user groups, mainly depending on the conditional judgment function of AnQiCMS template language. Its syntax is similar to common Web template engines, through{% if 条件 %}Tag to determine whether a user meets specific conditions and use it to control the rendering of its internal content.
The basic logic can be summarized as: First, obtain the user group information of the current visiting user, and then compare this information with the target user group we set.If a match is successful, the content will be displayed; otherwise, the alternative content will be hidden or displayed.
Get the current user's permission information.
In AnQiCMS template, if the user is already logged in, there is usually a globally accessibleuserobject that contains the basic information of the currently logged-in user, such asuser.Id(User ID)、user.UserName(Username),user.GroupId(User Group ID) anduser.Level(User Group Level)。
We can simply{% if user %}The statement to determine whether the user is logged in. Once confirmed that the user is logged in, further checks can be made on the user's group ID or level.
Retrieve detailed information of a specific user group
Sometimes, we may not want to hardcode the user group's ID or level directly in the template, but rather want to make judgments based on the user group's name or other attributes, or need to display a detailed description of a user group. At this time, you can use{% userGroupDetail %}Label to retrieve complete information for a specified user group.
For example, if we know the user group ID of VIP members is3,"{% userGroupDetail vipGroup with id=3 %}to retrieve all information about the user group,vipGroup.Title(User group name),vipGroup.Description(User group introduction)etc. This is very useful when building more dynamic permission logic.
Hands-on Practice: Show or hide content based on user group
Now, let's look at some specific application scenarios, demonstrating how to display or hide content in AnQiCMS templates based on user group permissions.
Scene one: Display VIP exclusive content or identification
Suppose our website has a "VIP Exclusive" identification, and we only want the VIP user group (suppose itsGroupIdresponse for3) to see it.
{# 检查用户是否登录 #}
{% if user %}
{# 检查当前用户的用户组ID是否为3(VIP组) #}
{% if user.GroupId == 3 %}
<span class="vip-badge">VIP专属特权!</span>
<p>这是只有尊贵VIP会员才能看到的内容。</p>
{% else %}
<p>升级为VIP会员,即可解锁更多专属内容!</p>
{% endif %}
{% else %}
<p>您尚未登录,请<a href="/login">登录</a>或<a href="/register">注册</a>成为会员。</p>
{% endif %}
Here, we first judgeuserwhether the object exists (i.e., whether the user is logged in). If logged in, further judgment is made aboutGroupIdIs it3. Users who do not meet the conditions will see an upgrade prompt, and unlogged-in users will see a login/register prompt.
Scenario two: control content access based on the user group level.
If the user group has clear level divisions, for example,Levelthe higher the permission, the more we can control the visibility of the content based on the level. For example, onlyLevelgreater than or equal to5The advanced users can download specific files.
`twig {% if user %}
{# 检查当前用户的用户组等级是否满足条件 #}
{% if user.Level >= 5 %}
<div class="download-section">
<h3>高级资料