Esempio n. 1
0
 /**
  * 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;
 }