ServerController.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Classes\Pterodactyl;
  4. use App\Models\Configuration;
  5. use App\Models\Egg;
  6. use App\Models\Location;
  7. use App\Models\Nest;
  8. use App\Models\Node;
  9. use App\Models\Product;
  10. use App\Models\Server;
  11. use App\Notifications\ServerCreationError;
  12. use Exception;
  13. use Illuminate\Database\Eloquent\Builder;
  14. use Illuminate\Http\Client\Response;
  15. use Illuminate\Http\RedirectResponse;
  16. use Illuminate\Http\Request;
  17. use Illuminate\Support\Facades\Auth;
  18. use Illuminate\Support\Facades\Request as FacadesRequest;
  19. class ServerController extends Controller
  20. {
  21. /** Display a listing of the resource. */
  22. public function index()
  23. {
  24. $servers = Auth::user()->servers;
  25. //Get and set server infos each server
  26. foreach ($servers as $server) {
  27. //Get server infos from ptero
  28. $serverAttributes = Pterodactyl::getServerAttributes($server->pterodactyl_id);
  29. $serverRelationships = $serverAttributes['relationships'];
  30. $serverLocationAttributes = $serverRelationships['location']['attributes'];
  31. //Set server infos
  32. $server->location = $serverLocationAttributes['long'] ?
  33. $serverLocationAttributes['long'] :
  34. $serverLocationAttributes['short'];
  35. $server->egg = $serverRelationships['egg']['attributes']['name'];
  36. $server->nest = $serverRelationships['nest']['attributes']['name'];
  37. //get productname by product_id for server
  38. $product = Product::find($server->product_id);
  39. $server->product = $product;
  40. }
  41. return view('servers.index')->with([
  42. 'servers' => $servers
  43. ]);
  44. }
  45. /** Show the form for creating a new resource. */
  46. public function create()
  47. {
  48. if (!is_null($this->validateConfigurationRules())) return $this->validateConfigurationRules();
  49. $productCount = Product::query()->where('disabled', '=', false)->count();
  50. $locations = Location::all();
  51. $nodeCount = Node::query()
  52. ->whereHas('products', function (Builder $builder) {
  53. $builder->where('disabled', '=', false);
  54. })->count();
  55. $eggs = Egg::query()
  56. ->whereHas('products', function (Builder $builder) {
  57. $builder->where('disabled', '=', false);
  58. })->get();
  59. $nests = Nest::query()
  60. ->whereHas('eggs', function (Builder $builder) {
  61. $builder->whereHas('products', function (Builder $builder) {
  62. $builder->where('disabled', '=', false);
  63. });
  64. })->get();
  65. return view('servers.create')->with([
  66. 'productCount' => $productCount,
  67. 'nodeCount' => $nodeCount,
  68. 'nests' => $nests,
  69. 'locations' => $locations,
  70. 'eggs' => $eggs,
  71. 'user' => Auth::user(),
  72. ]);
  73. }
  74. /**
  75. * @return null|RedirectResponse
  76. */
  77. private function validateConfigurationRules()
  78. {
  79. //limit validation
  80. if (Auth::user()->servers()->count() >= Auth::user()->server_limit) {
  81. return redirect()->route('servers.index')->with('error', __('Server limit reached!'));
  82. }
  83. // minimum credits
  84. if (FacadesRequest::has("product")) {
  85. $product = Product::findOrFail(FacadesRequest::input("product"));
  86. if (
  87. Auth::user()->credits <
  88. ($product->minimum_credits == -1
  89. ? Configuration::getValueByKey('MINIMUM_REQUIRED_CREDITS_TO_MAKE_SERVER', 50)
  90. : $product->minimum_credits)
  91. ) {
  92. return redirect()->route('servers.index')->with('error', "You do not have the required amount of " . CREDITS_DISPLAY_NAME . " to use this product!");
  93. }
  94. }
  95. //Required Verification for creating an server
  96. if (Configuration::getValueByKey('FORCE_EMAIL_VERIFICATION', 'false') === 'true' && !Auth::user()->hasVerifiedEmail()) {
  97. return redirect()->route('profile.index')->with('error', __("You are required to verify your email address before you can create a server."));
  98. }
  99. //Required Verification for creating an server
  100. if (Configuration::getValueByKey('FORCE_DISCORD_VERIFICATION', 'false') === 'true' && !Auth::user()->discordUser) {
  101. return redirect()->route('profile.index')->with('error', __("You are required to link your discord account before you can create a server."));
  102. }
  103. return null;
  104. }
  105. /** Store a newly created resource in storage. */
  106. public function store(Request $request)
  107. {
  108. /** @var Node $node */
  109. /** @var Egg $egg */
  110. /** @var Product $product */
  111. if (!is_null($this->validateConfigurationRules())) return $this->validateConfigurationRules();
  112. $request->validate([
  113. "name" => "required|max:191",
  114. "node" => "required|exists:nodes,id",
  115. "egg" => "required|exists:eggs,id",
  116. "product" => "required|exists:products,id"
  117. ]);
  118. //get required resources
  119. $product = Product::query()->findOrFail($request->input('product'));
  120. $egg = $product->eggs()->findOrFail($request->input('egg'));
  121. $node = $product->nodes()->findOrFail($request->input('node'));
  122. $server = $request->user()->servers()->create([
  123. 'name' => $request->input('name'),
  124. 'product_id' => $request->input('product'),
  125. ]);
  126. //get free allocation ID
  127. $allocationId = Pterodactyl::getFreeAllocationId($node);
  128. if (!$allocationId) return $this->noAllocationsError($server);
  129. //create server on pterodactyl
  130. $response = Pterodactyl::createServer($server, $egg, $allocationId);
  131. if ($response->failed()) return $this->serverCreationFailed($response, $server);
  132. $serverAttributes = $response->json()['attributes'];
  133. //update server with pterodactyl_id
  134. $server->update([
  135. 'pterodactyl_id' => $serverAttributes['id'],
  136. 'identifier' => $serverAttributes['identifier']
  137. ]);
  138. if (Configuration::getValueByKey('SERVER_CREATE_CHARGE_FIRST_HOUR', 'true') == 'true') {
  139. if ($request->user()->credits >= $server->product->getHourlyPrice()) {
  140. $request->user()->decrement('credits', $server->product->getHourlyPrice());
  141. }
  142. }
  143. return redirect()->route('servers.index')->with('success', __('Server created'));
  144. }
  145. /**
  146. * return redirect with error
  147. * @param Server $server
  148. * @return RedirectResponse
  149. */
  150. private function noAllocationsError(Server $server)
  151. {
  152. $server->delete();
  153. Auth::user()->notify(new ServerCreationError($server));
  154. return redirect()->route('servers.index')->with('error', __('No allocations satisfying the requirements for automatic deployment on this node were found.'));
  155. }
  156. /**
  157. * return redirect with error
  158. * @param Response $response
  159. * @param Server $server
  160. * @return RedirectResponse
  161. */
  162. private function serverCreationFailed(Response $response, Server $server)
  163. {
  164. $server->delete();
  165. return redirect()->route('servers.index')->with('error', json_encode($response->json()));
  166. }
  167. /** Remove the specified resource from storage. */
  168. public function destroy(Server $server)
  169. {
  170. try {
  171. $server->delete();
  172. return redirect()->route('servers.index')->with('success', __('Server removed'));
  173. } catch (Exception $e) {
  174. return redirect()->route('servers.index')->with('error', __('An exception has occurred while trying to remove a resource "') . $e->getMessage() . '"');
  175. }
  176. }
  177. }