Directly ported the profile plugin across
This commit is contained in:
parent
f10bd2cbc3
commit
ac938678d7
2 changed files with 199 additions and 0 deletions
57
src/Plugins/Profile/Controller.php
Normal file
57
src/Plugins/Profile/Controller.php
Normal file
|
@ -0,0 +1,57 @@
|
|||
<?php
|
||||
|
||||
namespace Plugins\Profile;
|
||||
|
||||
use \Slim\App;
|
||||
use Psr\Http\Message\ResponseInterface as Response;
|
||||
use Psr\Http\Message\ServerRequestInterface as Request;
|
||||
|
||||
class Controller
|
||||
{
|
||||
public function registerRoutes(App $app)
|
||||
{
|
||||
$profilePlugin = new Profile;
|
||||
|
||||
$app->get('/profile', function (Request $request, Response $response) use ($profilePlugin) {
|
||||
$profilePlugin->setRequest($request);
|
||||
$profilePlugin->SetResponse($response);
|
||||
return $profilePlugin->renderIndex();
|
||||
});
|
||||
|
||||
$app->get('/profile/firsttime', function (Request $request, Response $response) use ($profilePlugin) {
|
||||
$profilePlugin->setRequest($request);
|
||||
$profilePlugin->SetResponse($response);
|
||||
return $profilePlugin->renderFirstTime();
|
||||
});
|
||||
|
||||
$app->post('/profile/submitfirst', function (Request $request, Response $response) use ($profilePlugin) {
|
||||
$profilePlugin->setRequest($request);
|
||||
$profilePlugin->SetResponse($response);
|
||||
return $profilePlugin->submitfirst();
|
||||
});
|
||||
|
||||
$app->get('/profile/logout', function (Request $request, Response $response) use ($profilePlugin) {
|
||||
$profilePlugin->setRequest($request);
|
||||
$profilePlugin->SetResponse($response);
|
||||
return $profilePlugin->logout();
|
||||
});
|
||||
|
||||
$app->post('/profile/save', function (Request $request, Response $response) use ($profilePlugin) {
|
||||
$profilePlugin->setRequest($request);
|
||||
$profilePlugin->SetResponse($response);
|
||||
return $profilePlugin->save();
|
||||
});
|
||||
|
||||
$app->get('/profile/edit', function (Request $request, Response $response) use ($profilePlugin) {
|
||||
$profilePlugin->setRequest($request);
|
||||
$profilePlugin->SetResponse($response);
|
||||
return $profilePlugin->edit();
|
||||
});
|
||||
|
||||
$app->get('/profile/resetpassword', function (Request $request, Response $response) use ($profilePlugin) {
|
||||
$profilePlugin->setRequest($request);
|
||||
$profilePlugin->SetResponse($response);
|
||||
return $profilePlugin->resetpassword();
|
||||
});
|
||||
}
|
||||
}
|
142
src/Plugins/Profile/Profile.php
Normal file
142
src/Plugins/Profile/Profile.php
Normal file
|
@ -0,0 +1,142 @@
|
|||
<?php
|
||||
|
||||
namespace Plugins\Profile;
|
||||
|
||||
use AntCMS\AntPlugin;
|
||||
use AntCMS\AntConfig;
|
||||
use AntCMS\AntTools;
|
||||
use AntCMS\AntCMS;
|
||||
use AntCMS\AntTwig;
|
||||
use AntCMS\AntAuth;
|
||||
use AntCMS\AntUsers;
|
||||
|
||||
class Profile extends AntPlugin
|
||||
{
|
||||
protected AntAuth $antAuth;
|
||||
protected AntTwig $antTwig;
|
||||
protected array $params = [
|
||||
'AntCMSTitle' => 'AntCMS Profile Management',
|
||||
'AntCMSDescription' => 'AntCMS Profile Management',
|
||||
'AntCMSAuthor' => 'AntCMS',
|
||||
'AntCMSKeywords' => '',
|
||||
];
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->antAuth = new AntAuth;
|
||||
$this->antTwig = new AntTwig;
|
||||
}
|
||||
|
||||
public function renderIndex()
|
||||
{
|
||||
$this->antAuth->checkAuth();
|
||||
$this->params['user'] = AntUsers::getUserPublicalKeys($this->antAuth->getUsername());
|
||||
|
||||
$response = $this->response;
|
||||
$response->getBody()->write($this->antTwig->renderWithSubLayout('profile_landing', $this->params));
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function renderFirstTime()
|
||||
{
|
||||
if (file_exists(antUsersList)) {
|
||||
AntCMS::redirectWithoutRequest('/profile');
|
||||
}
|
||||
$response = $this->response;
|
||||
$response->getBody()->write($this->antTwig->renderWithSubLayout('profile_firstTime', $this->params));
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function submitfirst()
|
||||
{
|
||||
$POST = $this->request->getParsedBody();
|
||||
|
||||
if (file_exists(antUsersList)) {
|
||||
AntCMS::redirectWithoutRequest('/admin');
|
||||
}
|
||||
|
||||
if (isset($POST['username']) && isset($POST['password']) && isset($POST['display-name'])) {
|
||||
$data = [
|
||||
'username' => $POST['username'],
|
||||
'password' => $POST['password'],
|
||||
'name' => $POST['display-name'],
|
||||
];
|
||||
AntUsers::setupFirstUser($data);
|
||||
AntCMS::redirectWithoutRequest('/profile');
|
||||
} else {
|
||||
AntCMS::redirectWithoutRequest('/profile/firsttime');
|
||||
}
|
||||
}
|
||||
|
||||
public function save()
|
||||
{
|
||||
$POST = $this->request->getParsedBody();
|
||||
|
||||
$this->antAuth->checkAuth();
|
||||
$data['username'] = $POST['username'] ?? null;
|
||||
$data['name'] = $POST['display-name'] ?? null;
|
||||
$data['password'] = $POST['password'] ?? null;
|
||||
|
||||
foreach ($data as $key => $value) {
|
||||
if (is_null($value)) {
|
||||
unset($data[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
AntUsers::updateUser($this->antAuth->getUsername(), $data);
|
||||
AntCMS::redirectWithoutRequest('/profile');
|
||||
}
|
||||
|
||||
public function logout()
|
||||
{
|
||||
$response = $this->response;
|
||||
|
||||
$this->antAuth->invalidateSession();
|
||||
if (!$this->antAuth->isAuthenticated()) {
|
||||
$response->getBody()->write('You have been logged out.');
|
||||
} else {
|
||||
$response->getBody()->write('There was an error logging you out.');
|
||||
}
|
||||
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function edit()
|
||||
{
|
||||
$this->antAuth->checkAuth();
|
||||
$user = AntUsers::getUserPublicalKeys($this->antAuth->getUsername());
|
||||
|
||||
if (!$user) {
|
||||
AntCMS::redirectWithoutRequest('/profile');
|
||||
}
|
||||
|
||||
$user['username'] = $this->antAuth->getUsername();
|
||||
$this->params['user'] = $user;
|
||||
|
||||
$response = $this->response;
|
||||
$response->getBody()->write($this->antTwig->renderWithSubLayout('profile_edit', $this->params));
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function resetpassword()
|
||||
{
|
||||
$this->antAuth->checkAuth();
|
||||
$user = AntUsers::getUserPublicalKeys($this->antAuth->getUsername());
|
||||
|
||||
if (!$user) {
|
||||
AntCMS::redirectWithoutRequest('/profile');
|
||||
}
|
||||
|
||||
$user['username'] = $this->antAuth->getUsername();
|
||||
$this->params['user'] = $user;
|
||||
|
||||
$response = $this->response;
|
||||
$response->getBody()->write($this->antTwig->renderWithSubLayout('profile_resetPassword', $this->params));
|
||||
return $response;
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return 'Profile';
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue