/**
  * 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 setLanguage(Request $request, $language)
 {
     if (array_key_exists($language, $this->languages)) {
         if (Auth::check()) {
             $user = User::findOrFail(Auth::user()->id);
             $user->language = $language;
             $user->save();
         }
         Session::set('applocale', $language);
     }
     return redirect()->back();
 }
Exemple #3
0
 /**
  * Deletes a user on the panel, returns the number of records deleted.
  *
  * @param  integer $id
  * @return integer
  */
 public function delete($id)
 {
     if (Models\Server::where('owner', $id)->count() > 0) {
         throw new DisplayException('Cannot delete a user with active servers attached to thier account.');
     }
     DB::beginTransaction();
     try {
         Models\Permission::where('user_id', $id)->delete();
         Models\Subuser::where('user_id', $id)->delete();
         Models\User::destroy($id);
         DB::commit();
         return true;
     } catch (\Exception $ex) {
         DB::rollBack();
         throw $ex;
     }
 }
Exemple #4
0
 public function getView(Request $request, $id)
 {
     return view('admin.users.view', ['user' => User::findOrFail($id), 'servers' => Server::select('servers.*', 'nodes.name as nodeName', 'locations.long as location')->join('nodes', 'servers.node', '=', 'nodes.id')->join('locations', 'nodes.location', '=', 'locations.id')->where('owner', $id)->get()]);
 }
 /**
  * [updateDetails description]
  * @param  integer  $id
  * @param  array    $data
  * @return boolean
  */
 public function updateDetails($id, array $data)
 {
     $uuid = new UuidService();
     $resetDaemonKey = false;
     // Validate Fields
     $validator = Validator::make($data, ['owner' => 'email|exists:users,email', 'name' => 'regex:([\\w -]{4,35})']);
     // Run validator, throw catchable and displayable exception if it fails.
     // Exception includes a JSON result of failed validation rules.
     if ($validator->fails()) {
         throw new DisplayValidationException($validator->errors());
     }
     DB::beginTransaction();
     try {
         $server = Models\Server::findOrFail($id);
         $owner = Models\User::findOrFail($server->owner);
         // Update daemon secret if it was passed.
         if (isset($data['reset_token']) && $data['reset_token'] === true || isset($data['owner']) && $data['owner'] !== $owner->email) {
             $oldDaemonKey = $server->daemonSecret;
             $server->daemonSecret = $uuid->generate('servers', 'daemonSecret');
             $resetDaemonKey = true;
         }
         // Update Server Owner if it was passed.
         if (isset($data['owner']) && $data['owner'] !== $owner->email) {
             $newOwner = Models\User::select('id')->where('email', $data['owner'])->first();
             $server->owner = $newOwner->id;
         }
         // Update Server Name if it was passed.
         if (isset($data['name'])) {
             $server->name = $data['name'];
         }
         // Save our changes
         $server->save();
         // Do we need to update? If not, return successful.
         if (!$resetDaemonKey) {
             DB::commit();
             return true;
         }
         // If we need to update do it here.
         $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], 'exceptions' => false, 'json' => ['keys' => [(string) $oldDaemonKey => [], (string) $server->daemonSecret => $this->daemonPermissions]]]);
         if ($res->getStatusCode() === 204) {
             DB::commit();
             return true;
         } else {
             throw new DisplayException('Daemon returned a a non HTTP/204 error code. HTTP/' + $res->getStatusCode());
         }
     } catch (\Exception $ex) {
         DB::rollBack();
         Log::error($ex);
         throw new DisplayException('An error occured while attempting to update this server\'s information.');
     }
 }
 /**
  * Check if the provided user has TOTP enabled.
  *
  * @param  \Illuminate\Http\Request  $request
  * @return \Illuminate\Http\Response
  */
 public function checkTotp(Request $request)
 {
     return response()->json(User::select('id')->where('email', $request->input('email'))->where('use_totp', 1)->first());
 }
Exemple #7
0
 /**
  * Update an Existing User
  *
  * The data sent in the request will be used to update the existing user on the system.
  *
  * @Patch("/users/{id}")
  * @Versions({"v1"})
  * @Transaction({
  *      @Request({
  *          "email": "*****@*****.**"
  *      }, headers={"Authorization": "Bearer <token>"}),
  *      @Response(200, body={"email": "*****@*****.**"}),
  *      @Response(422)
  * })
  * @Parameters({
  *         @Parameter("id", type="integer", required=true, description="The ID of the user to modify.")
  * })
  */
 public function update(Request $request, $id)
 {
     try {
         $user = new UserRepository();
         $user->update($id, $request->all());
         return Models\User::findOrFail($id);
     } 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 update a user on the system due to an error.');
     }
 }