Avoid using AdminTrait in controllers

This commit is contained in:
Giuseppe Criscione 2020-12-19 15:44:18 +01:00
parent 58b8de8be7
commit fee6a40eef
11 changed files with 138 additions and 133 deletions

View File

@ -3,7 +3,6 @@
namespace Formwork\Admin\Controllers;
use Formwork\Admin\Admin;
use Formwork\Admin\AdminTrait;
use Formwork\Admin\AdminView;
use Formwork\Admin\Security\CSRFToken;
use Formwork\Admin\Users\User;
@ -15,8 +14,6 @@ use Formwork\Utils\Str;
abstract class AbstractController
{
use AdminTrait;
/**
* Current panel location
*
@ -39,6 +36,14 @@ abstract class AbstractController
$this->location = strtolower(Str::afterLast(static::class, '\\'));
}
/**
* Return admin instance
*/
protected function admin(): Admin
{
return Admin::instance();
}
/**
* Return site instance
*/
@ -59,25 +64,25 @@ abstract class AbstractController
'modals' => implode($this->modals),
'colorScheme' => $this->getColorScheme(),
'appConfig' => JSON::encode([
'baseUri' => $this->panelUri(),
'baseUri' => $this->admin()->panelUri(),
'DatePicker' => [
'weekStarts' => Formwork::instance()->config()->get('date.week_starts'),
'format' => Date::formatToPattern(Formwork::instance()->config()->get('date.format')),
'labels' => [
'today' => $this->translate('date.today'),
'weekdays' => ['long' => $this->translate('date.weekdays.long'), 'short' => $this->translate('date.weekdays.short')],
'months' => ['long' => $this->translate('date.months.long'), 'short' => $this->translate('date.months.short')]
'today' => $this->admin()->translate('date.today'),
'weekdays' => ['long' => $this->admin()->translate('date.weekdays.long'), 'short' => $this->admin()->translate('date.weekdays.short')],
'months' => ['long' => $this->admin()->translate('date.months.long'), 'short' => $this->admin()->translate('date.months.short')]
]
],
'DurationInput' => [
'labels' => [
'years' => $this->translate('date.duration.years'),
'months' => $this->translate('date.duration.months'),
'weeks' => $this->translate('date.duration.weeks'),
'days' => $this->translate('date.duration.days'),
'hours' => $this->translate('date.duration.hours'),
'minutes' => $this->translate('date.duration.minutes'),
'seconds' => $this->translate('date.duration.seconds')
'years' => $this->admin()->translate('date.duration.years'),
'months' => $this->admin()->translate('date.duration.months'),
'weeks' => $this->admin()->translate('date.duration.weeks'),
'days' => $this->admin()->translate('date.duration.days'),
'hours' => $this->admin()->translate('date.duration.hours'),
'minutes' => $this->admin()->translate('date.duration.minutes'),
'seconds' => $this->admin()->translate('date.duration.seconds')
]
]
])

View File

@ -18,28 +18,28 @@ class Authentication extends AbstractController
public function login(): void
{
$limiter = new AccessLimiter(
$this->registry('accessAttempts'),
$this->admin()->registry('accessAttempts'),
Formwork::instance()->config()->get('admin.login_attempts'),
Formwork::instance()->config()->get('admin.login_reset_time')
);
if ($limiter->hasReachedLimit()) {
$minutes = round(Formwork::instance()->config()->get('admin.login_reset_time') / 60);
$this->error($this->translate('login.attempt.too-many', $minutes));
$this->error($this->admin()->translate('login.attempt.too-many', $minutes));
return;
}
switch (HTTPRequest::method()) {
case 'GET':
if (Session::has('FORMWORK_USERNAME')) {
$this->redirectToPanel();
$this->admin()->redirectToPanel();
}
// Always generate a new CSRF token
CSRFToken::generate();
$this->view('authentication.login', [
'title' => $this->translate('login.login')
'title' => $this->admin()->translate('login.login')
]);
break;
@ -52,7 +52,7 @@ class Authentication extends AbstractController
// Ensure no required data is missing
if (!$data->hasMultiple(['username', 'password'])) {
$this->error($this->translate('login.attempt.failed'));
$this->error($this->admin()->translate('login.attempt.failed'));
}
$limiter->registerAttempt();
@ -66,20 +66,20 @@ class Authentication extends AbstractController
// Regenerate CSRF token
CSRFToken::generate();
$time = $this->log('access')->log($data->get('username'));
$this->registry('lastAccess')->set($data->get('username'), $time);
$time = $this->admin()->log('access')->log($data->get('username'));
$this->admin()->registry('lastAccess')->set($data->get('username'), $time);
$limiter->resetAttempts();
if (($destination = Session::get('FORMWORK_REDIRECT_TO')) !== null) {
Session::remove('FORMWORK_REDIRECT_TO');
$this->redirect($destination);
$this->admin()->redirect($destination);
}
$this->redirectToPanel();
$this->admin()->redirectToPanel();
}
$this->error($this->translate('login.attempt.failed'), [
$this->error($this->admin()->translate('login.attempt.failed'), [
'username' => $data->get('username'),
'error' => true
]);
@ -98,10 +98,10 @@ class Authentication extends AbstractController
Session::destroy();
if (Formwork::instance()->config()->get('admin.logout_redirect') === 'home') {
$this->redirectToSite();
$this->admin()->redirectToSite();
} else {
$this->notify($this->translate('login.logged-out'), 'info');
$this->redirectToPanel();
$this->admin()->notify($this->admin()->translate('login.logged-out'), 'info');
$this->admin()->redirectToPanel();
}
}
@ -116,8 +116,8 @@ class Authentication extends AbstractController
// Ensure CSRF token is re-generated
CSRFToken::generate();
$defaults = ['title' => $this->translate('login.login')];
$this->notify($message, 'error');
$defaults = ['title' => $this->admin()->translate('login.login')];
$this->admin()->notify($message, 'error');
$this->view('authentication.login', array_merge($defaults, $data));
}
}

View File

@ -22,12 +22,12 @@ class Backup extends AbstractController
try {
$file = $backupper->backup();
} catch (TranslatedException $e) {
JSONResponse::error($this->translate('backup.error.cannot-make', $e->getTranslatedMessage()), 500)->send();
JSONResponse::error($this->admin()->translate('backup.error.cannot-make', $e->getTranslatedMessage()), 500)->send();
}
$filename = basename($file);
JSONResponse::success($this->translate('backup.ready'), 200, [
JSONResponse::success($this->admin()->translate('backup.ready'), 200, [
'filename' => $filename,
'uri' => $this->uri('/backup/download/' . urlencode(base64_encode($filename)) . '/')
'uri' => $this->admin()->uri('/backup/download/' . urlencode(base64_encode($filename)) . '/')
])->send();
}
@ -42,11 +42,11 @@ class Backup extends AbstractController
if (FileSystem::isFile($file, false)) {
HTTPResponse::download($file);
} else {
throw new RuntimeException($this->translate('backup.error.cannot-download.invalid-filename'));
throw new RuntimeException($this->admin()->translate('backup.error.cannot-download.invalid-filename'));
}
} catch (TranslatedException $e) {
$this->notify($this->translate('backup.error.cannot-download', $e->getTranslatedMessage()), 'error');
$this->redirectToReferer(302, '/dashboard/');
$this->admin()->notify($this->admin()->translate('backup.error.cannot-download', $e->getTranslatedMessage()), 'error');
$this->admin()->redirectToReferer(302, '/dashboard/');
}
}
}

View File

@ -16,6 +16,6 @@ class Cache extends AbstractController
if (Formwork::instance()->config()->get('cache.enabled')) {
Formwork::instance()->cache()->clear();
}
JSONResponse::success($this->translate('cache.cleared'))->send();
JSONResponse::success($this->admin()->translate('cache.cleared'))->send();
}
}

View File

@ -24,7 +24,7 @@ class Dashboard extends AbstractController
$this->modal('deletePage');
$this->view('dashboard.index', [
'title' => $this->translate('dashboard.dashboard'),
'title' => $this->admin()->translate('dashboard.dashboard'),
'lastModifiedPages' => $this->view('pages.list', [
'pages' => $this->site()->descendants()->sort('lastModifiedTime', SORT_DESC)->slice(0, 5),
'subpages' => false,

View File

@ -15,8 +15,8 @@ class Errors extends AbstractController
public function notFound(): void
{
$this->displayError(404, 'not-found', [
'href' => $this->uri('/dashboard/'),
'label' => $this->translate('errors.action.return-to-dashboard')
'href' => $this->admin()->uri('/dashboard/'),
'label' => $this->admin()->translate('errors.action.return-to-dashboard')
]);
}
@ -27,7 +27,7 @@ class Errors extends AbstractController
{
$this->displayError(500, 'internal-server-error', [
'href' => $this->makeGitHubIssueUri($exception),
'label' => $this->translate('errors.action.report-to-github')
'label' => $this->admin()->translate('errors.action.report-to-github')
]);
}
@ -37,8 +37,8 @@ class Errors extends AbstractController
public function forbidden(): void
{
$this->displayError(403, 'forbidden', [
'href' => $this->uri('/dashboard/'),
'label' => $this->translate('errors.action.return-to-dashboard')
'href' => $this->admin()->uri('/dashboard/'),
'label' => $this->admin()->translate('errors.action.return-to-dashboard')
]);
}
@ -54,11 +54,11 @@ class Errors extends AbstractController
HTTPResponse::cleanOutputBuffers();
Header::status($status);
$this->view('errors.error', [
'title' => $this->translate('errors.error.' . $name . '.status'),
'title' => $this->admin()->translate('errors.error.' . $name . '.status'),
'code' => $status,
'status' => $this->translate('errors.error.' . $name . '.status'),
'heading' => $this->translate('errors.error.' . $name . '.heading'),
'description' => $this->translate('errors.error.' . $name . '.description'),
'status' => $this->admin()->translate('errors.error.' . $name . '.status'),
'heading' => $this->admin()->translate('errors.error.' . $name . '.heading'),
'description' => $this->admin()->translate('errors.error.' . $name . '.description'),
'action' => $action
]);
// Don't exit, otherwise the error will not be logged

View File

@ -29,7 +29,7 @@ class Options extends AbstractController
public function index(): void
{
$this->ensurePermission('options.system');
$this->redirect('/options/system/');
$this->admin()->redirect('/options/system/');
}
/**
@ -55,8 +55,8 @@ class Options extends AbstractController
FileSystem::touch(Formwork::instance()->config()->get('content.path'));
}
$this->notify($this->translate('options.updated'), 'success');
$this->redirect('/options/system/');
$this->admin()->notify($this->admin()->translate('options.updated'), 'success');
$this->admin()->redirect('/options/system/');
}
$fields->validate(Formwork::instance()->config());
@ -64,7 +64,7 @@ class Options extends AbstractController
$this->modal('changes');
$this->view('options.system', [
'title' => $this->translate('options.options'),
'title' => $this->admin()->translate('options.options'),
'tabs' => $this->view('options.tabs', [
'tabs' => $this->tabs,
'current' => 'system'
@ -96,8 +96,8 @@ class Options extends AbstractController
FileSystem::touch(Formwork::instance()->config()->get('content.path'));
}
$this->notify($this->translate('options.updated'), 'success');
$this->redirect('/options/site/');
$this->admin()->notify($this->admin()->translate('options.updated'), 'success');
$this->admin()->redirect('/options/site/');
}
$fields->validate(new DataGetter($this->site()->data()));
@ -105,7 +105,7 @@ class Options extends AbstractController
$this->modal('changes');
$this->view('options.site', [
'title' => $this->translate('options.options'),
'title' => $this->admin()->translate('options.options'),
'tabs' => $this->view('options.tabs', [
'tabs' => $this->tabs,
'current' => 'site'
@ -122,7 +122,7 @@ class Options extends AbstractController
$this->ensurePermission('options.updates');
$this->view('options.updates', [
'title' => $this->translate('options.updates'),
'title' => $this->admin()->translate('options.updates'),
'tabs' => $this->view('options.tabs', [
'tabs' => $this->tabs,
'current' => 'updates'
@ -218,7 +218,7 @@ class Options extends AbstractController
ksort($data['HTTP Response Headers']);
$this->view('options.info', [
'title' => $this->translate('options.options'),
'title' => $this->admin()->translate('options.options'),
'tabs' => $this->view('options.tabs', [
'tabs' => $this->tabs,
'current' => 'info'

View File

@ -54,7 +54,7 @@ class Pages extends AbstractController
$this->modal('deletePage');
$this->view('pages.index', [
'title' => $this->translate('pages.pages'),
'title' => $this->admin()->translate('pages.pages'),
'pagesList' => $this->view('pages.list', [
'pages' => $this->site()->pages(),
'subpages' => true,
@ -79,13 +79,13 @@ class Pages extends AbstractController
try {
$page = $this->createPage($data);
Session::set('FORMWORK_PAGE_TO_PUBLISH', $page->route());
$this->notify($this->translate('pages.page.created'), 'success');
$this->admin()->notify($this->admin()->translate('pages.page.created'), 'success');
} catch (TranslatedException $e) {
$this->notify($e->getTranslatedMessage(), 'error');
$this->redirectToReferer(302, '/pages/');
$this->admin()->notify($e->getTranslatedMessage(), 'error');
$this->admin()->redirectToReferer(302, '/pages/');
}
$this->redirect('/pages/' . trim($page->route(), '/') . '/edit/');
$this->admin()->redirect('/pages/' . trim($page->route(), '/') . '/edit/');
}
/**
@ -101,14 +101,14 @@ class Pages extends AbstractController
if ($params->has('language')) {
if (empty(Formwork::instance()->config()->get('languages.available'))) {
$this->redirect('/pages/' . trim($page->route(), '/') . '/edit/');
$this->admin()->redirect('/pages/' . trim($page->route(), '/') . '/edit/');
}
$language = $params->get('language');
if (!in_array($language, Formwork::instance()->config()->get('languages.available'), true)) {
$this->notify($this->translate('pages.page.cannot-edit.invalid-language', $language), 'error');
$this->redirect('/pages/' . trim($page->route(), '/') . '/edit/language/' . $this->site()->languages()->default() . '/');
$this->admin()->notify($this->admin()->translate('pages.page.cannot-edit.invalid-language', $language), 'error');
$this->admin()->redirect('/pages/' . trim($page->route(), '/') . '/edit/language/' . $this->site()->languages()->default() . '/');
}
if ($page->hasLanguage($language)) {
@ -116,7 +116,7 @@ class Pages extends AbstractController
}
} elseif ($page->language() !== null) {
// Redirect to proper language
$this->redirect('/pages/' . trim($page->route(), '/') . '/edit/language/' . $page->language() . '/');
$this->admin()->redirect('/pages/' . trim($page->route(), '/') . '/edit/language/' . $page->language() . '/');
}
// Check if page has to be published on next save
@ -153,22 +153,22 @@ class Pages extends AbstractController
// Update the page
try {
$page = $this->updatePage($page, $data, $fields);
$this->notify($this->translate('pages.page.edited'), 'success');
$this->admin()->notify($this->admin()->translate('pages.page.edited'), 'success');
} catch (TranslatedException $e) {
$this->notify($e->getTranslatedMessage(), 'error');
$this->admin()->notify($e->getTranslatedMessage(), 'error');
}
if (HTTPRequest::hasFiles()) {
try {
$this->processPageUploads($page);
} catch (TranslatedException $e) {
$this->notify($this->translate('uploader.error', $e->getTranslatedMessage()), 'error');
$this->admin()->notify($this->admin()->translate('uploader.error', $e->getTranslatedMessage()), 'error');
}
}
// Redirect if page route has changed
if ($params->get('page') !== ($route = trim($page->route(), '/'))) {
$this->redirect('/pages/' . $route . '/edit/');
$this->admin()->redirect('/pages/' . $route . '/edit/');
}
break;
@ -187,7 +187,7 @@ class Pages extends AbstractController
$this->modal('deleteFile');
$this->view('pages.editor', [
'title' => $this->translate('pages.edit-page', $page->title()),
'title' => $this->admin()->translate('pages.edit-page', $page->title()),
'page' => $page,
'fields' => $fields->render(true),
'templates' => $this->site()->templates(),
@ -207,16 +207,16 @@ class Pages extends AbstractController
$data = new DataGetter(HTTPRequest::postData());
if (!$data->hasMultiple(['parent', 'from', 'to'])) {
JSONResponse::error($this->translate('pages.page.cannot-move'))->send();
JSONResponse::error($this->admin()->translate('pages.page.cannot-move'))->send();
}
if (!is_numeric($data->get('from')) || !is_numeric($data->get('to'))) {
JSONResponse::error($this->translate('pages.page.cannot-move'))->send();
JSONResponse::error($this->admin()->translate('pages.page.cannot-move'))->send();
}
$parent = $this->resolveParent($data->get('parent'));
if ($parent === null || !$parent->hasChildren()) {
JSONResponse::error($this->translate('pages.page.cannot-move'))->send();
JSONResponse::error($this->admin()->translate('pages.page.cannot-move'))->send();
}
$pages = $parent->children()->toArray();
@ -240,7 +240,7 @@ class Pages extends AbstractController
}
}
JSONResponse::success($this->translate('pages.page.moved'))->send();
JSONResponse::success($this->admin()->translate('pages.page.moved'))->send();
}
/**
@ -259,14 +259,14 @@ class Pages extends AbstractController
if ($page->hasLanguage($language)) {
$page->setLanguage($language);
} else {
$this->notify($this->translate('pages.page.cannot-delete.invalid-language', $language), 'error');
$this->redirectToReferer(302, '/pages/');
$this->admin()->notify($this->admin()->translate('pages.page.cannot-delete.invalid-language', $language), 'error');
$this->admin()->redirectToReferer(302, '/pages/');
}
}
if (!$page->isDeletable()) {
$this->notify($this->translate('pages.page.cannot-delete.not-deletable'), 'error');
$this->redirectToReferer(302, '/pages/');
$this->admin()->notify($this->admin()->translate('pages.page.cannot-delete.not-deletable'), 'error');
$this->admin()->redirectToReferer(302, '/pages/');
}
// Delete just the content file only if there are more than one language
@ -276,13 +276,13 @@ class Pages extends AbstractController
FileSystem::delete($page->path(), true);
}
$this->notify($this->translate('pages.page.deleted'), 'success');
$this->admin()->notify($this->admin()->translate('pages.page.deleted'), 'success');
// Don't redirect to referer if it's to Pages@edit
if (!Str::startsWith(Uri::normalize(HTTPRequest::referer()), Uri::make(['path' => $this->uri('/pages/' . $params->get('page') . '/edit/')]))) {
$this->redirectToReferer(302, '/pages/');
if (!Str::startsWith(Uri::normalize(HTTPRequest::referer()), Uri::make(['path' => $this->admin()->uri('/pages/' . $params->get('page') . '/edit/')]))) {
$this->admin()->redirectToReferer(302, '/pages/');
} else {
$this->redirect('/pages/');
$this->admin()->redirect('/pages/');
}
}
@ -301,13 +301,13 @@ class Pages extends AbstractController
try {
$this->processPageUploads($page);
} catch (TranslatedException $e) {
$this->notify($this->translate('uploader.error', $e->getTranslatedMessage()), 'error');
$this->redirect('/pages/' . $params->get('page') . '/edit/');
$this->admin()->notify($this->admin()->translate('uploader.error', $e->getTranslatedMessage()), 'error');
$this->admin()->redirect('/pages/' . $params->get('page') . '/edit/');
}
}
$this->notify($this->translate('uploader.uploaded'), 'success');
$this->redirect('/pages/' . $params->get('page') . '/edit/');
$this->admin()->notify($this->admin()->translate('uploader.uploaded'), 'success');
$this->admin()->redirect('/pages/' . $params->get('page') . '/edit/');
}
/**
@ -322,14 +322,14 @@ class Pages extends AbstractController
$this->ensurePageExists($page, 'pages.page.cannot-delete-file.page-not-found');
if (!$page->files()->has($params->get('filename'))) {
$this->notify($this->translate('pages.page.cannot-delete-file.file-not-found'), 'error');
$this->redirect('/pages/' . $params->get('page') . '/edit/');
$this->admin()->notify($this->admin()->translate('pages.page.cannot-delete-file.file-not-found'), 'error');
$this->admin()->redirect('/pages/' . $params->get('page') . '/edit/');
}
FileSystem::delete($page->path() . $params->get('filename'));
$this->notify($this->translate('pages.page.file-deleted'), 'success');
$this->redirect('/pages/' . $params->get('page') . '/edit/');
$this->admin()->notify($this->admin()->translate('pages.page.file-deleted'), 'success');
$this->admin()->redirect('/pages/' . $params->get('page') . '/edit/');
}
/**
@ -365,7 +365,7 @@ class Pages extends AbstractController
throw new TranslatedException('Invalid page template', 'pages.page.cannot-create.invalid-template');
}
$scheme = $this->scheme($data->get('template'));
$scheme = $this->admin()->scheme($data->get('template'));
$path = $parent->path() . $this->makePageNum($parent, $scheme->get('num')) . '-' . $data->get('slug') . DS;
@ -527,8 +527,8 @@ class Pages extends AbstractController
protected function ensurePageExists(?Page $page, string $errorLanguageString): void
{
if ($page === null) {
$this->notify($this->translate($errorLanguageString), 'error');
$this->redirectToReferer(302, '/pages/');
$this->admin()->notify($this->admin()->translate($errorLanguageString), 'error');
$this->admin()->redirectToReferer(302, '/pages/');
}
}

View File

@ -22,7 +22,7 @@ class Register extends AbstractController
switch (HTTPRequest::method()) {
case 'GET':
$this->view('register.register', [
'title' => $this->translate('register.register')
'title' => $this->admin()->translate('register.register')
]);
break;
@ -31,8 +31,8 @@ class Register extends AbstractController
$data = new DataGetter(HTTPRequest::postData());
if (!$data->hasMultiple(['username', 'fullname', 'password', 'language', 'email'])) {
$this->notify($this->translate('users.user.cannot-create.var-missing'), 'error');
$this->redirectToPanel();
$this->admin()->notify($this->admin()->translate('users.user.cannot-create.var-missing'), 'error');
$this->admin()->redirectToPanel();
}
$userData = [
@ -47,10 +47,10 @@ class Register extends AbstractController
YAML::encodeToFile($userData, Admin::ACCOUNTS_PATH . $data->get('username') . '.yml');
Session::set('FORMWORK_USERNAME', $data->get('username'));
$time = $this->log('access')->log($data->get('username'));
$this->registry('lastAccess')->set($data->get('username'), $time);
$time = $this->admin()->log('access')->log($data->get('username'));
$this->admin()->registry('lastAccess')->set($data->get('username'), $time);
$this->redirectToPanel();
$this->admin()->redirectToPanel();
break;
}

View File

@ -21,16 +21,16 @@ class Updates extends AbstractController
try {
$upToDate = $updater->checkUpdates();
} catch (RuntimeException $e) {
JSONResponse::error($this->translate('updates.status.cannot-check'), 500, [
'status' => $this->translate('updates.status.cannot-check')
JSONResponse::error($this->admin()->translate('updates.status.cannot-check'), 500, [
'status' => $this->admin()->translate('updates.status.cannot-check')
])->send();
}
if ($upToDate) {
JSONResponse::success($this->translate('updates.status.up-to-date'), 200, [
JSONResponse::success($this->admin()->translate('updates.status.up-to-date'), 200, [
'uptodate' => true
])->send();
} else {
JSONResponse::success($this->translate('updates.status.found'), 200, [
JSONResponse::success($this->admin()->translate('updates.status.found'), 200, [
'uptodate' => false,
'release' => $updater->latestRelease()
])->send();
@ -49,23 +49,23 @@ class Updates extends AbstractController
try {
$backupper->backup();
} catch (TranslatedException $e) {
JSONResponse::error($this->translate('updates.status.cannot-make-backup'), 500, [
'status' => $this->translate('updates.status.cannot-make-backup')
JSONResponse::error($this->admin()->translate('updates.status.cannot-make-backup'), 500, [
'status' => $this->admin()->translate('updates.status.cannot-make-backup')
])->send();
}
}
try {
$updater->update();
} catch (RuntimeException $e) {
JSONResponse::error($this->translate('updates.status.cannot-install'), 500, [
'status' => $this->translate('updates.status.cannot-install')
JSONResponse::error($this->admin()->translate('updates.status.cannot-install'), 500, [
'status' => $this->admin()->translate('updates.status.cannot-install')
])->send();
}
if (Formwork::instance()->config()->get('cache.enabled')) {
Formwork::instance()->cache()->clear();
}
JSONResponse::success($this->translate('updates.installed'), 200, [
'status' => $this->translate('updates.status.up-to-date')
JSONResponse::success($this->admin()->translate('updates.installed'), 200, [
'status' => $this->admin()->translate('updates.status.up-to-date')
])->send();
}
}

View File

@ -32,7 +32,7 @@ class Users extends AbstractController
$this->modal('deleteUser');
$this->view('users.index', [
'title' => $this->translate('users.users'),
'title' => $this->admin()->translate('users.users'),
'users' => Admin::instance()->users()
]);
}
@ -48,14 +48,14 @@ class Users extends AbstractController
// Ensure no required data is missing
if (!$data->hasMultiple(['username', 'fullname', 'password', 'email', 'language'])) {
$this->notify($this->translate('users.user.cannot-create.var-missing'), 'error');
$this->redirect('/users/');
$this->admin()->notify($this->admin()->translate('users.user.cannot-create.var-missing'), 'error');
$this->admin()->redirect('/users/');
}
// Ensure there isn't a user with the same username
if (Admin::instance()->users()->has($data->get('username'))) {
$this->notify($this->translate('users.user.cannot-create.already-exists'), 'error');
$this->redirect('/users/');
$this->admin()->notify($this->admin()->translate('users.user.cannot-create.already-exists'), 'error');
$this->admin()->redirect('/users/');
}
$userData = [
@ -68,8 +68,8 @@ class Users extends AbstractController
YAML::encodeToFile($userData, Admin::ACCOUNTS_PATH . $data->get('username') . '.yml');
$this->notify($this->translate('users.user.created'), 'success');
$this->redirect('/users/');
$this->admin()->notify($this->admin()->translate('users.user.created'), 'success');
$this->admin()->redirect('/users/');
}
/**
@ -94,15 +94,15 @@ class Users extends AbstractController
FileSystem::delete(Admin::ACCOUNTS_PATH . $user->username() . '.yml');
$this->deleteAvatar($user);
} catch (TranslatedException $e) {
$this->notify($e->getTranslatedMessage(), 'error');
$this->redirectToReferer(302, '/users/');
$this->admin()->notify($e->getTranslatedMessage(), 'error');
$this->admin()->redirectToReferer(302, '/users/');
}
// Remove user last access from registry
$this->registry('lastAccess')->remove($user->username());
$this->admin()->registry('lastAccess')->remove($user->username());
$this->notify($this->translate('users.user.deleted'), 'success');
$this->redirect('/users/');
$this->admin()->notify($this->admin()->translate('users.user.deleted'), 'success');
$this->admin()->redirect('/users/');
}
/**
@ -118,8 +118,8 @@ class Users extends AbstractController
$user = Admin::instance()->users()->get($params->get('user'));
if ($user === null) {
$this->notify($this->translate('users.user.not-found'), 'error');
$this->redirect('/users/');
$this->admin()->notify($this->admin()->translate('users.user.not-found'), 'error');
$this->admin()->redirect('/users/');
}
// Disable password and/or role fields if they cannot be changed
@ -132,12 +132,12 @@ class Users extends AbstractController
$data = new DataSetter(HTTPRequest::postData());
$fields->validate($data);
$this->updateUser($user, $data);
$this->notify($this->translate('users.user.edited'), 'success');
$this->admin()->notify($this->admin()->translate('users.user.edited'), 'success');
} else {
$this->notify($this->translate('users.user.cannot-edit', $user->username()), 'error');
$this->admin()->notify($this->admin()->translate('users.user.cannot-edit', $user->username()), 'error');
}
$this->redirect('/users/' . $user->username() . '/profile/');
$this->admin()->redirect('/users/' . $user->username() . '/profile/');
}
$fields->validate(new DataGetter($user->toArray()));
@ -147,7 +147,7 @@ class Users extends AbstractController
$this->modal('deleteUser');
$this->view('users.profile', [
'title' => $this->translate('users.user-profile', $user->username()),
'title' => $this->admin()->translate('users.user-profile', $user->username()),
'user' => $user,
'fields' => $fields->render(true)
]);
@ -164,8 +164,8 @@ class Users extends AbstractController
if (!empty($data->get('password'))) {
// Ensure that password can be changed
if (!$this->user()->canChangePasswordOf($user)) {
$this->notify($this->translate('users.user.cannot-change-password'), 'error');
$this->redirect('/users/' . $user->username() . '/profile/');
$this->admin()->notify($this->admin()->translate('users.user.cannot-change-password'), 'error');
$this->admin()->redirect('/users/' . $user->username() . '/profile/');
}
// Hash the new password
@ -177,8 +177,8 @@ class Users extends AbstractController
// Ensure that user role can be changed
if ($data->get('role', $user->role()) !== $user->role() && !$this->user()->canChangeRoleOf($user)) {
$this->notify($this->translate('users.user.cannot-change-role', $user->username()), 'error');
$this->redirect('/users/' . $user->username() . '/profile/');
$this->admin()->notify($this->admin()->translate('users.user.cannot-change-role', $user->username()), 'error');
$this->admin()->redirect('/users/' . $user->username() . '/profile/');
}
// Handle incoming files
@ -209,8 +209,8 @@ class Users extends AbstractController
try {
$hasUploaded = $uploader->upload(FileSystem::randomName());
} catch (TranslatedException $e) {
$this->notify($this->translate('uploader.error', $e->getTranslatedMessage()), 'error');
$this->redirect('/users/' . $user->username() . '/profile/');
$this->admin()->notify($this->admin()->translate('uploader.error', $e->getTranslatedMessage()), 'error');
$this->admin()->redirect('/users/' . $user->username() . '/profile/');
}
if ($hasUploaded) {
@ -223,7 +223,7 @@ class Users extends AbstractController
// Delete old avatar
$this->deleteAvatar($user);
$this->notify($this->translate('user.avatar.uploaded'), 'success');
$this->admin()->notify($this->admin()->translate('user.avatar.uploaded'), 'success');
return $uploader->uploadedFiles()[0];
}
}