Trongate PHP Framework Docs
Introduction
Basic Concepts
Understanding Routing
Intercepting Requests
Module Fundamentals
Database Operations
Templates
Helpers
Form Handling
Form Validation
Working With Files
Image Manipulation
Working With Dates & Times
Language Control
Authorization & Authentication
Tips And Best Practices

The Trongate Security Module

The Trongate_security module provides a centralized way to handle authorization across your application using scenarios.

Instead of scattering security checks throughout your codebase, you call one method and let it route to the appropriate handler:

This approach keeps your security logic organized, consistent, and easy to maintain.

What Are Scenarios?

A scenario is a named security context that describes what access control rules should apply.

Examples of scenarios:

  • 'admin panel' - Only administrators can access
  • 'members area' - Any logged-in member can access
  • 'read comment' - Anyone can access (public)
  • 'edit comment' - Only comment owner or admin can access
  • 'premium content' - Only premium subscribers can access

Each scenario routes to a specific module that knows how to handle that particular type of access control.

It's for you - the developer - to decide how many scenarios your application is going to have. You also, as the developer, determine what names each scenario should be given.

How It Works

The Trongate_security module uses a switch statement to delegate scenarios to appropriate modules:

The commented-out case shows how to add custom scenarios. Uncomment and modify as needed for your application.

The Default Scenario

If you don't specify a scenario, it defaults to 'admin panel':

Both delegate to trongate_administrators->make_sure_allowed(), which:

  1. Checks for a valid token with user level 1 (admin)
  2. In dev mode: auto-generates a token for convenience
  3. In production: redirects to login if no valid token
  4. Returns the token string if successful

Return Values: Flexible by Design

The method is intentionally open-ended. It can return:

  • String - The validated token
  • Boolean - true for public access, false for denied
  • Object - Complete user object with permissions
  • Array - Custom data structure with multiple values
  • Nothing - Can redirect or die instead of returning

The return type depends on what your scenario needs. You decide.

This flexibility means make_sure_allowed() can adapt to any authorization pattern your application requires.

Example: Discussion Forum

Let's build a hypothetical discussion forum accessible by the public, with special privileges for admin users and member users.

The Requirements

  • Read comments - Open to anyone (no authentication)
  • Create comments - Must be a logged-in member
  • Edit/delete comments - Must be the comment owner OR an admin

Trongate_security.php

Add forum scenarios to the switch statement:

Members.php

The members module handles complex ownership checks:

The make_sure_allowed() method name, as shown above, uses to prevent URL invocation.

For more information about these topics check out:

Forum_comments.php (Controller)

Using the scenarios in your controller:

This example assumes you have public and members_area templates created.

The $params Array

The optional $params array allows fine-grained control:

Pass any data your scenario needs to make authorization decisions.

Dev Mode Auto-Login

The trongate_administrators module includes special behavior for development:

In dev mode, the first administrator is automatically logged in. This speeds up development by eliminating repeated logins.

Production Safety: This auto-login only works when ENV === 'dev' in your config. Production deployments always require proper authentication.

Building Your Own Scenarios

To add custom scenarios:

  1. Open Trongate_security.php
  2. Add a new case to the switch statement
  3. Either handle inline OR delegate to another module
  4. Return appropriate value (token, boolean, object, etc.)

Example: Premium Content

Why Scenarios Win

  • Centralized security logic - All authorization in one place
  • Consistent patterns - Same method call everywhere
  • Easy to audit - Review security in one file
  • Flexible delegation - Complex logic lives in appropriate modules
  • Testable - Mock scenarios for unit tests

Common Patterns

Pattern 1: Public Access

Pattern 2: Simple Member Check

Pattern 3: Delegate to Module

  • Use descriptive scenario names - 'edit comment' not 'scenario_7'
  • Delegate complex logic - Keep switch statement clean, push details to modules
  • Return consistent types per scenario - Don't mix tokens and booleans for same scenario
  • Document your scenarios - Add comments explaining what each scenario protects
  • Test thoroughly - Especially ownership checks and edge cases