Extract GET action processing in Moderation modules

This commit is contained in:
Hypolite Petovan 2025-01-22 21:51:03 -05:00
parent 3e465cb761
commit cbb6b7819a
3 changed files with 139 additions and 100 deletions

View file

@ -48,38 +48,8 @@ class Active extends BaseUsers
{
parent::content();
$action = (string) $this->parameters['action'] ?? '';
$uid = (int) $this->parameters['uid'] ?? 0;
$this->processGetActions();
if ($uid !== 0) {
$user = User::getById($uid, ['username', 'blocked']);
if (!$user) {
$this->systemMessages->addNotice($this->t('User not found'));
$this->baseUrl->redirect('moderation/users');
}
}
switch ($action) {
case 'delete':
if ($this->session->getLocalUserId() != $uid) {
self::checkFormSecurityTokenRedirectOnError('moderation/users/active', 'moderation_users_active', 't');
// delete user
User::remove($uid);
$this->systemMessages->addNotice($this->t('User "%s" deleted', $user['username']));
} else {
$this->systemMessages->addNotice($this->t('You can\'t remove yourself'));
}
$this->baseUrl->redirect('moderation/users/active');
break;
case 'block':
self::checkFormSecurityTokenRedirectOnError('moderation/users/active', 'moderation_users_active', 't');
User::block($uid);
$this->systemMessages->addNotice($this->t('User "%s" blocked', $user['username']));
$this->baseUrl->redirect('moderation/users/active');
break;
}
$pager = new Pager($this->l10n, $this->args->getQueryString(), 100);
$valid_orders = [
@ -143,4 +113,48 @@ class Active extends BaseUsers
'$pager' => $pager->renderFull($count),
]);
}
/**
* @return void
* @throws \Friendica\Network\HTTPException\FoundException
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \Friendica\Network\HTTPException\MovedPermanentlyException
* @throws \Friendica\Network\HTTPException\NotFoundException
* @throws \Friendica\Network\HTTPException\TemporaryRedirectException
*/
private function processGetActions(): void
{
$action = (string)$this->parameters['action'] ?? '';
$uid = (int)$this->parameters['uid'] ?? 0;
if ($uid === 0) {
return;
}
$user = User::getById($uid, ['username']);
if (!$user) {
$this->systemMessages->addNotice($this->t('User not found'));
$this->baseUrl->redirect('moderation/users');
}
switch ($action) {
case 'delete':
if ($this->session->getLocalUserId() != $uid) {
self::checkFormSecurityTokenRedirectOnError('moderation/users/active', 'moderation_users_active', 't');
// delete user
User::remove($uid);
$this->systemMessages->addNotice($this->t('User "%s" deleted', $user['username']));
} else {
$this->systemMessages->addNotice($this->t('You can\'t remove yourself'));
}
$this->baseUrl->redirect('moderation/users/active');
case 'block':
self::checkFormSecurityTokenRedirectOnError('moderation/users/active', 'moderation_users_active', 't');
User::block($uid);
$this->systemMessages->addNotice($this->t('User "%s" blocked', $user['username']));
$this->baseUrl->redirect('moderation/users/active');
}
}
}

View file

@ -48,37 +48,7 @@ class Blocked extends BaseUsers
{
parent::content();
$action = (string) $this->parameters['action'] ?? '';
$uid = (int) $this->parameters['uid'] ?? 0;
if ($uid !== 0) {
$user = User::getById($uid, ['username', 'blocked']);
if (!$user) {
$this->systemMessages->addNotice($this->t('User not found'));
$this->baseUrl->redirect('moderation/users');
}
}
switch ($action) {
case 'delete':
if ($this->session->getLocalUserId() != $uid) {
self::checkFormSecurityTokenRedirectOnError('/moderation/users/blocked', 'moderation_users_blocked', 't');
// delete user
User::remove($uid);
$this->systemMessages->addNotice($this->t('User "%s" deleted', $user['username']));
} else {
$this->systemMessages->addNotice($this->t('You can\'t remove yourself'));
}
$this->baseUrl->redirect('moderation/users/blocked');
break;
case 'unblock':
self::checkFormSecurityTokenRedirectOnError('/moderation/users/blocked', 'moderation_users_blocked', 't');
User::block($uid, false);
$this->systemMessages->addNotice($this->t('User "%s" unblocked', $user['username']));
$this->baseUrl->redirect('moderation/users/blocked');
break;
}
$this->processGetActions();
$pager = new Pager($this->l10n, $this->args->getQueryString(), 100);
@ -142,4 +112,47 @@ class Blocked extends BaseUsers
'$pager' => $pager->renderFull($count)
]);
}
/**
* @return void
* @throws \Friendica\Network\HTTPException\FoundException
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \Friendica\Network\HTTPException\MovedPermanentlyException
* @throws \Friendica\Network\HTTPException\NotFoundException
* @throws \Friendica\Network\HTTPException\TemporaryRedirectException
*/
private function processGetActions(): void
{
$action = (string)$this->parameters['action'] ?? '';
$uid = (int)$this->parameters['uid'] ?? 0;
if ($uid === 0) {
return;
}
$user = User::getById($uid, ['username']);
if (!$user) {
$this->systemMessages->addNotice($this->t('User not found'));
$this->baseUrl->redirect('moderation/users');
}
switch ($action) {
case 'delete':
if ($this->session->getLocalUserId() != $uid) {
self::checkFormSecurityTokenRedirectOnError('/moderation/users/blocked', 'moderation_users_blocked', 't');
// delete user
User::remove($uid);
$this->systemMessages->addNotice($this->t('User "%s" deleted', $user['username']));
} else {
$this->systemMessages->addNotice($this->t('You can\'t remove yourself'));
}
$this->baseUrl->redirect('moderation/users/blocked');
case 'unblock':
self::checkFormSecurityTokenRedirectOnError('/moderation/users/blocked', 'moderation_users_blocked', 't');
User::block($uid, false);
$this->systemMessages->addNotice($this->t('User "%s" unblocked', $user['username']));
$this->baseUrl->redirect('moderation/users/blocked');
}
}
}

View file

@ -55,44 +55,7 @@ class Index extends BaseUsers
{
parent::content();
$action = (string) $this->parameters['action'] ?? '';
$uid = (int) $this->parameters['uid'] ?? 0;
if ($uid !== 0) {
$user = User::getById($uid, ['username', 'blocked']);
if (!$user) {
$this->systemMessages->addNotice($this->t('User not found'));
$this->baseUrl->redirect('moderation/users');
}
}
switch ($action) {
case 'delete':
if ($this->session->getLocalUserId() != $uid) {
self::checkFormSecurityTokenRedirectOnError($this->baseUrl, 'moderation_users', 't');
// delete user
User::remove($uid);
$this->systemMessages->addNotice($this->t('User "%s" deleted', $user['username']));
} else {
$this->systemMessages->addNotice($this->t('You can\'t remove yourself'));
}
$this->baseUrl->redirect('moderation/users');
break;
case 'block':
self::checkFormSecurityTokenRedirectOnError('moderation/users', 'moderation_users', 't');
User::block($uid);
$this->systemMessages->addNotice($this->t('User "%s" blocked', $user['username']));
$this->baseUrl->redirect('moderation/users');
break;
case 'unblock':
self::checkFormSecurityTokenRedirectOnError('moderation/users', 'moderation_users', 't');
User::block($uid, false);
$this->systemMessages->addNotice($this->t('User "%s" unblocked', $user['username']));
$this->baseUrl->redirect('moderation/users');
break;
}
$this->processGetActions();
$pager = new Pager($this->l10n, $this->args->getQueryString(), 100);
@ -161,4 +124,53 @@ class Index extends BaseUsers
'$pager' => $pager->renderFull($count),
]);
}
/**
* @return void
* @throws \Friendica\Network\HTTPException\FoundException
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \Friendica\Network\HTTPException\MovedPermanentlyException
* @throws \Friendica\Network\HTTPException\NotFoundException
* @throws \Friendica\Network\HTTPException\TemporaryRedirectException
*/
private function processGetActions(): void
{
$action = (string) $this->parameters['action'] ?? '';
$uid = (int) $this->parameters['uid'] ?? 0;
if ($uid === 0) {
return;
}
$user = User::getById($uid, ['username']);
if (!$user) {
$this->systemMessages->addNotice($this->t('User not found'));
$this->baseUrl->redirect('moderation/users');
}
switch ($action) {
case 'delete':
if ($this->session->getLocalUserId() != $uid) {
self::checkFormSecurityTokenRedirectOnError($this->baseUrl, 'moderation_users', 't');
// delete user
User::remove($uid);
$this->systemMessages->addNotice($this->t('User "%s" deleted', $user['username']));
} else {
$this->systemMessages->addNotice($this->t('You can\'t remove yourself'));
}
$this->baseUrl->redirect('moderation/users');
case 'block':
self::checkFormSecurityTokenRedirectOnError('moderation/users', 'moderation_users', 't');
User::block($uid);
$this->systemMessages->addNotice($this->t('User "%s" blocked', $user['username']));
$this->baseUrl->redirect('moderation/users');
case 'unblock':
self::checkFormSecurityTokenRedirectOnError('moderation/users', 'moderation_users', 't');
User::block($uid, false);
$this->systemMessages->addNotice($this->t('User "%s" unblocked', $user['username']));
$this->baseUrl->redirect('moderation/users');
}
}
}