2020-03-22 18:15:38 -07:00
< ? php
2024-03-12 22:39:16 -04:00
namespace App\Http\Controllers\Api\Client ;
2020-03-22 18:15:38 -07:00
2024-03-12 22:39:16 -04:00
use App\Exceptions\DisplayException ;
2025-09-24 13:34:19 +02:00
use App\Facades\Activity ;
use App\Http\Requests\Api\Client\Account\StoreApiKeyRequest ;
2024-03-12 22:39:16 -04:00
use App\Http\Requests\Api\Client\ClientApiRequest ;
2025-09-24 13:34:19 +02:00
use App\Models\ApiKey ;
2024-03-12 22:39:16 -04:00
use App\Transformers\Api\Client\ApiKeyTransformer ;
2025-09-24 13:34:19 +02:00
use Illuminate\Http\JsonResponse ;
2020-03-22 18:15:38 -07:00
class ApiKeyController extends ClientApiController
{
/**
2025-02-26 16:12:19 +01:00
* List api keys
*
2022-10-14 10:59:20 -06:00
* Returns all the API keys that exist for the given client .
2025-03-03 14:41:19 -05:00
*
* @ return array < array - key , mixed >
2020-03-22 18:15:38 -07:00
*/
2022-10-14 10:59:20 -06:00
public function index ( ClientApiRequest $request ) : array
2020-03-22 18:15:38 -07:00
{
return $this -> fractal -> collection ( $request -> user () -> apiKeys )
-> transformWith ( $this -> getTransformer ( ApiKeyTransformer :: class ))
-> toArray ();
}
/**
2025-02-26 16:12:19 +01:00
* Create api key
*
2020-03-22 18:15:38 -07:00
* Store a new API key for a user ' s account .
2022-10-14 10:59:20 -06:00
*
2025-03-03 14:41:19 -05:00
* @ return array < array - key , mixed >
*
2025-09-08 13:12:33 -04:00
* @ throws DisplayException
2020-03-22 18:15:38 -07:00
*/
2022-10-08 14:14:03 -07:00
public function store ( StoreApiKeyRequest $request ) : array
2020-03-22 18:15:38 -07:00
{
2026-07-09 09:55:11 -04:00
throw_if ( $request -> user () -> apiKeys -> count () >= config ( 'panel.api.key_limit' ), new DisplayException ( 'You have reached the account limit for number of API keys.' ));
2020-03-22 18:15:38 -07:00
2022-05-22 19:03:51 -04:00
$token = $request -> user () -> createToken (
$request -> input ( 'description' ),
$request -> input ( 'allowed_ips' )
);
2020-03-22 18:15:38 -07:00
2022-05-29 18:48:35 -04:00
Activity :: event ( 'user:api-key.create' )
-> subject ( $token -> accessToken )
-> property ( 'identifier' , $token -> accessToken -> identifier )
-> log ();
2022-05-22 19:03:51 -04:00
return $this -> fractal -> item ( $token -> accessToken )
2020-03-22 18:15:38 -07:00
-> transformWith ( $this -> getTransformer ( ApiKeyTransformer :: class ))
2022-05-22 19:03:51 -04:00
-> addMeta ([ 'secret_token' => $token -> plainTextToken ])
2020-03-22 18:15:38 -07:00
-> toArray ();
}
2020-03-22 19:10:49 -07:00
/**
2025-02-26 16:12:19 +01:00
* Delete api key
*
2020-03-22 19:10:49 -07:00
* Deletes a given API key .
*/
2022-10-14 10:59:20 -06:00
public function delete ( ClientApiRequest $request , string $identifier ) : JsonResponse
2020-03-22 18:15:38 -07:00
{
2025-09-08 13:12:33 -04:00
/** @var ApiKey $key */
2022-05-29 18:48:35 -04:00
$key = $request -> user () -> apiKeys ()
-> where ( 'key_type' , ApiKey :: TYPE_ACCOUNT )
-> where ( 'identifier' , $identifier )
2022-05-29 20:39:51 -04:00
-> firstOrFail ();
2020-03-22 19:10:49 -07:00
2022-05-29 18:48:35 -04:00
Activity :: event ( 'user:api-key.delete' )
2022-06-18 12:16:54 -04:00
-> property ( 'identifier' , $key -> identifier )
2022-05-29 18:48:35 -04:00
-> log ();
$key -> delete ();
2020-03-22 19:10:49 -07:00
2022-05-04 19:23:01 -04:00
return new JsonResponse ([], JsonResponse :: HTTP_NO_CONTENT );
2020-03-22 18:15:38 -07:00
}
}