Skip to content

Commit 995b7d6

Browse files
authored
Merge pull request #4615 from BookStackApp/user_account
User preferences/options cleanup
2 parents 3274181 + 02bfaff commit 995b7d6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+1258
-619
lines changed

app/Access/Controllers/SocialController.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,12 @@
1616

1717
class SocialController extends Controller
1818
{
19-
protected SocialAuthService $socialAuthService;
20-
protected RegistrationService $registrationService;
21-
protected LoginService $loginService;
22-
23-
/**
24-
* SocialController constructor.
25-
*/
2619
public function __construct(
27-
SocialAuthService $socialAuthService,
28-
RegistrationService $registrationService,
29-
LoginService $loginService
20+
protected SocialAuthService $socialAuthService,
21+
protected RegistrationService $registrationService,
22+
protected LoginService $loginService,
3023
) {
3124
$this->middleware('guest')->only(['register']);
32-
$this->socialAuthService = $socialAuthService;
33-
$this->registrationService = $registrationService;
34-
$this->loginService = $loginService;
3525
}
3626

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

115-
return redirect(user()->getEditUrl());
105+
return redirect('/my-account/auth#social-accounts');
116106
}
117107

118108
/**

app/Access/SocialAuthService.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,21 +154,21 @@ public function handleLoginCallback(string $socialDriver, SocialUser $socialUser
154154
$currentUser->socialAccounts()->save($account);
155155
session()->flash('success', trans('settings.users_social_connected', ['socialAccount' => $titleCaseDriver]));
156156

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

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

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

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

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

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

215215
/**
216216
* Gets the names of the active social drivers.
217+
* @returns array<string, string>
217218
*/
218219
public function getActiveDrivers(): array
219220
{

app/Api/ApiDocsController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public function json()
3131

3232
/**
3333
* Redirect to the API docs page.
34+
* Required as a controller method, instead of the Route::redirect helper,
35+
* to ensure the URL is generated correctly.
3436
*/
3537
public function redirect()
3638
{

app/Api/ApiToken.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,12 @@ public function logDescriptor(): string
5252
{
5353
return "({$this->id}) {$this->name}; User: {$this->user->logDescriptor()}";
5454
}
55+
56+
/**
57+
* Get the URL for managing this token.
58+
*/
59+
public function getUrl(string $path = ''): string
60+
{
61+
return url("/api-tokens/{$this->user_id}/{$this->id}/" . trim($path, '/'));
62+
}
5563
}

app/Api/UserApiTokenController.php

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,19 @@ class UserApiTokenController extends Controller
1414
/**
1515
* Show the form to create a new API token.
1616
*/
17-
public function create(int $userId)
17+
public function create(Request $request, int $userId)
1818
{
19-
// Ensure user is has access-api permission and is the current user or has permission to manage the current user.
2019
$this->checkPermission('access-api');
2120
$this->checkPermissionOrCurrentUser('users-manage', $userId);
21+
$this->updateContext($request);
2222

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

25+
$this->setPageTitle(trans('settings.user_api_token_create'));
26+
2527
return view('users.api-tokens.create', [
2628
'user' => $user,
29+
'back' => $this->getRedirectPath($user),
2730
]);
2831
}
2932

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

63-
return redirect($user->getEditUrl('/api-tokens/' . $token->id));
66+
return redirect($token->getUrl());
6467
}
6568

6669
/**
6770
* Show the details for a user API token, with access to edit.
6871
*/
69-
public function edit(int $userId, int $tokenId)
72+
public function edit(Request $request, int $userId, int $tokenId)
7073
{
74+
$this->updateContext($request);
75+
7176
[$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
7277
$secret = session()->pull('api-token-secret:' . $token->id, null);
7378

79+
$this->setPageTitle(trans('settings.user_api_token'));
80+
7481
return view('users.api-tokens.edit', [
7582
'user' => $user,
7683
'token' => $token,
7784
'model' => $token,
7885
'secret' => $secret,
86+
'back' => $this->getRedirectPath($user),
7987
]);
8088
}
8189

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

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

100-
return redirect($user->getEditUrl('/api-tokens/' . $token->id));
108+
return redirect($token->getUrl());
101109
}
102110

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

118+
$this->setPageTitle(trans('settings.user_api_token_delete'));
119+
110120
return view('users.api-tokens.delete', [
111121
'user' => $user,
112122
'token' => $token,
@@ -123,7 +133,7 @@ public function destroy(int $userId, int $tokenId)
123133

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

126-
return redirect($user->getEditUrl('#api_tokens'));
136+
return redirect($this->getRedirectPath($user));
127137
}
128138

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

143153
return [$user, $token];
144154
}
155+
156+
/**
157+
* Update the context for where the user is coming from to manage API tokens.
158+
* (Track of location for correct return redirects)
159+
*/
160+
protected function updateContext(Request $request): void
161+
{
162+
$context = $request->query('context');
163+
if ($context) {
164+
session()->put('api-token-context', $context);
165+
}
166+
}
167+
168+
/**
169+
* Get the redirect path for the current api token editing session.
170+
* Attempts to recall the context of where the user is editing from.
171+
*/
172+
protected function getRedirectPath(User $relatedUser): string
173+
{
174+
$context = session()->get('api-token-context');
175+
if ($context === 'settings' || user()->id !== $relatedUser->id) {
176+
return $relatedUser->getEditUrl('#api_tokens');
177+
}
178+
179+
return url('/my-account/auth#api_tokens');
180+
}
145181
}

0 commit comments

Comments
 (0)