/** Datei: plugins/users/library/ARUNOX/Plugins/Users/API/v1/Routes.php **/ * @copyright 2025 ARUNOX * @version 1.1.0 * * @since 1.0.0 File is created */ namespace ARUNOX\Plugins\Users\API\v1; use ARUNOX\Core\Hooks; use ARUNOX\Core\Router; use ARUNOX\Plugins\REST; class Routes { public static function register() : void { /* ### MAIN ROUTES ############################## # */ Router::addAll( Controller::class ); /* ### MENU ROUTES ############################## # */ Hooks::add('router::get->api.v1.rest.routes', function( $routes ) { $routes[] = [ "path" => "/users", "label" => "Benutzer", "icon" => "bi bi-person", "menu" => "management", ]; $routes[] = [ "path" => "/users/{id}", "label" => "Bearbeiten", ]; $routes[] = [ "path" => "/logout", "icon" => "bi bi-power", "title" => "Abmelden", "color" => [ "color" => REST\Generator\Config\Color::Danger, "target" => "button" ], "menu" => [ "navbar", "logout" ], ]; $routes[] = [ "path" => "/users/me", "icon" => "bi bi-person", "title" => "Mein Profil", "menu" => "navbar", ]; return $routes; }); } } /** Datei: plugins/users/library/ARUNOX/Plugins/Users/API/v1/Controller.php **/ * @copyright 2025 ARUNOX * @version 1.1.0 * * @since 1.0.0 File is created */ namespace ARUNOX\Plugins\Users\API\v1; use ARUNOX\HTTP; use ARUNOX\API\PHP; use ARUNOX\Core\Hooks; use ARUNOX\Plugins\Logs; use ARUNOX\Plugins\REST; use ARUNOX\Plugins\Users; use ARUNOX\API\BaseController; use ARUNOX\Database\BaseEntity; #[PHP\Attributes\Controller\Label( "Benutzer" )] #[PHP\Attributes\Controller\Icon( REST\Generator\Config\Icon::Person )] #[PHP\Attributes\Controller\Route( "users" )] #[PHP\Attributes\Database\Definitions\Model( Users\Model::class )] class Controller extends BaseController { /* ### VARIABLES ################################################ # */ protected static array $NOTIFY = [ "notfound" => "Benutzer nicht gefunden", "saved" => "Benutzer wurde erfolgreich gespeichert", "deleted" => "Benutzer wurde erfolgreich gelöscht", "created" => "Benutzer wurde erfolgreich erstellt", "statuschanged.0" => "Benutzer wurde erfolgreich deaktiviert", "statuschanged.1" => "Benutzer wurde erfolgreich aktiviert", "notcreated" => "Benutzer konnte nicht erstellt werden", "notsaved" => "Benutzer konnte nicht gespeichert werden", "passwordchanged" => "Passwort wurde erfolgreich geändert", "loginfailed" => "Anmeldung nicht erfolgreich", "loggedin" => "Erfolgreich angemeldet", "loggedout" => "Erfolgreich abgemeldet", "simulationstarted" => "Simulation gestartet", "simulationstopped" => "Simulation beendet", ]; /* ### PUBLIC GET FUNCTIONS ##################################### # */ /** * @api * @method GET * * @route.path /users * @route.description Listet alle Benutzer */ public static function getAll() : array { return Users\Model::getAll(); } /** * @api * @method GET * * @route.path /users/{hashid} * @route.description Gibt einen einzelnen Benutzer anhand der ID zurück */ public static function get( int|string $hashid ) : BaseEntity | HTTP\Response { if( $result = Users\Model::get( $hashid ) ) return $result; return REST\Response::autoResponse( 404, static::$NOTIFY["notfound"] ); } /** * @api * @method POST * * @route.path /login * @route.description Check Benutzer Login */ public static function login( array $data ) : HTTP\Response { if( $result = Users\Model::getByEmail( $data["email"] ) ) { if( $result->checkPasswort( $data["password"] ) ) { Users\Model::setCurrent( $result ); return REST\Response::autoResponse( 200, static::$NOTIFY["loggedin"] ); } } return REST\Response::autoResponse( 500, static::$NOTIFY["loginfailed"] ); } /** * @api * @method GET * * @route.path /logout * @route.description Benutzer Logout */ public static function logout() : HTTP\Response { Users\Model::removeCurrent(); return REST\Response::autoResponse( 200, static::$NOTIFY["loggedout"] ); } /* ### PUBLIC SAVE FUNCTIONS #################################### # */ /** * @api * @method PUT * * @route.path /users/{hashid} * @route.description Speichert Änderungen an einem bestehenden Benutzer */ public static function save( int|string $hashid, array $data ) : HTTP\Response { if( $result = Users\Model::get( $hashid ) ) { if( $result->save( $data ) ) { return REST\Response::autoResponse( 200, static::$NOTIFY["saved"] ); } return REST\Response::autoResponse( 500, static::$NOTIFY["notsaved"] ); } return REST\Response::autoResponse( 404, static::$NOTIFY["notfound"] ); } /** * @api * @method POST * * @route.path /users * @route.description Erstellt einen neuen Agent */ public static function create( array $data ) : HTTP\Response { if( $hashid = Users\Model::save( $data ) ) { $result = Users\Model::get( $hashid ); return REST\Response::autoResponse( 200, static::$NOTIFY["created"], [ "data" => $result ] ); } return REST\Response::autoResponse( 500, static::$NOTIFY["notfound"] ); } /** * @api * @method DELETE * * @route.path /users/{hashid} * @route.description Löscht einen Agent anhand der ID */ public static function delete( int|string $hashid ) : HTTP\Response { if( Users\Model::delete( $hashid ) ) { return REST\Response::autoResponse( 200, static::$NOTIFY["deleted"] ); } return REST\Response::autoResponse( 500, static::$NOTIFY["notfound"] ); } /** * @api * @method PATCH * * @route.path /users/{hashid}/status * @route.description Wechselt den Status (aktiv/inaktiv) eines Benutzers */ public static function toggleStatus( int|string $hashid ) : HTTP\Response { if( $result = Users\Model::get( $hashid ) ) { $result->toggleStatus( TRUE ); return REST\Response::autoResponse( 200, static::$NOTIFY["statuschanged.{$result->status->getValue()}"], [ "data" => $result ] ); } return REST\Response::autoResponse( 404, static::$NOTIFY["notfound"] ); } /** * @api * @method PATCH * * @route.path /users/{hashid}/password * @route.description Ändern des Passworts eines Benutzers */ public static function changePassword( int|string $hashid, array $data ) { if( $result = Users\Model::get( $hashid ) ) { $result->changePassword( $data["password"], TRUE ); return REST\Response::autoResponse( 200, static::$NOTIFY["passwordchanged"], [ "data" => $result ] ); } return REST\Response::autoResponse( 404, static::$NOTIFY["notfound"] ); } public function startSimulate( int $id ) { if( $result = Users\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 ################################ # */ /** * @api * @method POST * * @route.path /users/{hashid}/validate/username * @route.description Validiert eingegeben Benutzernamen */ public static function validateUsername( int|string $hashid, array $data ) { if( isset( $data["value"] ) && $data["value"] ) { if( $result = Users\Model::getByUsername( $data["value"] ) ) { if( $result->id != $hashid && $result->hashid != $hashid ) { return REST\Response::jsonResponse( 200, [ "valid" => FALSE ] ); } } } return REST\Response::jsonResponse( 200, [ "valid" => TRUE ] ); } /** * @api * @method POST * * @route.path /users/{hashid}/validate/email * @route.description Validiert eingegebene E-Mail-Adresse */ public static function validateEmail( int|string $hashid, array $data ) { if( isset( $data["value"] ) && $data["value"] ) { if( $result = Users\Model::getByEmail( $data["value"] ) ) { if( $result->id != $hashid && $result->hashid != $hashid ) { return REST\Response::jsonResponse( 200, [ "valid" => FALSE ] ); } } } return REST\Response::jsonResponse( 200, [ "valid" => TRUE ] ); } /* ### PUBLIC REST FUNCTIONS #################################### # */ /* ### PUBLIC REST RESPONSE FUNCTIONS ########################### # */ /** * @internal rest * @method PUT * * @route.path /users/{hashid} * @route.description REST-Antwort nach dem Speichern eines Benutzers */ public static function restResponseSave( int|string $hashid, array $data ) : HTTP\Response { $result = static::save( $hashid, $data ); if( $result->isStatus( 200 ) ) { $data = Users\Model::get( $hashid ); REST\Response::addUpdater( [ "data/table/row", $hashid ], $data ); } return static::restResponse( $result ); } /** * @internal rest * @method POST * * @route.path /users * @route.description REST-Antwort nach dem Erstellen eines Benutzers (inkl. Notifikation, Redirect etc.) */ public static function restResponseCreate( array $data ) : HTTP\Response { $result = static::create( $data ); if( $result->isStatus( 200 ) ) { REST\Response::redirect( "/users/{$result->getBody("data", "hashid")}" ); } return static::restResponse( $result ); } /** * @internal rest * @method DELETE * * @route.path /users/{hashid} * @route.description REST-Antwort nach dem Löschen eines Benutzers */ public static function restResponseDelete( int|string $hashid ) : HTTP\Response { REST\Response::closeModals(); $result = static::delete( $hashid ); if( $result->isStatus( 200 ) ) { REST\Response::redirect( "/users" ); } return static::restResponse( $result ); } /** * @internal rest * @method PATCH * * @route.path /users/{hashid}/status * @route.description REST-Antwort zur Status-Umschaltung eines Benutzers (z. B. für Notifications) */ public static function restResponseToggleStatus( int|string $hashid ) : HTTP\Response { $result = static::toggleStatus( $hashid ); if( $result->isStatus( 200 ) ) { $data = $result->getBody("data"); REST\Response::addUpdater( [ "data/table/row", $hashid ], $data ); } return static::restResponse( $result ); } /** * @internal rest * @method PATCH * * @route.path /users/{hashid}/password * @route.description REST-Antwort zur Status-Umschaltung eines Benutzers (z. B. für Notifications) */ public static function restResponseChangePassword( int|string $hashid, array $data ) : HTTP\Response { $result = static::changePassword( $hashid, $data ); if( $result->isStatus( 200 ) ) { $data = $result->getBody("data"); REST\Response::addUpdater( [ "data/table/row", $hashid ], $data ); } return static::restResponse( $result ); } /** * @internal rest * @method POST * * @route.path /login * @route.description REST-Konfiguration für Benutzer Lougoin */ public static function restResponseLogin( array $data ) : HTTP\Response { return static::restResponse( static::login( $data ), function(){ $referer = HTTP\Request::referer(); // if( isset( $referer["query"]["redirect"] ) ) { // REST\Response::redirect( $referer["query"]["redirect"] ); // } redirect( "/api/v1/rest/{$referer["path"]}" ); }); } /** * @internal rest * @method GET * * @route.path /logout * @route.description REST-Konfiguration für Benutzer Logout */ public static function restResponseLogout() { return static::restResponse( static::logout(), function(){ static::restConfigLogin(); }, TRUE ); } /* ### PUBLIC REST CONFIG FUNCTIONS ############################# # */ /** * @internal rest * @method GET * * @route.path /users * @route.description REST-Konfiguration für Benutzer-Tabelle */ public static function restConfigOverview() : REST\Generator\Base\BaseGenerator { return REST\Response::addWrapper( "overview", "Benutzer" ) ->get() ->setApi( "/users" ) ->setAction( "/users/{hashid}" ) ->setOrder( "name", "asc" ) ->setSearch( TRUE ) ->addButton(...Hooks::filter( "rest.generator::button.name->create", [], "users" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->text", [], "username", TRUE, TRUE, TRUE, "users" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->icon", [ "icon" => "bi-key", "options" => Users\Role::getAll(), ], "role", [], "users" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->status", [], "status", [], "users", "hashid" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->email", [], "email", TRUE, TRUE, TRUE, "users" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->datetime", [], "date_edit", FALSE, FALSE, TRUE, "users" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->actions", [], [], FALSE, "users" ))->get() ->addButton(...Hooks::filter( "rest.generator::action.name->password", [], "users", "hashid" )) ->addButton(...Hooks::filter( "rest.generator::action.name->edit", [], "users", "hashid" )) ->addButton(...Hooks::filter( "rest.generator::action.name->delete", [], "users", "hashid" )); } /** * @internal rest * @method GET * * @route.path /users/{hashid} * @route.alternative.0 /users/{hashid}#kontext * @route.alternative.1 /users/{hashid}#chat * @route.alternative.2 /users/{hashid}#telefon * @route.description REST-Konfiguration für Detailansicht eines benutzers */ public static function restConfigDetails( int|string $hashid ) : REST\Generator\Base\BaseGenerator { return REST\Response::addWrapper( "details", "Assistent bearbeiten" ) ->setBacklink( "/users" ) ->get() ->setApi( "/users/{$hashid}" ) ->setAction( "/users/{$hashid}", "PUT" ) ->addTab( "User" )->get() ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "username", TRUE, "users" )) ->addField(...Hooks::filter( "rest.generator::control.type->email", [], "email", TRUE, "users" )) ->addField(...Hooks::filter( "rest.generator::control.type->select", [ "options" => Users\Role::getAll(), ], "role", TRUE, FALSE, "users" )) ->parent() ->addCallable( [ Logs\API\v1\Controller::class, "restConfigTab" ], $hashid ) ->addTab( "Password" )->get() ->setColor( REST\Generator\Config\Color::Secondary ) ->setIcon( "bi bi-key" ) ->setType( "button" ) ->setAction( "/users/{$hashid}/password", "GET" ) ->parent() ->addTab( "Speichern" )->get() ->setColor( REST\Generator\Config\Color::Primary ) ->setIcon( "bi bi-save" ) ->setType( "submit" ) ; } /** * @internal rest * @method GET * * @route.path /users/create * @route.description REST-Konfiguration für das Erstellen eines Benutzers (Formular) */ public static function restConfigCreate() : REST\Generator\Base\BaseGenerator { return REST\Response::addModal( "details", "Benutzer erstellen" ) ->setData([ "hashid" => "-NEW-" ]) ->get() ->setAction( "/users", "POST" ) ->addButton(...Hooks::filter( "rest.generator::button.type->submit", [], "Erstellen", "users", "hashid" )) ->addField(...Hooks::filter( "rest.generator::control.type->select", [ "options" => Users\Role::getAll(), ], "role", TRUE, FALSE, "users" )) ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "username", TRUE, "users", "hashid" )) ->addField(...Hooks::filter( "rest.generator::control.type->email", [], "email", TRUE, "users", "hashid" )) ; } /** * @internal rest * @method GET * * @route.path /users/{hashid}/delete * @route.description REST-Konfiguration für die Lösch-Bestätigung eines Benutzers */ public static function restConfigDelete( int|string $hashid ) : REST\Generator\Base\BaseGenerator { return REST\Response::addModal( "details", "Benutzer Löschen" ) ->setSize( REST\Wrapper\Modal\Size::sm ) ->get() ->setAction( "/users/{$hashid}", "DELETE" ) ->addText( "Möchtest du wirklich den Datensatz (#{$hashid}) löschen? " ) ->addButton(...Hooks::filter( "rest.generator::button.type->cancel", [], NULL, "users" )) ->addButton(...Hooks::filter( "rest.generator::button.type->delete", [], NULL, "users" )); } /** * @internal rest * @method GET * * @route.path /users/{hashid}/password * @route.description REST-Konfiguration für die Lösch-Bestätigung eines Benutzers */ public static function restConfigPassword( int|string $hashid ) : REST\Generator\Base\BaseGenerator { if( $result = Users\Model::get( $hashid ) ) { return REST\Response::addModal( "details", "Passwort ändern" ) ->get() ->setAction( "/api/v1/users/{$hashid}/password", "PATCH" ) ->addText( "Benutzer: {$result->username} (#{$result->hashid})" ) ->addButton(...Hooks::filter( "rest.generator::button.type->submit", [], "Speichern", "users" )) ->addField(...Hooks::filter( "rest.generator::control.type->password", [], "password", TRUE, "login" )); } else return REST\Response::addNotify( "danger", static::$NOTIFY["notfound"] ); } /** * @internal rest * @method GET * * @route.path /login * @route.description REST-Konfiguration für Benutzer Login */ public static function restConfigLogin() { REST\Response::setTemplate( "login" ); return REST\Response::addWrapper( "details", "Willkommen zurück" ) ->setHistory() ->get() ->addText( "Logge dich ein um fortzufahren" ) ->setAction( "/api/v1/login", "POST" ) ->addButton(...Hooks::filter( "rest.generator::button.type->submit", [], "Anmelden", "users" )) // ->addField(...Hooks::filter( "rest.generator::control.type->text", [], "text", TRUE, "login" )) ->addField(...Hooks::filter( "rest.generator::control.type->email", [], "email", TRUE, "login" )) ->addField(...Hooks::filter( "rest.generator::control.type->password", [], "password", FALSE, "login" )) ->top(); } /* ### PUBLIC REST PREPARE FUNCTIONS ############################ # */ public static function restConfigTable() { return REST\Generator\Data\Table::create() ->setApi( "/api/v1/users" ) ->setAction( "/api/v1/users/{hashid}" ) ->setOrder( "username", "asc" ) ->setSearch( TRUE ) ->addButton(...Hooks::filter( "rest.generator::button.name->create", [], "users" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->text", [], "username", TRUE, TRUE, TRUE, "users" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->icon", [], "role", [], "users" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->status", [], "status", [], "users" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->email", [], "email", TRUE, TRUE, TRUE, "users" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->datetime", [], "date_edit", FALSE, FALSE, TRUE, "users" )) ->addColumn(...Hooks::filter( "rest.generator::column.type->actions", [], [], FALSE, "users" ))->get() ->addButton(...Hooks::filter( "rest.generator::action.name->edit", [], "users" )) ->addButton(...Hooks::filter( "rest.generator::action.name->delete", [], "users" )) ->top(); } }