diff --git a/src/Ushahidi/Modules/V5/Actions/Category/Handlers/FetchCategoryByIdQueryHandler.php b/src/Ushahidi/Modules/V5/Actions/Category/Handlers/FetchCategoryByIdQueryHandler.php index 439eab2c9..ba1c512b0 100644 --- a/src/Ushahidi/Modules/V5/Actions/Category/Handlers/FetchCategoryByIdQueryHandler.php +++ b/src/Ushahidi/Modules/V5/Actions/Category/Handlers/FetchCategoryByIdQueryHandler.php @@ -34,7 +34,6 @@ public function __invoke(Action $query): Category $this->isSupported($query); return $this->categoryRepository->findById( $query->getId(), - false, array_unique(array_merge( $query->getFields(), $query->getFieldsForRelationship() diff --git a/src/Ushahidi/Modules/V5/Http/Resources/CategoryResource.php b/src/Ushahidi/Modules/V5/Http/Resources/CategoryResource.php index fcf30b495..e461ce47e 100644 --- a/src/Ushahidi/Modules/V5/Http/Resources/CategoryResource.php +++ b/src/Ushahidi/Modules/V5/Http/Resources/CategoryResource.php @@ -2,6 +2,7 @@ namespace Ushahidi\Modules\V5\Http\Resources; use Illuminate\Http\Resources\Json\JsonResource as Resource; +use Ushahidi\Core\Entity\Tag; class CategoryResource extends Resource { @@ -10,6 +11,24 @@ class CategoryResource extends Resource public static $wrap = 'result'; + private function getResourcePrivileges() + { + $authorizer = service('authorizer.tag'); + // Obtain v3 entity from the v5 post model + // Note that we use attributesToArray instead of toArray because the first + // would have the effect of causing unnecessary requests to the database + // (relations are not needed in this case by the authorizer) + $resource_array = $this->resource->attributesToArray(); + unset($resource_array['completed_stages']); + $entity = new Tag($resource_array); + // if there's no user the guards will kick them off already, but if there + // is one we need to check the authorizer to ensure we don't let + // users without admin perms create forms etc + // this is an unfortunate problem with using an old version of lumen + // that doesn't let me do guest user checks without adding more risk. + return $authorizer->getAllowedPrivs($entity); + } + /** * Transform the resource into an array. * @@ -18,27 +37,21 @@ class CategoryResource extends Resource */ public function toArray($request) { - // Preload key relations - $this->resource->loadMissing(['parent', 'children', 'translations']); - return [ - 'id' => $this->id, - 'parent_id' => $this->parent_id, - 'tag' => $this->tag, - 'slug' => $this->slug, - 'type' => $this->type, - 'color' => $this->color, - 'icon' => $this->icon, - 'description' => $this->description, - 'role' => $this->makeRole($this->role), - 'priority' => $this->priority, - 'children' => $this->makeChildren($this->parent, $this->children), - 'parent' => $this->makeParent($this->parent), - 'translations' => new TranslationCollection($this->translations), - 'enabled_languages' => [ - 'default'=> $this->base_language, - 'available' => $this->translations->groupBy('language')->keys() - ] - ]; + $data = $this->resource->toArray(); + if (isset($data['role'])) { + $data['role'] = $this->makeRole($this->role); + } + if (isset($data['children'])) { + $data['children'] = $this->makeChildren($this->parent, $this->children); + } + if (isset($data['parent'])) { + $data['parent'] = $this->makeRole($this->parent); + } + if (isset($data['translations'])) { + $data['translations'] = (new TranslationCollection($this->translations))->toArray(null); + } + $data['allowed_privileges']= $this->getResourcePrivileges(); + return $data; } protected function makeRole($role) diff --git a/src/Ushahidi/Modules/V5/Models/Category.php b/src/Ushahidi/Modules/V5/Models/Category.php index 7d0f36ff2..fcf99bd50 100644 --- a/src/Ushahidi/Modules/V5/Models/Category.php +++ b/src/Ushahidi/Modules/V5/Models/Category.php @@ -78,8 +78,8 @@ class Category extends BaseModel 'created' ]; public const ALLOWED_RELATIONSHIPS = [ - 'children' => ['fields' => [], 'relationships' => ['children']], - 'parent' => ['fields' => [], 'relationships' => ['parent']], + 'children' => ['fields' => ['parent_id'], 'relationships' => ['children']], + 'parent' => ['fields' => ['parent_id'], 'relationships' => ['parent']], 'translations' => ['fields' => [], 'relationships' => ["translations"]], 'enabled_languages' => ['fields' => ['base_language'], 'relationships' => ['translations']], ]; diff --git a/src/Ushahidi/Modules/V5/Repository/Category/CategoryRepository.php b/src/Ushahidi/Modules/V5/Repository/Category/CategoryRepository.php index 0265ef4ac..2b0db6dd1 100644 --- a/src/Ushahidi/Modules/V5/Repository/Category/CategoryRepository.php +++ b/src/Ushahidi/Modules/V5/Repository/Category/CategoryRepository.php @@ -16,10 +16,12 @@ interface CategoryRepository * Laravel Eloquent ORM. Will throw an exception if provided identifier does * not exist in the database. * @param int $id + * @param array $fields + * @param array $with * @return Category * @throws NotFoundException */ - public function findById(int $id): Category; + public function findById(int $id, array $fields = [], array $with = []): Category; /** diff --git a/src/Ushahidi/Modules/V5/Repository/Category/EloquentCategoryRepository.php b/src/Ushahidi/Modules/V5/Repository/Category/EloquentCategoryRepository.php index b2c97ffef..08cb00d2e 100644 --- a/src/Ushahidi/Modules/V5/Repository/Category/EloquentCategoryRepository.php +++ b/src/Ushahidi/Modules/V5/Repository/Category/EloquentCategoryRepository.php @@ -35,12 +35,23 @@ public function setSearchParams(SearchData $searchData) * Laravel Eloquent ORM. Will throw an exception if provided identifier does * not exist in the database. * @param int $id + * @param array $fields + * @param array $with * @return Category * @throws NotFoundException */ - public function findById(int $id): Category + public function findById(int $id, array $fields = [], array $with = []): Category { - $category = Category::find($id); + $query = Category::where('id', '=', $id); + if (count($fields)) { + $query->select($fields); + } + if (count($with)) { + $query->with($with); + } + $category = $query->first(); + dd($category); + if (!$category instanceof Category) { throw new NotFoundException('Category not found'); } @@ -136,7 +147,6 @@ public function paginate( ->orderBy('tags.'.$paging->getOrderBy(), $paging->getOrder()); $query = $this->setSearchCondition($search_fields, $query); - if (count($fields)) { $query->select($fields); }