Skip to content
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
18 changes: 4 additions & 14 deletions app/Access/Controllers/SocialController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,12 @@

class SocialController extends Controller
{
protected SocialAuthService $socialAuthService;
protected RegistrationService $registrationService;
protected LoginService $loginService;

/**
* SocialController constructor.
*/
public function __construct(
SocialAuthService $socialAuthService,
RegistrationService $registrationService,
LoginService $loginService
protected SocialAuthService $socialAuthService,
protected RegistrationService $registrationService,
protected LoginService $loginService,
) {
$this->middleware('guest')->only(['register']);
$this->socialAuthService = $socialAuthService;
$this->registrationService = $registrationService;
$this->loginService = $loginService;
}

/**
Expand Down Expand Up @@ -112,7 +102,7 @@ public function detach(string $socialDriver)
$this->socialAuthService->detachSocialAccount($socialDriver);
session()->flash('success', trans('settings.users_social_disconnected', ['socialAccount' => Str::title($socialDriver)]));

return redirect(user()->getEditUrl());
return redirect('/my-account/auth#social-accounts');
}

/**
Expand Down
7 changes: 4 additions & 3 deletions app/Access/SocialAuthService.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,21 +154,21 @@ public function handleLoginCallback(string $socialDriver, SocialUser $socialUser
$currentUser->socialAccounts()->save($account);
session()->flash('success', trans('settings.users_social_connected', ['socialAccount' => $titleCaseDriver]));

return redirect($currentUser->getEditUrl());
return redirect('/my-account/auth#social_accounts');
}

// When a user is logged in and the social account exists and is already linked to the current user.
if ($isLoggedIn && $socialAccount !== null && $socialAccount->user->id === $currentUser->id) {
session()->flash('error', trans('errors.social_account_existing', ['socialAccount' => $titleCaseDriver]));

return redirect($currentUser->getEditUrl());
return redirect('/my-account/auth#social_accounts');
}

// When a user is logged in, A social account exists but the users do not match.
if ($isLoggedIn && $socialAccount !== null && $socialAccount->user->id != $currentUser->id) {
session()->flash('error', trans('errors.social_account_already_used_existing', ['socialAccount' => $titleCaseDriver]));

return redirect($currentUser->getEditUrl());
return redirect('/my-account/auth#social_accounts');
}

// Otherwise let the user know this social account is not used by anyone.
Expand Down Expand Up @@ -214,6 +214,7 @@ protected function checkDriverConfigured(string $driver): bool

/**
* Gets the names of the active social drivers.
* @returns array<string, string>
*/
public function getActiveDrivers(): array
{
Expand Down
2 changes: 2 additions & 0 deletions app/Api/ApiDocsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public function json()

/**
* Redirect to the API docs page.
* Required as a controller method, instead of the Route::redirect helper,
* to ensure the URL is generated correctly.
*/
public function redirect()
{
Expand Down
8 changes: 8 additions & 0 deletions app/Api/ApiToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,12 @@ public function logDescriptor(): string
{
return "({$this->id}) {$this->name}; User: {$this->user->logDescriptor()}";
}

/**
* Get the URL for managing this token.
*/
public function getUrl(string $path = ''): string
{
return url("/api-tokens/{$this->user_id}/{$this->id}/" . trim($path, '/'));
}
}
48 changes: 42 additions & 6 deletions app/Api/UserApiTokenController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@ class UserApiTokenController extends Controller
/**
* Show the form to create a new API token.
*/
public function create(int $userId)
public function create(Request $request, int $userId)
{
// Ensure user is has access-api permission and is the current user or has permission to manage the current user.
$this->checkPermission('access-api');
$this->checkPermissionOrCurrentUser('users-manage', $userId);
$this->updateContext($request);

$user = User::query()->findOrFail($userId);

$this->setPageTitle(trans('settings.user_api_token_create'));

return view('users.api-tokens.create', [
'user' => $user,
'back' => $this->getRedirectPath($user),
]);
}

Expand Down Expand Up @@ -60,22 +63,27 @@ public function store(Request $request, int $userId)
session()->flash('api-token-secret:' . $token->id, $secret);
$this->logActivity(ActivityType::API_TOKEN_CREATE, $token);

return redirect($user->getEditUrl('/api-tokens/' . $token->id));
return redirect($token->getUrl());
}

/**
* Show the details for a user API token, with access to edit.
*/
public function edit(int $userId, int $tokenId)
public function edit(Request $request, int $userId, int $tokenId)
{
$this->updateContext($request);

[$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
$secret = session()->pull('api-token-secret:' . $token->id, null);

$this->setPageTitle(trans('settings.user_api_token'));

return view('users.api-tokens.edit', [
'user' => $user,
'token' => $token,
'model' => $token,
'secret' => $secret,
'back' => $this->getRedirectPath($user),
]);
}

Expand All @@ -97,7 +105,7 @@ public function update(Request $request, int $userId, int $tokenId)

$this->logActivity(ActivityType::API_TOKEN_UPDATE, $token);

return redirect($user->getEditUrl('/api-tokens/' . $token->id));
return redirect($token->getUrl());
}

/**
Expand All @@ -107,6 +115,8 @@ public function delete(int $userId, int $tokenId)
{
[$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);

$this->setPageTitle(trans('settings.user_api_token_delete'));

return view('users.api-tokens.delete', [
'user' => $user,
'token' => $token,
Expand All @@ -123,7 +133,7 @@ public function destroy(int $userId, int $tokenId)

$this->logActivity(ActivityType::API_TOKEN_DELETE, $token);

return redirect($user->getEditUrl('#api_tokens'));
return redirect($this->getRedirectPath($user));
}

/**
Expand All @@ -142,4 +152,30 @@ protected function checkPermissionAndFetchUserToken(int $userId, int $tokenId):

return [$user, $token];
}

/**
* Update the context for where the user is coming from to manage API tokens.
* (Track of location for correct return redirects)
*/
protected function updateContext(Request $request): void
{
$context = $request->query('context');
if ($context) {
session()->put('api-token-context', $context);
}
}

/**
* Get the redirect path for the current api token editing session.
* Attempts to recall the context of where the user is editing from.
*/
protected function getRedirectPath(User $relatedUser): string
{
$context = session()->get('api-token-context');
if ($context === 'settings' || user()->id !== $relatedUser->id) {
return $relatedUser->getEditUrl('#api_tokens');
}

return url('/my-account/auth#api_tokens');
}
}
Loading