[ticket/9687] Add unbanning to the manager

PHPBB3-9687
This commit is contained in:
Oliver Schramm 2018-09-29 05:00:23 +02:00 committed by Marc Alexander
parent 64af01f528
commit 95de576ccd
No known key found for this signature in database
GPG key ID: 50E0D2423696F995
5 changed files with 140 additions and 18 deletions

View file

@ -95,12 +95,12 @@ class manager
// TODO throw exception // TODO throw exception
} }
if ($ban_mode->get_log_string() !== false) if ($ban_mode->get_ban_log_string() !== false)
{ {
$ban_items_log = implode(', ', $ban_items); $ban_items_log = implode(', ', $ban_items);
$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, $ban_mode->get_log_string(), false, [$reason, $ban_items_log]); $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, $ban_mode->get_ban_log_string(), false, [$reason, $ban_items_log]);
$this->log->add('mod', $this->user->data['user_id'], $this->user->ip, $ban_mode->get_log_string(), false, [ $this->log->add('mod', $this->user->data['user_id'], $this->user->ip, $ban_mode->get_ban_log_string(), false, [
'forum_id' => 0, 'forum_id' => 0,
'topic_id' => 0, 'topic_id' => 0,
$reason, $reason,
@ -172,9 +172,50 @@ class manager
$this->cache->destroy('sql', $this->ban_table); $this->cache->destroy('sql', $this->ban_table);
} }
public function unban($mode, array $items, $reason, $logging = true) public function unban($mode, array $items)
{ {
if (!isset($this->types[$mode]))
{
throw new type_not_found_exception(); // TODO
}
/** @var \phpbb\ban\type\type_interface $ban_mode */
$ban_mode = $this->types[$mode];
$sql_ids = array_map('intval', $items);
$sql = 'SELECT ban_item
FROM ' . $this->ban_table . '
WHERE ' . $this->db->sql_in_set('ban_id', $sql_ids); // TODO (what if empty?)
$result = $this->db->sql_query($sql);
$unbanned_items = [];
while ($row = $this->db->sql_fetchrow($result))
{
$unbanned_items[] = $row['ban_item'];
}
$this->db->sql_freeresult($result);
$sql = 'DELETE FROM ' . $this->ban_table . '
WHERE ' . $this->db->sql_in_set('ban_id', $sql_ids);
$this->db->sql_query($sql);
if ($ban_mode->get_unban_log_string() !== false)
{
$unban_items_log = implode(', ', $unbanned_items);
$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, $ban_mode->get_unban_log_string(), false, [$unban_items_log]);
$this->log->add('mod', $this->user->data['user_id'], $this->user->ip, $ban_mode->get_unban_log_string(), false, [
'forum_id' => 0,
'topic_id' => 0,
$unban_items_log,
]);
}
$unban_data = [
'items' => $unbanned_items,
];
$ban_mode->after_unban($unban_data);
$this->cache->destroy('sql', $this->ban_table);
} }
public function check(array $user_data = []) public function check(array $user_data = [])
@ -183,5 +224,6 @@ class manager
public function tidy() public function tidy()
{ {
// TODO: Delete stale bans
} }
} }

View file

@ -37,12 +37,15 @@ abstract class base implements type_interface
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function after_ban($data) public function after_ban(array $data)
{ {
return true; return true;
} }
public function after_unban() /**
* {@inheritDoc}
*/
public function after_unban(array $data)
{ {
} }

View file

@ -15,11 +15,16 @@ namespace phpbb\ban\type;
class email extends base class email extends base
{ {
public function get_log_string() public function get_ban_log_string()
{ {
return 'LOG_BAN_EMAIL'; return 'LOG_BAN_EMAIL';
} }
public function get_unban_log_string()
{
return 'LOG_UNBAN_EMAIL';
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */

View file

@ -19,12 +19,20 @@ namespace phpbb\ban\type;
interface type_interface interface type_interface
{ {
/** /**
* Returns the language key that's used for the log entry. * Returns the language key that's used for the ban log entry.
* False, if there is none (and thus no logs are created) * False, if there is none (and thus no logs are created)
* *
* @return string|bool * @return string|bool
*/ */
public function get_log_string(); public function get_ban_log_string();
/**
* Returns the language key that's used for the unban log entry.
* False, if thee is none (and thus no logs are created)
*
* @return string|bool
*/
public function get_unban_log_string();
/** /**
* Returns the type identifier for this ban type * Returns the type identifier for this ban type
@ -44,7 +52,7 @@ interface type_interface
public function get_user_column(); public function get_user_column();
/** /**
* Gives the possibility to do some clean up after banning * Gives the possibility to do some clean up after banning.
* Returns true if affected users should be logged out and * Returns true if affected users should be logged out and
* false otherwise * false otherwise
* *
@ -54,9 +62,19 @@ interface type_interface
* *
* @return bool * @return bool
*/ */
public function after_ban($data); public function after_ban(array $data);
public function after_unban(); // ??? /**
* Gives the possiblity to do some clean up after unbanning.
* The return value of this method will be ignored and thus
* should be null
*
* @param array $data An array containing information about
* the unbans, e.g. the unbanned items.
*
* @return null
*/
public function after_unban(array $data);
/** /**
* In the case that get_user_column() returns null, this method * In the case that get_user_column() returns null, this method

View file

@ -22,17 +22,27 @@ class user extends base
protected $log; protected $log;
/** @var string */ /** @var string */
private $log_string = 'LOG_BAN_USER'; private $ban_log_string = 'LOG_BAN_USER';
private $unban_log_string = 'LOG_UNBAN_USER';
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function get_log_string() public function get_ban_log_string()
{ {
// Have to handle logging differently here // Have to handle logging differently here
return false; return false;
} }
/**
* {@inheritDoc}
*/
public function get_unban_log_string()
{
return false;
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@ -52,12 +62,12 @@ class user extends base
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function after_ban($data) public function after_ban(array $data)
{ {
$usernames_log = implode(', ', $this->banned_users); $usernames_log = implode(', ', $this->banned_users);
$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, $this->log_string, false, [$data['reason'], $usernames_log]); $this->log->add('admin', $this->user->data['user_id'], $this->user->ip, $this->ban_log_string, false, [$data['reason'], $usernames_log]);
$this->log->add('mod', $this->user->data['user_id'], $this->user->ip, $this->log_string, false, [ $this->log->add('mod', $this->user->data['user_id'], $this->user->ip, $this->ban_log_string, false, [
'forum_id' => 0, 'forum_id' => 0,
'topic_id' => 0, 'topic_id' => 0,
$data['reason'], $data['reason'],
@ -66,7 +76,7 @@ class user extends base
foreach ($this->banned_users as $user_id => $username) foreach ($this->banned_users as $user_id => $username)
{ {
$this->log->add('user', $this->user->data['user_id'], $this->user->ip, $this->log_string, false, [ $this->log->add('user', $this->user->data['user_id'], $this->user->ip, $this->ban_log_string, false, [
'reportee_id' => $user_id, 'reportee_id' => $user_id,
$data['reason'], $data['reason'],
$usernames_log, $usernames_log,
@ -76,6 +86,50 @@ class user extends base
return true; return true;
} }
/**
* {@inheritDoc}
*/
public function after_unban(array $data)
{
$user_ids = array_map('intval', $data['items']);
$sql = 'SELECT user_id, username
FROM ' . $this->users_table . '
WHERE ' . $this->db->sql_in_set('user_id', $user_ids);
$result = $this->db->sql_query($sql);
$real_user_ids = [];
$usernames = [];
while ($row = $this->db->sql_fetchrow($result))
{
$real_user_ids[] = $row['user_id'];
$usernames[] = $row['username'];
}
$this->db->sql_freeresult($result);
if (empty($usernames))
{
return;
}
$usernames_log = implode(', ', $usernames);
$this->log->add('admin', $this->user->data['user_id'], $this->user->ip, $this->unban_log_string, false, [$usernames_log]);
$this->log->add('mod', $this->user->data['user_id'], $this->user->ip, $this->unban_log_string, false, [
'forum_id' => 0,
'topic_id' => 0,
$usernames_log,
]);
foreach ($real_user_ids as $user_id)
{
$this->log->add('user', $this->user->data['user_id'], $this->user->ip, $this->unban_log_string, false, [
'reportee_id' => $user_id,
$usernames_log,
]);
}
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */