In website operation, displaying or hiding specific content based on user identity is a common strategy, which can help us achieve 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.
Learn about the user group mechanism of AnQiCMS
AnQiCMS comes with a comprehensive user group management and VIP system built-in.In the background, we can create different user groups and define unique permission levels for each group.This means we can have roles such as 'ordinary visitor', 'registered user', 'VIP member', 'admin', and assign different access permissions to them.Users will be assigned to the corresponding user group after registration or upgrade.The template engine uses this user group information to determine what content is visible to which users.
Core Mechanism: conditional 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, by{% if 条件 %}Tag to determine if a user meets specific conditions and use it to control the rendering of 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 have set.If the match is successful, then display the content; otherwise, hide or display the alternative content.
Get the current user's permission information
In the AnQiCMS template, if the user is already logged in, there will usually be a globally availableuserobject 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 use the simple{% if user %}To determine if the user is logged in. Once confirmed that the user is logged in, you can further check their group ID or level.
Retrieve details of a specific user group
Sometimes, we may not want to hardcode the user group ID or level directly in the template, but rather make judgments based on the user group name or other attributes, or need to display the detailed description of a user group. At this time, you can use{% userGroupDetail %}Label to get complete information for a specified user group.
For example, if we know the user group ID of VIP members is3you can use{% userGroupDetail vipGroup with id=3 %}to get all information about the user group, such asvipGroup.Title(User group name),vipGroup.Description(Group introduction) and so on. This is very useful when building more dynamic permission logic.
Practical exercise: Display or hide content based on user groups.
Now, let's look at some specific application scenarios, demonstrating how to display or hide content in the AnQiCMS template based on user group permissions.
Scene one: Display VIP exclusive content or identification
Suppose our website has a "VIP Exclusive" identification, we only want the VIP user group (assuming itsGroupIdWith3) to be able to see.
{# 检查用户是否登录 #}
{% 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., if the user is logged in). If logged in, further judge whether itGroupIdIs it3. Users who do not meet the requirements will see an upgrade prompt, and unlogged-in users will see a login/registration prompt.
Scenario two: control content access based on user group level
If the user group has a clear level division, for exampleLevelthe higher the permission, 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>高级资料