Skip to content

Commit

Permalink
Refactored validateAccess method.
Browse files Browse the repository at this point in the history
  • Loading branch information
danidou committed Apr 12, 2024
1 parent 1c9891b commit 247e8d7
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions src/Http/Controllers/Api/CourseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Ctrlweb\BadgeFactor2\Http\Controllers\Api;

use App\Helpers\ECommerceHelper;
use Ctrlweb\BadgeFactor2\Http\Controllers\Controller;
use Ctrlweb\BadgeFactor2\Models\Badges\BadgePage;
use Ctrlweb\BadgeFactor2\Models\Courses\Course;
Expand All @@ -11,27 +12,19 @@
*/
class CourseController extends Controller
{
public function validateAccess(string $locale, $course)
public function validateAccess(string $locale, string $slug)
{
$hasAccess = false;
$badgePage = BadgePage::where('slug->fr', $course)->first();
$course = Course::where('slug->fr', $slug)->firstOrFail();
$currentUser = auth()->user();
if ($currentUser && $badgePage) {
if ($currentUser->free_access) {
$hasAccess = true;
} else {
// TODO Check if course purchased.
}
}

if ($hasAccess) {
if ($currentUser->freeAccess || ECommerceHelper::hasAccess($currentUser, $course)) {
return response()->json([
'access' => $hasAccess,
'access' => true,
]);
} else {
return response()->json([
'access' => $hasAccess,
'redirect' => config('badgefactor2.frontend.url').'/badges/'.$badgePage->slug,
'access' => false,
'redirect' => config('badgefactor2.frontend.url').'/badges/'.$course->badgePage->slug,
], 302);
}
}
Expand Down

0 comments on commit 247e8d7

Please sign in to comment.