Open-source social media scheduler built for AI agents and the teams behind them. Create, schedule and publish posts to every network through MCP or the REST API, or write them yourself in a composer your team will love.
PHP ai-agentsblueskyfacebookinstagramlaravellinkedinmastodonmcpmcp-serveropen-sourcerest-apiself-hostedsocial-mediasocial-media-managementsocial-media-schedulerthreadstiktokvuejsxyoutube
refactor: move workspace tenancy check on Post into PostPolicy
Paulo Castellano3b96a9ebdb65
diff --git a/app/Http/Controllers/Api/PostController.php b/app/Http/Controllers/Api/PostController.phpindex e2f6ab172..2ef5eee82 100644--- a/app/Http/Controllers/Api/PostController.php+++ b/app/Http/Controllers/Api/PostController.php@@ -35,7 +35,7 @@ class PostController extends Controllerpublic function show(Request $request, Post $post): PostResource{- $this->ensurePostInCurrentWorkspace($request, $post);+ $this->authorize('view', $post);$post->load(['postPlatforms.socialAccount', 'user', 'labels']);@@ -59,7 +59,7 @@ class PostController extends Controllerpublic function update(UpdatePostRequest $request, Post $post): PostResource|JsonResponse{- $this->ensurePostInCurrentWorkspace($request, $post);+ $this->authorize('update', $post);$result = UpdatePost::execute($request->user()->currentWorkspace, $post, $request->validated());@@ -75,7 +75,7 @@ class PostController extends Controllerpublic function destroy(Request $request, Post $post): JsonResponse{- $this->ensurePostInCurrentWorkspace($request, $post);+ $this->authorize('delete', $post);DeletePost::execute($post);@@ -84,7 +84,7 @@ class PostController extends Controllerpublic function attachMedia(Request $request, Post $post): PostMediaAttachResource{- $this->ensurePostInCurrentWorkspace($request, $post);+ $this->authorize('update', $post);$validated = $request->validate(['urls' => ['required', 'array', 'min:1', 'max:10'],@@ -100,7 +100,7 @@ class PostController extends Controllerpublic function metrics(Request $request, Post $post): PostMetricsResource{- $this->ensurePostInCurrentWorkspace($request, $post);+ $this->authorize('view', $post);$post->load(['postPlatforms.socialAccount']);@@ -109,17 +109,10 @@ class PostController extends Controllerpublic function preview(Request $request, Post $post): PostPreviewResource{- $this->ensurePostInCurrentWorkspace($request, $post);+ $this->authorize('view', $post);$post->load(['postPlatforms.socialAccount']);return new PostPreviewResource($post);}-- private function ensurePostInCurrentWorkspace(Request $request, Post $post): void- {- if ($post->workspace_id !== $request->user()->currentWorkspace->id) {- abort(Response::HTTP_NOT_FOUND);- }- }}diff --git a/app/Http/Controllers/App/PostController.php b/app/Http/Controllers/App/PostController.phpindex afeba1dfc..b2b62409c 100644--- a/app/Http/Controllers/App/PostController.php+++ b/app/Http/Controllers/App/PostController.php@@ -165,13 +165,7 @@ class PostController extends Controllerpublic function platformMetrics(Request $request, Post $post, PostPlatform $postPlatform): JsonResponse{- $workspace = $request->user()->currentWorkspace;-- if (! $workspace || $post->workspace_id !== $workspace->id) {- abort(404);- }-- $this->authorize('view', $workspace);+ $this->authorize('view', $post);if ($postPlatform->post_id !== $post->id) {abort(404);@@ -188,11 +182,7 @@ class PostController extends Controllerreturn redirect()->route('app.workspaces.create');}- $this->authorize('view', $workspace);-- if ($post->workspace_id !== $workspace->id) {- abort(404);- }+ $this->authorize('view', $post);if (in_array($post->status, [PostStatus::Draft, PostStatus::Scheduled, PostStatus::Failed], true)) {return redirect()->route('app.posts.edit', $post);@@ -214,11 +204,7 @@ class PostController extends Controllerreturn redirect()->route('app.workspaces.create');}- $this->authorize('view', $workspace);-- if ($post->workspace_id !== $workspace->id) {- abort(404);- }+ $this->authorize('update', $post);if (in_array($post->status, [PostStatus::Publishing, PostStatus::Published, PostStatus::PartiallyPublished], true)) {return redirect()->route('app.posts.show', $post);@@ -274,11 +260,7 @@ class PostController extends Controllerreturn redirect()->route('app.workspaces.create');}- $this->authorize('createPost', $workspace);-- if ($post->workspace_id !== $workspace->id) {- abort(404);- }+ $this->authorize('update', $post);$result = UpdatePost::execute($workspace, $post, $request->validated());@@ -316,11 +298,7 @@ class PostController extends Controllerreturn redirect()->route('app.workspaces.create');}- $this->authorize('createPost', $workspace);-- if ($post->workspace_id !== $workspace->id) {- abort(404);- }+ $this->authorize('delete', $post);if (in_array($post->status, [PostStatus::Publishing, PostStatus::Published, PostStatus::PartiallyPublished], true)) {session()->flash('flash.banner', __('posts.flash.cannot_delete_published'));diff --git a/app/Policies/PostPolicy.php b/app/Policies/PostPolicy.phpindex 9c41bd74b..c89f82676 100644--- a/app/Policies/PostPolicy.php+++ b/app/Policies/PostPolicy.php@@ -6,18 +6,58 @@ namespace App\Policies;use App\Models\Post;use App\Models\User;+use Illuminate\Auth\Access\Response;class PostPolicy{+ /**+ * Authorize viewing a post. The post must live in the user's current+ * workspace; cross-workspace lookups deny as 404 so we don't leak+ * existence across tenants.+ */+ public function view(User $user, Post $post): bool|Response+ {+ if ($post->workspace_id !== $user->current_workspace_id) {+ return Response::denyAsNotFound();+ }++ return true;+ }++ /**+ * Authorize updating a post. Same workspace-tenancy guard as `view`.+ */+ public function update(User $user, Post $post): bool|Response+ {+ if ($post->workspace_id !== $user->current_workspace_id) {+ return Response::denyAsNotFound();+ }++ return true;+ }++ /**+ * Authorize deleting a post. Same workspace-tenancy guard as `view`.+ */+ public function delete(User $user, Post $post): bool|Response+ {+ if ($post->workspace_id !== $user->current_workspace_id) {+ return Response::denyAsNotFound();+ }++ return true;+ }+/*** Authorize duplicating a post into the user's current workspace as a- * fresh draft. The post must live in the user's current workspace and- * the user must have permission to create posts there.+ * fresh draft. The post must live in the user's current workspace+ * (404 otherwise — tenancy guard, see `view()`) AND the user must+ * have permission to create posts there (403 otherwise).*/- public function duplicate(User $user, Post $post): bool+ public function duplicate(User $user, Post $post): bool|Response{if ($post->workspace_id !== $user->current_workspace_id) {- return false;+ return Response::denyAsNotFound();}return $user->can('createPost', $user->currentWorkspace);