Beispiel #1
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;
     }
 }
Beispiel #2
0
 public function deleteServer($id, $force)
 {
     $server = Models\Server::findOrFail($id);
     $node = Models\Node::findOrFail($server->node);
     DB::beginTransaction();
     try {
         // Delete Allocations
         Models\Allocation::where('assigned_to', $server->id)->update(['assigned_to' => null]);
         // Remove Variables
         Models\ServerVariables::where('server_id', $server->id)->delete();
         // Remove SubUsers
         Models\Subuser::where('server_id', $server->id)->delete();
         // Remove Permissions
         Models\Permission::where('server_id', $server->id)->delete();
         // Remove Downloads
         Models\Download::where('server', $server->uuid)->delete();
         $client = Models\Node::guzzleRequest($server->node);
         $client->request('DELETE', '/servers', ['headers' => ['X-Access-Token' => $node->daemonSecret, 'X-Access-Server' => $server->uuid]]);
         $server->delete();
         DB::commit();
         return true;
     } catch (\GuzzleHttp\Exception\TransferException $ex) {
         if ($force === 'force') {
             $server->delete();
             DB::commit();
             return true;
         } else {
             DB::rollBack();
             throw new DisplayException('An error occured while attempting to delete the server on the daemon.', $ex);
         }
     } catch (\Exception $ex) {
         DB::rollBack();
         throw $ex;
     }
 }
Beispiel #3
0
 /**
  * Returns a single server specified by UUID.
  * DO NOT USE THIS TO MODIFY SERVER DETAILS OR SAVE THOSE DETAILS.
  * YOU WILL OVERWRITE THE SECRET KEY AND BREAK THINGS.
  *
  * @param  string $uuid The Short-UUID of the server to return an object about.
  * @return \Illuminate\Database\Eloquent\Collection
  */
 public static function getByUUID($uuid)
 {
     if (array_key_exists($uuid, self::$serverUUIDInstance)) {
         return self::$serverUUIDInstance[$uuid];
     }
     $query = self::select('servers.*', 'services.file as a_serviceFile')->join('services', 'services.id', '=', 'servers.service')->where('uuidShort', $uuid);
     if (self::$user->root_admin !== 1) {
         $query->whereIn('servers.id', Subuser::accessServers());
     }
     $result = $query->first();
     if (!is_null($result)) {
         $result->daemonSecret = self::getUserDaemonSecret($result);
     }
     self::$serverUUIDInstance[$uuid] = $result;
     return self::$serverUUIDInstance[$uuid];
 }
 public function deleteSubuser(Request $request, $uuid, $id)
 {
     $server = Models\Server::getByUUID($uuid);
     $this->authorize('delete-subuser', $server);
     try {
         $subuser = Models\Subuser::select('id')->where(DB::raw('md5(id)'), $id)->where('server_id', $server->id)->first();
         if (!$subuser) {
             throw new DisplayException('No subuser by that ID was found on the system.');
         }
         $repo = new SubuserRepository();
         $repo->delete($subuser->id);
         return response('', 204);
     } catch (DisplayException $ex) {
         response()->json(['error' => $ex->getMessage()], 422);
     } catch (\Exception $ex) {
         Log::error($ex);
         response()->json(['error' => 'An unknown error occured while attempting to delete this subuser.'], 503);
     }
 }
 /**
  * Updates permissions for a given subuser.
  * @param  integer $id  The ID of the subuser row in MySQL. (Not the user ID)
  * @param  array  $data
  * @throws DisplayValidationException
  * @throws DisplayException
  * @return void
  */
 public function update($id, array $data)
 {
     $validator = Validator::make($data, ['permissions' => 'required|array', 'user' => 'required|exists:users,id', 'server' => 'required|exists:servers,id']);
     if ($validator->fails()) {
         throw new DisplayValidationException(json_encode($validator->all()));
     }
     $subuser = Models\Subuser::findOrFail($id);
     $server = Models\Server::findOrFail($data['server']);
     DB::beginTransaction();
     try {
         Models\Permission::where('user_id', $subuser->user_id)->where('server_id', $subuser->server_id)->delete();
         $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' => $data['user'], 'server_id' => $data['server'], '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]]]);
         DB::commit();
         return true;
     } catch (\GuzzleHttp\Exception\TransferException $ex) {
         DB::rollBack();
         throw new DisplayException('There was an error attempting to connect to the daemon to update permissions.', $ex);
     } catch (\Exception $ex) {
         DB::rollBack();
         throw $ex;
     }
     return false;
 }