1111use App \Http \Resources \V1 \Task \TaskCollection ;
1212use App \Http \Resources \V1 \Task \TaskResource ;
1313use App \Models \Organization ;
14+ use App \Models \Project ;
1415use App \Models \Task ;
1516use Illuminate \Auth \Access \AuthorizationException ;
1617use Illuminate \Http \JsonResponse ;
@@ -27,6 +28,26 @@ protected function checkPermission(Organization $organization, string $permissio
2728 }
2829 }
2930
31+ /**
32+ * Check scoped permission and verify user has access to the project
33+ *
34+ * @throws AuthorizationException
35+ */
36+ private function checkScopedPermissionForProject (Organization $ organization , Project $ project , string $ permission ): void
37+ {
38+ $ this ->checkPermission ($ organization , $ permission );
39+
40+ $ user = $ this ->user ();
41+ $ hasAccess = Project::query ()
42+ ->where ('id ' , $ project ->id )
43+ ->visibleByEmployee ($ user )
44+ ->exists ();
45+
46+ if (! $ hasAccess ) {
47+ throw new AuthorizationException ('You do not have permission to ' .$ permission .' in this project. ' );
48+ }
49+ }
50+
3051 /**
3152 * Get tasks
3253 *
@@ -75,7 +96,15 @@ public function index(Organization $organization, TaskIndexRequest $request): Ta
7596 */
7697 public function store (Organization $ organization , TaskStoreRequest $ request ): JsonResource
7798 {
78- $ this ->checkPermission ($ organization , 'tasks:create ' );
99+ /** @var Project $project */
100+ $ project = Project::query ()->findOrFail ($ request ->input ('project_id ' ));
101+
102+ if ($ this ->hasPermission ($ organization , 'tasks:create:all ' )) {
103+ $ this ->checkPermission ($ organization , 'tasks:create:all ' );
104+ } else {
105+ $ this ->checkScopedPermissionForProject ($ organization , $ project , 'tasks:create ' );
106+ }
107+
79108 $ task = new Task ;
80109 $ task ->name = $ request ->input ('name ' );
81110 $ task ->project_id = $ request ->input ('project_id ' );
@@ -97,7 +126,17 @@ public function store(Organization $organization, TaskStoreRequest $request): Js
97126 */
98127 public function update (Organization $ organization , Task $ task , TaskUpdateRequest $ request ): JsonResource
99128 {
100- $ this ->checkPermission ($ organization , 'tasks:update ' , $ task );
129+ // Check task belongs to organization
130+ if ($ task ->organization_id !== $ organization ->id ) {
131+ throw new AuthorizationException ('Task does not belong to organization ' );
132+ }
133+
134+ if ($ this ->hasPermission ($ organization , 'tasks:update:all ' )) {
135+ $ this ->checkPermission ($ organization , 'tasks:update:all ' );
136+ } else {
137+ $ this ->checkScopedPermissionForProject ($ organization , $ task ->project , 'tasks:update ' );
138+ }
139+
101140 $ task ->name = $ request ->input ('name ' );
102141 if ($ this ->canAccessPremiumFeatures ($ organization ) && $ request ->has ('estimated_time ' )) {
103142 $ task ->estimated_time = $ request ->getEstimatedTime ();
@@ -119,7 +158,16 @@ public function update(Organization $organization, Task $task, TaskUpdateRequest
119158 */
120159 public function destroy (Organization $ organization , Task $ task ): JsonResponse
121160 {
122- $ this ->checkPermission ($ organization , 'tasks:delete ' , $ task );
161+ // Check task belongs to organization
162+ if ($ task ->organization_id !== $ organization ->id ) {
163+ throw new AuthorizationException ('Task does not belong to organization ' );
164+ }
165+
166+ if ($ this ->hasPermission ($ organization , 'tasks:delete:all ' )) {
167+ $ this ->checkPermission ($ organization , 'tasks:delete:all ' );
168+ } else {
169+ $ this ->checkScopedPermissionForProject ($ organization , $ task ->project , 'tasks:delete ' );
170+ }
123171
124172 if ($ task ->timeEntries ()->exists ()) {
125173 throw new EntityStillInUseApiException ('task ' , 'time_entry ' );
0 commit comments