5 changed files with 117 additions and 8 deletions
-
8app/Policies/Partners/PartnerPolicy.php
-
62app/Policies/Projects/ProjectPolicy.php
-
1app/Providers/AuthServiceProvider.php
-
5tests/Unit/Policies/PartnerPolicyTest.php
-
49tests/Unit/Policies/ProjectPolicyTest.php
@ -0,0 +1,62 @@ |
|||
<?php |
|||
|
|||
namespace App\Policies\Projects; |
|||
|
|||
use App\Entities\Projects\Project; |
|||
use App\Entities\Users\User; |
|||
use Illuminate\Auth\Access\HandlesAuthorization; |
|||
|
|||
class ProjectPolicy |
|||
{ |
|||
use HandlesAuthorization; |
|||
|
|||
/** |
|||
* Determine whether the user can view the project. |
|||
* |
|||
* @param \App\Entities\Users\User $user |
|||
* @param \App\Entities\Projects\Project $project |
|||
* @return mixed |
|||
*/ |
|||
public function view(User $user, Project $project) |
|||
{ |
|||
// User can only view the project if he is the project's agency owner.
|
|||
return $user->agency->id == $project->owner_id; |
|||
} |
|||
|
|||
/** |
|||
* Determine whether the user can create projects. |
|||
* |
|||
* @param \App\Entities\Users\User $user |
|||
* @param \App\Entities\Projects\Project $project |
|||
* @return mixed |
|||
*/ |
|||
public function create(User $user, Project $project) |
|||
{ |
|||
// User can create a project if they owns an agency.
|
|||
return ! is_null($user->agency); |
|||
} |
|||
|
|||
/** |
|||
* Determine whether the user can update the project. |
|||
* |
|||
* @param \App\Entities\Users\User $user |
|||
* @param \App\Entities\Projects\Project $project |
|||
* @return mixed |
|||
*/ |
|||
public function update(User $user, Project $project) |
|||
{ |
|||
return $this->view($user, $project); |
|||
} |
|||
|
|||
/** |
|||
* Determine whether the user can delete the project. |
|||
* |
|||
* @param \App\Entities\Users\User $user |
|||
* @param \App\Entities\Projects\Project $project |
|||
* @return mixed |
|||
*/ |
|||
public function delete(User $user, Project $project) |
|||
{ |
|||
return $this->view($user, $project); |
|||
} |
|||
} |
|||
@ -0,0 +1,49 @@ |
|||
<?php |
|||
|
|||
namespace Tests\Unit\Policies; |
|||
|
|||
use App\Entities\Agencies\Agency; |
|||
use App\Entities\Projects\Project; |
|||
use Tests\TestCase as TestCase; |
|||
|
|||
class ProjectPolicyTest extends TestCase |
|||
{ |
|||
/** @test */ |
|||
public function user_can_create_project() |
|||
{ |
|||
$user = $this->userSigningIn(); |
|||
$agency = factory(Agency::class)->create(['owner_id' => $user->id]); |
|||
|
|||
$this->assertTrue($user->can('create', new Project)); |
|||
} |
|||
|
|||
/** @test */ |
|||
public function user_can_view_project() |
|||
{ |
|||
$user = $this->userSigningIn(); |
|||
$agency = factory(Agency::class)->create(['owner_id' => $user->id]); |
|||
$project = factory(Project::class)->create(['owner_id' => $agency->id]); |
|||
|
|||
$this->assertTrue($user->can('view', $project)); |
|||
} |
|||
|
|||
/** @test */ |
|||
public function user_can_update_project() |
|||
{ |
|||
$user = $this->userSigningIn(); |
|||
$agency = factory(Agency::class)->create(['owner_id' => $user->id]); |
|||
$project = factory(Project::class)->create(['owner_id' => $agency->id]); |
|||
|
|||
$this->assertTrue($user->can('update', $project)); |
|||
} |
|||
|
|||
/** @test */ |
|||
public function user_can_delete_project() |
|||
{ |
|||
$user = $this->userSigningIn(); |
|||
$agency = factory(Agency::class)->create(['owner_id' => $user->id]); |
|||
$project = factory(Project::class)->create(['owner_id' => $agency->id]); |
|||
|
|||
$this->assertTrue($user->can('delete', $project)); |
|||
} |
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue