diff --git a/src/Plugins/Profile/Controller.php b/src/Plugins/Profile/Controller.php new file mode 100644 index 0000000..635d2de --- /dev/null +++ b/src/Plugins/Profile/Controller.php @@ -0,0 +1,57 @@ +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(); + }); + } +} diff --git a/src/Plugins/Profile/Profile.php b/src/Plugins/Profile/Profile.php new file mode 100644 index 0000000..2b199b5 --- /dev/null +++ b/src/Plugins/Profile/Profile.php @@ -0,0 +1,142 @@ + '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'; + } +}