Skip to content

Commit

Permalink
Add option to link services to user account
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaszkadziela committed Jun 7, 2024
1 parent 6b8b01d commit a2cd9dd
Show file tree
Hide file tree
Showing 12 changed files with 172 additions and 7 deletions.
62 changes: 62 additions & 0 deletions app/Http/Controllers/ServiceController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

namespace App\Http\Controllers;

use App\Models\Service;
use App\Models\User;
use App\View\Components\Notification;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Lang;

class ServiceController extends Controller
{
/**
* Link service to user account.
*/
public function link(Request $request): RedirectResponse
{
$request->validate([
'serviceId' => 'required',
]);

/** @var User $user */
$user = Auth::user();
$service = Service::enabled()
->where('id', '=', $request->serviceId)
->when(!$user?->is_admin, fn (Builder $query) => $query->where('is_public', '=', true))
->first();

if ($user === null || $service === null) {
Notification::push(
Lang::get('notifications.in-app.service-link-failed.title'),
Lang::get('notifications.in-app.service-link-failed.description'),
Notification::DANGER,
);

return redirect()->back();
}

$redirectLink = match ($service->name) {
'EverestCloud' => $service->link . '/apps/sociallogin/custom_oauth2/everestserver',
'EverestGit' => $service->link . '/-/profile/account',
default => null,
};

if ($redirectLink !== null) {
return redirect($redirectLink);
}

$user->services()->save($service);

Notification::push(
Lang::get('notifications.in-app.service-linked.title'),
Lang::get('notifications.in-app.service-linked.description', ['service' => $service->name]),
Notification::SUCCESS,
);

return redirect()->back();
}
}
1 change: 1 addition & 0 deletions lang/en/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
'try-again-later' => 'Please try again later',

'actions' => [
'cancel' => 'Cancel',
'close' => 'Close',
],
'cookies' => [
Expand Down
8 changes: 8 additions & 0 deletions lang/en/notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@
'title' => 'Reset link sent',
'description' => 'Password reset link has been sent successfully',
],
'service-link-failed' => [
'title' => 'Service link failed',
'description' => 'The requested service could not be linked to your account',
],
'service-linked' => [
'title' => 'Service linked',
'description' => ':service service has been successfully linked to your account',
],
'verification-link-sent' => [
'title' => 'Verification link sent',
'description' => 'Email verification link has been sent successfully',
Expand Down
1 change: 0 additions & 1 deletion lang/en/profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/

'actions' => [
'cancel' => 'Cancel',
'delete-account' => 'Delete account',
'save-confirmation' => 'Saved successfully',
'save' => 'Save',
Expand Down
9 changes: 9 additions & 0 deletions lang/en/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/

'identifier' => 'Account identifier',
'link-more-services' => 'Looking for another service? Link more services below!',
'link-new' => 'Click here to link a new service',
'navigate' => 'Navigate',
'not-available' => 'There are no services available at this time',
'not-linked' => 'No services have been linked to your account yet',
Expand All @@ -21,5 +23,12 @@
'everestgit' => 'Everest<span class="font-bold">Git</span>',
'everestpass' => 'Everest<span class="font-bold">Pass</span>',
],
'link-modal' => [
'title' => 'Link new service',
'description' => 'Below you can choose a service that you want to link with your account',
'description-2' => 'If you already have an account, you will be redirected to the selected service to connect the service from there. Otherwise, a new account will be created for you for this service',
'choose-service' => 'Choose service',
'link-service' => 'Link service',
],

];
1 change: 1 addition & 0 deletions lang/pl/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
'try-again-later' => 'Proszę spróbować później',

'actions' => [
'cancel' => 'Anuluj',
'close' => 'Zamknij',
],
'cookies' => [
Expand Down
8 changes: 8 additions & 0 deletions lang/pl/notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@
'title' => 'Wysłano link resetujący',
'description' => 'Link do resetowania hasła został pomyślnie wysłany',
],
'service-link-failed' => [
'title' => 'Powiązanie nie powiodło się',
'description' => 'Żądanej usługi nie można powiązać z Twoim kontem',
],
'service-linked' => [
'title' => 'Usługa powiązana',
'description' => 'Usługa :service została pomyślnie powiązana z Twoim kontem',
],
'verification-link-sent' => [
'title' => 'Wysłano link weryfikacyjny',
'description' => 'Link weryfikujący e-mail został pomyślnie wysłany',
Expand Down
1 change: 0 additions & 1 deletion lang/pl/profile.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*/

'actions' => [
'cancel' => 'Anuluj',
'delete-account' => 'Usuń konto',
'save-confirmation' => 'Pomyślnie zapisano',
'save' => 'Zapisz',
Expand Down
9 changes: 9 additions & 0 deletions lang/pl/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
*/

'identifier' => 'Identyfikator konta',
'link-more-services' => 'Szukasz innej usługi? Powiąż więcej usług poniżej!',
'link-new' => 'Kliknij tutaj, aby powiązać nową usługę',
'navigate' => 'Przejdź do',
'not-available' => 'Obecnie nie są dostępne żadne usługi',
'not-linked' => 'Żadne usługi nie zostały jeszcze powiązane z Twoim kontem',
Expand All @@ -21,5 +23,12 @@
'everestgit' => 'Everest<span class="font-bold">Git</span>',
'everestpass' => 'Everest<span class="font-bold">Pass</span>',
],
'link-modal' => [
'title' => 'Powiąż nową usługę',
'description' => 'Poniżej możesz wybrać usługę, którą chcesz powiązać ze swoim kontem',
'description-2' => 'Jeśli posiadasz już konto, zostaniesz przekierowany do wybranej usługi, w celu powiązania konta. W przeciwnym razie zostanie dla Ciebie utworzone nowe konto dla tej usługi',
'choose-service' => 'Wybierz usługę',
'link-service' => 'Powiąż usługę',
],

];
11 changes: 11 additions & 0 deletions resources/views/components/link-service-button.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@props(['modal'])

<button x-data
class="border-2 border-dashed border-gray-300 cursor-pointer flex gap-2 hover:border-gray-400 hover:text-gray-800 items-center p-4 rounded-lg text-gray-600 transition-colors"
@click="$dispatch('open-modal', '{{ $modal }}')"
>
<i class="fa-solid fa-square-plus text-2xl"></i>
<span class="font-medium text-lg">
{{ $slot }}
</span>
</button>
61 changes: 57 additions & 4 deletions resources/views/dashboard.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,13 @@
</h2>
</x-slot>

@php($enabledServices = Auth::user()->is_admin ? Service::enabled()->get() : Service::public()->enabled()->get())
@php($linkedServices = Auth::user()->services()->enabled()->get())
@php($unlinkedServices = $enabledServices->diff($linkedServices))

<main class="flex flex-1 flex-col my-12 w-full">
<div class="lg:px-8 max-w-7xl mx-auto sm:px-6 space-y-6 w-full">
@forelse (Auth::user()->services()->enabled()->get() as $service)
@forelse ($linkedServices as $service)
<div class="bg-white p-4 relative shadow sm:p-8 sm:rounded-lg">
<div class="flex gap-4 items-start justify-between">
<div>
Expand Down Expand Up @@ -47,11 +51,14 @@
</div>
</div>
@empty
<div>
@if (Service::enabled()->count() > 0)
<p class="font-bold">
<div class="bg-white p-4 relative shadow sm:p-8 sm:rounded-lg">
@if($enabledServices->count() > 0)
<p class="font-bold mb-4">
{{ Lang::get('services.not-linked') }}
</p>
<x-link-service-button modal="link-service">
{{ Lang::get('services.link-new') }}
</x-link-service-button>
@else
<p class="font-bold">
{{ Lang::get('services.not-available') }}
Expand All @@ -61,6 +68,52 @@
@endif
</div>
@endforelse
@if($linkedServices->count() > 0 && $unlinkedServices->count() > 0)
<div class="bg-white p-4 relative shadow sm:p-8 sm:rounded-lg">
<p class="font-bold mb-4">
{{ Lang::get('services.link-more-services') }}
</p>
<x-link-service-button modal="link-service">
{{ Lang::get('services.link-new') }}
</x-link-service-button>
</div>
@endif
</div>
<x-modal.main name="link-service">
<form method="post" action="{{ route('services.link') }}" class="p-6">
@csrf
<h2 class="flex font-medium gap-2 items-center mb-6 text-gray-900 text-lg">
<i class="fa-solid fa-square-plus text-2xl"></i>
{{ Lang::get('services.link-modal.title') }}
</h2>
<p class="mb-2 text-gray-600 text-sm">
{{ Lang::get('services.link-modal.description') }}.
</p>
<p class="mb-6 text-gray-600 text-sm">
{{ Lang::get('services.link-modal.description-2') }}.
</p>
<div>
<x-input-label for="serviceId" value="{{ Lang::get('services.link-modal.choose-service') }}" />
<select id="serviceId"
name="serviceId"
class="block border-gray-300 focus:border-blue-700 focus:ring-blue-700 mt-1 rounded-md shadow-sm w-full"
>
@foreach ($unlinkedServices as $service)
<option value="{{ $service->id }}">
{{ $service->name }}
</option>
@endforeach
</select>
</div>
<div class="mt-6 flex justify-end">
<x-modal.secondary-button @click="$dispatch('close')">
{{ Lang::get('main.actions.cancel') }}
</x-modal.secondary-button>
<x-modal.primary-button class="ms-3">
{{ Lang::get('services.link-modal.link-service') }}
</x-modal.primary-button>
</div>
</form>
</x-modal.main>
</main>
</x-main-layout>
7 changes: 6 additions & 1 deletion routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use App\Http\Controllers\LanguageController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\ServiceController;
use Illuminate\Support\Facades\Route;

/*
Expand All @@ -18,9 +19,13 @@
Route::get('/', fn () => view('home'))->name('home');
Route::get('/privacy', fn () => view('privacy'))->name('privacy');

Route::get('/dashboard', fn () => view('dashboard'))->middleware(['auth', 'verified'])->name('dashboard');
Route::get('/language/{code}', [LanguageController::class, 'change']);

Route::middleware(['auth', 'verified'])->group(function () {
Route::get('/dashboard', fn () => view('dashboard'))->name('dashboard');
Route::post('/services/link', [ServiceController::class, 'link'])->name('services.link');
});

Route::middleware(['auth', 'password.confirm'])->group(function () {
Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
Expand Down

0 comments on commit a2cd9dd

Please sign in to comment.