-- Create roles
CREATE ROLE readonly;
CREATE ROLE readwrite;
CREATE ROLE admin WITH LOGIN PASSWORD 'secure_password';
-- Grant permissions to roles
package com.shop.security;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.access.PermissionEvaluator;
import org.springframework.security.access.expression.method.DefaultMethodSecurityExpressionHandler;
class ApplicationPolicy
attr_reader :user, :record
def initialize(user, record)
@user = user
@record = record
const jwt = require('jsonwebtoken');
function authenticate(req, res, next) {
const header = req.headers.authorization || '';
const [scheme, token] = header.split(' ');
<?php
namespace App\Policies;
use App\Models\Article;
use App\Models\User;
<?php
namespace App\Security\Voter;
use App\Entity\Comment;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
export const PERMISSIONS = {
READ_POSTS: 'posts:read',
WRITE_POSTS: 'posts:write',
DELETE_POSTS: 'posts:delete',
MANAGE_USERS: 'users:manage',
} as const;
class ApplicationPolicy
attr_reader :user, :record
def initialize(user, record)
@user = user
@record = record
<?php
namespace App\Policies;
use App\Models\Post;
use App\Models\User;
module Authorizable
extend ActiveSupport::Concern
class NotAuthorized < StandardError; end
included do
const rolePermissions = {
guest: ['article:read'],
author: ['article:read', 'article:create', 'article:update:own'],
editor: ['article:publish', 'article:update:any'],
admin: ['user:manage', 'role:assign']
};