-
-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This is a ton of work. A new system user is generated while running the migrations. System tokes are bound to that user. Api calls need to be properly authorized, which feels really hacky at the moment. I only implemented link api tests for now.
- Loading branch information
Showing
44 changed files
with
847 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace App\Http\Controllers\Admin; | ||
|
||
use App\Enums\ActivityLog; | ||
use App\Enums\ApiToken; | ||
use App\Http\Controllers\Controller; | ||
use App\Http\Requests\Admin\CreateSystemApiTokenRequest; | ||
use App\Models\User; | ||
use Illuminate\Http\RedirectResponse; | ||
use Illuminate\Http\Request; | ||
use Laravel\Sanctum\PersonalAccessToken; | ||
|
||
class ApiTokenController extends Controller | ||
{ | ||
public function index() | ||
{ | ||
return view('admin.api-tokens.index', [ | ||
'tokens' => User::getSystemUser()->tokens()->get(), | ||
]); | ||
} | ||
|
||
public function show(PersonalAccessToken $token) | ||
{ | ||
return view('admin.api-tokens.show', [ | ||
'token' => $token, | ||
]); | ||
} | ||
|
||
public function store(CreateSystemApiTokenRequest $request): RedirectResponse | ||
{ | ||
$abilities = $request->validated('abilities'); | ||
|
||
if ($request->get('private_access', false)) { | ||
$abilities[] = ApiToken::ABILITY_SYSTEM_ACCESS_PRIVATE; | ||
} else { | ||
$abilities[] = ApiToken::ABILITY_SYSTEM_ACCESS; | ||
} | ||
|
||
$token = User::getSystemUser()->createToken($request->validated('token_name'), $abilities); | ||
|
||
activity() | ||
->by($request->user()) | ||
->withProperty('token_id', $token->accessToken->id) | ||
->log(ActivityLog::SYSTEM_API_TOKEN_GENERATED); | ||
|
||
session()->flash('new_token', $token->plainTextToken); | ||
|
||
return redirect()->route('system.api-tokens.show', ['api_token' => $token->accessToken]); | ||
} | ||
|
||
public function destroy(Request $request, PersonalAccessToken $token): RedirectResponse | ||
{ | ||
$this->authorize('delete', $token); | ||
|
||
$token->delete(); | ||
|
||
activity() | ||
->by($request->user()) | ||
->log(ActivityLog::SYSTEM_API_TOKEN_REVOKED); | ||
|
||
flash()->warning(trans('auth.api_tokens.revoke_successful')); | ||
return redirect()->route('system.api-tokens.index'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace App\Http\Requests\Admin; | ||
|
||
use App\Rules\ApiTokenAbilityRule; | ||
use Illuminate\Database\Query\Builder; | ||
use Illuminate\Foundation\Http\FormRequest; | ||
use Illuminate\Validation\Rule; | ||
|
||
class CreateSystemApiTokenRequest extends FormRequest | ||
{ | ||
public function rules(): array | ||
{ | ||
return [ | ||
'token_name' => [ | ||
'required', | ||
'alpha_dash', | ||
'min:3', | ||
'max:100', | ||
Rule::unique('personal_access_tokens', 'name')->where(function (Builder $query) { | ||
return $query->whereNull('tokenable_id'); | ||
}), | ||
], | ||
'abilities' => [ | ||
'required', | ||
new ApiTokenAbilityRule(), | ||
], | ||
'private_access' => [ | ||
'sometimes', | ||
'accepted', | ||
], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
namespace App\Policies\Api; | ||
|
||
use App\Enums\ApiToken; | ||
use App\Enums\ModelAttribute; | ||
use App\Models\Link; | ||
use App\Models\User; | ||
use Illuminate\Auth\Access\HandlesAuthorization; | ||
|
||
class LinkApiPolicy | ||
{ | ||
use HandlesAuthorization; | ||
|
||
public function viewAny(User $user): bool | ||
{ | ||
if ($user->isSystemUser()) { | ||
return $user->tokenCan(ApiToken::ABILITY_LINKS_READ); | ||
} | ||
return true; | ||
} | ||
|
||
public function view(User $user, Link $link): bool | ||
{ | ||
if ($user->isSystemUser()) { | ||
$canViewPrivate = $user->tokenCan(ApiToken::ABILITY_SYSTEM_ACCESS_PRIVATE); | ||
return $link->is_private ? $canViewPrivate : $user->tokenCan(ApiToken::ABILITY_LINKS_READ); | ||
} | ||
return $this->userCanAccessLink($user, $link); | ||
} | ||
|
||
public function create(User $user): bool | ||
{ | ||
return true; | ||
} | ||
|
||
public function update(User $user, Link $link): bool | ||
{ | ||
return $this->userCanAccessLink($user, $link); | ||
} | ||
|
||
public function delete(User $user, Link $link): bool | ||
{ | ||
return $link->user->is($user); | ||
} | ||
|
||
public function restore(User $user, Link $link): bool | ||
{ | ||
return $link->user->is($user); | ||
} | ||
|
||
public function forceDelete(User $user, Link $link): bool | ||
{ | ||
return $link->user->is($user); | ||
} | ||
|
||
// Link must be either owned by user, or be not private | ||
protected function userCanAccessLink(User $user, Link $link): bool | ||
{ | ||
if ($link->user_id === $user->id) { | ||
return true; | ||
} | ||
return $link->visibility !== ModelAttribute::VISIBILITY_PRIVATE; | ||
} | ||
} |
Oops, something went wrong.