authorization

ruby
class ApplicationPolicy
  attr_reader :user, :record

  def initialize(user, record)
    @user = user
    @record = record

Enforcing Controller Authorization with a Pundit-Style Policy Object in Rails

rails authorization pundit
by codesnips 4 tabs
php
<?php

namespace App\Policies;

use App\Models\Post;
use App\Models\User;

Authorize Post Editing With a Laravel Policy and Gate the Controller

laravel authorization policies
by codesnips 3 tabs
ruby
class Current < ActiveSupport::CurrentAttributes
  attribute :user, :request_id

  def permissions
    @permissions ||= PermissionSet.new(user)
  end

Memoizing Computed User Permissions Per Request with Current Attributes in Rails

rails authorization caching
by codesnips 4 tabs
ruby
module Authorizable
  extend ActiveSupport::Concern

  class NotAuthorized < StandardError; end

  included do

Rails Policy Authorization with a before_action Concern and 403 Rendering

rails authorization controllers
by codesnips 3 tabs
php
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

Soft-Delete and Restore Blog Posts with a Trash View in Laravel

laravel eloquent soft-deletes
by codesnips 4 tabs
java
package com.example.demo.config;

import com.example.demo.security.JwtAuthenticationFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;

Security with Spring Security and JWT

java spring-security jwt
by David Kumar 3 tabs
php
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;

Comment Moderation Queue With Pending Scope and Approval Action in Laravel

laravel eloquent moderation
by codesnips 3 tabs
typescript
import jwt, { JwtPayload, SignOptions } from 'jsonwebtoken';

const SECRET = process.env.JWT_SECRET as string;
const ISSUER = 'auth.example.com';
const AUDIENCE = 'api.example.com';

Signing and Verifying JWT Access Tokens with Express Middleware

jwt express authentication
by codesnips 3 tabs
typescript
import { Body, Controller, Get, Post, UseGuards } from '@nestjs/common';
import { FeatureFlagGuard } from './feature-flag.guard';
import { RequireFeature } from './feature-flag.decorator';

@Controller('experiments')
@UseGuards(FeatureFlagGuard)

Per-Request Feature Flags in NestJS with a Custom Decorator and Resolution Guard

nestjs feature-flags guards
by codesnips 4 tabs
python
from django.conf import settings
from django.db import models
from django.utils import timezone


class InvoiceQuerySet(models.QuerySet):

Scope a Django DetailView to the Request User with get_queryset

django authorization class-based-views
by codesnips 3 tabs
php
<?php

namespace App\Policies;

use App\Models\Post;
use App\Models\User;

Laravel policies for authorization

laravel authorization policies
by Carlos Mendez 3 tabs
php
<?php

namespace App\Tenancy;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;

Enforce Multi-Tenant Isolation in Laravel with a Global Scope Bound to the Current User

laravel multi-tenancy eloquent
by codesnips 4 tabs