permissions

This commit is contained in:
2023-10-17 22:57:25 +03:00
parent 4b9e9cb4f6
commit 632684f514
29 changed files with 370 additions and 223 deletions
+30 -7
View File
@@ -22,24 +22,23 @@ final class ChannelService
public static function fromConfig(): ChannelService
{
$schemasArray = [];
if(file_exists(schemas_path())){
if (file_exists(schemas_path())) {
$schemasJson = file_get_contents(schemas_path());
$schemasArray = json_decode($schemasJson, true);
}
$schemaService = new SchemaService();
$schemasCollection = (new Collection($schemasArray))->map([$schemaService, 'fromArray']);
$schemasCollection = (new Collection($schemasArray["schemas"] ?? []))->map([$schemaService, 'fromArray']);
$channel = new Channel(
name: config("lucent.name") ?? "",
url: rtrim( config("lucent.url") ?? "", "/"),
previewTarget: rtrim( config("lucent.previewTarget") ?? "", "/"),
url: rtrim(config("lucent.url") ?? "", "/"),
previewTarget: rtrim(config("lucent.previewTarget") ?? "", "/"),
generateCommand: config("lucent.generateCommand") ?? "",
schemas: $schemasCollection,
imageFilters: config("lucent.imageFilters") ?? [],
roles: $schemasArray["roles"] ?? []
);
$channelService = new ChannelService($schemaService);
$channelService->channel = $channel;
return $channelService;
@@ -58,4 +57,28 @@ final class ChannelService
return some($schema);
}
/**
* @param array<string> $roles
* @return array<string>
*/
public function schemasReadableByRoles(array $roles): array
{
$schemasAllRead = $this->channel->schemas->filter(fn(Schema $schema) => empty($schema->read))->values()->pluck("name");
$schemasCanRead = $this->channel->schemas->filter(fn(Schema $schema) => count(array_intersect($schema->read, $roles)) > 0)->values()->pluck("name");
$schemasCanWrite = $this->channel->schemas->filter(fn(Schema $schema) => count(array_intersect($schema->write, $roles)) > 0)->values()->pluck("name");
return $schemasAllRead->merge($schemasCanRead)->merge($schemasCanWrite)->unique()->values()->toArray();
}
/**
* @param array<string> $roles
* @return array<string>
*/
public function schemasWritableByRoles(array $roles): array
{
$schemasAllRead = $this->channel->schemas->filter(fn(Schema $schema) => empty($schema->write))->values()->pluck("name");
$schemasCanWrite = $this->channel->schemas->filter(fn(Schema $schema) => count(array_intersect($schema->write, $roles)) > 0)->values()->pluck("name");
return $schemasAllRead->merge($schemasCanWrite)->unique()->values()->toArray();
}
}