/** Datei: plugins/tenants/library/ARUNOX/Plugins/Tenants/Model.php **/ * @copyright 2025 ARUNOX * @version 1.1.0 * * @since 1.0.0 File is created */ namespace ARUNOX\Plugins\Tenants\API\v1; use ARUNOX\Core\Hooks; use ARUNOX\Core\Router; use ARUNOX\Plugins\Users; class Routes { public static function register() : void { Router::addAll( Controller::class ); Hooks::add('router::get->api.v1.rest.routes', function( $routes ) { if( !Users\Model::isSuperadmin() ) return $routes; $routes[] = [ "path" => "/tenants", "label" => "Tenants", "icon" => "bi bi-suitcase-lg", "menu" => "administration", ]; $routes[] = [ "path" => "/tenants/{id}", "label" => "Benutzer", ]; return $routes; }); } } /** Datei: plugins/tenants/library/ARUNOX/Plugins/Tenants/API/v1/Controller.php **/ * @copyright 2025 ARUNOX * @version 1.1.0 * * @since 1.0.0 File is created */ namespace ARUNOX\Plugins\Tenants\API\v1; use ARUNOX\HTTP; use ARUNOX\Core\Hooks; use ARUNOX\Plugins\REST; use ARUNOX\Plugins\Systems; use ARUNOX\Plugins\Tenants; use ARUNOX\API\BaseController; class Controller extends BaseController { /* ### VARIABLES ################################################ # */ protected static array $NOTIFY = [ "notfound" => "Tenant nicht gefunden", "saved" => "Tenant wurde erfolgreich gespeichert", "deleted" => "Tenant wurde erfolgreich gelöscht", "created" => "Tenant wurde erfolgreich erstellt", "notcreated" => "Tenant konnte nicht erstellt werden", ]; /* ### PUBLIC GET FUNCTIONS ##################################### # */ public function getAll() { $results = Tenants\Model::getAll(); static::restConfigOverview( $results ); return REST\Response::jsonResponse(); } public function get( int $id ) { if( $result = Tenants\Model::get( $id ) ) { static::restConfigDetails( $result ); } else REST\Response::addNotify( "danger", static::getNotify("notfound") ); return REST\Response::jsonResponse(); } /* ### PUBLIC SAVE FUNCTIONS #################################### # */ public function save( int $id, array $data ) { if( $result = Tenants\Model::get( $id ) ) { $result->save( $data ); REST\Response::addNotify( "success", static::getNotify("saved") ); REST\Response::closeModals(); REST\Response::addUpdater( [ "data/table/row", $id ], $result ); } else REST\Response::addNotify( "danger", static::$NOTIFY["notfound"] ); return REST\Response::jsonResponse(); } public function create( array $data ) { if( $id = Tenants\Model::save( $data ) ) { REST\Response::addNotify( "success", static::$NOTIFY["created"] ); REST\Response::closeModals(); REST\Response::redirect( "/tenants" ); } else REST\Response::addNotify( "danger", static::$NOTIFY["notcreated"] ); return REST\Response::jsonResponse(); } public function delete( int $id ) { REST\Response::closeModals(); if( Tenants\Model::delete( $id ) ) { REST\Response::addNotify( "success", static::$NOTIFY["deleted"] ); REST\Response::redirect( "/tenants" ); } else REST\Response::addNotify( "danger", static::$NOTIFY["notfound"] ); return REST\Response::jsonResponse(); } public function toggleStatus( int $id ) { if( $result = Tenants\Model::get( $id ) ) { $result->toggleStatus( TRUE ); REST\Response::addUpdater( [ "data/table/row", $id ], $result ); } else REST\Response::addNotify( "danger", static::$NOTIFY["notfound"] ); return REST\Response::jsonResponse(); } /* ### PUBLIC VALIDATE FUNCTIONS ################################ # */ public function validateUUID( int $id, array $data ) { if( isset( $data["value"] ) && $data["value"] ) { if( $result = Tenants\Model::getByUUID( $data["value"] ) ) { if( $result->id != $id ) { return $this->jsonResponse([ "valid" => FALSE ] ); } } } return $this->jsonResponse([ "valid" => TRUE ] ); } /* ### PUBLIC REST FUNCTIONS #################################### # */ public static function restConfigOverview( array $data = [] ) { return REST\Response::addWrapper( "overview", "Tenant" ) ->setList( $data ) ->get() ->setAction( "/tenants/{id}" ) ->setOrder( "name", "asc" ) ->setSearch( TRUE ) ->addButton(...Hooks::filter( "rest.generator::button.name->create", [], "tenants" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->text", [], "name", TRUE, TRUE, TRUE, "tenants" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->status", [], "status", [], "tenants" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->icon", [ "label" => "Datenbank", "icon" => "bi-database-gear", "options" => Systems\DBStatus::getAll(), ], "db_status", [], "tenants" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->email", [], "email", TRUE, TRUE, TRUE, "tenants" )) // ->addColumn(...Hooks::filter( "rest.generator::column.type->text", [], "domain", TRUE, TRUE, TRUE, "tenants" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->actions", [], [], FALSE, "tenants" ))->get() ->addButton(...Hooks::filter( "rest.generator::action.name->list", [ "label" => "Systeme", "icon" => "bi-gear-wide-connected", "action" => "/tenants/{id}#systems", ], "tenants" )) ->addButton(...Hooks::filter( "rest.generator::action.name->edit", [], "tenants" )) ->addButton(...Hooks::filter( "rest.generator::action.name->delete", [], "tenants" )) ->top(); } public static function restConfigDetails( Tenants\Entity $data ) { return REST\Response::addWrapper( "details", "Tenant bearbeiten" ) ->setData( $data ) ->setBacklink( "/tenants" ) ->get() ->setAction( "/api/v1/tenants/{id}", "PUT" ) ->addTab( "Tenant" )->get() ->addGroup() ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "name", TRUE, "tenants" )) // ->addField(...Hooks::filter( "rest.generator::control.type->url", [], "domain", FALSE, "tenants" )) ->addField(...Hooks::filter( "rest.generator::control.type->email", [], "email", TRUE, "tenants" )) ->parent() ->addGroup() ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "address", TRUE, "tenants" )) ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "zipcode", TRUE, "tenants" )) ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "city", TRUE, "tenants" )) ->parent() ->parent() ->addTab( "Systems" )->get() ->addContent( Tenants\Systems\API\v1\Controller::restConfigTable( $data->id ) ) ->parent() ->addTab( "Speichern" )->get() ->setColor( REST\Generator\Config\Color::Primary ) ->setIcon( "bi bi-save" ) ->setType( "submit" ); } public static function restConfigCreate() { return REST\Response::addModal( "details", "Tenant erstellen" ) ->setData([ "id" => 0 ]) ->get() ->setAction( "/api/v1/tenants", "POST" ) ->addButton(...Hooks::filter( "rest.generator::button.type->submit", [], "Erstellen", "tenants" )) ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "uuid", TRUE, "tenants" )) ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "hook", TRUE, "tenants" )) ->addField(...Hooks::filter( "rest.generator::control.type->select", [], "interval", FALSE, TRUE, "tenants" )) ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "runtime", FALSE, "tenants" )) ->top(); } public static function restConfigDelete( int $id ) { return REST\Response::addModal( "details", "Tenant Löschen" ) ->setData([ "id" => $id ]) ->setSize( REST\Wrapper\Modal\Size::sm ) ->get() ->setAction( "/api/v1/tenants/{id}", "DELETE" ) ->addText( "Möchtest du wirklich den Datensatz (#{$id}) löschen? " ) ->addButton(...Hooks::filter( "rest.generator::button.type->cancel", [], NULL, "tenants" )) ->addButton(...Hooks::filter( "rest.generator::button.type->delete", [], NULL, "tenants" )) ->top(); } } /** Datei: plugins/tenants/library/ARUNOX/Plugins/Tenants/Systems/Model.php **/ * @copyright 2025 ARUNOX * @version 1.1.0 * * @since 1.0.0 File is created */ namespace ARUNOX\Plugins\Tenants\Systems\API\v1; use ARUNOX\Core\Hooks; use ARUNOX\Core\Router; use ARUNOX\Plugins\Users; class Routes { public static function register() : void { Router::addAll( Controller::class ); // Router::get('/api/v1/tenants/{id}/systems', [ Controller::class, 'getAll' ]); // // Router::get('/api/v1/tenants/{id}/systems/create', [ Controller::class, 'restConfigCreate' ]); // Router::post('/api/v1/tenants/{id}/systems', [ Controller::class, 'create' ]); // // Router::get('/api/v1/tenants/{id}/systems/{id}', [ Controller::class, 'get' ]); // Router::put('/api/v1/tenants/{id}/systems/{id}', [ Controller::class, 'save' ]); // Router::patch('/api/v1/tenants/{id}/systems/{id}/status', [ Controller::class, 'toggleStatus' ]); // Router::patch('/api/v1/tenants/{id}/systems/{id}/db_status', [ Controller::class, 'resolveDBStatus' ]); } } /** Datei: plugins/tenants/library/ARUNOX/Plugins/Tenants/Systems/API/v1/Controller.php **/ * @copyright 2025 ARUNOX * @version 1.1.0 * * @since 1.0.0 File is created */ namespace ARUNOX\Plugins\Tenants\Systems\API\v1; use ARUNOX\HTTP; use ARUNOX\Core\Hooks; use ARUNOX\Plugins\REST; use ARUNOX\Plugins\Systems; use ARUNOX\Plugins\Tenants; use ARUNOX\API\BaseController; class Controller extends BaseController { /* ### VARIABLES ################################################ # */ protected static array $NOTIFY = [ "notfound" => "System nicht gefunden", "saved" => "System wurde erfolgreich gespeichert", "added" => "System wurde erfolgreich hinzugefügt", "notadded" => "System konnte nicht hinzugefügt werden", ]; /* ### PUBLIC GET FUNCTIONS ##################################### # */ public function getAll( int $tenant_id ) { $results = Tenants\Systems\Model::getAll([ "tenant_id" => $tenant_id ]); return $this->jsonResponse( $results ); } public function get( int $tenant_id, int $id ) { if( $result = Tenants\Systems\Model::get( $id ) ) { static::restConfigDetails( $result ); } else REST\Response::addNotify( "danger", static::$NOTIFY["notfound"] ); return REST\Response::jsonResponse(); } /* ### PUBLIC SAVE FUNCTIONS #################################### # */ public function save( int $tenant_id, int $id, array $data ) { if( $result = Tenants\Systems\Model::get( $id ) ) { $result->save( $data ); REST\Response::addNotify( "success", static::$NOTIFY["saved"] ); REST\Response::closeModals(); REST\Response::addUpdater( [ "data/table/row", "/api/v1/tenants/{$tenant_id}/systems", $id ], $result ); } else REST\Response::addNotify( "danger", static::$NOTIFY["notfound"] ); return REST\Response::jsonResponse(); } public function create( int $tenant_id, array $data ) { if( $id = Tenants\Systems\Model::save( $data ) ) { REST\Response::addNotify( "success", static::$NOTIFY["added"] ); REST\Response::closeModals(); REST\Response::redirect( "/tenants/{$tenant_id}#systems" ); } else REST\Response::addNotify( "danger", static::$NOTIFY["notadded"] ); return REST\Response::jsonResponse(); } public function toggleStatus( int $tenant_id, int $id ) { if( $result = Tenants\Systems\Model::get( $id ) ) { $result->toggleStatus( TRUE ); REST\Response::addUpdater( [ "data/table/row", "/api/v1/tenants/{$tenant_id}/systems", $id ], $result ); } else REST\Response::addNotify( "danger", static::$NOTIFY["notfound"] ); return REST\Response::jsonResponse(); } public function resolveDBStatus( int $tenant_id, int $id ) { if( $result = Tenants\Systems\Model::get( $id ) ) { $result->resolveDBStatus(); REST\Response::addUpdater( [ "data/table/row", "/api/v1/tenants/{$tenant_id}/systems", $id ], $result ); REST\Response::addNotify( $result->db_status->getColor()->value, $result->db_status->getLabel(), $result->db_status->getIcon() ); } else REST\Response::addNotify( "danger", static::$NOTIFY["notfound"] ); return REST\Response::jsonResponse(); } /* ### PUBLIC REST FUNCTIONS #################################### # */ public static function restConfigDetails( Tenants\Systems\Entity $data ) { $form = REST\Response::addModal( "details", "System bearbeiten" ) ->setData( $data ) ->get() ->setAction( "/tenants/{tenant_id}/systems/{id}", "PUT" ) ->addButton(...Hooks::filter( "rest.generator::button.type->submit", [], "Speichern", "tenants/{tenant_id}/systems" )) ->addField(...Hooks::filter( "rest.generator::control.type->select", [ "options" => Systems\Model::getAsOptions(), ], "system_id", TRUE, FALSE, "tenants/{tenant_id}/systems" )); if( $data->type->isTenantIsolated() ) { $form->addField(...Hooks::filter( "rest.generator::control.type->select", [ "label" => "Datenbankname", "options" => $data->getDatabasesAsOptions() ], "db_name", TRUE, TRUE, "tenants/{tenant_id}/systems" )); } return $form; } public static function restConfigCreate( int $tenant_id ) { return REST\Response::addModal( "details", "System hinzufügen" ) ->setData([ "tenant_id" => $tenant_id ]) ->get() ->setAction( "/tenants/{tenant_id}/systems", "POST" ) ->addButton(...Hooks::filter( "rest.generator::button.type->submit", [], "Erstellen", "tenants/{tenant_id}/systems" )) ->addField(...Hooks::filter( "rest.generator::control.type->hidden", [], "tenant_id", $tenant_id, "tenants/{tenant_id}/systems" )) ->addField(...Hooks::filter( "rest.generator::control.type->select", [ "options" => Systems\Model::getAsOptions(), ], "system_id", TRUE, FALSE, "tenants/{tenant_id}/systems" )); } public static function restConfigTable( int $tenant_id ) { return Systems\API\v1\Controller::restConfigTable("tenants/{$tenant_id}") ->setData([ "tenant_id" => $tenant_id ]) ->setAction( "/tenants/{tenant_id}/systems/{id}" ) ->getColumn("actions") ->addButton(...Hooks::filter( "rest.generator::action.name->edit", [], "tenants/{tenant_id}/systems" )) ->beforeColumn( ...Hooks::filter( "rest.generator::column.type->text", [], "db_name", TRUE, TRUE, TRUE, "tenants/{tenant_id}" )); } } /** Datei: plugins/tenants/library/ARUNOX/Plugins/Tenants/Systems/Plugins/Model.php **/ DBStatus::class, "status" => Attribute\Status::class, "date_create" => Attribute\DateTime::class, "date_edit" => Attribute\DateTime::class, "type" => "system->type", "name" => "system->name", "domain" => "system->domain", ]; /* ### PUBLIC FUNCTIONS ######################################### # */ public function toggleStatus( bool $save = FALSE ) { $this->status->toggle(); if( $save ) $this->save(); } public function connect() { return $this->system->connect( $this->db_name ); } public function getDBConn() { return $this->system->db_conn; } // public function getPlugins() { return $this->system->plugins; } public function checkDBStatus() { return $this->system->checkDBStatus( $this ); } public function resolveDBStatus() { return $this->system->resolveDBStatus( $this ); } public function getSystem() { if( $this->system_id ) { return Systems\Model::get( $this->system_id ); } return NULL; } public function getDatabasesAsOptions() { $system = strtolower( $this->name ); $results = Database::getDatabase(); $options = []; foreach( $results as $item ) { if( preg_match( "/_schema$/", $item ) ) continue; if( !preg_match( "/(^|_){$system}(_|$)/", $item ) ) continue; $options[] = [ "value" => $item, "label" => $item, ]; } return $options; } public function getPlugins() { $plugins = $this->system->plugins; $plugins = array_filter( $plugins, fn( $item ) => $item->status->isActive() ); foreach( $plugins as $plugin ) { $check = Plugins\Model::get([ "tenant_id" => $this->tenant_id, "system_id" => $this->system_id, "plugin" => $plugin->plugin, ]); if( $check ) continue; Plugins\Model::save([ "tenant_id" => $this->tenant_id, "system_id" => $this->system_id, "plugin" => $plugin->plugin, ]); } $plugins = Plugins\Model::getAll([ "tenant_id" => $this->tenant_id, "system_id" => $this->system_id, ]); return $plugins; } /* ### MAGIC FUNCTIONS ########################################## # */ public function __construct() { if( !$this->system->type->isTenantIsolated() ) { $this->db_status = $this->system->db_status->getValue(); } parent::__construct(); } } /** Datei: plugins/tenants/library/ARUNOX/Plugins/Tenants/Entity.php **/ Attribute\Status::class, "date_create" => Attribute\DateTime::class, "date_edit" => Attribute\DateTime::class, "db_status" => "DBStatus", ]; /* ### PUBLIC FUNCTIONS ######################################### # */ public function toggleStatus( bool $save = FALSE ) { $this->status->toggle(); if( $save ) $this->save(); } public function getSystems() { return Systems\Model::getAll([ "tenant_id" => $this->id ]); } public function getDBStatus() { $systems = $this->systems; if( !$systems ) return DBStatus::ByTenant; $status = DBStatus::Ok; foreach( $this->systems as $system ) { if( $system->db_status->getValue() < $status ) { $status = $system->db_status->getValue(); } } return $status; } } /** Datei: plugins/tenants/library/ARUNOX/Plugins/Tenants/Users/API/v1/Routes.php **/ * @copyright 2025 ARUNOX * @version 1.1.0 * * @since 1.0.0 File is created */ namespace ARUNOX\Plugins\Tenants\Users\API\v1; use ARUNOX\Core\Hooks; use ARUNOX\Core\Router; use ARUNOX\Plugins\Users; class Routes { public static function register() : void { Router::addAll( Controller::class ); // Router::get('/api/v1/tenants/{id}/users', [ Controller::class, 'getAll' ]); // Router::get('/api/v1/tenants/create', [ Controller::class, 'restConfigCreate' ]); // Router::post('/api/v1/tenants', [ Controller::class, 'create' ]); // Router::get('/api/v1/tenants/{id}/users/{id}', [ Controller::class, 'get' ]); // Router::put('/api/v1/tenants/{id}', [ Controller::class, 'save' ]); // Router::patch('/api/v1/tenants/{id}/status', [ Controller::class, 'toggleStatus' ]); // Router::get('/api/v1/tenants/{id}/delete', [ Controller::class, 'restConfigDelete' ]); // Router::delete('/api/v1/tenants/{id}', [ Controller::class, 'delete' ]); Hooks::add('router::get->api.v1.rest.routes', function( $routes ) { if( !Users\Model::isSuperadmin() ) return $routes; $routes[] = [ "path" => "/tenants/{id}/users/{id}", "label" => "Benutzer", ]; return $routes; }); } } /** Datei: plugins/tenants/library/ARUNOX/Plugins/Tenants/Users/API/v1/Controller.php **/ * @copyright 2025 ARUNOX * @version 1.1.0 * * @since 1.0.0 File is created */ namespace ARUNOX\Plugins\Tenants\Users\API\v1; use ARUNOX\HTTP; use ARUNOX\Core\Hooks; use ARUNOX\Plugins\REST; use ARUNOX\Plugins\Users; use ARUNOX\Plugins\Tenants; use ARUNOX\API\BaseController; class Controller extends BaseController { /* ### VARIABLES ################################################ # */ protected static array $NOTIFY = [ "notfound" => "Benutzer nicht gefunden", ]; /* ### PUBLIC GET FUNCTIONS ##################################### # */ public function getAll( int $tenant_id ) : HTTP\Response { $results = Users\Model::getAll([ "tenant_id__meta" => $tenant_id ]); return $this->jsonResponse( $results ); } public function get( int $tenant_id, int $user_id ) : HTTP\Response { $result = Users\Model::get([ "id" => $user_id, "tenant_id__meta" => $tenant_id ]); if( $result ) { Users\API\v1\Controller::restConfigDetails( $result ) ->setBacklink( "/tenants/{$tenant_id}#users" ); } else REST\Response::addNotify( "danger", static::getNotify("notfound") ); return REST\Response::jsonResponse(); } /* ### PUBLIC REST FUNCTIONS #################################### # */ public static function restConfigTable() : REST\Generator\Base\BaseGenerator { $table = Users\API\v1\Controller::restConfigTable() ->setApi( "/tenants/{id}/users" ) ->setAction( "/api/v1/tenants/{tenant_id}/users/{id}" ); foreach( $table->get()->getButtons() as $button ) { $action = $button->getAction(); if( $action["route"] != "/users/{id}" ) continue; $action["route"] = "/tenants/{tenant_id}/users/{id}"; $button->setAction( ...$action ); break; } return $table; } }