Files
opensourcepos/app/Controllers/Login.php
Travis Garrison 16ad193af2 fix(login): skip auth validation on new install to allow migration
On fresh installs with no DB version, login validation would fail
before migrations could run. Check MY_Migration::getCurrentVersion()
and bypass credential check when no version exists.

Signed-off-by: Travis Garrison <travis@chiraqbookstore.com>
2026-07-30 15:02:57 +04:00

120 lines
3.6 KiB
PHP

<?php
namespace App\Controllers;
use App\Libraries\MY_Migration;
use App\Models\Employee;
use CodeIgniter\HTTP\RedirectResponse;
use CodeIgniter\HTTP\ResponseInterface;
use CodeIgniter\Model;
use Config\OSPOS;
use Config\Services;
/**
* @property employee employee
*/
class Login extends BaseController
{
public Model $employee;
/**
* @return RedirectResponse|string
*/
public function index(): string|RedirectResponse
{
$this->employee = model(Employee::class);
if (!$this->employee->is_logged_in()) {
$migration = new MY_Migration(config('Migrations'));
$config = config(OSPOS::class)->settings;
$gcaptchaEnabled = array_key_exists('gcaptcha_enable', $config)
? $config['gcaptcha_enable']
: false;
$migration->migrateToCI4();
$validation = Services::validation();
$data = [
'hasErrors' => false,
'isNewInstall' => !(MY_Migration::getCurrentVersion()),
'isLatest' => $migration->isLatest(),
'latestVersion' => $migration->getLatestMigration(),
'gcaptchaEnabled' => $gcaptchaEnabled,
'config' => $config,
'validation' => $validation
];
if ($this->request->getMethod() !== 'POST') {
return view('login', $data);
}
if (!$data['isLatest'] || $data['isNewInstall']) {
set_time_limit(3600);
$migration->setNamespace('App')->latest();
return redirect()->to('login');
}
$rules = ['username' => 'required|login_check[data]'];
$messages = [
'username' => [
'required' => lang('Login.required_username'),
'login_check' => lang('Login.invalid_username_and_password'),
]
];
if (!$this->validate($rules, $messages)) {
$data['has_errors'] = !empty($validation->getErrors());
return view('login', $data);
}
}
return redirect()->to('home');
}
public function migrate(): ResponseInterface
{
$isNewInstall = !MY_Migration::getCurrentVersion();
if (!$isNewInstall) {
$rules = ['username' => 'required|login_check[data]'];
$messages = [
'username' => [
'required' => lang('Login.required_username'),
'login_check' => lang('Login.invalid_username_and_password'),
]
];
if (!$this->validate($rules, $messages)) {
return $this->response->setJSON([
'success' => false,
'message' => lang('Login.invalid_username_and_password')
])->setStatusCode(401);
}
}
try {
$migration = new MY_Migration(config('Migrations'));
$migration->migrateToCI4();
set_time_limit(3600);
$migration->setNamespace('App')->latest();
return $this->response->setJSON([
'success' => true,
'message' => 'Migration completed successfully'
]);
} catch (\Exception $e) {
log_message('error', 'Migration failed: ' . $e->getMessage());
return $this->response->setJSON([
'success' => false,
'message' => 'Migration failed: ' . $e->getMessage()
])->setStatusCode(500);
}
}
}