From 31e5d56f4fcde54867528e4fc596ccc5f45801a4 Mon Sep 17 00:00:00 2001 From: Nafies Luthfi Date: Mon, 3 Jul 2017 08:54:48 +0800 Subject: [PATCH] Added manager_id attribute to User Model as user manager Added UserPolicy Class with test --- app/Policies/UserPolicy.php | 16 ++++++++++++++++ app/Providers/AuthServiceProvider.php | 2 +- app/User.php | 5 +++++ database/factories/ModelFactory.php | 1 + .../2014_10_12_000000_create_users_table.php | 1 + routes/web.php | 1 + tests/Feature/UsersProfileTest.php | 7 +++++++ tests/Unit/Policies/UserPolicyTest.php | 21 +++++++++++++++++++++ 8 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 app/Policies/UserPolicy.php create mode 100644 tests/Unit/Policies/UserPolicyTest.php diff --git a/app/Policies/UserPolicy.php b/app/Policies/UserPolicy.php new file mode 100644 index 0000000..10de3cc --- /dev/null +++ b/app/Policies/UserPolicy.php @@ -0,0 +1,16 @@ +manager_id == $user->id; + } +} diff --git a/app/Providers/AuthServiceProvider.php b/app/Providers/AuthServiceProvider.php index 9784b1a..4661b6a 100644 --- a/app/Providers/AuthServiceProvider.php +++ b/app/Providers/AuthServiceProvider.php @@ -13,7 +13,7 @@ class AuthServiceProvider extends ServiceProvider * @var array */ protected $policies = [ - 'App\Model' => 'App\Policies\ModelPolicy', + 'App\User' => 'App\Policies\UserPolicy', ]; /** diff --git a/app/User.php b/app/User.php index 991f8ab..2e5067c 100644 --- a/app/User.php +++ b/app/User.php @@ -155,4 +155,9 @@ class User extends Authenticatable { return $this->belongsTo(Couple::class); } + + public function manager() + { + return $this->belongsTo(User::class); + } } diff --git a/database/factories/ModelFactory.php b/database/factories/ModelFactory.php index 67ce81a..697591e 100644 --- a/database/factories/ModelFactory.php +++ b/database/factories/ModelFactory.php @@ -20,6 +20,7 @@ $factory->define(User::class, function (Faker\Generator $faker) { 'name' => $name, 'nickname' => $name, 'gender_id' => rand(1, 2), + 'manager_id' => 1, ]; }); diff --git a/database/migrations/2014_10_12_000000_create_users_table.php b/database/migrations/2014_10_12_000000_create_users_table.php index 79f9b52..a3e4633 100644 --- a/database/migrations/2014_10_12_000000_create_users_table.php +++ b/database/migrations/2014_10_12_000000_create_users_table.php @@ -29,6 +29,7 @@ class CreateUsersTable extends Migration $table->string('address')->nullable(); $table->string('city')->nullable(); $table->string('phone')->nullable(); + $table->unsignedInteger('manager_id')->nullable(); $table->rememberToken(); $table->timestamps(); }); diff --git a/routes/web.php b/routes/web.php index e85876f..1f2a708 100644 --- a/routes/web.php +++ b/routes/web.php @@ -26,6 +26,7 @@ Route::post('family-actions/{user}/add-wife', 'FamilyActionsController@addWife') Route::post('family-actions/{user}/add-husband', 'FamilyActionsController@addHusband')->name('family-actions.add-husband'); Route::post('family-actions/{user}/set-parent', 'FamilyActionsController@setParent')->name('family-actions.set-parent'); +Route::get('profile-search', 'UsersController@index')->name('users.search'); Route::get('users/{user}', 'UsersController@show')->name('users.show'); Route::get('users/{user}/edit', 'UsersController@edit')->name('users.edit'); Route::patch('users/{user}', 'UsersController@update')->name('users.update'); diff --git a/tests/Feature/UsersProfileTest.php b/tests/Feature/UsersProfileTest.php index 88631af..5da404e 100644 --- a/tests/Feature/UsersProfileTest.php +++ b/tests/Feature/UsersProfileTest.php @@ -53,4 +53,11 @@ class UsersProfileTest extends TestCase 'password' => null, ]); } + + /** @test */ + public function guest_can_search_users_profile() + { + $this->visit(route('users.search')); + $this->seePageIs(route('users.search')); + } } diff --git a/tests/Unit/Policies/UserPolicyTest.php b/tests/Unit/Policies/UserPolicyTest.php new file mode 100644 index 0000000..d050f4b --- /dev/null +++ b/tests/Unit/Policies/UserPolicyTest.php @@ -0,0 +1,21 @@ +create(); + $manager = $user->manager; + + $this->assertTrue($manager->can('edit', $user)); + } +}