Skip to content

Commit

Permalink
complete user role
Browse files Browse the repository at this point in the history
  • Loading branch information
mizzy-123 committed Oct 19, 2023
1 parent a8d112c commit eb9c3ff
Show file tree
Hide file tree
Showing 12 changed files with 189 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/Http/Controllers/LoginController.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function login(Request $request)
return response()->json([
'status' => 400,
'message' => 'password or email and nim is wrong',
]);
], 400);
}

return response()->json([
Expand Down
33 changes: 33 additions & 0 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Http\Controllers;

use App\Http\Resources\AllUser;
use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;

class UserController extends Controller
{
public function index()
{
if (Gate::check("superAdmin")) {
$dataUser = User::with(['role:name', 'organization:name_organization,foto'])->whereNotIn('id', [1])->get();
return response()->json([
'status' => 200,
'data' => AllUser::collection($dataUser),
]);
} elseif (Gate::check("admin")) {
$dataUser = User::with(['role:name', 'organization:name_organization,foto'])->whereNotIn('id', [1, 2])->get();
return response()->json([
'status' => 200,
'data' => AllUser::collection($dataUser),
]);
} else {
return response()->json([
'status' => 401,
'message' => 'Unauthorization',
], 404);
}
}
}
26 changes: 26 additions & 0 deletions app/Http/Resources/AllUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace App\Http\Resources;

use Illuminate\Http\Resources\Json\JsonResource;

class AllUser 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 [
"id" => $this->id,
"name" => $this->name,
"nim" => $this->nim,
"email" => $this->email,
"organization" => $this->organization,
"role" => $this->role,
];
}
}
11 changes: 11 additions & 0 deletions app/Models/Organization.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Organization extends Model
{
use HasFactory;
}
5 changes: 5 additions & 0 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public function role()
return $this->belongsToMany(Role::class, 'user_roles');
}

public function organization()
{
return $this->belongsToMany(Organization::class, 'user_organizations');
}

public function superAdmin()
{
return $this->role()->first()->id == 1;
Expand Down
11 changes: 11 additions & 0 deletions app/Models/UserOrganization.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class UserOrganization extends Model
{
use HasFactory;
}
11 changes: 10 additions & 1 deletion app/Providers/AuthServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
namespace App\Providers;

// use Illuminate\Support\Facades\Gate;

use App\Models\User;
use Illuminate\Foundation\Support\Providers\AuthServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Gate;

class AuthServiceProvider extends ServiceProvider
{
Expand All @@ -25,6 +28,12 @@ public function boot()
{
$this->registerPolicies();

//
Gate::define('superAdmin', function (User $user) {
return $user->superAdmin();
});

Gate::define('admin', function (User $user) {
return $user->admin();
});
}
}
2 changes: 1 addition & 1 deletion database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function definition()
'email' => fake()->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => Hash::make('123456789'), // password
'remember_token' => Str::random(10),
// 'remember_token' => Str::random(10),
];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('organizations', function (Blueprint $table) {
$table->id();
$table->string('name_organization');
$table->string('foto')->nullable();
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('organizations');
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('user_organizations', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained('users')->onDelete('cascade');
$table->foreignId('organization_id')->constrained('organizations')->onDelete('cascade');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('user_organizations');
}
};
22 changes: 22 additions & 0 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Models\User;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;

class DatabaseSeeder extends Seeder
{
Expand Down Expand Up @@ -36,5 +37,26 @@ public function run()

$superAdmin = User::factory()->create();
$superAdmin->role()->attach(1);
$anggota = [
[
'name' => fake()->name(),
'nim' => '4.33.21.2.17',
'email' => fake()->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => Hash::make('123456789'),
],
[
'name' => fake()->name(),
'nim' => '4.33.21.2.18',
'email' => fake()->unique()->safeEmail(),
'email_verified_at' => now(),
'password' => Hash::make('123456789'),
],
];

foreach ($anggota as $a) {
$user = User::create($a);
$user->role()->attach(1);
}
}
}
4 changes: 3 additions & 1 deletion routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use App\Http\Controllers\LoginController;
use App\Http\Controllers\RegisterController;
use App\Http\Controllers\UserController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Expand All @@ -20,12 +21,13 @@
// return $request->user();
// });

Route::post('/register', [RegisterController::class, 'store']);

Route::post('/login', [LoginController::class, 'login']);

Route::middleware('auth:sanctum')->group(function () {
Route::post('/register', [RegisterController::class, 'store']);
Route::get('/logout', [LoginController::class, 'logout']);
Route::get('/allUser', [UserController::class, 'index']);
});

Route::middleware(['auth:sanctum', 'Admin'])->group(function () {
Expand Down

0 comments on commit eb9c3ff

Please sign in to comment.