Skip to content

Commit

Permalink
feat: add admin roles in api response
Browse files Browse the repository at this point in the history
  • Loading branch information
nozomu-y committed Aug 2, 2023
1 parent f35978c commit 3014770
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 8 deletions.
3 changes: 2 additions & 1 deletion api/app/Http/Controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ public function me()
{
$user = auth()->user()->toArray();
$profile = auth()->user()->profile->toArray();
$me = array_merge($user, $profile);
$admin = auth()->user()->admin->toArray();
$me = array_merge($user, $profile, $admin);

return response()->json($me);
}
Expand Down
25 changes: 20 additions & 5 deletions api/app/Models/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Enums\Role;
use App\Models\User;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

Expand All @@ -13,11 +14,7 @@ class Admin extends Model

protected $fillable = [
'user_id',
'role',
];

protected $casts = [
'role' => Role::class,
'roles',
];

protected $primaryKey = 'user_id';
Expand All @@ -26,4 +23,22 @@ public function user()
{
return $this->hasOne(User::class, 'user_id');
}

private function is_role($value)
{
return in_array($value, Role::getValues());
}

/**
* Get the roles attribute.
*
* @return \Illuminate\Database\Eloquent\Casts\Attribute
*/
protected function roles(): Attribute
{
return Attribute::make(
get: fn ($value) => array_filter(explode(',', $value), [$this, 'is_role']),
set: fn ($value) => implode(',', $value),
);
}
}
47 changes: 47 additions & 0 deletions api/database/migrations/2023_07_31_151057_alter_table_admins.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

use App\Enums\Role;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class() extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('admins', function (Blueprint $table) {
$table->set('roles', [Role::MASTER, Role::MANAGER, Role::ACCOUNTANT, Role::CAMP])->nullable(true)->after('role');
});
$records = DB::table('admins')->get();
foreach ($records as $record) {
DB::table('admins')->where('user_id', $record->user_id)->update(['roles' => $record->role]);
}
Schema::table('admins', function (Blueprint $table) {
$table->dropColumn('role');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('admins', function (Blueprint $table) {
$table->string('role', 32)->nullable(true)->after('user_id');
});
$records = DB::table('admins')->get();
foreach ($records as $record) {
DB::table('admins')->where('user_id', $record->user_id)->update(['role' => explode(',', $record->roles)[0]]);
}
Schema::table('admins', function (Blueprint $table) {
$table->dropColumn('roles');
});
}
};
4 changes: 2 additions & 2 deletions api/database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function createAdminUser($role)
]);
\App\Models\Admin::create([
'user_id' => $user->id,
'role' => $role,
'roles' => [$role],
]);
}

Expand Down Expand Up @@ -58,7 +58,7 @@ public function run()
]);
\App\Models\Admin::create([
'user_id' => $user->id,
'role' => Role::MASTER,
'roles' => [Role::MASTER],
]);
echo 'Email: admin@chorkleines.com'.PHP_EOL;
echo 'Password: password'.PHP_EOL;
Expand Down
2 changes: 2 additions & 0 deletions api/tests/Feature/AuthTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function test_get_user_info_using_authenticated_user()
'grade',
'part',
'birthday',
'roles' => [],
]);
$response->assertJson([
'email' => 'admin@chorkleines.com',
Expand All @@ -46,6 +47,7 @@ public function test_get_user_info_using_authenticated_user()
'grade' => 18,
'part' => Part::TENOR,
'birthday' => '2000-01-01',
'roles' => ['MASTER'],
]);
}

Expand Down
2 changes: 2 additions & 0 deletions client/composables/useAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type User = {
birthday: string;
status: string;
emailVerifiedAt: string;
roles: string[];
createdAt: string;
updatedAt: string;
displayName: string;
Expand Down Expand Up @@ -40,6 +41,7 @@ export const useAuth = () => {
user.value.birthday = u.birthday;
user.value.status = u.status;
user.value.emailVerifiedAt = u.email_verified_at;
user.value.roles = u.roles;
user.value.createdAt = u.created_at;
user.value.updatedAt = u.updated_at;
user.value.displayName = `${u.grade}${u.part} ${u.last_name}${u.first_name}`;
Expand Down

0 comments on commit 3014770

Please sign in to comment.