mirror of
https://github.com/grocy/grocy.git
synced 2025-08-20 12:20:22 +00:00
Replace the single user (defined in /data/config.php) with a multi user management thing
This commit is contained in:
@@ -8,21 +8,38 @@ class DatabaseMigrationService extends BaseService
|
||||
{
|
||||
$this->DatabaseService->ExecuteDbStatement("CREATE TABLE IF NOT EXISTS migrations (migration INTEGER NOT NULL PRIMARY KEY UNIQUE, execution_time_timestamp DATETIME DEFAULT (datetime('now', 'localtime')))");
|
||||
|
||||
$migrationFiles = array();
|
||||
$sqlMigrationFiles = array();
|
||||
foreach (new \FilesystemIterator(__DIR__ . '/../migrations') as $file)
|
||||
{
|
||||
$migrationFiles[$file->getBasename('.sql')] = $file->getPathname();
|
||||
if ($file->getExtension() === 'sql')
|
||||
{
|
||||
$sqlMigrationFiles[$file->getBasename('.sql')] = $file->getPathname();
|
||||
}
|
||||
}
|
||||
ksort($migrationFiles);
|
||||
|
||||
foreach($migrationFiles as $migrationNumber => $migrationFile)
|
||||
ksort($sqlMigrationFiles);
|
||||
foreach($sqlMigrationFiles as $migrationNumber => $migrationFile)
|
||||
{
|
||||
$migrationNumber = ltrim($migrationNumber, '0');
|
||||
$this->ExecuteMigrationWhenNeeded($migrationNumber, file_get_contents($migrationFile));
|
||||
$this->ExecuteSqlMigrationWhenNeeded($migrationNumber, file_get_contents($migrationFile));
|
||||
}
|
||||
|
||||
$phpMigrationFiles = array();
|
||||
foreach (new \FilesystemIterator(__DIR__ . '/../migrations') as $file)
|
||||
{
|
||||
if ($file->getExtension() === 'php')
|
||||
{
|
||||
$phpMigrationFiles[$file->getBasename('.php')] = $file->getPathname();
|
||||
}
|
||||
}
|
||||
ksort($phpMigrationFiles);
|
||||
foreach($phpMigrationFiles as $migrationNumber => $migrationFile)
|
||||
{
|
||||
$migrationNumber = ltrim($migrationNumber, '0');
|
||||
$this->ExecutePhpMigrationWhenNeeded($migrationNumber, $migrationFile);
|
||||
}
|
||||
}
|
||||
|
||||
private function ExecuteMigrationWhenNeeded(int $migrationId, string $sql)
|
||||
private function ExecuteSqlMigrationWhenNeeded(int $migrationId, string $sql)
|
||||
{
|
||||
$rowCount = $this->DatabaseService->ExecuteDbQuery('SELECT COUNT(*) FROM migrations WHERE migration = ' . $migrationId)->fetchColumn();
|
||||
if (intval($rowCount) === 0)
|
||||
@@ -31,4 +48,14 @@ class DatabaseMigrationService extends BaseService
|
||||
$this->DatabaseService->ExecuteDbStatement('INSERT INTO migrations (migration) VALUES (' . $migrationId . ')');
|
||||
}
|
||||
}
|
||||
|
||||
private function ExecutePhpMigrationWhenNeeded(int $migrationId, string $phpFile)
|
||||
{
|
||||
$rowCount = $this->DatabaseService->ExecuteDbQuery('SELECT COUNT(*) FROM migrations WHERE migration = ' . $migrationId)->fetchColumn();
|
||||
if (intval($rowCount) === 0)
|
||||
{
|
||||
include $phpFile;
|
||||
$this->DatabaseService->ExecuteDbStatement('INSERT INTO migrations (migration) VALUES (' . $migrationId . ')');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@@ -16,6 +16,8 @@ class DemoDataGeneratorService extends BaseService
|
||||
$loremIpsum = 'Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.';
|
||||
|
||||
$sql = "
|
||||
INSERT INTO users (id, username, password) VALUES (-1, '{$localizationService->Localize('Demo User')}', 'x');
|
||||
|
||||
INSERT INTO locations (name) VALUES ('{$localizationService->Localize('Pantry')}'); --2
|
||||
INSERT INTO locations (name) VALUES ('{$localizationService->Localize('Candy cupboard')}'); --3
|
||||
INSERT INTO locations (name) VALUES ('{$localizationService->Localize('Tinned food cupboard')}'); --4
|
||||
|
@@ -33,11 +33,12 @@ class SessionService extends BaseService
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function CreateSession()
|
||||
public function CreateSession($userId)
|
||||
{
|
||||
$newSessionKey = $this->GenerateSessionKey();
|
||||
|
||||
$sessionRow = $this->Database->sessions()->createRow(array(
|
||||
'user_id' => $userId,
|
||||
'session_key' => $newSessionKey,
|
||||
'expires' => date('Y-m-d H:i:s', time() + 2592000) // Default is that sessions expire in 30 days
|
||||
));
|
||||
@@ -51,6 +52,16 @@ class SessionService extends BaseService
|
||||
$this->Database->sessions()->where('session_key', $sessionKey)->delete();
|
||||
}
|
||||
|
||||
public function GetUserBySessionKey($sessionKey)
|
||||
{
|
||||
$sessionRow = $this->Database->sessions()->where('session_key', $sessionKey)->fetch();
|
||||
if ($sessionRow !== null)
|
||||
{
|
||||
return $this->Database->users($sessionRow->user_id);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private function GenerateSessionKey()
|
||||
{
|
||||
return RandomString(50);
|
||||
|
47
services/UsersService.php
Normal file
47
services/UsersService.php
Normal file
@@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace Grocy\Services;
|
||||
|
||||
class UsersService extends BaseService
|
||||
{
|
||||
public function CreateUser(string $username, string $firstName, string $lastName, string $password)
|
||||
{
|
||||
$newUserRow = $this->Database->users()->createRow(array(
|
||||
'username' => $username,
|
||||
'first_name' => $firstName,
|
||||
'last_name' => $lastName,
|
||||
'password' => password_hash($password, PASSWORD_DEFAULT)
|
||||
));
|
||||
$newUserRow->save();
|
||||
}
|
||||
|
||||
public function EditUser(int $userId, string $username, string $firstName, string $lastName, string $password)
|
||||
{
|
||||
if (!$this->UserExists($userId))
|
||||
{
|
||||
throw new \Exception('User does not exist');
|
||||
}
|
||||
|
||||
$user = $this->Database->users($userId);
|
||||
$user->update(array(
|
||||
'username' => $username,
|
||||
'first_name' => $firstName,
|
||||
'last_name' => $lastName,
|
||||
'password' => password_hash($password, PASSWORD_DEFAULT)
|
||||
));
|
||||
}
|
||||
|
||||
public function DeleteUser($userId)
|
||||
{
|
||||
$row = $this->Database->users($args['userId']);
|
||||
$row->delete();
|
||||
$success = $row->isClean();
|
||||
return $this->ApiResponse(array('success' => $success));
|
||||
}
|
||||
|
||||
private function UserExists($userId)
|
||||
{
|
||||
$userRow = $this->Database->users()->where('id = :1', $userId)->fetch();
|
||||
return $userRow !== null;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user