<?php namespace BookStack\Http\Controllers;
+use BookStack\Actions\ActivityType;
use BookStack\Api\ApiToken;
use BookStack\Auth\User;
use Illuminate\Http\Request;
-use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;
{
// 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('manage-users', $userId);
+ $this->checkPermissionOrCurrentUser('users-manage', $userId);
$user = User::query()->findOrFail($userId);
return view('users.api-tokens.create', [
public function store(Request $request, int $userId)
{
$this->checkPermission('access-api');
- $this->checkPermissionOrCurrentUser('manage-users', $userId);
+ $this->checkPermissionOrCurrentUser('users-manage', $userId);
$this->validate($request, [
'name' => 'required|max:250',
$user = User::query()->findOrFail($userId);
$secret = Str::random(32);
- $expiry = $request->get('expires_at', (Carbon::now()->addYears(100))->format('Y-m-d'));
$token = (new ApiToken())->forceFill([
'name' => $request->get('name'),
- 'client_id' => Str::random(32),
- 'client_secret' => Hash::make($secret),
+ 'token_id' => Str::random(32),
+ 'secret' => Hash::make($secret),
'user_id' => $user->id,
- 'expires_at' => $expiry
+ 'expires_at' => $request->get('expires_at') ?: ApiToken::defaultExpiry(),
]);
- while (ApiToken::query()->where('client_id', '=', $token->client_id)->exists()) {
- $token->client_id = Str::random(32);
+ while (ApiToken::query()->where('token_id', '=', $token->token_id)->exists()) {
+ $token->token_id = Str::random(32);
}
$token->save();
- // TODO - Notification and activity?
+
session()->flash('api-token-secret:' . $token->id, $secret);
+ $this->showSuccessNotification(trans('settings.user_api_token_create_success'));
+ $this->logActivity(ActivityType::API_TOKEN_CREATE, $token);
+
return redirect($user->getEditUrl('/api-tokens/' . $token->id));
}
]);
[$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
+ $token->fill([
+ 'name' => $request->get('name'),
+ 'expires_at' => $request->get('expires_at') ?: ApiToken::defaultExpiry(),
+ ])->save();
- $token->fill($request->all())->save();
- // TODO - Notification and activity?
+ $this->showSuccessNotification(trans('settings.user_api_token_update_success'));
+ $this->logActivity(ActivityType::API_TOKEN_UPDATE, $token);
return redirect($user->getEditUrl('/api-tokens/' . $token->id));
}
[$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
$token->delete();
- // TODO - Notification and activity?, Might have text in translations already (user_api_token_delete_success)
+ $this->showSuccessNotification(trans('settings.user_api_token_delete_success'));
+ $this->logActivity(ActivityType::API_TOKEN_DELETE, $token);
+
return redirect($user->getEditUrl('#api_tokens'));
}
*/
protected function checkPermissionAndFetchUserToken(int $userId, int $tokenId): array
{
- $this->checkPermission('access-api');
- $this->checkPermissionOrCurrentUser('manage-users', $userId);
+ $this->checkPermissionOr('users-manage', function () use ($userId) {
+ return $userId === user()->id && userCan('access-api');
+ });
$user = User::query()->findOrFail($userId);
$token = ApiToken::query()->where('user_id', '=', $user->id)->where('id', '=', $tokenId)->firstOrFail();