44
55use Pterodactyl \Models \ApiKey ;
66use Illuminate \Http \JsonResponse ;
7+ use Pterodactyl \Facades \Activity ;
78use Pterodactyl \Exceptions \DisplayException ;
8- use Illuminate \Contracts \Encryption \Encrypter ;
9- use Pterodactyl \Services \Api \KeyCreationService ;
10- use Pterodactyl \Repositories \Eloquent \ApiKeyRepository ;
119use Pterodactyl \Http \Requests \Api \Client \ClientApiRequest ;
1210use Pterodactyl \Transformers \Api \Client \ApiKeyTransformer ;
13- use Symfony \Component \HttpKernel \Exception \NotFoundHttpException ;
1411use Pterodactyl \Http \Requests \Api \Client \Account \StoreApiKeyRequest ;
1512
1613class ApiKeyController extends ClientApiController
1714{
18- /**
19- * @var \Pterodactyl\Services\Api\KeyCreationService
20- */
21- private $ keyCreationService ;
22-
23- /**
24- * @var \Illuminate\Contracts\Encryption\Encrypter
25- */
26- private $ encrypter ;
27-
28- /**
29- * @var \Pterodactyl\Repositories\Eloquent\ApiKeyRepository
30- */
31- private $ repository ;
32-
33- /**
34- * ApiKeyController constructor.
35- */
36- public function __construct (
37- Encrypter $ encrypter ,
38- KeyCreationService $ keyCreationService ,
39- ApiKeyRepository $ repository
40- ) {
41- parent ::__construct ();
42-
43- $ this ->encrypter = $ encrypter ;
44- $ this ->keyCreationService = $ keyCreationService ;
45- $ this ->repository = $ repository ;
46- }
47-
4815 /**
4916 * Returns all of the API keys that exist for the given client.
5017 *
@@ -75,6 +42,11 @@ public function store(StoreApiKeyRequest $request)
7542 $ request ->input ('allowed_ips ' )
7643 );
7744
45+ Activity::event ('user:api-key.create ' )
46+ ->subject ($ token ->accessToken )
47+ ->property ('identifier ' , $ token ->accessToken ->identifier )
48+ ->log ();
49+
7850 return $ this ->fractal ->item ($ token ->accessToken )
7951 ->transformWith ($ this ->getTransformer (ApiKeyTransformer::class))
8052 ->addMeta (['secret_token ' => $ token ->plainTextToken ])
@@ -88,15 +60,16 @@ public function store(StoreApiKeyRequest $request)
8860 */
8961 public function delete (ClientApiRequest $ request , string $ identifier )
9062 {
91- $ response = $ this ->repository ->deleteWhere ([
92- 'key_type ' => ApiKey::TYPE_ACCOUNT ,
93- 'user_id ' => $ request ->user ()->id ,
94- 'identifier ' => $ identifier ,
95- ]);
63+ $ key = $ request ->user ()->apiKeys ()
64+ ->where ('key_type ' , ApiKey::TYPE_ACCOUNT )
65+ ->where ('identifier ' , $ identifier )
66+ ->first ();
9667
97- if (!$ response ) {
98- throw new NotFoundHttpException ();
99- }
68+ Activity::event ('user:api-key.delete ' )
69+ ->property ('identifer ' , $ key ->identifer )
70+ ->log ();
71+
72+ $ key ->delete ();
10073
10174 return new JsonResponse ([], JsonResponse::HTTP_NO_CONTENT );
10275 }
0 commit comments