/** * Creates a new subuser on the server. * @param integer $id The ID of the server to add this subuser to. * @param array $data * @throws DisplayValidationException * @throws DisplayException * @return integer Returns the ID of the newly created subuser. */ public function create($sid, array $data) { $server = Models\Server::findOrFail($sid); $validator = Validator::make($data, ['permissions' => 'required|array', 'email' => 'required|email']); if ($validator->fails()) { throw new DisplayValidationException(json_encode($validator->errors())); } DB::beginTransaction(); try { // Determine if this user exists or if we need to make them an account. $user = Models\User::where('email', $data['email'])->first(); if (!$user) { $password = str_random(16); try { $repo = new UserRepository(); $uid = $repo->create($data['email'], $password); $user = Models\User::findOrFail($uid); } catch (\Exception $ex) { throw $ex; } } $uuid = new UuidService(); $subuser = new Models\Subuser(); $subuser->fill(['user_id' => $user->id, 'server_id' => $server->id, 'daemonSecret' => (string) $uuid->generate('servers', 'uuid')]); $subuser->save(); $daemonPermissions = $this->coreDaemonPermissions; foreach ($data['permissions'] as $permission) { if (array_key_exists($permission, $this->permissions)) { // Build the daemon permissions array for sending. if (!is_null($this->permissions[$permission])) { array_push($daemonPermissions, $this->permissions[$permission]); } $model = new Models\Permission(); $model->fill(['user_id' => $user->id, 'server_id' => $server->id, 'permission' => $permission]); $model->save(); } } // Contact Daemon // We contact even if they don't have any daemon permissions to overwrite // if they did have them previously. $node = Models\Node::getByID($server->node); $client = Models\Node::guzzleRequest($server->node); $res = $client->request('PATCH', '/server', ['headers' => ['X-Access-Server' => $server->uuid, 'X-Access-Token' => $node->daemonSecret], 'json' => ['keys' => [$subuser->daemonSecret => $daemonPermissions]]]); $email = $data['email']; Mail::queue('emails.added-subuser', ['serverName' => $server->name, 'url' => route('server.index', $server->uuidShort)], function ($message) use($email) { $message->to($email); $message->from(Settings::get('email_from', env('MAIL_FROM')), Settings::get('email_sender_name', env('MAIL_FROM_NAME', 'Pterodactyl Panel'))); $message->subject(Settings::get('company') . ' - Added to Server'); }); DB::commit(); return $subuser->id; } catch (\GuzzleHttp\Exception\TransferException $ex) { DB::rollBack(); throw new DisplayException('There was an error attempting to connect to the daemon to add this user.', $ex); } catch (\Exception $ex) { DB::rollBack(); throw $ex; } return false; }
public function postNew(Request $request) { try { $user = new UserRepository(); $userid = $user->create($request->input('email'), $request->input('password')); Alert::success('Account has been successfully created.')->flash(); return redirect()->route('admin.users.view', $userid); } catch (DisplayValidationException $ex) { return redirect()->route('admin.users.new')->withErrors(json_decode($ex->getMessage()))->withInput(); } catch (\Exception $ex) { Log::error($ex); Alert::danger('An error occured while attempting to add a new user.')->flash(); return redirect()->route('admin.users.new'); } }
/** * Execute the console command. * * @return mixed */ public function handle() { $email = $this->ask('Email'); $password = $this->secret('Password'); $password_confirmation = $this->secret('Confirm Password'); if ($password !== $password_confirmation) { return $this->error('The passwords provided did not match!'); } $admin = $this->confirm('Is this user a root administrator?'); try { $user = new UserRepository(); $user->create($email, $password, $admin); return $this->info('User successfully created.'); } catch (\Exception $ex) { return $this->error($ex->getMessage()); } }
/** * Create a New User * * @Post("/users") * @Versions({"v1"}) * @Transaction({ * @Request({ * "email": "*****@*****.**", * "password": "******", * "admin": false * }, headers={"Authorization": "Bearer <token>"}), * @Response(201), * @Response(422, body={ * "message": "A validation error occured.", * "errors": { * "email": {"The email field is required."}, * "password": {"The password field is required."}, * "admin": {"The admin field is required."} * }, * "status_code": 422 * }) * }) */ public function create(Request $request) { try { $user = new UserRepository(); $create = $user->create($request->input('email'), $request->input('password'), $request->input('admin')); return $this->response->created(route('api.users.view', ['id' => $create])); } catch (DisplayValidationException $ex) { throw new ResourceException('A validation error occured.', json_decode($ex->getMessage(), true)); } catch (DisplayException $ex) { throw new ResourceException($ex->getMessage()); } catch (\Exception $ex) { throw new ServiceUnavailableHttpException('Unable to create a user on the system due to an error.'); } }