<?php namespace BookStack\Auth;
use BookStack\Api\ApiToken;
+use BookStack\Entities\Tools\SlugGenerator;
use BookStack\Interfaces\Loggable;
use BookStack\Interfaces\Sluggable;
use BookStack\Model;
*/
public function getProfileUrl(): string
{
- return url('/user/' . $this->id);
+ return url('/user/' . $this->slug);
}
/**
{
return "({$this->id}) {$this->name}";
}
+
+ /**
+ * @inheritDoc
+ */
+ public function refreshSlug(): string
+ {
+ $this->slug = app(SlugGenerator::class)->generate($this);
+ return $this->slug;
+ }
}
return User::query()->findOrFail($id);
}
+ /**
+ * Get a user by their slug.
+ */
+ public function getBySlug(string $slug): User
+ {
+ return User::query()->where('slug', '=', $slug)->firstOrFail();
+ }
+
/**
* Get all the users with their permissions.
*/
}
/**
- * Generate and set a new URL slug for this model.
+ * @inheritdoc
*/
public function refreshSlug(): string
{
- $this->slug = (new SlugGenerator)->generate($this);
+ $this->slug = app(SlugGenerator::class)->generate($this);
return $this->slug;
}
}
use BookStack\Auth\Access\UserInviteService;
use BookStack\Auth\User;
use BookStack\Auth\UserRepo;
+use BookStack\Exceptions\ImageUploadException;
use BookStack\Exceptions\UserUpdateException;
use BookStack\Uploads\ImageRepo;
+use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
+use Illuminate\Validation\ValidationException;
class UserController extends Controller
{
/**
* Store a newly created user in storage.
* @throws UserUpdateException
- * @throws \Illuminate\Validation\ValidationException
+ * @throws ValidationException
*/
public function store(Request $request)
{
$user->external_auth_id = $request->get('external_auth_id');
}
+ $user->refreshSlug();
$user->save();
if ($sendInvite) {
/**
* Update the specified user in storage.
* @throws UserUpdateException
- * @throws \BookStack\Exceptions\ImageUploadException
- * @throws \Illuminate\Validation\ValidationException
+ * @throws ImageUploadException
+ * @throws ValidationException
*/
public function update(Request $request, int $id)
{
$user->email = $request->get('email');
}
+ // Refresh the slug if the user's name has changed
+ if ($user->isDirty('name')) {
+ $user->refreshSlug();
+ }
+
// Role updates
if (userCan('users-manage') && $request->filled('roles')) {
$roles = $request->get('roles');
/**
* Remove the specified user from storage.
- * @throws \Exception
+ * @throws Exception
*/
public function destroy(Request $request, int $id)
{
return redirect('/settings/users');
}
- /**
- * Show the user profile page
- */
- public function showProfilePage($id)
- {
- $user = $this->userRepo->getById($id);
-
- $userActivity = $this->userRepo->getActivity($user);
- $recentlyCreated = $this->userRepo->getRecentlyCreated($user, 5);
- $assetCounts = $this->userRepo->getAssetCounts($user);
-
- return view('users.profile', [
- 'user' => $user,
- 'activity' => $userActivity,
- 'recentlyCreated' => $recentlyCreated,
- 'assetCounts' => $assetCounts
- ]);
- }
-
/**
* Update the user's preferred book-list display setting.
*/
--- /dev/null
+<?php namespace BookStack\Http\Controllers;
+
+use BookStack\Auth\UserRepo;
+
+class UserProfileController extends Controller
+{
+ /**
+ * Show the user profile page
+ */
+ public function show(UserRepo $repo, string $slug)
+ {
+ $user = $repo->getBySlug($slug);
+
+ $userActivity = $repo->getActivity($user);
+ $recentlyCreated = $repo->getRecentlyCreated($user, 5);
+ $assetCounts = $repo->getAssetCounts($user);
+
+ return view('users.profile', [
+ 'user' => $user,
+ 'activity' => $userActivity,
+ 'recentlyCreated' => $recentlyCreated,
+ 'assetCounts' => $assetCounts
+ ]);
+ }
+}
interface Sluggable
{
+ /**
+ * Regenerate the slug for this model.
+ */
+ public function refreshSlug(): string;
+
}
\ No newline at end of file
</span>
<ul refs="dropdown@menu" class="dropdown-menu" role="menu">
<li>
- <a href="{{ url("/user/{$currentUser->id}") }}">@icon('user'){{ trans('common.view_profile') }}</a>
+ <a href="{{ $currentUser->getProfileUrl() }}">@icon('user'){{ trans('common.view_profile') }}</a>
</li>
<li>
- <a href="{{ url("/settings/users/{$currentUser->id}") }}">@icon('edit'){{ trans('common.edit_profile') }}</a>
+ <a href="{{ $currentUser->getEditUrl() }}">@icon('edit'){{ trans('common.edit_profile') }}</a>
</li>
<li>
@if(config('auth.method') === 'saml2')
});
// User Profile routes
- Route::get('/user/{userId}', 'UserController@showProfilePage');
+ Route::get('/user/{slug}', 'UserProfileController@show');
// Image routes
Route::get('/images/gallery', 'Images\GalleryImageController@list');