diff options
Diffstat (limited to 'admin/user')
-rw-r--r-- | admin/user/delete.php | 70 | ||||
-rw-r--r-- | admin/user/index.php | 24 | ||||
-rw-r--r-- | admin/user/insert.php | 35 | ||||
-rw-r--r-- | admin/user/update.php | 90 |
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; |