Something went wrong. Try again.
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576<?php
declare(strict_types=1);
namespace Wotnak\Atproto\Auth\Permission;
/** * Represents granular repository operation permission. * * @api */final readonly class RepoPermission implements PermissionInterface {
private const ACTIONS = ['create', 'update', 'delete'];
/** * @param list<string> $collections * @param list<string> $actions */ public function __construct( public array $collections, public array $actions = [], ) { PermissionCodec::assertNsidOrWildcardList($collections, 'Repository permission collections'); if (\count($actions) !== \count(\array_unique($actions))) { throw new \InvalidArgumentException('Repository permission actions must be unique.'); } foreach ($actions as $action) { if (!\in_array($action, self::ACTIONS, TRUE)) { throw new \InvalidArgumentException('Repository permission actions must be create, update, or delete.'); } } }
/** * Returns the permission resource identifier. */ public function resource(): string { return 'repo'; }
/** * Returns the canonical OAuth scope. */ public function toScope(): string { $positional = \count($this->collections) === 1 ? $this->collections[0] : NULL; $parameters = []; if ($positional === NULL) { $parameters['collection'] = $this->collections; } if ($this->actions !== []) { $parameters['action'] = $this->actions; } return PermissionCodec::encode('repo', $positional, $parameters); }
/** * Returns the Lexicon permission declaration. */ public function toLexiconPermission(): array { if ($this->collections === ['*']) { throw new \LogicException('Repository wildcards are not allowed inside permission sets.'); } $permission = [ 'type' => 'permission', 'resource' => 'repo', 'collection' => $this->collections, ]; if ($this->actions !== []) { $permission['action'] = $this->actions; } return $permission; }
}