Skip to content

Commit

Permalink
Merge pull request #16 from ricom/feature/permission-in-save
Browse files Browse the repository at this point in the history
added permission to save resource.
  • Loading branch information
nic-schi authored Aug 18, 2022
2 parents 62cb06f + a5aff91 commit 2e568f6
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 30 deletions.
11 changes: 8 additions & 3 deletions app/Http/Controllers/SaveController.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function store(Request $request): JsonResponse

$validate = $request->validate([
"name" => "required|string",
"description"=>"string",
"description" => "string",
"data" => "nullable|json",
"tool_id" => "required|exists:tools,id"
]);
Expand All @@ -73,11 +73,16 @@ public function store(Request $request): JsonResponse
* @see SavePolicy
* @see Save::$last_opened
*/
public function show(Save $save): SaveResource
public function show(Request $request, Save $save): SaveResource
{
$user = $request->user();
$this->authorize("view", $save);
$save->last_opened = Carbon::now();
if ($save->isContributor($user)) {
$save->setRelation('pivot', $user->sharedSaves()->where('save_id', $save->id)->first());
}
$save->save();

return new SaveResource($save);
}

Expand Down Expand Up @@ -106,7 +111,7 @@ public function update(Request $request, Save $save): Response
"lock" => "required|boolean",
"data" => "prohibited",
"name" => "prohibited",
"description"=>"prohibited"
"description" => "prohibited"
]);

if (is_null($save->locked_by_id) || $save->locked_by_id === $user->id || $save->owner_id === $user->id) {
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/SharedSaveController.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ public function update(Request $request, SharedSave $sharedSave): Response
{
$this->authorize("update", $sharedSave);
$validated = $request->validate([
"permission" => ["integer", "min:0", "min:2"],
"permission" => ["integer", "min:0", "max:2"],
"revoked" => ["boolean"],
]);
$sharedSave->fill($validated);
Expand Down
26 changes: 22 additions & 4 deletions app/Http/Controllers/UserSavesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;

/**
* Controller, eine Route zum Anzeigen aller Speicherstände eins zugehörigen Users
Expand Down Expand Up @@ -44,7 +45,7 @@ public function index(Request $request, User $user)
}

$savesQuerry = $user->saves();
$contributorSavesQuery = $user->accessibleShares(false);
$contributorSavesQuery = $user->accessibleShares(true);

$where = function ($query) use ($validated) {
if (key_exists("tool_id", $validated)) {
Expand All @@ -55,19 +56,36 @@ public function index(Request $request, User $user)
$query->where("name", "Like", "%" . $validated["name"] . "%");
}
if (key_exists("description", $validated)) {
if($validated["search_both"]){
if ($validated["search_both"]) {
$query->where("description", "Like", "%" . $validated["description"] . "%");
}else{
} else {
$query->orWhere("description", "Like", "%" . $validated["description"] . "%");
}
}
};

$savesQuerry->where($where);
// add because of missing pivot table data which would make the union fail
$savesQuerry->select([
"saves.*",
'pivot_user_id' => DB::raw("NULL"),
'pivot_save_id' => DB::raw("NULL"),
'pivot_permission' => DB::raw("NULL"),
'pivot_created_at' => DB::raw("NULL"),
'pivot_updated_at' => DB::raw("NULL")
]);
$contributorSavesQuery->where($where);


$saves = $savesQuerry->union($contributorSavesQuery->getBaseQuery())->paginate();
$saves = $contributorSavesQuery->union($savesQuerry->getBaseQuery())->paginate(null, []/* empty array to prevent duplicate "saves.*" select*/);

// remove previously added null pivot columns
foreach ($saves->items() as $save) {
if ($save->pivot->permission == null) {
$save->unsetRelation('pivot');
}
}

return SimpleSaveResource::collection($saves);
}
}
3 changes: 1 addition & 2 deletions app/Http/Resources/InvitationLinkResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ class InvitationLinkResource extends JsonResource
public function toArray($request)
{
return [
$this->merge(new PermissionResource($this->resource)),
"expiry_date" => $this->expiry_date,
"permission" => $this->permission,
"save" => new SimplerSaveResource($this->safe),
"created_at" => $this->created_at,
"token" => $this->when($this->safe->hasAtLeasPermission(Auth::user(), PermissionHelper::$PERMISSION_ADMIN), $this->token)
];
}
Expand Down
22 changes: 22 additions & 0 deletions app/Http/Resources/PermissionResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class PermissionResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
*/
public function toArray($request)
{
return [
"permission" => $this->permission,
"created_at" => $this->created_at
];
}
}
14 changes: 5 additions & 9 deletions app/Http/Resources/SaveResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,11 @@ class SaveResource extends JsonResource
*/
public function toArray($request)
{
$simpleResource = new SimpleSaveResource($this->resource);
return array_merge_recursive($simpleResource->toArray($request), [
return [
$this->merge(new SimpleSaveResource($this->resource)),
"data" => $this->data,
"contributors" => $this->contributors->map(function ($c) {
return new SimplestUserResource($c);
})->toArray(),
"invited" => $this->invited->map(function ($c) {
return new SimplestUserResource($c);
})->toArray(),
]);
"contributors" => SimplestUserResource::collection($this->contributors),
"invited" => SimplestUserResource::collection($this->invited),
];
}
}
4 changes: 2 additions & 2 deletions app/Http/Resources/SharedSaveResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ public function toArray($request)
"id" => $this->id,
"user" => new SimplestUserResource($this->user),
"save" => new SimplerSaveResource($this->safe),
"permission" => $this->permission,
"accepted" => $this->accepted,
"declined" => $this->declined,
"revoked" => $this->revoked
"revoked" => $this->revoked,
$this->merge(new PermissionResource($this->resource)),
];
}
}
5 changes: 3 additions & 2 deletions app/Http/Resources/SharedSaveUserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

/**
* Klasse, welche eine Beziehung von einem Speicherstand zu einem User darstellt.
*
Expand All @@ -22,8 +23,8 @@ class SharedSaveUserResource extends JsonResource
public function toArray($request)
{
return [
"save_id" => $this->id,
"permission" => $this->pivot->permission,
"save" => new SimplerSaveResource($this->resource),
$this->merge(new PermissionResource($this->pivot))
];
}
}
3 changes: 3 additions & 0 deletions app/Http/Resources/SimpleSaveResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public function toArray($request)
"owner" => new SimplestUserResource($this->owner),
"owner_deleting" => $this->owner->trashed(),
"tool_id" => $this->tool_id,
"permission" => $this->whenPivotLoaded('shared_save', function () {
return new PermissionResource($this->pivot);
}),
"updated_at" => $this->updated_at,
"created_at" => $this->created_at,
/*"contributors" => $this->contributors->map(function ($c) {
Expand Down
12 changes: 9 additions & 3 deletions app/Models/Save.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
*/
class Save extends Model
{
use HasFactory, SoftDeletes,Limitable;
use HasFactory, SoftDeletes, Limitable;

/**
* Attribute, welche Massen zuweisbar sind
Expand Down Expand Up @@ -147,7 +147,7 @@ public function invited(): BelongsToMany
return $this->belongsToMany(User::class, 'shared_save')->using(SharedSave::class)
->withPivot(["permission", "accepted", "declined", "revoked"])
->withPivotValue("accepted", false)
->withPivotValue("revoked",false)
->withPivotValue("revoked", false)
->withTimestamps();
}

Expand Down Expand Up @@ -187,6 +187,12 @@ public function contributors(): BelongsToMany
->withTimestamps();
}

public function isContributor(User|int $user)
{
$id = is_int($user) ? $user : $user->id;
return $this->contributors->firstWhere('id', $id) !== null;
}

/**
* Prüft, ob der übergebene User mindestens die angegebene Berechtigung bei diesem Speicherstand besitzt
* @param User $user Der zu überprüfende User
Expand All @@ -197,7 +203,7 @@ public function hasAtLeasPermission(User $user, int $permission)
{
if ($user->id === $this->owner_id) {
return true;
} else if (($contributor = $this->contributors()->firstWhere('user_id', '=', $user->id)) !== null) {
} else if (($contributor = $this->contributors->firstWhere('id', '=', $user->id)) !== null) {
$hasPermission = $contributor->pivot->permission;
if (PermissionHelper::isAtLeastPermission($hasPermission, $permission)) {
return true;
Expand Down
9 changes: 5 additions & 4 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,16 @@ public function accessibleShares(bool $withPivot = true): BelongsToMany

public function settings($setting_id = -1): BelongsToMany
{
$q = $this->belongsToMany(Setting::class, 'user_settings')->withPivot(['id','value']);
if($setting_id !== -1){
$q->where('setting_id' , $setting_id);
$q = $this->belongsToMany(Setting::class, 'user_settings')->withPivot(['id', 'value']);
if ($setting_id !== -1) {
$q->where('setting_id', $setting_id);
}

return $q;
}

public function getSetting($setting_id){
public function getSetting($setting_id)
{
return $this->hasMany(UserSetting::class);
}

Expand Down

0 comments on commit 2e568f6

Please sign in to comment.