ServerController.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. use Illuminate\Support\Facades\Log;
  20. class ServerController extends Controller
  21. {
  22. /** Display a listing of the resource. */
  23. public function index()
  24. {
  25. $servers = Auth::user()->servers;
  26. //Get and set server infos each server
  27. foreach ($servers as $server) {
  28. //Get server infos from ptero
  29. $serverAttributes = Pterodactyl::client()->get('/application/servers/' . $server->pterodactyl_id . '?include=egg,nest,location')->json()['attributes'];
  30. $serverRelationships = $serverAttributes['relationships'];
  31. $serverLocationAttributes = $serverRelationships['location']['attributes'];
  32. //Set server infos
  33. $server->location = $serverLocationAttributes['long'] ?
  34. $serverLocationAttributes['long'] :
  35. $serverLocationAttributes['short'];
  36. $server->egg = $serverRelationships['egg']['attributes']['name'];
  37. $server->nest = $serverRelationships['nest']['attributes']['name'];
  38. //get productname by product_id for server
  39. $productName = Product::find($server->product_id)->name;
  40. $server->resourceplanName = $productName;
  41. }
  42. return view('servers.index')->with([
  43. 'servers' => $servers
  44. ]);
  45. }
  46. /** Show the form for creating a new resource. */
  47. public function create()
  48. {
  49. if (!is_null($this->validateConfigurationRules())) return $this->validateConfigurationRules();
  50. $productCount = Product::query()->where('disabled', '=', false)->count();
  51. $locations = Location::all();
  52. $nodeCount = Node::query()
  53. ->whereHas('products', function (Builder $builder) {
  54. $builder->where('disabled', '=', false);
  55. })->count();
  56. $eggs = Egg::query()
  57. ->whereHas('products', function (Builder $builder) {
  58. $builder->where('disabled', '=', false);
  59. })->get();
  60. $nests = Nest::query()
  61. ->whereHas('eggs', function (Builder $builder) {
  62. $builder->whereHas('products', function (Builder $builder) {
  63. $builder->where('disabled', '=', false);
  64. });
  65. })->get();
  66. return view('servers.create')->with([
  67. 'productCount' => $productCount,
  68. 'nodeCount' => $nodeCount,
  69. 'nests' => $nests,
  70. 'locations' => $locations,
  71. 'eggs' => $eggs,
  72. 'user' => Auth::user(),
  73. ]);
  74. }
  75. /**
  76. * @return null|RedirectResponse
  77. */
  78. private function validateConfigurationRules()
  79. {
  80. //limit validation
  81. if (Auth::user()->servers()->count() >= Auth::user()->server_limit) {
  82. return redirect()->route('servers.index')->with('error', __('Server limit reached!'));
  83. }
  84. // minimum credits
  85. if (FacadesRequest::has("product")) {
  86. $product = Product::findOrFail(FacadesRequest::input("product"));
  87. if (
  88. Auth::user()->credits <
  89. ($product->minimum_credits == -1
  90. ? Configuration::getValueByKey('MINIMUM_REQUIRED_CREDITS_TO_MAKE_SERVER', 50)
  91. : $product->minimum_credits)
  92. ) {
  93. return redirect()->route('servers.index')->with('error', "You do not have the required amount of " . CREDITS_DISPLAY_NAME . " to use this product!");
  94. }
  95. }
  96. //Required Verification for creating an server
  97. if (Configuration::getValueByKey('FORCE_EMAIL_VERIFICATION', 'false') === 'true' && !Auth::user()->hasVerifiedEmail()) {
  98. return redirect()->route('profile.index')->with('error', __("You are required to verify your email address before you can create a server."));
  99. }
  100. //Required Verification for creating an server
  101. if (Configuration::getValueByKey('FORCE_DISCORD_VERIFICATION', 'false') === 'true' && !Auth::user()->discordUser) {
  102. return redirect()->route('profile.index')->with('error', __("You are required to link your discord account before you can create a server."));
  103. }
  104. return null;
  105. }
  106. /** Store a newly created resource in storage. */
  107. public function store(Request $request)
  108. {
  109. /** @var Node $node */
  110. /** @var Egg $egg */
  111. /** @var Product $product */
  112. if (!is_null($this->validateConfigurationRules())) return $this->validateConfigurationRules();
  113. $request->validate([
  114. "name" => "required|max:191",
  115. "node" => "required|exists:nodes,id",
  116. "egg" => "required|exists:eggs,id",
  117. "product" => "required|exists:products,id"
  118. ]);
  119. //get required resources
  120. $product = Product::query()->findOrFail($request->input('product'));
  121. $egg = $product->eggs()->findOrFail($request->input('egg'));
  122. $node = $product->nodes()->findOrFail($request->input('node'));
  123. $server = $request->user()->servers()->create([
  124. 'name' => $request->input('name'),
  125. 'product_id' => $request->input('product'),
  126. ]);
  127. //get free allocation ID
  128. $allocationId = Pterodactyl::getFreeAllocationId($node);
  129. if (!$allocationId) return $this->noAllocationsError($server);
  130. //create server on pterodactyl
  131. $response = Pterodactyl::createServer($server, $egg, $allocationId);
  132. if ($response->failed()) return $this->serverCreationFailed($response, $server);
  133. //update server with pterodactyl_id
  134. $server->update([
  135. 'pterodactyl_id' => $response->json()['attributes']['id'],
  136. 'identifier' => $response->json()['attributes']['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. }