(array $input)
| 19 | class UserCreateController implements ControllerInterface |
| 20 | { |
| 21 | public function __invoke(array $input): ResponseInterface |
| 22 | { |
| 23 | // Check if authentication is enabled (any user exists) |
| 24 | $repository = ServiceContainer::get(Repository::class); |
| 25 | $auth_enabled = $repository->userTableNotEmpty(); |
| 26 | |
| 27 | // If auth is enabled already and user exists, raise an error |
| 28 | if ($auth_enabled) { |
| 29 | return data([ |
| 30 | 'success' => false, |
| 31 | 'message' => 'User has been created already.' |
| 32 | ], 400); |
| 33 | } |
| 34 | |
| 35 | $username = trim($input['username'] ?? ''); |
| 36 | validateUsername($username); |
| 37 | |
| 38 | $password = $input['password'] ?? ''; |
| 39 | $confirm_password = $input['confirm_password'] ?? ''; |
| 40 | validatePassword($password); |
| 41 | validatePasswordConfirmation($password, $confirm_password); |
| 42 | |
| 43 | $password_hash = password_hash($password, Config::getPasswordAlgo()); |
| 44 | $user_id = $repository->createUser($username, $password_hash); |
| 45 | |
| 46 | if (!$user_id) { |
| 47 | throw new DataWriteException('Failed to create user.'); |
| 48 | } |
| 49 | |
| 50 | loginUser($user_id); |
| 51 | |
| 52 | $user = $repository->getUser($user_id); |
| 53 | |
| 54 | return success('User created successfully.', [ |
| 55 | 'user' => buildPublicUserObject($user) |
| 56 | ], 201); |
| 57 | } |
| 58 | } |
nothing calls this directly
no test coverage detected