Skip to content

Commit

Permalink
new user from ldap have their author personnal bookmark created
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-peugnet committed Oct 31, 2024
1 parent 43f6537 commit 0698bc3
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 35 deletions.
34 changes: 29 additions & 5 deletions app/class/Controllerconnect.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ protected function login(): void

try {
$this->user = $this->usermanager->get($userid); // May throw DatabaseException
$userisindb = true;
} catch (RuntimeException $e) {
$userisindb = false;
if (Config::ldapuserlevel() > 0) {
$this->user = new User(['password' => null, 'level' => Config::ldapuserlevel(), 'id' => $userid]);
} else {
Expand Down Expand Up @@ -97,8 +99,16 @@ protected function login(): void
$this->usermanager->add($this->user);
$this->servicesession->setuser($this->user->id());
$this->sendflashmessage("Successfully logged in as " . $this->user->id(), self::FLASH_SUCCESS);
} catch (RuntimeException $e) {
$this->sendflashmessage('Error while trying to persist user in database', self::FLASH_ERROR);
Logger::errorex($e);
if (! $userisindb) { // if user was'nt in database, this mean creation failed. In this case, abort.
return;
}
}

if (!empty($_POST['rememberme'])) {
if (!empty($_POST['rememberme'])) {
try {
if ($this->user->cookie() > 0) {
$wsessionid = $this->user->newsession();
$this->modelconnect->createauthcookie(
Expand All @@ -112,11 +122,25 @@ protected function login(): void
$message = "Can't remember you beccause user cookie conservation time is set to 0 days";
$this->sendflashmessage($message, self::FLASH_WARNING);
}
} catch (RuntimeException $e) {
$message = "Can't create authentification cookie : $e";
$this->sendflashmessage($message, self::FLASH_WARNING);
Logger::error($message);
}
}

// if user was not in database before, we can creat it's author personnal bookmark
if (!$userisindb) {
try {
$bookmarkmanager = new Modelbookmark();
$bookmarkmanager->addauthorbookmark($this->user);
} catch (RuntimeException $e) {
$this->sendflashmessage(
'error while creating user\'s personnal author bookmark',
self::FLASH_WARNING
);
Logger::errorex($e);
}
} catch (RuntimeException $e) {
$message = "Can't create authentification cookie : $e";
$this->sendflashmessage($message, self::FLASH_WARNING);
Logger::error($message);
}
}
}
Expand Down
41 changes: 11 additions & 30 deletions app/class/Controlleruser.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,18 @@ public function add()
$this->sendflashmessage('User successfully added', self::FLASH_SUCCESS);
} catch (Databaseexception $e) {
$this->sendflashmessage($e->getMessage(), self::FLASH_ERROR);
Logger::errorex($e);
}
try {
$bookmarkmanager = new Modelbookmark();
$bookmarkmanager->addauthorbookmark($user);
} catch (RuntimeException $e) {
$this->sendflashmessage(
'error while creating user\'s personnal author bookmark',
self::FLASH_WARNING
);
Logger::errorex($e);
}
$this->addauthorbookmark($user);
$this->routedirect('user');
}
}
Expand Down Expand Up @@ -137,33 +147,4 @@ protected function update(array $datas): void
$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) {
$this->sendflashmessage(
'Could not create personnal user author bookmark: ' . $e->getMessage(),
self::FLASH_ERROR
);
}
}
}
22 changes: 22 additions & 0 deletions app/class/Modelbookmark.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,28 @@ public function update(Bookmark $bookmark): Bookmark
return $bookmark;
}

/**
* Create a bookmark that filter pages where the given user is an author.
*
* @param User $user The concerned user (need to be already added in database)
*
* @throws RuntimeException If the process failed
*/
public function addauthorbookmark(User $user): void
{
$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());
$this->add($userbookmark);
}

/**
* @param Bookmark|string $id string ID or bookmark
*
Expand Down

0 comments on commit 0698bc3

Please sign in to comment.