Skip to content

Instantly share code, notes, and snippets.

@sasin91
Created February 11, 2020 10:26
Show Gist options
  • Save sasin91/c174e419996e1f6b26cb2978f37dc45c to your computer and use it in GitHub Desktop.
Save sasin91/c174e419996e1f6b26cb2978f37dc45c to your computer and use it in GitHub Desktop.
<?php
namespace App\Enums;
use App\User;
use BenSampo\Enum\FlaggedEnum;
use function collect;
use function is_object;
final class RBAC extends FlaggedEnum
{
// Member: 1 - 20
const ViewHome = 1 << 0;
const ViewWorkouts = 1 << 1;
const ViewWorkoutOfTheMonth = 1 << 2;
const ViewDiets = 1 << 3;
const ViewVideos = 1 << 4;
const ViewDiscounts = 1 << 5;
const ViewForum = 1 << 6;
const CreateForumThreads = 1 << 7;
const CreateForumPosts = 1 << 8;
const CreateComments = 1 << 9;
// Moderator: 21 - 60
const CreateForumCategories = 1 << 21;
const EditForumCategories = 1 << 22;
const DeleteForumCategories = 1 << 23;
const EditForumThreads = 1 << 24;
const LockForumThreads = 1 << 25;
const UnlockForumThreads = 1 << 26;
const DeleteForumThreads = 1 << 27;
const EditForumPosts = 1 << 28;
const DeleteForumPosts = 1 << 29;
const EditComments = 1 << 30;
const DeleteComments = 1 << 31;
const ViewTrashedWorkouts = 1 << 32;
const ViewUnpublishedWorkouts = 1 << 33;
// Unassigned: 100 &>
//
// Admins will by default have all permissions granted implicitly.
// Shortcuts
const Member = self::ViewHome
| self::ViewWorkouts
| self::ViewWorkoutOfTheMonth
| self::ViewDiets
| self::ViewVideos
| self::ViewDiscounts
| self::ViewForum
| self::CreateForumThreads
| self::CreateForumPosts
| self::CreateComments;
const Moderator = self::Member
| self::CreateForumCategories
| self::EditForumCategories
| self::DeleteForumCategories
| self::EditForumThreads
| self::LockForumThreads
| self::UnlockForumThreads
| self::DeleteForumThreads
| self::EditForumPosts
| self::DeleteForumPosts
| self::EditComments
| self::DeleteComments;
public static function check(User $user, ...$permissions): bool
{
return $user->permissions >= collect($permissions)->map(fn ($permission) => is_object($permission) ? $permission->value : $permission)->sum();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment