2019-12-29 08:02:26 -05:00
|
|
|
<?php namespace BookStack\Http\Controllers;
|
|
|
|
|
2020-11-20 13:53:01 -05:00
|
|
|
use BookStack\Actions\ActivityType;
|
2019-12-29 12:03:52 -05:00
|
|
|
use BookStack\Api\ApiToken;
|
|
|
|
use BookStack\Auth\User;
|
2019-12-29 08:02:26 -05:00
|
|
|
use Illuminate\Http\Request;
|
2019-12-29 12:03:52 -05:00
|
|
|
use Illuminate\Support\Facades\Hash;
|
|
|
|
use Illuminate\Support\Str;
|
2019-12-29 08:02:26 -05:00
|
|
|
|
|
|
|
class UserApiTokenController extends Controller
|
|
|
|
{
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the form to create a new API token.
|
|
|
|
*/
|
|
|
|
public function create(int $userId)
|
|
|
|
{
|
2019-12-29 12:03:52 -05:00
|
|
|
// Ensure user is has access-api permission and is the current user or has permission to manage the current user.
|
2019-12-29 08:02:26 -05:00
|
|
|
$this->checkPermission('access-api');
|
2019-12-29 14:46:46 -05:00
|
|
|
$this->checkPermissionOrCurrentUser('users-manage', $userId);
|
2019-12-29 08:02:26 -05:00
|
|
|
|
2019-12-29 12:03:52 -05:00
|
|
|
$user = User::query()->findOrFail($userId);
|
|
|
|
return view('users.api-tokens.create', [
|
|
|
|
'user' => $user,
|
|
|
|
]);
|
2019-12-29 08:02:26 -05:00
|
|
|
}
|
|
|
|
|
2019-12-29 12:03:52 -05:00
|
|
|
/**
|
|
|
|
* Store a new API token in the system.
|
|
|
|
*/
|
|
|
|
public function store(Request $request, int $userId)
|
|
|
|
{
|
|
|
|
$this->checkPermission('access-api');
|
2019-12-29 14:46:46 -05:00
|
|
|
$this->checkPermissionOrCurrentUser('users-manage', $userId);
|
2019-12-29 12:03:52 -05:00
|
|
|
|
|
|
|
$this->validate($request, [
|
|
|
|
'name' => 'required|max:250',
|
|
|
|
'expires_at' => 'date_format:Y-m-d',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$user = User::query()->findOrFail($userId);
|
|
|
|
$secret = Str::random(32);
|
2019-12-29 15:18:37 -05:00
|
|
|
|
2019-12-29 12:03:52 -05:00
|
|
|
$token = (new ApiToken())->forceFill([
|
|
|
|
'name' => $request->get('name'),
|
2019-12-29 15:07:28 -05:00
|
|
|
'token_id' => Str::random(32),
|
|
|
|
'secret' => Hash::make($secret),
|
2019-12-29 12:03:52 -05:00
|
|
|
'user_id' => $user->id,
|
2019-12-30 09:51:28 -05:00
|
|
|
'expires_at' => $request->get('expires_at') ?: ApiToken::defaultExpiry(),
|
2019-12-29 12:03:52 -05:00
|
|
|
]);
|
|
|
|
|
2019-12-29 15:07:28 -05:00
|
|
|
while (ApiToken::query()->where('token_id', '=', $token->token_id)->exists()) {
|
|
|
|
$token->token_id = Str::random(32);
|
2019-12-29 12:03:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
$token->save();
|
2019-12-29 14:46:46 -05:00
|
|
|
|
2019-12-29 12:03:52 -05:00
|
|
|
session()->flash('api-token-secret:' . $token->id, $secret);
|
2019-12-29 14:46:46 -05:00
|
|
|
$this->showSuccessNotification(trans('settings.user_api_token_create_success'));
|
2020-11-20 13:53:01 -05:00
|
|
|
$this->logActivity(ActivityType::API_TOKEN_CREATE, $token);
|
|
|
|
|
2019-12-29 12:03:52 -05:00
|
|
|
return redirect($user->getEditUrl('/api-tokens/' . $token->id));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the details for a user API token, with access to edit.
|
|
|
|
*/
|
|
|
|
public function edit(int $userId, int $tokenId)
|
|
|
|
{
|
|
|
|
[$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
|
|
|
|
$secret = session()->pull('api-token-secret:' . $token->id, null);
|
|
|
|
|
|
|
|
return view('users.api-tokens.edit', [
|
|
|
|
'user' => $user,
|
|
|
|
'token' => $token,
|
|
|
|
'model' => $token,
|
|
|
|
'secret' => $secret,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Update the API token.
|
|
|
|
*/
|
|
|
|
public function update(Request $request, int $userId, int $tokenId)
|
|
|
|
{
|
2019-12-30 09:51:28 -05:00
|
|
|
$this->validate($request, [
|
2019-12-29 12:03:52 -05:00
|
|
|
'name' => 'required|max:250',
|
|
|
|
'expires_at' => 'date_format:Y-m-d',
|
|
|
|
]);
|
|
|
|
|
|
|
|
[$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
|
2019-12-30 09:51:28 -05:00
|
|
|
$token->fill([
|
|
|
|
'name' => $request->get('name'),
|
|
|
|
'expires_at' => $request->get('expires_at') ?: ApiToken::defaultExpiry(),
|
|
|
|
])->save();
|
2019-12-29 12:03:52 -05:00
|
|
|
|
2019-12-29 14:46:46 -05:00
|
|
|
$this->showSuccessNotification(trans('settings.user_api_token_update_success'));
|
2020-11-20 13:53:01 -05:00
|
|
|
$this->logActivity(ActivityType::API_TOKEN_UPDATE, $token);
|
2019-12-29 12:03:52 -05:00
|
|
|
return redirect($user->getEditUrl('/api-tokens/' . $token->id));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show the delete view for this token.
|
|
|
|
*/
|
|
|
|
public function delete(int $userId, int $tokenId)
|
|
|
|
{
|
|
|
|
[$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
|
|
|
|
return view('users.api-tokens.delete', [
|
|
|
|
'user' => $user,
|
|
|
|
'token' => $token,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy a token from the system.
|
|
|
|
*/
|
|
|
|
public function destroy(int $userId, int $tokenId)
|
|
|
|
{
|
|
|
|
[$user, $token] = $this->checkPermissionAndFetchUserToken($userId, $tokenId);
|
|
|
|
$token->delete();
|
|
|
|
|
2019-12-29 14:46:46 -05:00
|
|
|
$this->showSuccessNotification(trans('settings.user_api_token_delete_success'));
|
2020-11-20 13:53:01 -05:00
|
|
|
$this->logActivity(ActivityType::API_TOKEN_DELETE, $token);
|
|
|
|
|
2019-12-29 12:03:52 -05:00
|
|
|
return redirect($user->getEditUrl('#api_tokens'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Check the permission for the current user and return an array
|
|
|
|
* where the first item is the user in context and the second item is their
|
|
|
|
* API token in context.
|
|
|
|
*/
|
|
|
|
protected function checkPermissionAndFetchUserToken(int $userId, int $tokenId): array
|
|
|
|
{
|
2019-12-29 14:46:46 -05:00
|
|
|
$this->checkPermissionOr('users-manage', function () use ($userId) {
|
|
|
|
return $userId === user()->id && userCan('access-api');
|
|
|
|
});
|
2019-12-29 12:03:52 -05:00
|
|
|
|
|
|
|
$user = User::query()->findOrFail($userId);
|
|
|
|
$token = ApiToken::query()->where('user_id', '=', $user->id)->where('id', '=', $tokenId)->firstOrFail();
|
|
|
|
return [$user, $token];
|
|
|
|
}
|
2019-12-29 08:02:26 -05:00
|
|
|
}
|