UserService

Table of contents

  1. Overview
  2. Public Methods
  3. Related Components
  4. Notes


1. Overview Table of Contents

The UserService class provides high-level user management operations such as account deactivation, password updates, profile image handling, and access restrictions. It is designed to support both user self-management and admin-level user administration.

Setup

use Core\Services\UserService;

Common Use Cases

  • Safely delete users (excluding admins)
  • Manage and sort profile images
  • Update and validate user passwords
  • Handle account deactivation and reset flags
  • Send user-related emails (e.g., password reset, deactivation)


2. ⚙️ Public Methods Table of Contents

deleteIfAllowed(int $id, bool $unlink = false): void

Deletes a user if they are not an admin. Optionally removes their profile images if $unlink is true.

UserService::deleteIfAllowed(5, true);


deleteProfileImage(Input $request): array

Deletes a profile image based on an ID passed via request. Returns a JSON-compatible response array.

$response = UserService::deleteProfileImage($request);


ensureAuthenticatedUser(Users $user): void

Ensures that the user being modified matches the currently logged-in user. If not, redirects with an error message.

UserService::ensureAuthenticatedUser($user);


handleProfileImages(Users $user, ?Uploads $uploads, ?string $sortedImages): void

Handles profile image uploading and image order sorting.

UserService::handleProfileImages($user, $uploads, $sortedJson);


updatePassword(Users $user, Input $request): bool

Updates the user’s password if the current password is correct and the new password passes validation.

$success = UserService::updatePassword($user, $request);


sendWhenSetToInactive(Users $user, bool $shouldSendEmail = false): void

Sends an account deactivation email if $shouldSendEmail is true.

UserService::sendWhenSetToInactive($user, true);


sendWhenSetToResetPW(Users $user, bool $shouldSendEmail = false): void

Sends a password reset email if $shouldSendEmail is true.

UserService::sendWhenSetToResetPW($user, true);


toggleAccountStatus(Users $user, Input $request, ?int $currentInactive = null): bool

Toggles the inactive status based on request input. Returns true if the account was just deactivated.

$shouldEmail = UserService::toggleAccountStatus($user, $request, $previousInactive);


toggleResetPassword(Users $user, Input $request, ?int $currentReset = null): bool

Toggles the reset_password flag based on request input. Returns true if it was just activated.

$shouldEmail = UserService::toggleResetPassword($user, $request, $previousReset);


  • AuthService – Used to validate current user identity and confirm password fields.
  • ProfileImages – Handles image persistence, deletion, and sorting.
  • Uploads – File upload handler.
  • AccountDeactivatedMailer / PasswordResetMailer – Responsible for user notification emails.
  • Users – Model representing application users.


4. 🧠 Notes Table of Contents

  • Admin users (["Admin"] ACL) are protected from deletion.
  • Upload handling assumes that $_FILES['profileImage'] is present for Uploads.
  • Email methods like sendWhenSetToInactive() and sendWhenSetToResetPW() rely on AccountDeactivatedMailer and PasswordResetMailer respectively.
  • toggleAccountStatus() and toggleResetPassword() help controllers determine if emails should be triggered post-form submission.