Skip to content

Commit

Permalink
When an user is created, a personnal bookmark is created that filter …
Browse files Browse the repository at this point in the history
…user as author, close  #433

+ various refactor accross controlleruser
+ renamed route userupdate to useredit
  • Loading branch information
vincent-peugnet committed Oct 13, 2024
1 parent 0c8bbb7 commit 659d674
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 19 deletions.
68 changes: 51 additions & 17 deletions app/class/Controlleruser.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,28 @@ public function add()
if ($this->user->isadmin() && isset($_POST['id'])) {
$user = new User($_POST);
if (empty($user->id()) || $this->usermanager->exist($user)) {
$this->routedirect('user', [], ['error' => 'wrong_id']);
} elseif (empty($user->password()) || !$user->validpassword()) {
$this->routedirect('user', [], ['error' => 'change_password']);
} else {
if ($user->passwordhashed()) {
$user->hashpassword();
}
try {
$this->usermanager->add($user);
Model::sendflashmessage('User successfully added', Model::FLASH_SUCCESS);
} catch (Databaseexception $e) {
Model::sendflashmessage($e->getMessage(), Model::FLASH_ERROR);
}
Model::sendflashmessage('Error: problem with ID', Model::FLASH_ERROR);
$this->routedirect('user');
}
if (!$user->validpassword()) {
Model::sendflashmessage('Error: invalid password', Model::FLASH_ERROR);
$this->routedirect('user');
}
if ($user->passwordhashed()) {
$user->hashpassword();
}
try {
$this->usermanager->add($user);
Model::sendflashmessage('User successfully added', Model::FLASH_SUCCESS);
} catch (Databaseexception $e) {
Model::sendflashmessage($e->getMessage(), Model::FLASH_ERROR);
}
$this->addauthorbookmark($user);
$this->routedirect('user');
}
}

public function update()
public function edit()
{
if ($this->user->isadmin() && isset($_POST['action'])) {
try {
Expand All @@ -69,7 +72,7 @@ public function update()
break;

case 'update':
$this->change($_POST);
$this->update($_POST);
Model::sendflashmessage('User successfully updated', Model::FLASH_SUCCESS);
$this->routedirect('user');
break;
Expand All @@ -84,10 +87,12 @@ public function update()
}
}

// ________________________________ F U N _________________________________________

/**
* @throws Notfoundexception If user is not found in the database
*/
protected function delete(array $datas)
protected function delete(array $datas): void
{
$user = new User($datas);
$user = $this->usermanager->get($user);
Expand All @@ -103,7 +108,7 @@ protected function delete(array $datas)
* @throws Databaseexception If an error occured with database
* @throws Runtimeexception In case of other various problems
*/
protected function change(array $datas)
protected function update(array $datas): void
{
$user = $this->usermanager->get($datas['id']);
$userupdate = clone $user;
Expand Down Expand Up @@ -132,4 +137,33 @@ protected function change(array $datas)
$this->usermanager->update($userupdate);
}
}

/**
* Create a bookmark that filter pages where the user is an author.
* Send a flash message in case of error.
*
* @param User $user The concerned user (need to be already added in database)
*/
protected function addauthorbookmark(User $user): void
{
try {
$bookmarkmanager = new Modelbookmark();
$userbookmark = new Bookmark();
$uid = $user->id();
$userbookmark->init(
"$uid-is-author",
"?authorfilter[0]=$uid&submit=filter",
'👤',
"$uid's pages",
"Pages where $uid is listed as an author",
);
$userbookmark->setuser($user->id());
$bookmarkmanager->add($userbookmark);
} catch (RuntimeException $e) {
Model::sendflashmessage(
'Could not create personnal user author bookmark: ' . $e->getMessage(),
Model::FLASH_ERROR
);
}
}
}
2 changes: 1 addition & 1 deletion app/class/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function match()
['POST', '/!admin/database', 'Controlleradmin#database', 'admindatabase'],
['GET', '/!user', 'Controlleruser#desktop', 'user'],
['POST', '/!user/add', 'Controlleruser#add', 'useradd'],
['POST', '/!user/update', 'Controlleruser#update', 'userupdate'],
['POST', '/!user/edit', 'Controlleruser#edit', 'useredit'],
['GET', '/!profile', 'Controllerprofile#desktop', 'profile'],
['POST', '/!profile', 'Controllerprofile#update', 'profileupdate'],
['POST', '/!profile/password', 'Controllerprofile#password', 'profilepassword'],
Expand Down
2 changes: 1 addition & 1 deletion app/view/templates/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
?>

<tr>
<form action="<?= $this->url('userupdate') ?>" method="post">
<form action="<?= $this->url('useredit') ?>" method="post">

<td>
<?= $user->id() ?>
Expand Down

0 comments on commit 659d674

Please sign in to comment.