aboutsummaryrefslogtreecommitdiffstats
path: root/admin/user
diff options
context:
space:
mode:
authorThomas Lange <code@nerdmind.de>2021-06-22 01:18:02 +0200
committerThomas Lange <code@nerdmind.de>2021-06-22 01:46:42 +0200
commit8cd1105b111b89106f24c5b50795afb5ff28a935 (patch)
treec789faac9de4d83cb409c45779e76be8938a4d66 /admin/user
parent7937df540b7d70b2bb87797442d0a0a0df197133 (diff)
downloadblog-8cd1105b111b89106f24c5b50795afb5ff28a935.tar.gz
blog-8cd1105b111b89106f24c5b50795afb5ff28a935.tar.xz
blog-8cd1105b111b89106f24c5b50795afb5ff28a935.zip
Implement new Repository and Entity classes
This commit adds new Repository and Entity classes which are better abstracted from the rest of the application. They dont know anymore about configuration options or how to parse to HTML because this is not the job for the ORM but for other parts of the application. The previous commits were a preparation for this big change. An entity now represents just a single record from a specific table of the database – nothing more. The repositories job is it to fetch or update records of the database and instantiate the entities. Another problem that was solved is the high amount of database queries that was needed before. For example, on the blogs home page first were all 10 latest post IDs fetched from the database and then another query was executed with "WHERE id = :id" for *each* single post?! ... This problem is solved with the new repository classes; they now use a single query to fetch and build the entities of the 10 latest posts. This change also solves the problem with database queries spread across the application and limits the exzessive use of try/catch blocks which were used before. The new classes make the whole code much cleaner. :)
Diffstat (limited to 'admin/user')
-rw-r--r--admin/user/delete.php70
-rw-r--r--admin/user/index.php24
-rw-r--r--admin/user/insert.php35
-rw-r--r--admin/user/update.php90
4 files changed, 118 insertions, 101 deletions
diff --git a/admin/user/delete.php b/admin/user/delete.php
index 6f0af6f..758e536 100644
--- a/admin/user/delete.php
+++ b/admin/user/delete.php
@@ -11,46 +11,46 @@ define('AUTHENTICATION', TRUE);
require '../../core/application.php';
#===============================================================================
-# TRY: User\Exception
+# Get repositories
#===============================================================================
-try {
- $User = User\Factory::build(HTTP::GET('id'));
- $Attribute = $User->getAttribute();
+$UserRepository = Application::getRepository('User');
- if(HTTP::issetPOST(['token' => Application::getSecurityToken()], 'delete')) {
- try {
- if($Attribute->delete($Database)) {
- HTTP::redirect(Application::getAdminURL('user/'));
- }
- } catch(PDOException $Exception) {
- $messages[] = $Exception->getMessage();
+#===============================================================================
+# Throw 404 error if user could not be found
+#===============================================================================
+if(!$User = $UserRepository->find(HTTP::GET('id'))) {
+ Application::error404();
+}
+
+#===============================================================================
+# Check for delete request
+#===============================================================================
+if(HTTP::issetPOST(['token' => Application::getSecurityToken()], 'delete')) {
+ try {
+ if($UserRepository->delete($User)) {
+ HTTP::redirect(Application::getAdminURL('user/'));
}
+ } catch(PDOException $Exception) {
+ $messages[] = $Exception->getMessage();
}
-
- #===============================================================================
- # Build document
- #===============================================================================
- $FormTemplate = Template\Factory::build('user/form');
- $FormTemplate->set('HTML', parseEntityContent($User));
- $FormTemplate->set('FORM', [
- 'TYPE' => 'DELETE',
- 'INFO' => $messages ?? [],
- 'DATA' => array_change_key_case($Attribute->getAll(['password']), CASE_UPPER),
- 'TOKEN' => Application::getSecurityToken()
- ]);
-
- $DeleteTemplate = Template\Factory::build('user/delete');
- $DeleteTemplate->set('HTML', $FormTemplate);
-
- $MainTemplate = Template\Factory::build('main');
- $MainTemplate->set('NAME', $Language->text('title_user_delete'));
- $MainTemplate->set('HTML', $DeleteTemplate);
- echo $MainTemplate;
}
#===============================================================================
-# CATCH: User\Exception
+# Build document
#===============================================================================
-catch(User\Exception $Exception) {
- Application::error404();
-}
+$FormTemplate = Template\Factory::build('user/form');
+$FormTemplate->set('HTML', parseEntityContent($User));
+$FormTemplate->set('FORM', [
+ 'TYPE' => 'DELETE',
+ 'INFO' => $messages ?? [],
+ 'DATA' => array_change_key_case($User->getAll(['password']), CASE_UPPER),
+ 'TOKEN' => Application::getSecurityToken()
+]);
+
+$DeleteTemplate = Template\Factory::build('user/delete');
+$DeleteTemplate->set('HTML', $FormTemplate);
+
+$MainTemplate = Template\Factory::build('main');
+$MainTemplate->set('NAME', $Language->text('title_user_delete'));
+$MainTemplate->set('HTML', $DeleteTemplate);
+echo $MainTemplate;
diff --git a/admin/user/index.php b/admin/user/index.php
index 8ae4f1a..78219f3 100644
--- a/admin/user/index.php
+++ b/admin/user/index.php
@@ -11,12 +11,18 @@ define('AUTHENTICATION', TRUE);
require '../../core/application.php';
#===============================================================================
+# Get repositories
+#===============================================================================
+$UserRepository = Application::getRepository('User');
+
+#===============================================================================
# Pagination
#===============================================================================
$site_size = Application::get('ADMIN.USER.LIST_SIZE');
$site_sort = Application::get('ADMIN.USER.LIST_SORT');
-$lastSite = ceil($Database->query(sprintf('SELECT COUNT(id) FROM %s', User\Attribute::TABLE))->fetchColumn() / $site_size);
+$count = $UserRepository->getCount();
+$lastSite = ceil($count / $site_size);
$currentSite = HTTP::GET('site') ?? 1;
$currentSite = intval($currentSite);
@@ -26,16 +32,16 @@ if($currentSite < 1 OR ($currentSite > $lastSite AND $lastSite > 0)) {
}
#===============================================================================
-# Fetch user IDs from database
+# Get paginated user list
#===============================================================================
-$execSQL = "SELECT id FROM %s ORDER BY {$site_sort} LIMIT ".(($currentSite-1) * $site_size).", {$site_size}";
-$userIDs = $Database->query(sprintf($execSQL, User\Attribute::TABLE))->fetchAll($Database::FETCH_COLUMN);
+$users = $UserRepository->getPaginated(
+ $site_sort,
+ $site_size,
+ ($currentSite-1) * $site_size
+);
-foreach($userIDs as $userID) {
- try {
- $User = User\Factory::build($userID);
- $templates[] = generateUserItemTemplate($User);
- } catch(User\Exception $Exception){}
+foreach($users as $User) {
+ $templates[] = generateUserItemTemplate($User);
}
#===============================================================================
diff --git a/admin/user/insert.php b/admin/user/insert.php
index acb49ad..96fcb84 100644
--- a/admin/user/insert.php
+++ b/admin/user/insert.php
@@ -10,22 +10,33 @@ define('AUTHENTICATION', TRUE);
#===============================================================================
require '../../core/application.php';
-$Attribute = new User\Attribute();
+#===============================================================================
+# Get repositories
+#===============================================================================
+$UserRepository = Application::getRepository('User');
+#===============================================================================
+# Instantiate new User entity
+#===============================================================================
+$User = new User\Entity;
+
+#===============================================================================
+# Check for insert request
+#===============================================================================
if(HTTP::issetPOST('slug', 'username', 'password', 'fullname', 'mailaddr', 'body', 'argv', 'time_insert', 'time_update', 'insert')) {
- $Attribute->set('slug', HTTP::POST('slug') ? HTTP::POST('slug') : generateSlug(HTTP::POST('username')));
- $Attribute->set('username', HTTP::POST('username') ? HTTP::POST('username') : NULL);
- $Attribute->set('password', HTTP::POST('password') ? password_hash(HTTP::POST('password'), PASSWORD_BCRYPT, ['cost' => 10]) : FALSE);
- $Attribute->set('fullname', HTTP::POST('fullname') ? HTTP::POST('fullname') : NULL);
- $Attribute->set('mailaddr', HTTP::POST('mailaddr') ? HTTP::POST('mailaddr') : NULL);
- $Attribute->set('body', HTTP::POST('body') ? HTTP::POST('body') : NULL);
- $Attribute->set('argv', HTTP::POST('argv') ? HTTP::POST('argv') : NULL);
- $Attribute->set('time_insert', HTTP::POST('time_insert') ?: date('Y-m-d H:i:s'));
- $Attribute->set('time_update', HTTP::POST('time_update') ?: date('Y-m-d H:i:s'));
+ $User->set('slug', HTTP::POST('slug') ? HTTP::POST('slug') : generateSlug(HTTP::POST('username')));
+ $User->set('username', HTTP::POST('username') ? HTTP::POST('username') : NULL);
+ $User->set('password', HTTP::POST('password') ? password_hash(HTTP::POST('password'), PASSWORD_BCRYPT, ['cost' => 10]) : FALSE);
+ $User->set('fullname', HTTP::POST('fullname') ? HTTP::POST('fullname') : NULL);
+ $User->set('mailaddr', HTTP::POST('mailaddr') ? HTTP::POST('mailaddr') : NULL);
+ $User->set('body', HTTP::POST('body') ? HTTP::POST('body') : NULL);
+ $User->set('argv', HTTP::POST('argv') ? HTTP::POST('argv') : NULL);
+ $User->set('time_insert', HTTP::POST('time_insert') ?: date('Y-m-d H:i:s'));
+ $User->set('time_update', HTTP::POST('time_update') ?: date('Y-m-d H:i:s'));
if(HTTP::issetPOST(['token' => Application::getSecurityToken()])) {
try {
- if($Attribute->insert($Database)) {
+ if($UserRepository->insert($User)) {
HTTP::redirect(Application::getAdminURL('user/'));
}
} catch(PDOException $Exception) {
@@ -45,7 +56,7 @@ $FormTemplate = Template\Factory::build('user/form');
$FormTemplate->set('FORM', [
'TYPE' => 'INSERT',
'INFO' => $messages ?? [],
- 'DATA' => array_change_key_case($Attribute->getAll(['password']), CASE_UPPER),
+ 'DATA' => array_change_key_case($User->getAll(['password']), CASE_UPPER),
'TOKEN' => Application::getSecurityToken()
]);
diff --git a/admin/user/update.php b/admin/user/update.php
index f53d996..1f3309b 100644
--- a/admin/user/update.php
+++ b/admin/user/update.php
@@ -11,59 +11,59 @@ define('AUTHENTICATION', TRUE);
require '../../core/application.php';
#===============================================================================
-# TRY: User\Exception
+# Get repositories
#===============================================================================
-try {
- $User = User\Factory::build(HTTP::GET('id'));
- $Attribute = $User->getAttribute();
+$UserRepository = Application::getRepository('User');
- if(HTTP::issetPOST('slug', 'username', 'password', 'fullname', 'mailaddr', 'body', 'argv', 'time_insert', 'time_update', 'update')) {
- $Attribute->set('slug', HTTP::POST('slug') ? HTTP::POST('slug') : generateSlug(HTTP::POST('username')));
- $Attribute->set('username', HTTP::POST('username') ? HTTP::POST('username') : NULL);
- $Attribute->set('password', HTTP::POST('password') ? password_hash(HTTP::POST('password'), PASSWORD_BCRYPT, ['cost' => 10]) : FALSE);
- $Attribute->set('fullname', HTTP::POST('fullname') ? HTTP::POST('fullname') : NULL);
- $Attribute->set('mailaddr', HTTP::POST('mailaddr') ? HTTP::POST('mailaddr') : NULL);
- $Attribute->set('body', HTTP::POST('body') ? HTTP::POST('body') : NULL);
- $Attribute->set('argv', HTTP::POST('argv') ? HTTP::POST('argv') : NULL);
- $Attribute->set('time_insert', HTTP::POST('time_insert') ?: date('Y-m-d H:i:s'));
- $Attribute->set('time_update', HTTP::POST('time_update') ?: date('Y-m-d H:i:s'));
+#===============================================================================
+# Throw 404 error if user could not be found
+#===============================================================================
+if(!$User = $UserRepository->find(HTTP::GET('id'))) {
+ Application::error404();
+}
- if(HTTP::issetPOST(['token' => Application::getSecurityToken()])) {
- try {
- $Attribute->update($Database);
- } catch(PDOException $Exception) {
- $messages[] = $Exception->getMessage();
- }
- }
+#===============================================================================
+# Check for update request
+#===============================================================================
+if(HTTP::issetPOST('slug', 'username', 'password', 'fullname', 'mailaddr', 'body', 'argv', 'time_insert', 'time_update', 'update')) {
+ $User->set('slug', HTTP::POST('slug') ? HTTP::POST('slug') : generateSlug(HTTP::POST('username')));
+ $User->set('username', HTTP::POST('username') ? HTTP::POST('username') : NULL);
+ $User->set('password', HTTP::POST('password') ? password_hash(HTTP::POST('password'), PASSWORD_BCRYPT, ['cost' => 10]) : FALSE);
+ $User->set('fullname', HTTP::POST('fullname') ? HTTP::POST('fullname') : NULL);
+ $User->set('mailaddr', HTTP::POST('mailaddr') ? HTTP::POST('mailaddr') : NULL);
+ $User->set('body', HTTP::POST('body') ? HTTP::POST('body') : NULL);
+ $User->set('argv', HTTP::POST('argv') ? HTTP::POST('argv') : NULL);
+ $User->set('time_insert', HTTP::POST('time_insert') ?: date('Y-m-d H:i:s'));
+ $User->set('time_update', HTTP::POST('time_update') ?: date('Y-m-d H:i:s'));
- else {
- $messages[] = $Language->text('error_security_csrf');
+ if(HTTP::issetPOST(['token' => Application::getSecurityToken()])) {
+ try {
+ $UserRepository->update($User);
+ } catch(PDOException $Exception) {
+ $messages[] = $Exception->getMessage();
}
}
- #===============================================================================
- # Build document
- #===============================================================================
- $FormTemplate = Template\Factory::build('user/form');
- $FormTemplate->set('FORM', [
- 'TYPE' => 'UPDATE',
- 'INFO' => $messages ?? [],
- 'DATA' => array_change_key_case($Attribute->getAll(['password']), CASE_UPPER),
- 'TOKEN' => Application::getSecurityToken()
- ]);
-
- $InsertTemplate = Template\Factory::build('user/update');
- $InsertTemplate->set('HTML', $FormTemplate);
-
- $MainTemplate = Template\Factory::build('main');
- $MainTemplate->set('NAME', $Language->text('title_user_update'));
- $MainTemplate->set('HTML', $InsertTemplate);
- echo $MainTemplate;
+ else {
+ $messages[] = $Language->text('error_security_csrf');
+ }
}
#===============================================================================
-# CATCH: User\Exception
+# Build document
#===============================================================================
-catch(User\Exception $Exception) {
- Application::error404();
-}
+$FormTemplate = Template\Factory::build('user/form');
+$FormTemplate->set('FORM', [
+ 'TYPE' => 'UPDATE',
+ 'INFO' => $messages ?? [],
+ 'DATA' => array_change_key_case($User->getAll(['password']), CASE_UPPER),
+ 'TOKEN' => Application::getSecurityToken()
+]);
+
+$InsertTemplate = Template\Factory::build('user/update');
+$InsertTemplate->set('HTML', $FormTemplate);
+
+$MainTemplate = Template\Factory::build('main');
+$MainTemplate->set('NAME', $Language->text('title_user_update'));
+$MainTemplate->set('HTML', $InsertTemplate);
+echo $MainTemplate;