Code Explainers

Code explainers tagged #authorization

go
package middleware
 
import (
	"net/http"

Per-plan export limits in Gin middleware

middleware rate-limiting authorization
Intermediate 7 steps
ruby
class AdminConstraint
  def self.matches?(request)
    new(request).matches?
  end

How routing constraints gate admin routes in Rails

routing authorization constraints
Intermediate 8 steps
python
from fastapi import Depends, FastAPI, Header, HTTPException, status
from sqlalchemy import select
from sqlalchemy.ext.asyncio import AsyncSession
 

Chaining tenant dependencies in FastAPI

dependency injection multi-tenancy authentication
Intermediate 7 steps
php
<?php
 
namespace App\Http\Requests;
 

Validating nested order payloads in Laravel

validation authorization nested-data
Intermediate 8 steps
python
from fastapi import APIRouter, Depends, HTTPException, status
 
from app.config import settings
from app.dependencies import get_current_user

Gating a FastAPI route behind a feature flag

feature-flags dependency-injection authorization
Intermediate 7 steps
javascript
const express = require('express');
const createError = require('http-errors');
const { Article } = require('../models');
 

Sharing route logic with router.param in Express

middleware authorization dry
Intermediate 10 steps
php
<?php
 
namespace App\Http\Controllers;
 

How a soft-delete restore endpoint works in Laravel

soft-deletes authorization single-action-controller
Intermediate 5 steps
ruby
class Admin::AccountsController < Admin::BaseController
  before_action :set_account, only: :destroy
 
  def destroy

A guarded destroy action in Rails

controllers callbacks transactions
Intermediate 8 steps
php
<?php
 
namespace App\Services;
 

How user impersonation works in Laravel

authentication authorization session
Intermediate 8 steps
python
import secrets
 
from fastapi import Depends, FastAPI, HTTPException, Security, status
from fastapi.security import APIKeyHeader

API key authentication as a FastAPI dependency

authentication dependency-injection api-keys
Intermediate 8 steps
php
class OrderReceiptController extends Controller
{
    public function store(Request $request, Order $order)
    {

Handling receipt uploads in a Laravel controller

file-upload validation authorization
Intermediate 5 steps
python
from functools import wraps
 
from flask import Blueprint, abort, jsonify
from flask_login import current_user, login_required

Building an admin-only decorator in Flask

decorators authorization access-control
Intermediate 7 steps
php
<?php
 
namespace App\Providers;
 

Defining authorization gates in Laravel

authorization gates service-provider
Intermediate 6 steps
go
package middleware
 
import (
	"net/http"

Role-based access control middleware in Gin

middleware authorization closures
Intermediate 7 steps
javascript
const ROLE_PERMISSIONS = {
  admin: ['users:read', 'users:write', 'billing:read', 'billing:write'],
  manager: ['users:read', 'billing:read'],
  member: ['users:read'],

Role-based permissions middleware in Express

authorization middleware rbac
Intermediate 9 steps
php
<?php
 
namespace App\Broadcasting;
 

Authorizing presence channels in Laravel

broadcasting authorization presence-channels
Intermediate 3 steps
php
<?php
 
namespace App\Http\Controllers;
 

Broadcasting typing indicators in Laravel

websockets authorization presence-channels
Intermediate 9 steps
typescript
import {
  CanActivate,
  ExecutionContext,
  Injectable,

Role-based access with a NestJS guard

authorization guards decorators
Intermediate 6 steps