diff --git a/.gitignore b/.gitignore index 8cacf425..7626638e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ .phpunit.result.cache .php-cs-fixer.cache +vendor/composer/*.zip~ diff --git a/docs/03_login_logout_profile_register.md b/docs/03_login_logout_profile_register.md index 8ed6eabb..16bace05 100644 --- a/docs/03_login_logout_profile_register.md +++ b/docs/03_login_logout_profile_register.md @@ -120,6 +120,16 @@ action|ycom_auth_db ## Registrierung +> **Hinweis zu Status-Werten:** Die Status-Werte sind als Konstanten in der Klasse `rex_ycom_user` definiert. Während in YForm-Formularen die numerischen Werte verwendet werden (z.B. `hidden|status|0`), kann im PHP-Code auf die entsprechenden Konstanten zurückgegriffen werden: +> - `STATUS_INACTIVE_TERMINATION = -3` +> - `STATUS_INACTIVE_LOGINS = -2` +> - `STATUS_INACTIVE = -1` +> - `STATUS_REQUESTED = 0` +> - `STATUS_CONFIRMED = 1` +> - `STATUS_ACTIVE = 2` +> +> Siehe auch die [Status-Liste](05_passwords.md#status-liste-eines-ycom-accounts) für weitere Details. + ### Registrierungs-Formular ```php diff --git a/docs/05_passwords.md b/docs/05_passwords.md index 780b7243..d2602b4d 100644 --- a/docs/05_passwords.md +++ b/docs/05_passwords.md @@ -120,14 +120,45 @@ Der Status kann hier auch 2 sein (`status=2`), wenn man mit selbst angelegten Ac #### Status Liste eines YCom-Accounts -Interner Name | Übersetzung | Wert ------- | ------ | ------ -ycom_account_inactive_termination | Zugang wurde gekündigt | -3 -ycom_account_inactive_logins | Zugang wurde deaktiviert [Loginfehlversuche] | -2 -ycom_account_inactive | Zugang ist inaktiv | -1 -ycom_account_requested | Zugang wurde angefragt | 0 -ycom_account_confirm | Zugang wurde bestätigt und ist aktiv | 1 -ycom_account_active | Zugang ist aktiv | 2 +Interner Name | Übersetzung | Wert | Konstante +------ | ------ | ------ | ------ +ycom_account_inactive_termination | Zugang wurde gekündigt | -3 | `rex_ycom_user::STATUS_INACTIVE_TERMINATION` +ycom_account_inactive_logins | Zugang wurde deaktiviert [Loginfehlversuche] | -2 | `rex_ycom_user::STATUS_INACTIVE_LOGINS` +ycom_account_inactive | Zugang ist inaktiv | -1 | `rex_ycom_user::STATUS_INACTIVE` +ycom_account_requested | Zugang wurde angefragt | 0 | `rex_ycom_user::STATUS_REQUESTED` +ycom_account_confirm | Zugang wurde bestätigt und ist aktiv | 1 | `rex_ycom_user::STATUS_CONFIRMED` +ycom_account_active | Zugang ist aktiv | 2 | `rex_ycom_user::STATUS_ACTIVE` + +##### Status-Konstanten + +Seit Version X.X werden die Status-Werte als Konstanten in der Klasse `rex_ycom_user` definiert. Diese können im Code verwendet werden, um die Lesbarkeit zu erhöhen: + +```php +// Statt: +$data['status'] = 1; + +// Verwende: +$data['status'] = rex_ycom_user::STATUS_CONFIRMED; +``` + +##### Status-Optionen erweitern + +Die Status-Optionen können über den Extension Point `YCOM_USER_STATUS_OPTIONS` erweitert oder angepasst werden: + +```php +rex_extension::register('YCOM_USER_STATUS_OPTIONS', function (rex_extension_point $ep) { + /** @var array $statusOptions */ + $statusOptions = $ep->getSubject(); + + // Eigenen Status hinzufügen + $statusOptions[3] = 'translate:ycom_account_premium'; + + // Optional: Sortierung anpassen + ksort($statusOptions); + + return $statusOptions; +}); +``` #### Weitere Hinweise diff --git a/docs/12_status_examples.md b/docs/12_status_examples.md new file mode 100644 index 00000000..0f5d4381 --- /dev/null +++ b/docs/12_status_examples.md @@ -0,0 +1,104 @@ +# YCom User Status - Beispiele + +## Status-Konstanten verwenden + +### Im PHP-Code + +```php +setValue('email', 'test@example.com'); +$user->setValue('login', 'test@example.com'); +$user->setValue('status', rex_ycom_user::STATUS_CONFIRMED); +$user->save(); + +// Status prüfen +$currentUser = rex_ycom_user::getMe(); +if ($currentUser && $currentUser->getValue('status') >= rex_ycom_user::STATUS_CONFIRMED) { + // User ist eingeloggt und bestätigt +} + +// Status auf inaktiv setzen +$user->setValue('status', rex_ycom_user::STATUS_INACTIVE); +$user->save(); +``` + +## Status-Optionen erweitern via Extension Point + +### Beispiel 1: Eigenen Status hinzufügen + +```php + $statusOptions */ + $statusOptions = $ep->getSubject(); + + // Premium-Status hinzufügen (Wert 3) + $statusOptions[3] = 'translate:ycom_account_premium'; + + // VIP-Status hinzufügen (Wert 4) + $statusOptions[4] = 'translate:ycom_account_vip'; + + return $statusOptions; +}); +``` + +### Beispiel 2: Status-Optionen anpassen und sortieren + +```php + $statusOptions */ + $statusOptions = $ep->getSubject(); + + // Bestimmte Stati entfernen (optional) + unset($statusOptions[rex_ycom_user::STATUS_INACTIVE_TERMINATION]); + + // Eigene Stati hinzufügen + $statusOptions[10] = 'translate:ycom_account_special'; + + // Nach Schlüssel sortieren + ksort($statusOptions); + + return $statusOptions; +}); +``` + +### Beispiel 3: Status-Labels projektspezifisch anpassen + +```php + $statusOptions */ + $statusOptions = $ep->getSubject(); + + // Standard-Labels durch eigene ersetzen + $statusOptions[rex_ycom_user::STATUS_ACTIVE] = 'Aktives Mitglied'; + $statusOptions[rex_ycom_user::STATUS_CONFIRMED] = 'Bestätigtes Mitglied'; + $statusOptions[rex_ycom_user::STATUS_REQUESTED] = 'Registrierung ausstehend'; + + return $statusOptions; +}); +``` + +## Verfügbare Status-Konstanten + +| Konstante | Wert | Standard-Label | +|-----------|------|----------------| +| `rex_ycom_user::STATUS_INACTIVE_TERMINATION` | -3 | Zugang wurde gekündigt | +| `rex_ycom_user::STATUS_INACTIVE_LOGINS` | -2 | Zugang wurde deaktiviert [Loginfehlversuche] | +| `rex_ycom_user::STATUS_INACTIVE` | -1 | Zugang ist inaktiv | +| `rex_ycom_user::STATUS_REQUESTED` | 0 | Zugang wurde angefragt | +| `rex_ycom_user::STATUS_CONFIRMED` | 1 | Zugang wurde bestätigt und ist aktiv | +| `rex_ycom_user::STATUS_ACTIVE` | 2 | Zugang ist aktiv | + +## Status-Logik + +- **Login erlaubt**: Status >= 1 (STATUS_CONFIRMED oder höher) +- **Login nicht erlaubt**: Status <= -1 (STATUS_INACTIVE oder niedriger) +- **Registriert, aber Nutzungsbedingungen fehlen**: Status = 0 (STATUS_REQUESTED) diff --git a/install/tablesets/yform_user.json b/install/tablesets/yform_user.json index f9efdc66..8eeb015e 100644 --- a/install/tablesets/yform_user.json +++ b/install/tablesets/yform_user.json @@ -177,7 +177,7 @@ "attributes": "", "choice_attributes": "", "choice_label": "", - "choices": "translate:ycom_account_inactive_termination=-3,translate:ycom_account_inactive_logins=-2,translate:ycom_account_inactive=-1,translate:ycom_account_requested=0,translate:ycom_account_confirm=1,translate:ycom_account_active=2", + "choices": "callable::rex_ycom_user::getStatusOptions", "db_type": "text", "default": "-1", "expanded": "", diff --git a/lib/ycom_user.php b/lib/ycom_user.php index ae2100c9..15c194dc 100644 --- a/lib/ycom_user.php +++ b/lib/ycom_user.php @@ -5,6 +5,24 @@ class rex_ycom_user extends rex_yform_manager_dataset public string $password = ''; public int $login_tries = 0; + // Status constants + public const STATUS_INACTIVE_TERMINATION = -3; + public const STATUS_INACTIVE_LOGINS = -2; + public const STATUS_INACTIVE = -1; + public const STATUS_REQUESTED = 0; + public const STATUS_CONFIRMED = 1; + public const STATUS_ACTIVE = 2; + + // Default status options + public const DEFAULT_STATUS_OPTIONS = [ + self::STATUS_INACTIVE_TERMINATION => 'translate:ycom_account_inactive_termination', + self::STATUS_INACTIVE_LOGINS => 'translate:ycom_account_inactive_logins', + self::STATUS_INACTIVE => 'translate:ycom_account_inactive', + self::STATUS_REQUESTED => 'translate:ycom_account_requested', + self::STATUS_CONFIRMED => 'translate:ycom_account_confirm', + self::STATUS_ACTIVE => 'translate:ycom_account_active', + ]; + /** * @return rex_ycom_user|null */ @@ -53,7 +71,7 @@ public function getGroups(): array */ public static function createUserByEmail(array $data) { - $data['status'] = 1; + $data['status'] = self::STATUS_CONFIRMED; $data['password'] = str_shuffle('1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'); $data['login'] = $data['email']; $data['login_tries'] = 0; @@ -111,4 +129,18 @@ public function resetOTPTries(): self $this->setValue('otp_last_try_time', time()); return $this; } + + /** + * Get status options with extension point for customization. + * @return array + */ + public static function getStatusOptions(): array + { + // Allow additional status options via extension point + $statusOptions = rex_extension::registerPoint(new rex_extension_point('YCOM_USER_STATUS_OPTIONS', self::DEFAULT_STATUS_OPTIONS)); + if (!is_array($statusOptions)) { + $statusOptions = self::DEFAULT_STATUS_OPTIONS; + } + return $statusOptions; + } } diff --git a/tests/unit/user_status_test.php b/tests/unit/user_status_test.php new file mode 100644 index 00000000..55757d45 --- /dev/null +++ b/tests/unit/user_status_test.php @@ -0,0 +1,65 @@ +