Files
unsupervised-scheduler/src/Auth/InviteRepository.php
T
thatguygriffandClaude Fable 5 356d9f984d
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
Add multi-use group invite links with expiry and auto-approval on email confirmation
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]>
2026-07-22 10:44:16 -03:00

108 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
namespace Unsupervised\Schedular\Auth;
class InviteRepository {
private string $table;
public function __construct( private \wpdb $db ) {
$this->table = $db->prefix . 'us_invites';
}
public function insert( Invite $invite ): int {
$this->db->insert(
$this->table,
[
'email' => $invite->email,
'token' => $invite->token,
'role' => $invite->role,
'kind' => $invite->kind,
'status' => $invite->status,
'invited_by' => $invite->invitedBy,
'accepted_user_id' => $invite->acceptedUserId,
'created_at' => current_time( 'mysql' ),
'accepted_at' => $invite->acceptedAt,
'expires_at' => $invite->expiresAt,
],
[ '%s', '%s', '%s', '%s', '%s', '%d', '%d', '%s', '%s', '%s' ]
);
return $this->db->insert_id;
}
public function findByToken( string $token ): ?Invite {
$row = $this->db->get_row(
$this->db->prepare( 'SELECT * FROM %i WHERE token = %s', $this->table, $token )
);
return $row ? Invite::fromRow( $row ) : null;
}
public function findById( int $id ): ?Invite {
$row = $this->db->get_row(
$this->db->prepare( 'SELECT * FROM %i WHERE id = %d', $this->table, $id )
);
return $row ? Invite::fromRow( $row ) : null;
}
/**
* The most recent pending invite for an email, if any.
*/
public function findPendingByEmail( string $email ): ?Invite {
$row = $this->db->get_row(
$this->db->prepare(
'SELECT * FROM %i WHERE email = %s AND status = %s ORDER BY id DESC LIMIT 1',
$this->table,
$email,
Invite::STATUS_PENDING
)
);
return $row ? Invite::fromRow( $row ) : null;
}
/**
* All invites awaiting acceptance, newest first.
*
* @return list<Invite>
*/
public function findPending(): array {
$rows = $this->db->get_results(
$this->db->prepare(
'SELECT * FROM %i WHERE status = %s ORDER BY created_at DESC',
$this->table,
Invite::STATUS_PENDING
)
);
return array_map( Invite::fromRow( ... ), $rows ?? [] );
}
public function markAccepted( int $id, int $userId ): bool {
return false !== $this->db->update(
$this->table,
[
'status' => Invite::STATUS_ACCEPTED,
'accepted_user_id' => $userId,
'accepted_at' => current_time( 'mysql' ),
],
[ 'id' => $id ],
[ '%s', '%d', '%s' ],
[ '%d' ]
);
}
public function revoke( int $id ): bool {
return false !== $this->db->update(
$this->table,
[ 'status' => Invite::STATUS_REVOKED ],
[ 'id' => $id ],
[ '%s' ],
[ '%d' ]
);
}
}