SASS Pos / Cloud

PosToCloud has been one of the most complex technological solutions I have designed and developed, it is a SASS system that allows to generate different instances of applications which can be accessed in a unified client by entering the serial of the application.

The applications also have variant modules that can be activated / deactivated from the central administrative panel from the cloud at any time, and each one has its own database generated dynamically when creating the application.

The databases execute migrations, creating new tables and deleting those that are no longer needed when new modules are installed / uninstalled within them.

As I already mentioned, this system made use of dynamic databases, but all in the same unified backend, for this to be possible we had to use two different eloquent “connections”, a static one that would contain the main database, where the users, clients, applications, modules, etc would be stored; and another dynamic connection which would access the database of the application that is required at the moment.

image.png

For the second connection to work, we had to make a middleware that would detect the serial of the application from which the request was being made and thus the connection would become the database of that application.

The code of this middleware and the main reason for the operation of this system is:

<?php
 namespace App\Http\Middleware;

use Closure;
use App\Aplication;
use App\Helpers\ConectionDB;
use Carbon\Carbon;

class AppSecurity
{
    public function handle($request, Closure $next)
    {
    
        //Obtener el codigo de serial de la aplicacion
        $codigo = $request->header('App-Key');

        if (!$codigo) return response()->json('Codigo de aplicacion no encontrado en header.',401);

        session(['app-serial' => $codigo]);

        $app = Aplication::where('serial', $codigo)->first();
        if(!$app) return response()->json('Serial no encontrado.',404);

        session(['app-current' => $app]);
        session(['app-config' => $app->getApp()]);

        $today = Carbon::now()->toDateTimeString();

        if(strtotime($app->expiration) < strtotime($today)){
          return response()->json('La aplicacion ha expirado, consulte con su proveedor', 498);
        }

        if ($app->active == false) {
          return response()->json('El servicio para esta aplicacion fue cortado, consulte con su proveedor', 423);
        }

        $conexion = new ConectionDB($app);
        $conexion->set_database();

        auth()->setDefaultDriver('local');

        return $next($request);
    }
}

The part of the ConectionDB code we are interested in is:

<?php namespace App\Helpers;

use App\DataBase;
use Artisan;
use Illuminate\Support\Facades\DB;
use PDO;
use Config;

class ConectionDB
{

  public $app = null;
  public $database = null;

  function __construct($aplication) {
    $this->app = $aplication;
    $this->database = DataBase::find($this->app->database_app);
  }

  /* . . . */

  //Modificando archivo de databese para conectarce con otra base de datos
  public function set_database(){
    $configDb = array(
      'driver' => 'mysql',
      'url' => env('DATABASE_URL'),
      'host' => env('DB_HOST', '127.0.0.1'),
      'port' => env('DB_PORT', '3306'),
      'database' => $this->database->name,
      'username' => $this->database->username,
      'password' => $this->database->password,
      'unix_socket' => env('DB_SOCKET', ''),
      'charset' => 'utf8mb4',
      'collation' => 'utf8mb4_unicode_ci',
      'prefix' => '',
      'prefix_indexes' => true,
      'strict' => true,
      'engine' => null,
      'options' => extension_loaded('pdo_mysql') ? array_filter([
          PDO::MYSQL_ATTR_SSL_CA => env('MYSQL_ATTR_SSL_CA'),
      ]) : [],
    );
    Config::set('database.connections.mysql_local', $configDb);
  }




}

Technologies

Thanks to Edward Mendelson of Columbia University for developing PDFtoPrinter which was the interface used in node-pdf-printer (the library we used) to communicate with PDF-XChange Viewer.


First Phase Participation

Requirements Analysis

Deployment

Management

Backend

Sketching / Wireframe

Frontend