post-image

Have you ever wanted to build an application where users have different levels of access to information? Whether it’s a social platform where users can control who sees their profiles, a corporate dashboard with role-based visibility, or a multi-tenant SaaS application, implementing a robust visibility system is crucial for both security and user experience. 

In this blog, we will explore multiple ways to implement a visibility system in a REST API and React application. We’ll discuss their advantages and disadvantages and determine the best approach based on performance, scalability, and multi-tenant support. 

Managing visibility in a modern web application involves handling both backend-level access control and frontend-level UI restrictions. 

1.Backend Visibility Control

Approach 1: Role-Based Access Control (RBAC) 

RBAC is a widely used method where users are assigned roles, and each role has permissions to access specific resources. 

Implementation Example: 

  1. Define roles and permissions in a database table. 
  2. Assign roles to users. 
  3. Use middleware to filter API responses based on roles. 

Pros: 

Cons: 

Approach 2: Attribute-Based Access Control (ABAC)

ABAC extends RBAC by allowing policies to consider attributes of the user, resource, and environment. 

Implementation Example: 

Pros: 

Cons: 

Approach 3: Query-Based Filtering 

Instead of blocking requests, the backend filters the data returned based on user permissions. 

Managing User-To-User (or any other Resource) Visibility With Table in MSSQL.

For managing which users can see other users in the system, we can create a UserVisibility table in MSSQL. 

Schema Definition: 

And use it on each API call to determine whether user has visibility on a resource. 

To improve performance, we can modify the UserVisibility table to allow a wildcard (*) for cases where a user should see all others. Instead of storing multiple rows for each user-to-user visibility rule, we introduce a special visibleUserId value (e.g., -1 or *) to indicate full visibility.

Performance Benefits: 

Also, to improve performance, we can do:

Caching: Store visibility rules in an in-memory cache (e.g., Redis) to avoid frequent database hits. 

Partitioning: For multi-tenant architectures, partitioning the UserVisibility table by tenant ID can improve lookup performance. 

Materialized Views: Precompute and store frequently accessed visibility relationships to speed up queries. 

Pre-filtering at Query Level: Instead of processing visibility rules in loops, we can filter out non-visible records at the database query stage itself, reducing the processing overhead at the application layer. 

Implementation Example: 

Pros: 

Cons: 

 

2. Frontend Visibility Control 

Approach 1: Role-Based Rendering 

The UI conditionally renders components based on the user’s role.

Pros: 

Cons: 

Approach 2: Server-Defined UI Configuration 

The backend sends a configuration object defining what UI elements should be displayed. 

Example: 

Pros: 

Cons: 

Approach 3: Client-Side Feature Flags 

Feature flags stored in a state management system (e.g., Redux) control UI visibility. 

Pros: 

Cons: 

 

3. Comparison of Approaches

 

4. Common Misconceptions and Security Risks

Misconception 1: Frontend-Based Security is Enough 

Many developers assume that hiding UI elements in React is enough to restrict access. However, attackers can easily bypass this by making direct API requests using tools like Postman or browser dev tools. 

Misconception 2: Relying Solely on Role-Based Access Control (RBAC)

RBAC is useful but lacks flexibility when users need different levels of visibility beyond predefined roles. 

Misconception 3: Assuming Database Queries Always Enforce Visibility

A common mistake is exposing records that should be hidden due to missing query filters. 

Misconception 4: Storing Visibility Rules in the Frontend 

Using client-side feature flags or local storage for visibility settings can be manipulated by attackers. 

Misconception 5: Ignoring API Rate Limiting and Logging 

Even with proper visibility controls, APIs can be exploited via brute-force enumeration.

 

By carefully designing a visibility system, developers can ensure that users only see what they should, improving both security and user experience.

Have any questions or alternative approaches? Share them in the comments! 

 

Article author: Aleksandar Ćirić