Skip to content

Commit

Permalink
fix: correct providerplatform request behavior, attempt to fix user role
Browse files Browse the repository at this point in the history
  • Loading branch information
PThorpe92 committed Mar 18, 2024
1 parent 4d739fa commit 7937c64
Show file tree
Hide file tree
Showing 18 changed files with 56 additions and 24 deletions.
4 changes: 2 additions & 2 deletions app/Enums/UserRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

enum UserRole: string
{
case Admin = 'admin';
case Student = 'student';
case ADMIN = 'admin';
case STUDENT = 'student';
}
2 changes: 1 addition & 1 deletion app/Http/Controllers/Auth/RegisteredUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function store(Request $request): RedirectResponse
'name_last' => $request->name_last,
'password' => Hash::make($request->password),
'reset_password' => false,
'role' => UserRole::Student,
'role' => UserRole::STUDENT,
]);

event(new Registered($user));
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/Auth/StudentNewPasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function store(Request $request): JsonResponse|RedirectResponse
// set `reset_password` to `true` so that on next login they can be
// prompted to set their new password.
$user = User::where('username', $request->username)->first();
if ($user['role'] === UserRole::Student) {
if ($user['role'] === UserRole::STUDENT) {
$pw = $user->createTempPassword();

return response()->json([
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Controllers/v1/Actions/StoreCanvasUsersAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function __invoke(Request $request)
'email' => $user_profile['email'],
'name_first' => $user_profile['first_name'],
'name_last' => $user_profile['last_name'],
'role' => UserRole::Student,
'role' => UserRole::STUDENT,
'password' => bcrypt('ChangeMe!'),
'password_reset' => true,
]);
Expand Down
6 changes: 6 additions & 0 deletions app/Http/Controllers/v1/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public function index(AdminRequest $request)
*/
public function store(StoreUserRequest $request)
{
$role = match ($request->role) {
'admin' => 'admin',
'student' => 'student',
default => 'student',
};
try {
$user = $request->validated();
} catch (\Throwable $th) {
Expand All @@ -55,6 +60,7 @@ public function store(StoreUserRequest $request)
], 422);
}
$newUser = new User($user);
$newUser->role = $role;
$pw = $newUser->createTempPassword();

return response(NewUserResource::withPassword($newUser, $pw), 201);
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Requests/StoreProviderPlatformRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function rules(): array
'type' => [Rule::enum(ProviderPlatformType::class)],
'description' => 'nullable|string|max:255',
'icon_url' => 'nullable|url:http,https',
'account_id' => 'required|unique:provider_platforms,account_id',
'account_id' => 'required|integer',
'access_key' => 'required|string|max:255',
'base_url' => 'required|url:http,https',
'state' => [Rule::enum(ProviderPlatformState::class)],
Expand Down
2 changes: 1 addition & 1 deletion app/Http/Requests/StoreUserRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function rules(): array
'name_last' => 'required|string|max:35',
'email' => 'nullable|email|max:90|unique:users',
'username' => 'required|string|max:70|unique:users',
'role' => 'required|string|in:student,admin',
'role' => 'required|string',
];
}
}
9 changes: 3 additions & 6 deletions app/Http/Requests/UpdateProviderPlatformRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

namespace App\Http\Requests;

use App\Enums\ProviderPlatformState;
use App\Enums\ProviderPlatformType;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class UpdateProviderPlatformRequest extends FormRequest
{
Expand All @@ -26,13 +23,13 @@ public function rules(): array
{
return [
'name' => 'nullable|string|max:255',
'type' => [Rule::enum(ProviderPlatformType::class)],
'type' => 'required|string',
'description' => 'nullable|string|max:255',
'icon_url' => 'nullable|url:http,https',
'account_id' => 'nullable|int|unique:provider_platforms,account_id',
'account_id' => 'nullable|int',
'access_key' => 'nullable|string|max:255',
'base_url' => 'nullable|url:http,https',
'state' => [Rule::enum(ProviderPlatformState::class)],
'state' => 'required|string',
];
}
}
2 changes: 1 addition & 1 deletion app/Jobs/UserCourseActivityTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class UserCourseActivityTask implements ShouldQueue
*/
public function __construct()
{
$this->users = User::where('role', UserRole::Student)->get();
$this->users = User::where('role', UserRole::STUDENT)->get();
$this->providers = ProviderPlatform::all();
}

Expand Down
2 changes: 1 addition & 1 deletion app/Models/ProviderPlatform.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function decryptAccessKey()

public function getCanvasServices(): \App\Services\CanvasServices
{
return new \App\Services\CanvasServices($this['id'], $this['account_id'], $this['access_key'], $this['base_url']);
return new \App\Services\CanvasServices($this->attributes['id'], $this->attributes['account_id'], $this->attributes['access_key'], $this->attributes['base_url']);
}

public function hasUserMapping($user)
Expand Down
2 changes: 1 addition & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public function providerUserMappings()

public function isAdmin(): bool
{
return $this->role === UserRole::Admin;
return $this->role === UserRole::ADMIN;
}

public function userActivity()
Expand Down
4 changes: 2 additions & 2 deletions database/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function definition(): array
'email_verified_at' => now(),
'password' => static::$password ??= Hash::make('password'),
'remember_token' => Str::random(10),
'role' => UserRole::Student,
'role' => UserRole::STUDENT,
'username' => $first.$last.$count,
];
}
Expand All @@ -43,7 +43,7 @@ public function definition(): array
public function admin(): static
{
return $this->state(fn (array $attributes) => [
'role' => UserRole::Admin,
'role' => UserRole::ADMIN,
]);
}

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

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

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('provider_platforms', function (Blueprint $table) {
$table->longText('access_key')->change();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('provider_platforms', function (Blueprint $table) {
$table->string('access_key')->change();
});
}
};
4 changes: 2 additions & 2 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function run(): void
'username' => 'SuperAdmin',
'password' => bcrypt('ChangeMe!'),
'password_reset' => true,
'role' => UserRole::Admin,
'role' => UserRole::ADMIN,
]);
DB::table('categories')->insert([
'name' => 'Unlocked Labs',
Expand Down Expand Up @@ -61,7 +61,7 @@ public function run(): void
'name_last' => $userInfo[3],
'password' => bcrypt('ChangeMe!'),
'password_reset' => true,
'role' => UserRole::Admin,
'role' => UserRole::ADMIN,
]);
if (count($userInfo) > 4) {
// there is provider info included
Expand Down
2 changes: 1 addition & 1 deletion database/seeders/DefaultAdmin.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function run(): void
'username' => 'SuperAdmin',
'password' => bcrypt('ChangeMe!'),
'password_reset' => true,
'role' => UserRole::Admin,
'role' => UserRole::ADMIN,
]);
DB::table('provider_platforms')->insert([
'type' => ProviderPlatformType::CANVAS_OSS,
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/CanvasIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function test_fetching_users_from_canvas(): void
];
$resp = $this->actingAs($admin)->post('api/v1/actions/import-canvas-users', $req);
$resp->assertSuccessful();
$users = User::where(['role' => UserRole::Student])->get();
$users = User::where(['role' => UserRole::STUDENT])->get();
$this->assertDatabaseCount('users', 7);
foreach ($users as $user) {
// assert each has a provider mapping, and that they exist
Expand Down
3 changes: 2 additions & 1 deletion tests/Feature/UserControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests\Feature;

use App\Enums\UserRole;
use Database\Seeders\TestSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
Expand Down Expand Up @@ -58,7 +59,7 @@ public function testUpdateUser()
{
$this->seed(TestSeeder::class);
$user = \App\Models\User::factory()->admin()->create();
$response = $this->actingAs($user)->patch($this->uri.'/'.$user->id, ['name_first' => 'TestUpdate']);
$response = $this->actingAs($user)->patch($this->uri.'/'.$user->id, ['name_first' => 'TestUpdate', 'role' => UserRole::ADMIN]);
$response->assertStatus(200);
$this->assertTrue($response['data']['name_first'] == 'TestUpdate');
$response->assertJsonStructure([
Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/UserCourseActivityJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function it_processes_user_activity_data_correctly()
$resp = $this->actingAs($admin)->post('api/v1/actions/store-canvas-courses', ['provider_platform_id' => 1]);
$resp->assertSuccessful();

$users = User::where('role', UserRole::Student)->get();
$users = User::where('role', UserRole::STUDENT)->get();
foreach ($users as $user) {
$id = $user->id;
$resp = $this->actingAs($admin)->post('api/v1/actions/store-user-enrollments', ['provider_platform_id' => 1, 'user_id' => $id]);
Expand Down

0 comments on commit 7937c64

Please sign in to comment.