CI / Tests (PHP 8.2) (pull_request) Successful in 44s
CI / Tests (PHP 8.1) (pull_request) Successful in 46s
CI / No Debug Code (pull_request) Successful in 2s
CI / Coding Standards (pull_request) Successful in 2m46s
CI / PHPStan (pull_request) Successful in 2m51s
CI / Tests (PHP 8.3) (pull_request) Successful in 2m38s
CI / Build Plugin Zip (pull_request) Skipped
A studio admin can generate a shareable group invite link (e.g. for a newsletter) from the Invites page, choosing a required expiry date. Anyone with the link may register while it is valid, in any registration mode: the form collects their own email, they must confirm it via the usual hashed token, and confirming approves the account immediately — group signups never enter the Pending Students queue. - us_invites grows kind (personal/group) and expires_at; an explicit expiry wins over the personal 14-day window. Group links stay pending (multi-use) until revoked or expired. - RegistrationPage: group signups create the account pending with the us_auto_approve marker and send the confirmation email; no auto-login. - EmailConfirmationHandler: auto-approve accounts are approved on confirmation, emailed the approved notice, and redirected to a new us_confirmed=ready notice with a sign-in link. Closes #77 Co-Authored-By: Claude Fable 5 <[email protected]>
128 lines
3.8 KiB
PHP
128 lines
3.8 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Unsupervised\Schedular\Auth;
|
|
|
|
use Unsupervised\Schedular\Val;
|
|
|
|
class RegistrationController {
|
|
|
|
/**
|
|
* Option storing the page ID that hosts the [us_student_register] shortcode.
|
|
*/
|
|
public const OPTION_PAGE = 'us_registration_page_id';
|
|
|
|
public function __construct( private InviteRepository $invites ) {}
|
|
|
|
public function renderPage(): void {
|
|
if ( ! current_user_can( RoleManager::CAP_MANAGE_STUDENTS ) ) {
|
|
wp_die( esc_html__( 'You do not have permission to manage invites.', 'unsupervised-schedular' ) );
|
|
}
|
|
|
|
$newInviteUrl = '';
|
|
if ( isset( $_POST['usc_action'] ) && check_admin_referer( 'usc_invite_action' ) ) {
|
|
$newInviteUrl = $this->handleFormAction();
|
|
}
|
|
|
|
$pendingInvites = $this->invites->findPending();
|
|
$registrationPageId = Val::int( get_option( self::OPTION_PAGE, 0 ) );
|
|
$registrationPageUrl = $registrationPageId > 0 ? (string) get_permalink( $registrationPageId ) : '';
|
|
|
|
include USC_PLUGIN_DIR . 'templates/admin/invites.php';
|
|
}
|
|
|
|
/**
|
|
* Handle a posted admin action. Returns the registration link for a freshly
|
|
* created invite — the only time it can be shown, since just the token's hash
|
|
* is stored — or an empty string for every other action.
|
|
*/
|
|
private function handleFormAction(): string {
|
|
// Nonce is verified by the caller (renderPage) before this method runs.
|
|
// phpcs:disable WordPress.Security.NonceVerification.Missing
|
|
$action = sanitize_key( Val::string( wp_unslash( $_POST['usc_action'] ?? '' ) ) );
|
|
|
|
if ( 'set_page' === $action ) {
|
|
update_option( self::OPTION_PAGE, absint( Val::int( $_POST['registration_page_id'] ?? 0 ) ) );
|
|
}
|
|
|
|
if ( 'invite' === $action ) {
|
|
$email = sanitize_email( Val::string( wp_unslash( $_POST['email'] ?? '' ) ) );
|
|
|
|
if (
|
|
is_email( $email )
|
|
&& false === email_exists( $email )
|
|
&& null === $this->invites->findPendingByEmail( $email )
|
|
) {
|
|
$rawToken = wp_generate_password( 32, false );
|
|
|
|
$this->invites->insert(
|
|
new Invite(
|
|
email: $email,
|
|
token: Invite::hashToken( $rawToken ),
|
|
invitedBy: get_current_user_id(),
|
|
)
|
|
);
|
|
|
|
return $this->registrationLink( $rawToken );
|
|
}
|
|
}
|
|
|
|
if ( 'group_invite' === $action ) {
|
|
$expiresAt = $this->normalizeExpiry( sanitize_text_field( Val::string( wp_unslash( $_POST['expires_at'] ?? '' ) ) ) );
|
|
|
|
if ( null !== $expiresAt ) {
|
|
$rawToken = wp_generate_password( 32, false );
|
|
|
|
$this->invites->insert(
|
|
new Invite(
|
|
email: '',
|
|
token: Invite::hashToken( $rawToken ),
|
|
invitedBy: get_current_user_id(),
|
|
kind: Invite::KIND_GROUP,
|
|
expiresAt: $expiresAt,
|
|
)
|
|
);
|
|
|
|
return $this->registrationLink( $rawToken );
|
|
}
|
|
}
|
|
|
|
if ( 'revoke' === $action ) {
|
|
$inviteId = absint( Val::int( $_POST['invite_id'] ?? 0 ) );
|
|
if ( $inviteId > 0 ) {
|
|
$this->invites->revoke( $inviteId );
|
|
}
|
|
}
|
|
// phpcs:enable WordPress.Security.NonceVerification.Missing
|
|
|
|
return '';
|
|
}
|
|
|
|
/**
|
|
* Validate a submitted group-link expiry date (strict `Y-m-d`, today or
|
|
* later) and expand it to the end of that day; null when invalid or past.
|
|
*/
|
|
private function normalizeExpiry( string $date ): ?string {
|
|
$day = \DateTimeImmutable::createFromFormat( '!Y-m-d', $date );
|
|
if ( false === $day || $day->format( 'Y-m-d' ) !== $date ) {
|
|
return null;
|
|
}
|
|
|
|
if ( $date < Val::string( current_time( 'Y-m-d' ) ) ) {
|
|
return null;
|
|
}
|
|
|
|
return $date . ' 23:59:59';
|
|
}
|
|
|
|
/**
|
|
* Build the registration URL for a raw invite token.
|
|
*/
|
|
private function registrationLink( string $rawToken ): string {
|
|
$pageId = Val::int( get_option( self::OPTION_PAGE, 0 ) );
|
|
$linkBase = $pageId > 0 ? (string) get_permalink( $pageId ) : '';
|
|
|
|
return add_query_arg( 'us_invite', rawurlencode( $rawToken ), '' !== $linkBase ? $linkBase : home_url( '/' ) );
|
|
}
|
|
}
|