Files
nnterp-react-admin/docs/PLAN-auto-create-users.md

2.7 KiB

Auto-Create User Account Feature Plan

Context

The user requested a feature to automatically create portal login accounts when creating a Customer or Vendor, avoiding a manual two-step process. Following clarification:

  1. No welcome emails will be sent immediately.
  2. The initial password will be 123456, but the system will force the user to change their password upon their very first login.

Proposed Changes

Phase 1: Database & Middleware (Force Password Change)

  1. Migration: Create a migration to add a boolean column requires_password_change to the users table, defaulting to false.
  2. Middleware: Create ForcePasswordChangeMiddleware.php.
    • If Auth::check() and $request->user()->requires_password_change == true, intercept the request.
    • Redirect to a dedicated "Change Password" screen (unless they are already on the change password or logout routes).
  3. Routing & UI:
    • Register the middleware in bootstrap/app.php or Kernel.php to run globally for web routes.
    • Create a React page Auth/ForcePasswordChange.tsx with a form asking for "New Password" and "Confirm New Password".
    • Create a controller route to handle the password update and set requires_password_change to false.

Phase 2: Form Requests Validation

  • StoreCustomerRequest.php & StoreVendorRequest.php:
    • Add create_user_account (boolean) to the validation rules.
    • Validate that contact_person_email is required and unique in the users table if create_user_account is true.

Phase 3: Controller Logic Updates

  • CustomerController@store & VendorController@store:
    • If $request->create_user_account is true:
    • Create a new User record using the provided email and name.
    • Set the password to Hash::make('123456').
    • Set requires_password_change = true.
    • Assign the appropriate role (client or vendor).
    • Assign the new user_id to the Customer/Vendor record before saving.

Phase 4: Frontend UI Updates

  • Account/Customers/Create.tsx & Account/Vendors/Create.tsx:
    • Add a "Create Portal Account Automatically" Checkbox below the "User" dropdown.
    • When checked, disable the "User" dropdown since the system handles it.

Verification Checklist

  • Migration applied and users table has new column.
  • Middleware correctly intercepts users with requires_password_change.
  • "Force Password Change" UI successfully updates the password and removes the flag.
  • Creating a customer with the checkbox successfully spawns a client user.
  • Creating a vendor with the checkbox successfully spawns a vendor user.
  • Form validation prevents creating duplicate accounts with the same email.