/**
* Unban a user.
*
* @param int $UserID The user to unban.
* @param array $Options Options for the unban.
* @since 2.1
*/
public function unBan($UserID, $Options = [])
{
$User = $this->getID($UserID, DATASET_TYPE_ARRAY);
if (!$User) {
throw notFoundException();
}
$Banned = $User['Banned'];
if (!BanModel::isBanned($Banned, BanModel::BAN_AUTOMATIC | BanModel::BAN_MANUAL)) {
throw new Gdn_UserException(t("The user isn't banned.", "The user isn't banned or is banned by some other function."));
}
// Unban the user.
$NewBanned = BanModel::setBanned($Banned, false, BanModel::BAN_AUTOMATIC | BanModel::BAN_MANUAL);
$this->setField($UserID, 'Banned', $NewBanned);
// Restore the user's content.
if (val('RestoreContent', $Options)) {
$BanLogID = $this->getAttribute($UserID, 'BanLogID');
if ($BanLogID) {
$LogModel = new LogModel();
try {
$LogModel->restore($BanLogID);
} catch (Exception $Ex) {
if ($Ex->getCode() != 404) {
throw $Ex;
}
}
$this->saveAttribute($UserID, 'BanLogID', null);
}
}
// Add an activity for the unbanning.
if (val('AddActivity', $Options, true)) {
$ActivityModel = new ActivityModel();
$Story = val('Story', $Options, null);
// Notify the moderators of the unban.
$Activity = ['ActivityType' => 'Ban', 'NotifyUserID' => ActivityModel::NOTIFY_MODS, 'ActivityUserID' => $UserID, 'RegardingUserID' => Gdn::session()->UserID, 'HeadlineFormat' => t('HeadlineFormat.Unban', '{RegardingUserID,You} unbanned {ActivityUserID,you}.'), 'Story' => $Story, 'Data' => ['Unban' => true]];
$ActivityModel->queue($Activity);
// Notify the user of the unban.
$Activity['NotifyUserID'] = $UserID;
$Activity['Emailed'] = ActivityModel::SENT_PENDING;
$Activity['HeadlineFormat'] = t('HeadlineFormat.Unban.Notification', "You've been unbanned.");
$ActivityModel->queue($Activity, false, ['Force' => true]);
$ActivityModel->saveQueue();
}
}