Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SUD-277 Add scorm owner #43

Merged
merged 1 commit into from
Jan 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use EscolaLms\Core\Models\User;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddUserIdColumnToScormTable extends Migration
{
public function up(): void
{
Schema::table('scorm', function (Blueprint $table) {
$table->foreignIdFor(User::class)->nullable()->constrained()->nullOnDelete();
});
}

public function down(): void
{
Schema::table('scorm', function (Blueprint $table) {
$table->dropColumn('user_id');
});
}
}
19 changes: 4 additions & 15 deletions database/seeders/PermissionTableSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,16 @@

class PermissionTableSeeder extends Seeder
{
public function run()
public function run(): void
{
// create permissions
$admin = Role::findOrCreate('admin', 'api');
$tutor = Role::findOrCreate('tutor', 'api');

$permissions = [
ScormPermissionsEnum::SCORM_LIST,
ScormPermissionsEnum::SCORM_READ,
ScormPermissionsEnum::SCORM_CREATE,
ScormPermissionsEnum::SCORM_UPDATE,
ScormPermissionsEnum::SCORM_DELETE,
ScormPermissionsEnum::SCORM_SET_TRACK,
ScormPermissionsEnum::SCORM_GET_TRACK,
];

foreach ($permissions as $permission) {
foreach (ScormPermissionsEnum::getValues() as $permission) {
Permission::findOrCreate($permission, 'api');
}

$admin->givePermissionTo($permissions);
$tutor->givePermissionTo($permissions);
$admin->givePermissionTo(ScormPermissionsEnum::adminPermissions());
$tutor->givePermissionTo(ScormPermissionsEnum::tutorPermissions());
}
}
29 changes: 29 additions & 0 deletions src/Enums/ScormPermissionsEnum.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,33 @@ class ScormPermissionsEnum extends BasicEnum
const SCORM_UPDATE = 'scorm_update';
const SCORM_GET_TRACK = 'scorm_track-read';
const SCORM_SET_TRACK = 'scorm_track-update';
const SCORM_LIST_OWN = 'scorm_list-own';
const SCORM_READ_OWN = 'scorm_read-own';
const SCORM_DELETE_OWN = 'scorm_delete-own';

public static function adminPermissions(): array
{
return [
ScormPermissionsEnum::SCORM_LIST,
ScormPermissionsEnum::SCORM_READ,
ScormPermissionsEnum::SCORM_CREATE,
ScormPermissionsEnum::SCORM_UPDATE,
ScormPermissionsEnum::SCORM_DELETE,
ScormPermissionsEnum::SCORM_SET_TRACK,
ScormPermissionsEnum::SCORM_GET_TRACK,
];
}

public static function tutorPermissions(): array
{
return [
ScormPermissionsEnum::SCORM_LIST_OWN,
ScormPermissionsEnum::SCORM_READ_OWN,
ScormPermissionsEnum::SCORM_CREATE,
ScormPermissionsEnum::SCORM_UPDATE,
ScormPermissionsEnum::SCORM_DELETE_OWN,
ScormPermissionsEnum::SCORM_SET_TRACK,
ScormPermissionsEnum::SCORM_GET_TRACK,
];
}
}
5 changes: 5 additions & 0 deletions src/EscolaLmsScormServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,9 @@ public function boot()
$this->loadMigrationsFrom(__DIR__ . '/../database/migrations');
$this->loadTranslationsFrom(__DIR__ . '/../resources/lang', 'scorm');
}

public function register(): void
{
$this->app->register(AuthServiceProvider::class);
}
}
27 changes: 15 additions & 12 deletions src/Http/Controllers/ScormController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function upload(ScormCreateRequest $request): JsonResponse
return $this->sendError($error->getMessage(), 422);
}

return $this->sendResponse($data, "Scorm Package uploaded successfully");
return $this->sendResponse($data, 'Scorm Package uploaded successfully');
}

public function parse(ScormCreateRequest $request): JsonResponse
Expand All @@ -56,7 +56,7 @@ public function parse(ScormCreateRequest $request): JsonResponse
$this->sendError($error->getMessage(), 422);
}

return $this->sendResponse($data, "Scorm Package uploaded successfully");
return $this->sendResponse($data, 'Scorm Package uploaded successfully');
}

public function show(string $uuid, Request $request): View
Expand All @@ -73,28 +73,31 @@ public function show(string $uuid, Request $request): View
public function index(ScormListRequest $request): JsonResponse
{
$list = $this->scormQueryService->get($request->pageParams(), ['*'], $request->searchParams(), OrderDto::instantiateFromRequest($request));
return $this->sendResponse($list, "Scorm list fetched successfully");

return $this->sendResponse($list, 'Scorm list fetched successfully');
}

public function getScos(ScormListRequest $request): JsonResponse
{
$columns = [
"id",
"scorm_id",
"uuid",
"entry_url",
"identifier",
"title",
"sco_parameters"
'id',
'scorm_id',
'uuid',
'entry_url',
'identifier',
'title',
'sco_parameters',
];

$list = $this->scormQueryService->allScos($columns);
return $this->sendResponse($list, "Scos list fetched successfully");

return $this->sendResponse($list, 'Scos list fetched successfully');
}

public function delete(ScormDeleteRequest $request, ScormModel $scormModel): JsonResponse
{
$this->scormService->deleteScormData($scormModel);
return $this->sendSuccess("Scorm Package deleted successfully");

return $this->sendSuccess('Scorm Package deleted successfully');
}
}
16 changes: 3 additions & 13 deletions src/Http/Requests/ScormCreateRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,17 @@

namespace EscolaLms\Scorm\Http\Requests;

use EscolaLms\Core\Models\User;
use EscolaLms\Scorm\Enums\ScormPermissionsEnum;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Gate;
use Peopleaps\Scorm\Model\ScormModel;

class ScormCreateRequest extends FormRequest
{
/**
* @return bool
*/
public function authorize(): bool
{
/** @var User $user */
$user = $this->user();
return $user->can(ScormPermissionsEnum::SCORM_CREATE, 'api');
return Gate::allows('create', ScormModel::class);
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
{
return [
Expand Down
13 changes: 2 additions & 11 deletions src/Http/Requests/ScormDeleteRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,15 @@
use EscolaLms\Core\Models\User;
use EscolaLms\Scorm\Enums\ScormPermissionsEnum;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Gate;

class ScormDeleteRequest extends FormRequest
{
/**
* @return bool
*/
public function authorize(): bool
{
/** @var User $user */
$user = $this->user();
return $user->can(ScormPermissionsEnum::SCORM_DELETE, 'api');
return Gate::allows('delete', $this->route('scormModel'));
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
{
return [];
Expand Down
25 changes: 11 additions & 14 deletions src/Http/Requests/ScormListRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,18 @@

namespace EscolaLms\Scorm\Http\Requests;

use EscolaLms\Core\Models\User;
use EscolaLms\Scorm\Enums\ScormPermissionsEnum;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Gate;
use Peopleaps\Scorm\Model\ScormModel;

class ScormListRequest extends FormRequest
{
/**
* @return bool
*/
public function authorize(): bool
{
/** @var User $user */
$user = $this->user();
return $user->can(ScormPermissionsEnum::SCORM_LIST, 'api');
return Gate::allows('list', ScormModel::class);
}

/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules(): array
{
return [];
Expand All @@ -35,8 +26,14 @@ public function pageParams(): ?int
: $this->get('per_page');
}

public function searchParams(): ?array
public function searchParams(): array
{
return $this->except(['limit', 'skip', 'order', 'order_by']);
$search = $this->except(['limit', 'skip', 'order', 'order_by']);

if (!$this->user()->can(ScormPermissionsEnum::SCORM_LIST) && $this->user()->can(ScormPermissionsEnum::SCORM_LIST_OWN)) {
$search['user_id'] = $this->user()->getKey();
}

return $search;
}
}
27 changes: 9 additions & 18 deletions src/Policies/ScormPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,38 +13,29 @@

public function list(User $user): bool
{
return $user->can(ScormPermissionsEnum::SCORM_LIST);
return $user->canAny([
ScormPermissionsEnum::SCORM_LIST,
ScormPermissionsEnum::SCORM_LIST_OWN,
]);
}

public function read(User $user): bool
public function read(User $user, ScormModel $scorm): bool

Check warning on line 22 in src/Policies/ScormPolicy.php

View check run for this annotation

Codecov / codecov/patch

src/Policies/ScormPolicy.php#L22

Added line #L22 was not covered by tests
{
return $user->can(ScormPermissionsEnum::SCORM_READ);
return $user->can(ScormPermissionsEnum::SCORM_READ)
|| ($user->can(ScormPermissionsEnum::SCORM_READ_OWN) && $scorm->user_id === $user->getKey());

Check warning on line 25 in src/Policies/ScormPolicy.php

View check run for this annotation

Codecov / codecov/patch

src/Policies/ScormPolicy.php#L24-L25

Added lines #L24 - L25 were not covered by tests
}

/**
* @param User $user
* @return bool
*/
public function create(User $user): bool
{
return $user->can(ScormPermissionsEnum::SCORM_CREATE);
}

/**
* @param User $user
* @param ScormModel $scorm
* @return bool
*/
public function delete(User $user, ScormModel $scorm): bool
{
return $user->can(ScormPermissionsEnum::SCORM_DELETE);
return $user->can(ScormPermissionsEnum::SCORM_DELETE)
|| ($user->can(ScormPermissionsEnum::SCORM_DELETE_OWN) && $scorm->user_id === $user->getKey());
}

/**
* @param User $user
* @param ScormModel $scorm
* @return bool
*/
public function update(User $user, ScormModel $scorm): bool
{
return $user->can(ScormPermissionsEnum::SCORM_UPDATE);
Expand Down
3 changes: 2 additions & 1 deletion src/Repositories/ScormRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public function listQuery(?array $columns = ['*'], ?array $search = []): Builder
->select($columns)
->when(isset($search['search']), fn($query) => $query
->whereHas('scos', fn($query) => $query->where('title', $this->likeOperator(), '%' . $search['search'] . '%'))
);
)
->when(isset($search['user_id']), fn($query) => $query->where('user_id', $search['user_id']));
}

private function likeOperator(): string
Expand Down
2 changes: 2 additions & 0 deletions src/Services/ScormService.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Peopleaps\Scorm\Entity\Scorm;
Expand Down Expand Up @@ -64,6 +65,7 @@ public function uploadScormArchive(UploadedFile $file): array
$scorm->origin_file = $scormData['name'];
$scorm->origin_file_mime = $scormData['type'];
$scorm->uuid = $scormData['hashName'];
$scorm->user_id = Auth::user() ? Auth::user()->getKey() : null;
$scorm->save();

$this->saveToDb($scormData['scos'], $scorm);
Expand Down
Loading
Loading