Add account registration with signup policy acceptance
CI / Tests (PHP 8.1) (pull_request) Successful in 47s
CI / No Debug Code (pull_request) Successful in 2s
CI / Build Plugin Zip (pull_request) Has been skipped
CI / Coding Standards (pull_request) Successful in 52s
CI / PHPStan (pull_request) Successful in 1m1s
CI / Tests (PHP 8.2) (pull_request) Successful in 48s
CI / Tests (PHP 8.3) (pull_request) Successful in 45s
CI / Tests (PHP 8.1) (pull_request) Successful in 47s
CI / No Debug Code (pull_request) Successful in 2s
CI / Build Plugin Zip (pull_request) Has been skipped
CI / Coding Standards (pull_request) Successful in 52s
CI / PHPStan (pull_request) Successful in 1m1s
CI / Tests (PHP 8.2) (pull_request) Successful in 48s
CI / Tests (PHP 8.3) (pull_request) Successful in 45s
Implements #16: invite-only student self-registration through a front-end page, accepting signup-scoped policies at account creation. Policy domain: - us_policies.acceptance_scope (signup/booking/both); Policy::appliesTo(); PolicyRepository::findForScope(); scope threaded through PolicyService, the REST create, the admin controller, and the Policies form. - PolicyAcceptance::REG_ACCOUNT (registration_id = the new user's ID). Auth: - Invite value object + InviteRepository; us_invites table. - RegistrationController + Invites admin page (manage_students): invite an email, share the registration link, revoke. - RegistrationPage ([us_student_register] shortcode): validates the invite token, collects name/password, renders signup-scoped published policies with required acceptance, creates the us_student user, records account-type acceptances, marks the invite accepted, and logs the user in. - RoleManager: manage_students cap added to STUDIO_ADMIN_CAPS. Invite-only is implemented; the us_registration_mode self_approval path is a documented future seam. Docs: docs/features/account-registration.md; policies.md updated. Tests: tests/Unit/Auth/ (Invite, InviteRepository) plus Policy scope updates. composer test (104), cs, and PHPStan level 6 all pass. Refs #16 Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
+16
-1
@@ -5,6 +5,8 @@ namespace Unsupervised\Schedular;
|
||||
|
||||
use Unsupervised\Schedular\Availability\AvailabilityController;
|
||||
use Unsupervised\Schedular\Availability\AvailabilityRepository;
|
||||
use Unsupervised\Schedular\Auth\InviteRepository;
|
||||
use Unsupervised\Schedular\Auth\RegistrationController;
|
||||
use Unsupervised\Schedular\Auth\RoleManager;
|
||||
use Unsupervised\Schedular\Booking\BookingRepository;
|
||||
use Unsupervised\Schedular\Booking\LessonController;
|
||||
@@ -24,13 +26,15 @@ class AdminMenu {
|
||||
private OfferingController $offeringController;
|
||||
private QuestionController $questionController;
|
||||
private PolicyController $policyController;
|
||||
private RegistrationController $registrationController;
|
||||
|
||||
public function __construct( AvailabilityRepository $availability, BookingRepository $bookings, OfferingRepository $offerings, QuestionRepository $questions, PolicyRepository $policies, PolicyVersionRepository $policyVersions, PolicyService $policyService ) {
|
||||
public function __construct( AvailabilityRepository $availability, BookingRepository $bookings, OfferingRepository $offerings, QuestionRepository $questions, PolicyRepository $policies, PolicyVersionRepository $policyVersions, PolicyService $policyService, InviteRepository $invites ) {
|
||||
$this->availabilityController = new AvailabilityController( $availability, $offerings );
|
||||
$this->lessonController = new LessonController( $bookings );
|
||||
$this->offeringController = new OfferingController( $offerings );
|
||||
$this->questionController = new QuestionController( $questions, $offerings );
|
||||
$this->policyController = new PolicyController( $policies, $policyVersions, $policyService );
|
||||
$this->registrationController = new RegistrationController( $invites );
|
||||
}
|
||||
|
||||
public function register(): void {
|
||||
@@ -92,6 +96,17 @@ class AdminMenu {
|
||||
34
|
||||
);
|
||||
|
||||
// Studio admin: invite students to register.
|
||||
add_menu_page(
|
||||
__( 'Invites', 'unsupervised-schedular' ),
|
||||
__( 'Invites', 'unsupervised-schedular' ),
|
||||
RoleManager::CAP_MANAGE_STUDENTS,
|
||||
'us-invites',
|
||||
[ $this->registrationController, 'renderPage' ],
|
||||
'dashicons-email',
|
||||
35
|
||||
);
|
||||
|
||||
// Instructor: view their upcoming lessons.
|
||||
add_menu_page(
|
||||
__( 'My Lessons', 'unsupervised-schedular' ),
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Unsupervised\Schedular\Auth;
|
||||
|
||||
class Invite {
|
||||
|
||||
public const STATUS_PENDING = 'pending';
|
||||
public const STATUS_ACCEPTED = 'accepted';
|
||||
public const STATUS_REVOKED = 'revoked';
|
||||
|
||||
/**
|
||||
* All valid invite statuses.
|
||||
*
|
||||
* @var list<string>
|
||||
*/
|
||||
public const VALID_STATUSES = [ self::STATUS_PENDING, self::STATUS_ACCEPTED, self::STATUS_REVOKED ];
|
||||
|
||||
public function __construct(
|
||||
public readonly string $email,
|
||||
public readonly string $token,
|
||||
public readonly string $role = RoleManager::STUDENT,
|
||||
public readonly string $status = self::STATUS_PENDING,
|
||||
public readonly ?int $invitedBy = null,
|
||||
public readonly ?int $acceptedUserId = null,
|
||||
public readonly ?string $acceptedAt = null,
|
||||
public readonly ?int $id = null,
|
||||
) {}
|
||||
|
||||
public static function fromRow( object $row ): self {
|
||||
return new self(
|
||||
email: $row->email,
|
||||
token: $row->token,
|
||||
role: $row->role,
|
||||
status: $row->status,
|
||||
invitedBy: null !== $row->invited_by ? (int) $row->invited_by : null,
|
||||
acceptedUserId: null !== $row->accepted_user_id ? (int) $row->accepted_user_id : null,
|
||||
acceptedAt: $row->accepted_at,
|
||||
id: (int) $row->id,
|
||||
);
|
||||
}
|
||||
|
||||
public function isPending(): bool {
|
||||
return self::STATUS_PENDING === $this->status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a plain array representation of the invite.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function toArray(): array {
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'email' => $this->email,
|
||||
'token' => $this->token,
|
||||
'role' => $this->role,
|
||||
'status' => $this->status,
|
||||
'invited_by' => $this->invitedBy,
|
||||
'accepted_user_id' => $this->acceptedUserId,
|
||||
'accepted_at' => $this->acceptedAt,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
<?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,
|
||||
'status' => $invite->status,
|
||||
'invited_by' => $invite->invitedBy,
|
||||
'accepted_user_id' => $invite->acceptedUserId,
|
||||
'created_at' => current_time( 'mysql' ),
|
||||
'accepted_at' => $invite->acceptedAt,
|
||||
],
|
||||
[ '%s', '%s', '%s', '%s', '%d', '%d', '%s', '%s' ]
|
||||
);
|
||||
|
||||
return $this->db->insert_id;
|
||||
}
|
||||
|
||||
public function findByToken( string $token ): ?Invite {
|
||||
$row = $this->db->get_row(
|
||||
$this->db->prepare( "SELECT * FROM {$this->table} WHERE token = %s", $token )
|
||||
);
|
||||
|
||||
return $row ? Invite::fromRow( $row ) : null;
|
||||
}
|
||||
|
||||
public function findById( int $id ): ?Invite {
|
||||
$row = $this->db->get_row(
|
||||
$this->db->prepare( "SELECT * FROM {$this->table} WHERE id = %d", $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 {$this->table} WHERE email = %s AND status = %s ORDER BY id DESC LIMIT 1",
|
||||
$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 {$this->table} WHERE status = %s ORDER BY created_at DESC",
|
||||
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' ]
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Unsupervised\Schedular\Auth;
|
||||
|
||||
class RegistrationController {
|
||||
|
||||
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' ) );
|
||||
}
|
||||
|
||||
if ( isset( $_POST['usc_action'] ) && check_admin_referer( 'usc_invite_action' ) ) {
|
||||
$this->handleFormAction();
|
||||
}
|
||||
|
||||
$pendingInvites = $this->invites->findPending();
|
||||
|
||||
include USC_PLUGIN_DIR . 'templates/admin/invites.php';
|
||||
}
|
||||
|
||||
private function handleFormAction(): void {
|
||||
// Nonce is verified by the caller (renderPage) before this method runs.
|
||||
// phpcs:disable WordPress.Security.NonceVerification.Missing
|
||||
$action = sanitize_key( wp_unslash( $_POST['usc_action'] ?? '' ) );
|
||||
|
||||
if ( 'invite' === $action ) {
|
||||
$email = sanitize_email( wp_unslash( $_POST['email'] ?? '' ) );
|
||||
|
||||
if (
|
||||
is_email( $email )
|
||||
&& false === email_exists( $email )
|
||||
&& null === $this->invites->findPendingByEmail( $email )
|
||||
) {
|
||||
$this->invites->insert(
|
||||
new Invite(
|
||||
email: $email,
|
||||
token: wp_generate_password( 32, false ),
|
||||
invitedBy: get_current_user_id(),
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if ( 'revoke' === $action ) {
|
||||
$inviteId = absint( $_POST['invite_id'] ?? 0 );
|
||||
if ( $inviteId > 0 ) {
|
||||
$this->invites->revoke( $inviteId );
|
||||
}
|
||||
}
|
||||
// phpcs:enable WordPress.Security.NonceVerification.Missing
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,159 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Unsupervised\Schedular\Auth;
|
||||
|
||||
use Unsupervised\Schedular\Policy\AcceptanceRepository;
|
||||
use Unsupervised\Schedular\Policy\Policy;
|
||||
use Unsupervised\Schedular\Policy\PolicyAcceptance;
|
||||
use Unsupervised\Schedular\Policy\PolicyRepository;
|
||||
use Unsupervised\Schedular\Policy\PolicyVersionRepository;
|
||||
|
||||
class RegistrationPage {
|
||||
|
||||
public function __construct(
|
||||
private InviteRepository $invites,
|
||||
private PolicyRepository $policies,
|
||||
private PolicyVersionRepository $versions,
|
||||
private AcceptanceRepository $acceptances,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Renders the student registration shortcode output.
|
||||
*
|
||||
* @param array<string, string> $atts Shortcode attributes (unused — reserved for future options).
|
||||
*/
|
||||
public function render( array $atts ): string { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.Found
|
||||
if ( is_user_logged_in() ) {
|
||||
return '<p>' . esc_html__( 'You already have an account and are logged in.', 'unsupervised-schedular' ) . '</p>';
|
||||
}
|
||||
|
||||
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- token identifies the invite; the form submit is nonce-checked below.
|
||||
$token = sanitize_text_field( wp_unslash( $_REQUEST['us_invite'] ?? '' ) );
|
||||
$invite = '' !== $token ? $this->invites->findByToken( $token ) : null;
|
||||
|
||||
$error = '';
|
||||
$success = false;
|
||||
|
||||
if ( isset( $_POST['us_register'] ) && check_admin_referer( 'us_student_register' ) ) {
|
||||
$result = $this->handleSubmit( $invite );
|
||||
if ( true === $result ) {
|
||||
$success = true;
|
||||
} else {
|
||||
$error = $result;
|
||||
}
|
||||
}
|
||||
|
||||
$policyForms = $this->signupPolicies();
|
||||
$canRegister = null !== $invite && $invite->isPending();
|
||||
|
||||
ob_start();
|
||||
include USC_PLUGIN_DIR . 'templates/frontend/register-page.php';
|
||||
return (string) ob_get_clean();
|
||||
}
|
||||
|
||||
/**
|
||||
* Process the submitted registration. Returns true on success or an error
|
||||
* message string on failure.
|
||||
*/
|
||||
private function handleSubmit( ?Invite $invite ): string|bool {
|
||||
if ( null === $invite || ! $invite->isPending() ) {
|
||||
return esc_html__( 'This invitation is invalid or has already been used.', 'unsupervised-schedular' );
|
||||
}
|
||||
|
||||
// The submit nonce is verified by the caller (render) before this runs.
|
||||
// phpcs:disable WordPress.Security.NonceVerification.Missing
|
||||
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- passwords must not be sanitized.
|
||||
$password = (string) wp_unslash( $_POST['password'] ?? '' );
|
||||
$displayName = sanitize_text_field( wp_unslash( $_POST['display_name'] ?? '' ) );
|
||||
|
||||
if ( strlen( $password ) < 8 ) {
|
||||
return esc_html__( 'Please choose a password of at least 8 characters.', 'unsupervised-schedular' );
|
||||
}
|
||||
|
||||
$policyForms = $this->signupPolicies();
|
||||
$accepted = array_map( 'absint', (array) ( $_POST['accept'] ?? [] ) );
|
||||
// phpcs:enable WordPress.Security.NonceVerification.Missing
|
||||
|
||||
foreach ( $policyForms as $form ) {
|
||||
if ( ! in_array( (int) $form['version']->id, $accepted, true ) ) {
|
||||
return esc_html__( 'You must accept all required policies to register.', 'unsupervised-schedular' );
|
||||
}
|
||||
}
|
||||
|
||||
if ( email_exists( $invite->email ) ) {
|
||||
return esc_html__( 'An account already exists for this email.', 'unsupervised-schedular' );
|
||||
}
|
||||
|
||||
$userId = wp_insert_user(
|
||||
[
|
||||
'user_login' => $invite->email,
|
||||
'user_email' => $invite->email,
|
||||
'user_pass' => $password,
|
||||
'display_name' => '' !== $displayName ? $displayName : $invite->email,
|
||||
'role' => $invite->role,
|
||||
]
|
||||
);
|
||||
|
||||
if ( is_wp_error( $userId ) ) {
|
||||
return esc_html__( 'Could not create the account. Please contact the studio.', 'unsupervised-schedular' );
|
||||
}
|
||||
|
||||
$this->recordAcceptances( $policyForms, (int) $userId );
|
||||
$this->invites->markAccepted( (int) $invite->id, (int) $userId );
|
||||
|
||||
wp_set_current_user( (int) $userId );
|
||||
wp_set_auth_cookie( (int) $userId );
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Record account-time acceptances for each signup policy version.
|
||||
*
|
||||
* @param list<array{policy: Policy, version: \Unsupervised\Schedular\Policy\PolicyVersion}> $policyForms
|
||||
*/
|
||||
private function recordAcceptances( array $policyForms, int $userId ): void {
|
||||
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- IP is stored verbatim for audit.
|
||||
$ip = sanitize_text_field( wp_unslash( $_SERVER['REMOTE_ADDR'] ?? '' ) );
|
||||
|
||||
foreach ( $policyForms as $form ) {
|
||||
$this->acceptances->insert(
|
||||
new PolicyAcceptance(
|
||||
policyVersionId: (int) $form['version']->id,
|
||||
studentId: $userId,
|
||||
registrationType: PolicyAcceptance::REG_ACCOUNT,
|
||||
registrationId: $userId,
|
||||
ipAddress: '' !== $ip ? $ip : null,
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Signup-scoped policies that have a current published version.
|
||||
*
|
||||
* @return list<array{policy: Policy, version: \Unsupervised\Schedular\Policy\PolicyVersion}>
|
||||
*/
|
||||
private function signupPolicies(): array {
|
||||
$out = [];
|
||||
|
||||
foreach ( $this->policies->findForScope( Policy::SCOPE_SIGNUP ) as $policy ) {
|
||||
if ( null === $policy->currentVersionId ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$version = $this->versions->findById( $policy->currentVersionId );
|
||||
if ( null === $version || ! $version->isPublished() ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$out[] = [
|
||||
'policy' => $policy,
|
||||
'version' => $version,
|
||||
];
|
||||
}
|
||||
|
||||
return $out;
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@ class RoleManager {
|
||||
public const CAP_BOOK_LESSON = 'book_lesson';
|
||||
|
||||
public const CAP_MANAGE_INSTRUCTORS = 'manage_instructors';
|
||||
public const CAP_MANAGE_STUDENTS = 'manage_students';
|
||||
public const CAP_MANAGE_OFFERINGS = 'manage_offerings';
|
||||
public const CAP_MANAGE_QUESTIONS = 'manage_questions';
|
||||
public const CAP_MANAGE_POLICIES = 'manage_policies';
|
||||
@@ -31,6 +32,7 @@ class RoleManager {
|
||||
*/
|
||||
public const STUDIO_ADMIN_CAPS = [
|
||||
self::CAP_MANAGE_INSTRUCTORS,
|
||||
self::CAP_MANAGE_STUDENTS,
|
||||
self::CAP_MANAGE_OFFERINGS,
|
||||
self::CAP_MANAGE_QUESTIONS,
|
||||
self::CAP_MANAGE_POLICIES,
|
||||
|
||||
+6
-2
@@ -3,10 +3,12 @@ declare(strict_types=1);
|
||||
|
||||
namespace Unsupervised\Schedular;
|
||||
|
||||
use Unsupervised\Schedular\Auth\InviteRepository;
|
||||
use Unsupervised\Schedular\Auth\RoleManager;
|
||||
use Unsupervised\Schedular\Availability\AvailabilityRepository;
|
||||
use Unsupervised\Schedular\Booking\BookingRepository;
|
||||
use Unsupervised\Schedular\Offering\OfferingRepository;
|
||||
use Unsupervised\Schedular\Policy\AcceptanceRepository;
|
||||
use Unsupervised\Schedular\Policy\PolicyRepository;
|
||||
use Unsupervised\Schedular\Policy\PolicyService;
|
||||
use Unsupervised\Schedular\Policy\PolicyVersionRepository;
|
||||
@@ -25,10 +27,12 @@ class Plugin {
|
||||
$policies = new PolicyRepository( $wpdb );
|
||||
$policyVersions = new PolicyVersionRepository( $wpdb );
|
||||
$policyService = new PolicyService( $policies, $policyVersions );
|
||||
$acceptances = new AcceptanceRepository( $wpdb );
|
||||
$invites = new InviteRepository( $wpdb );
|
||||
|
||||
( new RoleManager() )->register();
|
||||
( new AdminMenu( $availability, $bookings, $offerings, $questions, $policies, $policyVersions, $policyService ) )->register();
|
||||
( new AdminMenu( $availability, $bookings, $offerings, $questions, $policies, $policyVersions, $policyService, $invites ) )->register();
|
||||
( new RestRegistrar( $availability, $bookings, $offerings, $questions, $policies, $policyVersions, $policyService ) )->register();
|
||||
( new ShortcodeRegistrar() )->register();
|
||||
( new ShortcodeRegistrar( $invites, $policies, $policyVersions, $acceptances ) )->register();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,10 +5,22 @@ namespace Unsupervised\Schedular\Policy;
|
||||
|
||||
class Policy {
|
||||
|
||||
public const SCOPE_SIGNUP = 'signup';
|
||||
public const SCOPE_BOOKING = 'booking';
|
||||
public const SCOPE_BOTH = 'both';
|
||||
|
||||
/**
|
||||
* All valid acceptance scopes — when a policy must be accepted.
|
||||
*
|
||||
* @var list<string>
|
||||
*/
|
||||
public const VALID_SCOPES = [ self::SCOPE_SIGNUP, self::SCOPE_BOOKING, self::SCOPE_BOTH ];
|
||||
|
||||
public function __construct(
|
||||
public readonly string $title,
|
||||
public readonly string $slug,
|
||||
public readonly ?int $currentVersionId = null,
|
||||
public readonly string $acceptanceScope = self::SCOPE_BOOKING,
|
||||
public readonly ?int $id = null,
|
||||
) {}
|
||||
|
||||
@@ -17,10 +29,18 @@ class Policy {
|
||||
title: $row->title,
|
||||
slug: $row->slug,
|
||||
currentVersionId: null !== $row->current_version_id ? (int) $row->current_version_id : null,
|
||||
acceptanceScope: $row->acceptance_scope,
|
||||
id: (int) $row->id,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this policy must be accepted in the given gate (`signup` or `booking`).
|
||||
*/
|
||||
public function appliesTo( string $scope ): bool {
|
||||
return $this->acceptanceScope === $scope || self::SCOPE_BOTH === $this->acceptanceScope;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a plain array representation of the policy.
|
||||
*
|
||||
@@ -32,6 +52,7 @@ class Policy {
|
||||
'title' => $this->title,
|
||||
'slug' => $this->slug,
|
||||
'current_version_id' => $this->currentVersionId,
|
||||
'acceptance_scope' => $this->acceptanceScope,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,15 +5,17 @@ namespace Unsupervised\Schedular\Policy;
|
||||
|
||||
class PolicyAcceptance {
|
||||
|
||||
public const REG_ACCOUNT = 'account';
|
||||
public const REG_LESSON = 'lesson';
|
||||
public const REG_ENROLLMENT = 'enrollment';
|
||||
|
||||
/**
|
||||
* Polymorphic registration targets an acceptance can attach to.
|
||||
* Polymorphic registration targets an acceptance can attach to. For `account`
|
||||
* the registration id is the WordPress user ID.
|
||||
*
|
||||
* @var list<string>
|
||||
*/
|
||||
public const VALID_REGISTRATION_TYPES = [ self::REG_LESSON, self::REG_ENROLLMENT ];
|
||||
public const VALID_REGISTRATION_TYPES = [ self::REG_ACCOUNT, self::REG_LESSON, self::REG_ENROLLMENT ];
|
||||
|
||||
public function __construct(
|
||||
public readonly int $policyVersionId,
|
||||
|
||||
@@ -40,9 +40,14 @@ class PolicyController {
|
||||
$title = sanitize_text_field( wp_unslash( $_POST['title'] ?? '' ) );
|
||||
$slugRaw = sanitize_text_field( wp_unslash( $_POST['slug'] ?? '' ) );
|
||||
$slug = sanitize_title( '' !== $slugRaw ? $slugRaw : $title );
|
||||
$scope = sanitize_key( wp_unslash( $_POST['acceptance_scope'] ?? Policy::SCOPE_BOOKING ) );
|
||||
|
||||
if ( ! in_array( $scope, Policy::VALID_SCOPES, true ) ) {
|
||||
$scope = Policy::SCOPE_BOOKING;
|
||||
}
|
||||
|
||||
if ( '' !== $title && '' !== $slug && null === $this->policies->findBySlug( $slug ) ) {
|
||||
$this->service->createPolicy( $title, $slug );
|
||||
$this->service->createPolicy( $title, $slug, $scope );
|
||||
}
|
||||
|
||||
return;
|
||||
|
||||
@@ -113,7 +113,12 @@ class PolicyEndpoint {
|
||||
return new \WP_Error( 'duplicate_slug', __( 'A policy with that slug already exists.', 'unsupervised-schedular' ), [ 'status' => 409 ] );
|
||||
}
|
||||
|
||||
$id = $this->service->createPolicy( $title, $slug );
|
||||
$scope = (string) ( $request->get_param( 'acceptance_scope' ) ?? Policy::SCOPE_BOOKING );
|
||||
if ( ! in_array( $scope, Policy::VALID_SCOPES, true ) ) {
|
||||
return $this->invalid( __( 'Invalid acceptance scope.', 'unsupervised-schedular' ) );
|
||||
}
|
||||
|
||||
$id = $this->service->createPolicy( $title, $slug, $scope );
|
||||
|
||||
return new \WP_REST_Response( [ 'id' => $id ], 201 );
|
||||
}
|
||||
|
||||
@@ -18,14 +18,33 @@ class PolicyRepository {
|
||||
'title' => $policy->title,
|
||||
'slug' => $policy->slug,
|
||||
'current_version_id' => $policy->currentVersionId,
|
||||
'acceptance_scope' => $policy->acceptanceScope,
|
||||
'created_at' => current_time( 'mysql' ),
|
||||
],
|
||||
[ '%s', '%s', '%d', '%s' ]
|
||||
[ '%s', '%s', '%d', '%s', '%s' ]
|
||||
);
|
||||
|
||||
return $this->db->insert_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Policies that must be accepted in the given gate — those scoped to it or to
|
||||
* `both`. Pass `Policy::SCOPE_SIGNUP` or `Policy::SCOPE_BOOKING`.
|
||||
*
|
||||
* @return list<Policy>
|
||||
*/
|
||||
public function findForScope( string $scope ): array {
|
||||
$rows = $this->db->get_results(
|
||||
$this->db->prepare(
|
||||
"SELECT * FROM {$this->table} WHERE acceptance_scope = %s OR acceptance_scope = %s ORDER BY title ASC",
|
||||
$scope,
|
||||
Policy::SCOPE_BOTH
|
||||
)
|
||||
);
|
||||
|
||||
return array_map( Policy::fromRow( ... ), $rows ?? [] );
|
||||
}
|
||||
|
||||
public function updateCurrentVersion( int $policyId, int $versionId ): bool {
|
||||
return false !== $this->db->update(
|
||||
$this->table,
|
||||
|
||||
@@ -13,8 +13,8 @@ class PolicyService {
|
||||
private PolicyVersionRepository $versions,
|
||||
) {}
|
||||
|
||||
public function createPolicy( string $title, string $slug ): int {
|
||||
return $this->policies->insert( new Policy( $title, $slug ) );
|
||||
public function createPolicy( string $title, string $slug, string $scope = Policy::SCOPE_BOOKING ): int {
|
||||
return $this->policies->insert( new Policy( $title, $slug, acceptanceScope: $scope ) );
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+19
-1
@@ -100,9 +100,11 @@ class Schema {
|
||||
title VARCHAR(191) NOT NULL,
|
||||
slug VARCHAR(191) NOT NULL,
|
||||
current_version_id BIGINT UNSIGNED DEFAULT NULL,
|
||||
acceptance_scope VARCHAR(20) NOT NULL DEFAULT 'booking',
|
||||
created_at DATETIME NOT NULL,
|
||||
PRIMARY KEY (id),
|
||||
UNIQUE KEY slug (slug)
|
||||
UNIQUE KEY slug (slug),
|
||||
KEY acceptance_scope (acceptance_scope)
|
||||
) {$charset};",
|
||||
|
||||
"CREATE TABLE {$prefix}us_policy_versions (
|
||||
@@ -131,6 +133,22 @@ class Schema {
|
||||
KEY student_id (student_id),
|
||||
KEY registration (registration_type, registration_id)
|
||||
) {$charset};",
|
||||
|
||||
"CREATE TABLE {$prefix}us_invites (
|
||||
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||
email VARCHAR(191) NOT NULL,
|
||||
token VARCHAR(64) NOT NULL,
|
||||
role VARCHAR(32) NOT NULL DEFAULT 'us_student',
|
||||
status VARCHAR(20) NOT NULL DEFAULT 'pending',
|
||||
invited_by BIGINT UNSIGNED DEFAULT NULL,
|
||||
accepted_user_id BIGINT UNSIGNED DEFAULT NULL,
|
||||
created_at DATETIME NOT NULL,
|
||||
accepted_at DATETIME DEFAULT NULL,
|
||||
PRIMARY KEY (id),
|
||||
UNIQUE KEY token (token),
|
||||
KEY email (email),
|
||||
KEY status (status)
|
||||
) {$charset};",
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,22 +3,35 @@ declare(strict_types=1);
|
||||
|
||||
namespace Unsupervised\Schedular;
|
||||
|
||||
use Unsupervised\Schedular\Auth\InviteRepository;
|
||||
use Unsupervised\Schedular\Auth\LoginPage;
|
||||
use Unsupervised\Schedular\Auth\RegistrationPage;
|
||||
use Unsupervised\Schedular\Booking\BookingPage;
|
||||
use Unsupervised\Schedular\Policy\AcceptanceRepository;
|
||||
use Unsupervised\Schedular\Policy\PolicyRepository;
|
||||
use Unsupervised\Schedular\Policy\PolicyVersionRepository;
|
||||
|
||||
class ShortcodeRegistrar {
|
||||
|
||||
private BookingPage $bookingPage;
|
||||
private LoginPage $loginPage;
|
||||
private RegistrationPage $registrationPage;
|
||||
|
||||
public function __construct() {
|
||||
$this->bookingPage = new BookingPage();
|
||||
$this->loginPage = new LoginPage();
|
||||
public function __construct(
|
||||
InviteRepository $invites,
|
||||
PolicyRepository $policies,
|
||||
PolicyVersionRepository $policyVersions,
|
||||
AcceptanceRepository $acceptances,
|
||||
) {
|
||||
$this->bookingPage = new BookingPage();
|
||||
$this->loginPage = new LoginPage();
|
||||
$this->registrationPage = new RegistrationPage( $invites, $policies, $policyVersions, $acceptances );
|
||||
}
|
||||
|
||||
public function register(): void {
|
||||
add_shortcode( 'us_booking', [ $this->bookingPage, 'render' ] );
|
||||
add_shortcode( 'us_student_login', [ $this->loginPage, 'render' ] );
|
||||
add_shortcode( 'us_student_register', [ $this->registrationPage, 'render' ] );
|
||||
add_action( 'wp_enqueue_scripts', [ $this, 'enqueueAssets' ] );
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user