Skip to content

Commit

Permalink
refactor user add method using errors
Browse files Browse the repository at this point in the history
+ fix CI
  • Loading branch information
vincent-peugnet committed Dec 5, 2023
1 parent f1beddf commit af0097e
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 47 deletions.
7 changes: 6 additions & 1 deletion app/class/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Wcms;

use RuntimeException;
use Wcms\Exception\Databaseexception;

class Application
{
Expand Down Expand Up @@ -47,7 +48,11 @@ public function wakeup()
$userdata['level'] = 10;
$user = new User($userdata);
$user->hashpassword();
$this->usermanager->add($user);
try {
$this->usermanager->add($user);
} catch (Databaseexception $e) {
Logger::errorex($e);
}
header('Location: ./');
exit;
} else {
Expand Down
15 changes: 10 additions & 5 deletions app/class/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use League\Plates\Engine;
use RuntimeException;
use Wcms\Exception\Database\Notfoundexception;
use Wcms\Exception\Databaseexception;

class Controller
{
Expand Down Expand Up @@ -183,11 +184,15 @@ public function sendstatflashmessage(int $count, int $total, string $message)
*/
protected function disconnect()
{
$this->user->destroysession($this->session->wsession);
$cookiemanager = new Modelconnect();
$cookiemanager->deleteauthcookie();
$this->session->empty();
$this->usermanager->add($this->user);
try {
$this->user->destroysession($this->session->wsession);
$cookiemanager = new Modelconnect();
$cookiemanager->deleteauthcookie();
$this->session->empty();
$this->usermanager->update($this->user);
} catch (Databaseexception $e) {
Logger::errorex($e);
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/class/Controllerhome.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
namespace Wcms;

use RuntimeException;
use Wcms\Exception\Database\Databaseexception;
use Wcms\Exception\Databaseexception;

class Controllerhome extends Controller
{
Expand Down
43 changes: 27 additions & 16 deletions app/class/Controllerprofile.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Wcms;

use RuntimeException;
use Wcms\Exception\Databaseexception;
use Wcms\Exception\Database\Notfoundexception;

class Controllerprofile extends Controller
Expand Down Expand Up @@ -34,7 +35,8 @@ public function update()
try {
$user = $this->usermanager->get($this->user);
$user->hydrateexception($_POST);
$this->usermanager->add($user);
$this->usermanager->update($user);
Model::sendflashmessage('Successfully updated', Model::FLASH_SUCCESS);
} catch (Notfoundexception $e) {
Model::sendflashmessage($e->getMessage(), Model::FLASH_ERROR);
} catch (RuntimeException $e) {
Expand All @@ -46,32 +48,41 @@ public function update()
$this->routedirect('profile');
}

/**
* Update the user's password.
*/
public function password()
{
if (
!isset($_POST['currentpassword']) ||
!$this->usermanager->passwordcheck($this->user->id(), $_POST['currentpassword'])
!$this->usermanager->passwordcheck($this->user, $_POST['currentpassword'])
) {
Model::sendflashmessage("wrong current password", 'error');
$this->routedirect('profile');
}

if (
!empty($_POST['password1']) &&
!empty($_POST['password2']) &&
$_POST['password1'] === $_POST['password2']
empty($_POST['password1']) ||
empty($_POST['password2']) ||
$_POST['password1'] !== $_POST['password2']
) {
if (
$this->user->setpassword($_POST['password1']) &&
$this->user->hashpassword() &&
$this->usermanager->add($this->user)
) {
Model::sendflashmessage('password updated successfully', 'success');
} else {
Model::sendflashmessage("password is not compatible or an error occured", 'error');
}
} else {
Model::sendflashmessage("passwords does not match", "error");
Model::sendflashmessage("passwords does not match", Model::FLASH_ERROR);
$this->routedirect('profile');
}

if (
!$this->user->setpassword($_POST['password1']) ||
!$this->user->hashpassword()
) {
Model::sendflashmessage("password is not compatible", Model::FLASH_ERROR);
$this->routedirect('profile');
}

try {
$this->usermanager->add($this->user);
Model::sendflashmessage('password updated successfully', Model::FLASH_SUCCESS);
} catch (Databaseexception $e) {
Model::sendflashmessage($e->getMessage(), Model::FLASH_ERROR);
}
$this->routedirect('profile');
}
Expand Down
35 changes: 23 additions & 12 deletions app/class/Controlleruser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace Wcms;

use Wcms\Exception\Database\Databaseexception;
use RuntimeException;
use Wcms\Exception\Databaseexception;
use Wcms\Exception\Database\Notfoundexception;

class Controlleruser extends Controller
Expand Down Expand Up @@ -41,7 +42,12 @@ public function add()
if ($user->passwordhashed()) {
$user->hashpassword();
}
$this->usermanager->add($user);
try {
$this->usermanager->add($user);
Model::sendflashmessage('User successfully added', Model::FLASH_SUCCESS);
} catch (Databaseexception $e) {
Model::sendflashmessage($e->getMessage(), Model::FLASH_ERROR);
}
$this->routedirect('user');
}
}
Expand All @@ -64,12 +70,17 @@ public function update()

case 'update':
$this->change($_POST);
Model::sendflashmessage('User successfully updated', Model::FLASH_SUCCESS);
$this->routedirect('user');
break;
}
} catch (Databaseexception $e) {
} catch (RuntimeException $e) {
Model::sendflashmessage('Error : ' . $e->getMessage(), Model::FLASH_ERROR);
}
} else {
$this->routedirect('home');
http_response_code(403);
$this->showtemplate('forbidden', []);
exit;
}
}

Expand All @@ -89,6 +100,8 @@ protected function delete(array $datas)

/**
* @throws Notfoundexception If User is not found in the database
* @throws Databaseexception If an error occured with database
* @throws Runtimeexception In case of other various problems
*/
protected function change(array $datas)
{
Expand All @@ -100,25 +113,23 @@ protected function change(array $datas)
&& (empty($userupdate->password())
|| !$userupdate->validpassword())
) {
Model::sendflashmessage('Unvalid password', Model::FLASH_ERROR);
$this->routedirect('user');
} elseif (
throw new RuntimeException('Unvalid password');
}

if (
$user->level() === 10
&& $userupdate->level() !== 10
&& $this->user->id() === $user->id()
) {
Model::sendflashmessage('You cannot quit administration', Model::FLASH_ERROR);
$this->routedirect('user');
throw new RuntimeException('You cannot quit administration job');
} else {
if ($userupdate->password() !== $user->password() && $user->passwordhashed()) {
$userupdate->setpasswordhashed(false);
}
if ($userupdate->passwordhashed() && !$user->passwordhashed()) {
$userupdate->hashpassword();
}
$this->usermanager->add($userupdate);
Model::sendflashmessage('User successfully updated', Model::FLASH_SUCCESS);
$this->routedirect('user');
$this->usermanager->update($userupdate);
}
}
}
2 changes: 1 addition & 1 deletion app/class/Exception/Database/Notfoundexception.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Wcms\Exception\Database;

use RuntimeException;
use Wcms\Exception\Databaseexception;

class Notfoundexception extends Databaseexception
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace Wcms\Exception\Database;
namespace Wcms\Exception;

use RuntimeException;

Expand Down
3 changes: 1 addition & 2 deletions app/class/Modelbookmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@

use InvalidArgumentException;
use JamesMoss\Flywheel\Document;
use PhpParser\Node\Expr\Instanceof_;
use RuntimeException;
use Wcms\Exception\Database\Databaseexception;
use Wcms\Exception\Databaseexception;
use Wcms\Exception\Database\Notfoundexception;

class Modelbookmark extends Modeldb
Expand Down
11 changes: 7 additions & 4 deletions app/class/Modeldb.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use InvalidArgumentException;
use JamesMoss\Flywheel;
use JamesMoss\Flywheel\DocumentInterface;
use JamesMoss\Flywheel\Document;
use RuntimeException;
use Wcms\Flywheel\Formatter\JSON;
use Wcms\Flywheel\Query;
Expand Down Expand Up @@ -44,12 +45,13 @@ protected function isdiskfree(): bool
/**
* Store Document but only if there is enough space left on disk
*
* @param DocumentInterface $document Flywheel Document
* @param Document $document Flywheel Document
* @return bool True in case of success, otherwise false
*
* @todo use exceptions to create a disctinction between differents possible problems
*/
protected function storedoc(DocumentInterface $document): bool {
protected function storedoc(DocumentInterface $document): bool
{
if (!$this->isdiskfree()) {
Logger::error("Not enough free space on disk to store datas in database");
return false;
Expand All @@ -60,12 +62,13 @@ protected function storedoc(DocumentInterface $document): bool {
/**
* Update Document but only if there is enough space left on disk
*
* @param DocumentInterface $document Flywheel Document
* @param Document $document Flywheel Document
* @return bool True in case of success, otherwise false
*
* @todo use exceptions to create a disctinction between differents possible problems
*/
protected function updatedoc(DocumentInterface $document): bool {
protected function updatedoc(DocumentInterface $document): bool
{
if (!$this->isdiskfree()) {
Logger::error("Not enough free space on disk to update datas in database");
return false;
Expand Down
27 changes: 23 additions & 4 deletions app/class/Modeluser.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use InvalidArgumentException;
use JamesMoss\Flywheel\Document;
use Wcms\Exception\Databaseexception;
use Wcms\Exception\Database\Notfoundexception;

class Modeluser extends Modeldb
Expand Down Expand Up @@ -121,17 +122,35 @@ public function passwordcheck(User $user, string $pass): bool
}

/**
* Add a new user in the database
*
* @param User $user
*
* @return bool depending on success
* @throws Databaseexception in case of error
*/
public function add(User $user): void
{
$userdata = new Document($user->dry());
$userdata->setId($user->id());
if (!$this->storedoc($userdata)) {
throw new Databaseexception("Database error while editing user.");
}
}

/**
* Update an user in the database
*
* @param User $user
*
* @todo throw error instead of returning boolean
* @throws Databaseexception in case of error
*/
public function add(User $user): bool
public function update(User $user): void
{
$userdata = new Document($user->dry());
$userdata->setId($user->id());
return $this->storedoc($userdata);
if (!$this->updatedoc($userdata)) {
throw new Databaseexception("Database error while editing user.");
}
}


Expand Down

0 comments on commit af0097e

Please sign in to comment.