How does AnQi CMS display or hide specific content based on user group permissions?

Calendar 👁️ 66

In the operation of daily websites, we often encounter such needs: some content is intended to be seen by everyone, while some is only open to specific members or displays different information for users of different levels.AnQiCMS provides powerful user group permission management functions, allowing us to flexibly control the visibility of website content and achieve the 'thousand faces of content'.

Why is it necessary to display or hide content based on user groups?

Imagine that you are running a website offering a variety of services, including free basic articles, paid in-depth reports, and exclusive tutorials available only to premium members.If there is no user group permission function, these contents cannot effectively differentiate the audience, either all public will devalue the paid content, or all hidden will lose potential users.The user group management and VIP system of AnQi CMS is designed to solve this pain point, help us realize content monetization, enhance user stickiness, and provide a personalized browsing experience.

How does AnQi CMS identify user and content permissions?

AnQi CMS cleverly constructs a content permission system through the two core concepts of "user group level" and "content reading level".

  1. User group levelIn the AnQi CMS backend, we can create different user groups, such as "Visitors", "Registered Users", "VIP Members", "Senior VIP" and so on.Each user group can be assigned a specific 'level', which is usually a number, with higher levels representing greater privileges.For example, visitors may be level 0, registered users level 1, VIP members level 5, and senior VIPs level 10.

  2. Content Reading Level (ReadLevel)When we publish or edit articles, products and other content, Anqie CMS allows us to set a 'reading level' for these contents.This level determines which user groups have permission to view this content.If an article's reading level is set to 5, only users with a level of 5 or higher can read it completely.

In this way, the system automatically compares the user's level and the content's reading level when the user accesses the content, thereby deciding whether to display the full content or prompt the user that they do not have sufficient permissions.

Set content reading permissions in the background

Setting content reading permissions in the Anqi CMS backend management interface is a relatively intuitive process

When creating or editing a document (whether an article or a product), you will find an option called "Reading Level" in the "Other Parameters" section of the editing page.Here, we can assign a reading level to the current content based on its importance, value, and target audience.For example, a regular news article can be set to level 0, indicating that it is visible to everyone;A deep research report can be set to level 5, available only to VIP members;And an internal document may be set to level 10, only the highest-level users can access it.

Implement the display and hide of content in the front-end template

The front-end template is where users finally see the website content, and it is also the key to dynamically displaying content based on user group permissions. The template tag system of Anqi CMS, especiallyuserDetail/userGroupDetailandarchiveDetailThese labels make this process very flexible.

Generally, we will take the following steps to judge and display content:

  1. Get the level of the currently logged-in userFirstly, we need to know who the current visitor to the website is and what user group level they belong to. In the Anqicms template, we can do this byuserDetailLabel to get the basic information of the current user, including the ID of the user group he belongs to. Then, make use ofuserGroupDetailLabel, obtain the specific level of the user group according to the user group ID. If the user is not logged in, we can consider it as the default 'visitor' level (for example, level 0).'

    A rough logic might look like this:

    {% userDetail currentUser with name="Id" %}
    {% set userIsLoggedIn = (currentUser > 0) %}
    {% set currentUserId = currentUser %}
    
    {% set currentUserLevel = 0 %} {# 默认访客等级为0 #}
    {% if userIsLoggedIn %}
        {% userDetail currentGroupId with name="GroupId" id=currentUserId %}
        {% userGroupDetail userGroupInfo with name="Level" id=currentGroupId %}
        {% set currentUserLevel = userGroupInfo %} {# 获取实际用户等级 #}
    {% endif %}
    
  2. Get the reading level of the current content: For the specific content being accessed (such as an article detail page), we can usearchiveDetailTag to get the "ReadLevel" (reading level) set for the content.

    {% archiveDetail contentReadLevel with name="ReadLevel" %}
    {# contentReadLevel 现在存储着当前内容的阅读等级 #}
    
  3. According to the level comparison, condition display is implemented.Now, we have the current user's level and the reading level of the content, so we can useiflogical judgment tags to decide how to display the content.

    On the article detail page, we need to determine whether the user has the right to view the full content of the article:

    {% if currentUserLevel >= contentReadLevel %}
        {# 用户等级满足,显示完整内容 #}
        {% archiveDetail articleContent with name="Content" %}
        {{ articleContent|safe }}
    {% else %}
        {# 用户等级不足,显示提示信息或部分内容 #}
        <div class="restricted-content-message">
            <p>此内容为付费或会员专属,您的当前等级不足,请<a href="/login">登录</a>或<a href="/upgrade-vip">升级VIP</a>查看。</p>
            {# 也可以显示一些简介或摘要 #}
            {% archiveDetail articleDescription with name="Description" %}
            <p>{{ articleDescription }}</p>
        </div>
    {% endif %}
    

Similarly, on the content list page, although we may not be able to directly inarchiveListFilter out content that does not meet the permissions, but you can iterate over each article and make permission judgments for each article, then decide whether to display the article title and link or display a 'VIP Exclusive' prompt.

For example, in an article list: “`twig {% archiveList archives with type=“page” limit=“10” %}

{% for item in archives %}
    <li>
        {% if currentUserLevel >= item.ReadLevel %}
            {# 用户等级满足,显示文章标题和链接 #}
            <a href="{{item.Link}}">{{item.Title}}</a>
            <p>{{item.Description}}</p>

Related articles

How to dynamically display the current year or a specified date on a website?

In website operation, we often need to display some dynamic time information, such as the copyright year at the bottom of the website (such as “© 2023-Current Year”), the publication or update time of articles, or the deadline for specific events, etc.These dynamic dates not only maintain the timeliness of website information, but also improve user experience, and in some cases, are also beneficial for SEO. The AnQi CMS provides a series of flexible template tags that allow you to easily display the current year or a specified date dynamically at any location on the website.We will discuss in detail how to make use of these features.

2025-11-08

How to use the template inheritance mechanism to unify and display the website page structure?

When building a website, maintaining consistent page structure, improving development efficiency, and reducing maintenance costs are key to website operations.AnQiCMS (AnQiCMS) leverages its powerful functionality based on the Django template engine syntax to provide us with an efficient template inheritance mechanism, making it easy to unify and flexibly display the structure of web pages. ### The core concept of template inheritance: building the skeleton of a website Imagine that your website is like a house made up of different rooms. Each room (page) has its unique functions and content, but they share the same foundation

2025-11-08

How to reference and display common code snippets (such as header, footer) in AnQiCMS templates?

It is crucial to maintain consistency in page structure and code maintainability in website construction and content management.For a content management system like AnQiCMS, effectively referencing and managing common code snippets, such as the website header (Header) and footer (Footer), is the key to achieving this goal.Through AnQiCMS's flexible template mechanism, even users with little understanding of technical details can easily implement these features, thus building a website with clear structure and easy maintenance.###

2025-11-08

How to define and display temporary variables in the template?

In Anqi CMS template development, mastering how to define and display temporary variables in the template is a very practical skill.It can help us handle data more flexibly, optimize template structures, and write more concise and efficient code.Temporary variables are like the "small tags" you grab on the fly while making a page, used to temporarily store some data for later use.### Why do we need temporary variables? Imagine a scenario: you might need to get a value from a label and then perform a series of operations on it (such as truncating strings, formatting time)

2025-11-08

How to display the document list matching the keywords on the search results page?

When visitors come to your website and hope to quickly find the information they need, a highly efficient and accurate on-site search feature is particularly important.AnQiCMS (AnQiCMS) took this into consideration from the beginning of its design, providing you with a flexible and powerful content management and template system, allowing you to easily display a list of documents matching the search keywords on the search results page.Next, let's see how to implement this function together.### Learn about AnQi CMS search mechanism AnQi CMS has built-in powerful search capabilities, it will automatically index the content you publish

2025-11-08

How to prevent content collection interference code and watermark display in AnQi CMS?

Today, with the increasing richness of digital content, the value of original content becomes more and more prominent, but the problems of content plagiarism and collection that come with it also make many content creators and operators headache.AnQiCMS (AnQiCMS) fully understands this pain point, and therefore built-in strong content protection mechanisms from the very beginning of the system design, including anti-crawling interference codes and image watermark functions.These features are designed to help users effectively defend original copyright, ensure content exclusivity, and thereby enhance content value.

2025-11-08

How to dynamically display the canonical URL corresponding to the current page in the template?

In website operation, maintaining search engine friendliness is one of the key links to improving website visibility.Among them, the 'Canonical URL' plays a crucial role.It acts like a compass for search engines, telling them which page is the main version of the content, thus effectively avoiding ranking fragmentation due to content duplication or similarity.For content operators, correctly setting and displaying standard links is the foundation for ensuring that website content is efficiently crawled and indexed by search engines

2025-11-08

How to safely display HTML tags in a template without being parsed by the browser?

In website content management, we often encounter the need to display HTML code snippets, such as code examples in tutorials, and language annotations in technical articles.However, if the content containing HTML tags is directly output to the page, the browser will parse and render it, rather than displaying it as plain text.This could lead to layout errors on the page, more seriously, if unprocessed user input contains malicious scripts (such as `<script>alert('XSS')</script>`), it can trigger cross-site scripting (XSS) attacks

2025-11-08