33namespace Pterodactyl \Transformers \Api \Application ;
44
55use Carbon \CarbonImmutable ;
6+ use Illuminate \Http \Request ;
7+ use Webmozart \Assert \Assert ;
68use Pterodactyl \Models \ApiKey ;
79use Illuminate \Container \Container ;
810use Illuminate \Database \Eloquent \Model ;
911use League \Fractal \TransformerAbstract ;
1012use Pterodactyl \Services \Acl \Api \AdminAcl ;
11- use Pterodactyl \Exceptions \Transformer \InvalidTransformerLevelException ;
1213
1314/**
1415 * @method array transform(Model $model)
@@ -17,15 +18,7 @@ abstract class BaseTransformer extends TransformerAbstract
1718{
1819 public const RESPONSE_TIMEZONE = 'UTC ' ;
1920
20- /**
21- * @var \Pterodactyl\Models\ApiKey
22- */
23- private $ key ;
24-
25- /**
26- * Return the resource name for the JSONAPI output.
27- */
28- abstract public function getResourceName (): string ;
21+ protected Request $ request ;
2922
3023 /**
3124 * BaseTransformer constructor.
@@ -39,54 +32,69 @@ public function __construct()
3932 }
4033
4134 /**
42- * Set the HTTP request class being used for this request.
35+ * Return the resource name for the JSONAPI output.
36+ */
37+ abstract public function getResourceName (): string ;
38+
39+ /**
40+ * Sets the request on the instance.
4341 *
44- * @return $this
42+ * @return static
4543 */
46- public function setKey ( ApiKey $ key )
44+ public function setRequest ( Request $ request ): self
4745 {
48- $ this ->key = $ key ;
46+ $ this ->request = $ request ;
4947
5048 return $ this ;
5149 }
5250
5351 /**
54- * Return the request instance being used for this transformer.
52+ * Returns a new transformer instance with the request set on the instance.
53+ *
54+ * @return \Pterodactyl\Transformers\Api\Application\BaseTransformer
5555 */
56- public function getKey (): ApiKey
56+ public static function fromRequest ( Request $ request )
5757 {
58- return $ this -> key ;
58+ return app ( static ::class)-> setRequest ( $ request ) ;
5959 }
6060
6161 /**
6262 * Determine if the API key loaded onto the transformer has permission
6363 * to access a different resource. This is used when including other
6464 * models on a transformation request.
65+ *
66+ * @deprecated — prefer $user->can/cannot methods
6567 */
6668 protected function authorize (string $ resource ): bool
6769 {
68- return AdminAcl::check ($ this ->getKey (), $ resource , AdminAcl::READ );
70+ $ token = $ this ->request ->user ()->currentAccessToken ();
71+ if (!$ token instanceof ApiKey || $ token ->key_type !== ApiKey::TYPE_APPLICATION ) {
72+ return false ;
73+ }
74+
75+ return AdminAcl::check ($ token , $ resource , AdminAcl::READ );
6976 }
7077
7178 /**
7279 * Create a new instance of the transformer and pass along the currently
7380 * set API key.
7481 *
75- * @return \Pterodactyl\Transformers\Api\Application\BaseTransformer
82+ * @template T of \Pterodactyl\Transformers\Api\Application\BaseTransformer
83+ *
84+ * @param class-string<T> $abstract
85+ *
86+ * @return T
7687 *
7788 * @throws \Pterodactyl\Exceptions\Transformer\InvalidTransformerLevelException
89+ *
90+ * @noinspection PhpUndefinedClassInspection
91+ * @noinspection PhpDocSignatureInspection
7892 */
79- protected function makeTransformer (string $ abstract, array $ parameters = [] )
93+ protected function makeTransformer (string $ abstract )
8094 {
81- /** @var \Pterodactyl\Transformers\Api\Application\BaseTransformer $transformer */
82- $ transformer = Container::getInstance ()->makeWith ($ abstract , $ parameters );
83- $ transformer ->setKey ($ this ->getKey ());
84-
85- if (!$ transformer instanceof self) {
86- throw new InvalidTransformerLevelException ('Calls to ' . __METHOD__ . ' must return a transformer that is an instance of ' . __CLASS__ );
87- }
95+ Assert::subclassOf ($ abstract , self ::class);
8896
89- return $ transformer ;
97+ return $ abstract :: fromRequest ( $ this -> request ) ;
9098 }
9199
92100 /**
0 commit comments