Blacklist

This commit is contained in:
n1rwana 2022-08-27 18:31:02 +03:00
parent 4891030ed0
commit aaad80f4f8
12 changed files with 255 additions and 17 deletions

View file

@ -0,0 +1,31 @@
<?php declare(strict_types=1);
namespace openvk\Web\Models\Entities;
use openvk\Web\Models\RowModel;
use openvk\Web\Util\DateTime;
use openvk\Web\Models\Entities\{User, Manager};
use openvk\Web\Models\Repositories\{Users, Clubs};
class BlacklistItem extends RowModel
{
protected $tableName = "blacklists";
function getId(): int
{
return $this->getRecord()->index;
}
function getAuthor(): ?User
{
return (new Users)->get($this->getRecord()->author);
}
function getTarget(): ?User
{
return (new Users)->get($this->getRecord()->target);
}
function getCreationDate(): DateTime
{
return new DateTime($this->getRecord()->created);
}
}

View file

@ -5,7 +5,7 @@ use openvk\Web\Themes\{Themepack, Themepacks};
use openvk\Web\Util\DateTime;
use openvk\Web\Models\RowModel;
use openvk\Web\Models\Entities\{Photo, Message, Correspondence, Gift};
use openvk\Web\Models\Repositories\{Users, Clubs, Albums, Gifts, Notifications};
use openvk\Web\Models\Repositories\{Users, Clubs, Albums, Gifts, Notifications, Blacklists};
use openvk\Web\Models\Exceptions\InvalidUserNameException;
use Nette\Database\Table\ActiveRow;
use Chandler\Database\DatabaseConnection;
@ -438,6 +438,12 @@ class User extends RowModel
return $permStatus === User::PRIVACY_EVERYONE;
else if($user->getId() === $this->getId())
return true;
else if ((new Blacklists)->isBanned($this, $user)) {
if ($user->isAdmin())
return true;
return false;
}
switch($permStatus) {
case User::PRIVACY_ONLY_FRIENDS:
@ -1018,5 +1024,10 @@ class User extends RowModel
return (bool) $this->getRecord()->activated;
}
function isAdmin(): bool
{
return $this->getChandlerUser()->can("access")->model("admin")->whichBelongsTo(NULL);
}
use Traits\TSubscribable;
}

View file

@ -0,0 +1,37 @@
<?php declare(strict_types=1);
namespace openvk\Web\Models\Repositories;
use openvk\Web\Models\Entities\{User, BlacklistItem};
use openvk\Web\Models\Repositories\{Clubs, Users};
use Nette\Database\Table\ActiveRow;
use Chandler\Database\DatabaseConnection as DB;
class Blacklists
{
private $context;
private $blacklists;
function __construct()
{
$this->context = DB::i()->getContext();
$this->blacklists = $this->context->table("blacklists");
}
function getList(User $user, $page = 1): \Traversable
{
foreach($this->blacklists->where("author", $user->getId())->order("created DESC")->page($page, 10) as $blacklistItem)
yield new BlacklistItem($blacklistItem);
}
function getCount(User $user): int
{
return sizeof($this->blacklists->where("author", $user->getId())->fetch());
}
function isBanned(User $author, User $target): bool
{
if (!$author || !$target)
return FALSE;
return sizeof(DB::i()->getContext()->table("blacklists")->where(["author" => $author->getId(), "target" => $target->getId()])->fetch()) > 0;
}
}

View file

@ -0,0 +1,43 @@
<?php declare(strict_types=1);
namespace openvk\Web\Presenters;
use openvk\Web\Models\Entities\{BlacklistItem};
use openvk\Web\Models\Repositories\{Blacklists, Users};
use Chandler\Database\DatabaseConnection as DB;
final class BlacklistPresenter extends OpenVKPresenter
{
private $blacklists;
function __construct(Blacklists $blacklists)
{
$this->blacklists = $blacklists;
}
function renderAddToBl(): void
{
$this->willExecuteWriteAction();
$this->assertUserLoggedIn();
$record = new BlacklistItem;
$target = (new Users)->get((int) $this->postParam("id"));
$record->setAuthor($this->user->identity->getId());
$record->setTarget($this->postParam("id"));
$record->setCreated(time());
$record->save();
$this->flashFail("succ", "Успех", $target->getCanonicalName() . " занесён в чёрный список.");
}
function renderRemoveFromBl(): void
{
$this->willExecuteWriteAction();
$this->assertUserLoggedIn();
$record = new BlacklistItem(DB::i()->getContext()->table("blacklists")->where([ "author" => $this->user->identity->getId(), "target" => $this->postParam("id") ])->fetch());
$name = $record->getTarget()->getCanonicalName();
$record->delete(FALSE);
$this->flashFail("succ", "Успех", "$name удалён из чёрного списка.");
}
}

View file

@ -1,6 +1,6 @@
<?php declare(strict_types=1);
namespace openvk\Web\Presenters;
use openvk\Web\Models\Repositories\{Users, Notes};
use openvk\Web\Models\Repositories\{Users, Notes, Blacklists};
use openvk\Web\Models\Entities\Note;
final class NotesPresenter extends OpenVKPresenter
@ -18,8 +18,12 @@ final class NotesPresenter extends OpenVKPresenter
{
$user = (new Users)->get($owner);
if(!$user) $this->notFound();
if(!$user->getPrivacyPermission('notes.read', $this->user->identity ?? NULL))
if(!$user->getPrivacyPermission('notes.read', $this->user->identity ?? NULL)) {
if ((new Blacklists)->isBanned($user, $this->user->identity))
$this->flashFail("err", tr("forbidden"), "Пользователь внёс Вас в чёрный список.");
$this->flashFail("err", tr("forbidden"), tr("forbidden_comment"));
}
$this->template->notes = $this->notes->getUserNotes($user, (int)($this->queryParam("p") ?? 1));
$this->template->count = $this->notes->getUserNotesCount($user);

View file

@ -1,7 +1,7 @@
<?php declare(strict_types=1);
namespace openvk\Web\Presenters;
use openvk\Web\Models\Entities\{Club, Photo, Album};
use openvk\Web\Models\Repositories\{Photos, Albums, Users, Clubs};
use openvk\Web\Models\Repositories\{Photos, Albums, Users, Clubs, Blacklists};
use Nette\InvalidStateException as ISE;
final class PhotosPresenter extends OpenVKPresenter
@ -24,8 +24,12 @@ final class PhotosPresenter extends OpenVKPresenter
if($owner > 0) {
$user = $this->users->get($owner);
if(!$user) $this->notFound();
if (!$user->getPrivacyPermission('photos.read', $this->user->identity ?? NULL))
if (!$user->getPrivacyPermission('photos.read', $this->user->identity ?? NULL)) {
if ((new Blacklists)->isBanned($user, $this->user->identity))
$this->flashFail("err", tr("forbidden"), "Пользователь внёс Вас в чёрный список.");
$this->flashFail("err", tr("forbidden"), tr("forbidden_comment"));
}
$this->template->albums = $this->albums->getUserAlbums($user, $this->queryParam("p") ?? 1);
$this->template->count = $this->albums->getUserAlbumsCount($user);
$this->template->owner = $user;
@ -138,8 +142,12 @@ final class PhotosPresenter extends OpenVKPresenter
if($owner > 0 /* bc we currently don't have perms for clubs */) {
$ownerObject = (new Users)->get($owner);
if(!$ownerObject->getPrivacyPermission('photos.read', $this->user->identity ?? NULL))
if(!$ownerObject->getPrivacyPermission('photos.read', $this->user->identity ?? NULL)) {
if ((new Blacklists)->isBanned($ownerObject, $this->user->identity))
$this->flashFail("err", tr("forbidden"), "Пользователь внёс Вас в чёрный список.");
$this->flashFail("err", tr("forbidden"), tr("forbidden_comment"));
}
}
$this->template->album = $album;
@ -158,6 +166,9 @@ final class PhotosPresenter extends OpenVKPresenter
$photo = $this->photos->getByOwnerAndVID($ownerId, $photoId);
if(!$photo || $photo->isDeleted()) $this->notFound();
if ((new Blacklists)->isBanned($photo->getOwner(), $this->user->identity))
$this->flashFail("err", tr("forbidden"), "Пользователь внёс Вас в чёрный список.");
if(!is_null($this->queryParam("from"))) {
if(preg_match("%^album([0-9]++)$%", $this->queryParam("from"), $matches) === 1) {
$album = $this->albums->get((int) $matches[1]);

View file

@ -4,7 +4,7 @@ use openvk\Web\Util\Sms;
use openvk\Web\Themes\Themepacks;
use openvk\Web\Models\Entities\{Photo, Post, EmailChangeVerification};
use openvk\Web\Models\Entities\Notifications\{CoinsTransferNotification, RatingUpNotification};
use openvk\Web\Models\Repositories\{Users, Clubs, Albums, Videos, Notes, Vouchers, EmailChangeVerifications};
use openvk\Web\Models\Repositories\{Users, Clubs, Albums, Videos, Notes, Vouchers, EmailChangeVerifications, Blacklists};
use openvk\Web\Models\Exceptions\InvalidUserNameException;
use openvk\Web\Util\Validator;
use Chandler\Security\Authenticator;
@ -15,12 +15,14 @@ use Nette\Database\UniqueConstraintViolationException;
final class UserPresenter extends OpenVKPresenter
{
private $users;
private $blacklists;
public $deactivationTolerant = false;
function __construct(Users $users)
function __construct(Users $users, Blacklists $blacklists)
{
$this->users = $users;
$this->blacklists = $blacklists;
parent::__construct();
}
@ -28,6 +30,11 @@ final class UserPresenter extends OpenVKPresenter
function renderView(int $id): void
{
$user = $this->users->get($id);
if ($this->user->identity)
if ($this->blacklists->isBanned($user, $this->user->identity) && !$this->user->identity->isAdmin())
$this->flashFail("err", tr("forbidden"), "Пользователь внёс Вас в чёрный список.");
if(!$user || $user->isDeleted()) {
if($user->isDeactivated()) {
$this->template->_template = "User/deactivated.xml";
@ -43,8 +50,11 @@ final class UserPresenter extends OpenVKPresenter
$this->template->videosCount = (new Videos)->getUserVideosCount($user);
$this->template->notes = (new Notes)->getUserNotes($user, 1, 4);
$this->template->notesCount = (new Notes)->getUserNotesCount($user);
$this->template->blacklists = $this->blacklists;
$this->template->user = $user;
$this->template->isBlacklistedThem = $this->blacklists->isBanned($this->user->identity, $user);
$this->template->isBlacklistedByThem = $this->blacklists->isBanned($user, $this->user->identity);
}
}
@ -56,8 +66,12 @@ final class UserPresenter extends OpenVKPresenter
$page = abs($this->queryParam("p") ?? 1);
if(!$user)
$this->notFound();
elseif (!$user->getPrivacyPermission('friends.read', $this->user->identity ?? NULL))
elseif (!$user->getPrivacyPermission('friends.read', $this->user->identity ?? NULL)) {
if ($this->blacklists->isBanned($user, $this->user->identity))
$this->flashFail("err", tr("forbidden"), "Пользователь внёс Вас в чёрный список.");
$this->flashFail("err", tr("forbidden"), tr("forbidden_comment"));
}
else
$this->template->user = $user;
@ -84,8 +98,12 @@ final class UserPresenter extends OpenVKPresenter
$user = $this->users->get($id);
if(!$user)
$this->notFound();
elseif (!$user->getPrivacyPermission('groups.read', $this->user->identity ?? NULL))
elseif (!$user->getPrivacyPermission('groups.read', $this->user->identity ?? NULL)) {
if ($this->blacklists->isBanned($user, $this->user->identity))
$this->flashFail("err", tr("forbidden"), "Пользователь внёс Вас в чёрный список.");
$this->flashFail("err", tr("forbidden"), tr("forbidden_comment"));
}
else {
if($this->queryParam("act") === "managed" && $this->user->id !== $user->getId())
$this->flashFail("err", tr("forbidden"), tr("forbidden_comment"));
@ -454,7 +472,7 @@ final class UserPresenter extends OpenVKPresenter
$this->flash("succ", tr("changes_saved"), tr("changes_saved_comment"));
}
$this->template->mode = in_array($this->queryParam("act"), [
"main", "privacy", "finance", "finance.top-up", "interface"
"main", "privacy", "finance", "finance.top-up", "interface", "blacklist"
]) ? $this->queryParam("act")
: "main";
@ -469,6 +487,11 @@ final class UserPresenter extends OpenVKPresenter
$this->template->qrCodeData = $qrCode[1];
}
if($this->template->mode == "blacklist") {
$this->template->items = $this->blacklists->getList($user);
$this->template->count = $this->blacklists->getCount($user);
}
$this->template->user = $user;
$this->template->themes = Themepacks::i()->getThemeList();
}

View file

@ -1,7 +1,7 @@
<?php declare(strict_types=1);
namespace openvk\Web\Presenters;
use openvk\Web\Models\Entities\Video;
use openvk\Web\Models\Repositories\{Users, Videos};
use openvk\Web\Models\Repositories\{Users, Videos, Blacklists};
use Nette\InvalidStateException as ISE;
final class VideosPresenter extends OpenVKPresenter
@ -21,8 +21,12 @@ final class VideosPresenter extends OpenVKPresenter
{
$user = $this->users->get($id);
if(!$user) $this->notFound();
if(!$user->getPrivacyPermission('videos.read', $this->user->identity ?? NULL))
if(!$user->getPrivacyPermission('videos.read', $this->user->identity ?? NULL)) {
if ((new Blacklists)->isBanned($user, $this->user->identity))
$this->flashFail("err", tr("forbidden"), "Пользователь внёс Вас в чёрный список.");
$this->flashFail("err", tr("forbidden"), tr("forbidden_comment"));
}
$this->template->user = $user;
$this->template->videos = $this->videos->getByUser($user, (int) ($this->queryParam("p") ?? 1));
@ -39,8 +43,12 @@ final class VideosPresenter extends OpenVKPresenter
{
$user = $this->users->get($owner);
if(!$user) $this->notFound();
if(!$user->getPrivacyPermission('videos.read', $this->user->identity ?? NULL))
if(!$user->getPrivacyPermission('videos.read', $this->user->identity ?? NULL)) {
if ((new Blacklists)->isBanned($user, $this->user->identity))
$this->flashFail("err", tr("forbidden"), "Пользователь внёс Вас в чёрный список.");
$this->flashFail("err", tr("forbidden"), tr("forbidden_comment"));
}
if($this->videos->getByOwnerAndVID($owner, $vId)->isDeleted()) $this->notFound();

View file

@ -12,6 +12,7 @@
{var $isFinance = $mode === 'finance'}
{var $isFinanceTU = $mode === 'finance.top-up'}
{var $isInterface = $mode === 'interface'}
{var $isBlackList = $mode === 'blacklist'}
<div class="tabs">
<div n:attr="id => ($isMain ? 'activetabs' : 'ki')" class="tab">
@ -26,6 +27,9 @@
<div n:attr="id => ($isInterface ? 'activetabs' : 'ki')" class="tab">
<a n:attr="id => ($isInterface ? 'act_tab_a' : 'ki')" href="/settings?act=interface">{_interface}</a>
</div>
<div n:attr="id => ($isBlackList ? 'activetabs' : 'ki')" class="tab">
<a n:attr="id => ($isBlackList ? 'act_tab_a' : 'ki')" href="/settings?act=blacklist">Чёрный список</a>
</div>
</div>
<div class="container_gray">
@ -656,6 +660,48 @@
</table>
</form>
{elseif $isBlackList}
{if $count < 1}
{include "../components/nothing.xml"}
{/if}
<div n:foreach="$items as $item" class="content">
<table>
<tbody>
<tr>
<td valign="top">
<a href="/id2">
<img src="{$item->getTarget()->getAvatarURL()}" width="75" alt="Фотография пользователя">
</a>
</td>
<td valign="top" style="width: 100%">
<a href="/id2">
<b>
{$item->getTarget()->getCanonicalName()}
<img n:if="$item->getTarget()->isVerified()" class="name-checkmark" src="/assets/packages/static/openvk/img/checkmark.png">
</b>
</a>
<br>
<table>
<tbody>
<tr>
<td width="120" valign="top"><span class="nobold">Дата добавления:</span></td>
<td>{$item->getCreationDate()}</td>
</tr>
</tbody>
</table>
</td>
<td valign="top" class="action_links" style="width: 150px;">
<form action="/removeFromBl" method="post" class="profile_link_form">
<input type="hidden" name="act" value="rem">
<input type="hidden" name="id" value="{$item->getTarget()->getId()}">
<input type="hidden" name="hash" value="{$csrfToken}">
<input type="submit" class="profile_link" value="удалить из чёрного списка">
</form>
</td>
</tr>
</tbody>
</table>
</div>
{/if}
</div>

View file

@ -139,6 +139,19 @@
{/if}
{/if}
<a n:if="$user->getFollowersCount() > 0" href="/friends{$user->getId()}?act=incoming" class="profile_link">{tr("followers", $user->getFollowersCount())}</a>
{if $isBlacklistedThem}
<form n:if="$thisUser->getId() != $user->getId()" action="/removeFromBl" method="post" class="profile_link_form">
<input type="hidden" name="id" value="{$user->getId()}" />
<input type="hidden" name="hash" value="{$csrfToken}" />
<input type="submit" class="profile_link" value="Удалить из чёрного списка" />
</form>
{else}
<form n:if="$thisUser->getId() != $user->getId()" action="/addToBl" method="post" class="profile_link_form">
<input type="hidden" name="id" value="{$user->getId()}" />
<input type="hidden" name="hash" value="{$csrfToken}" />
<input type="submit" class="profile_link" value="Добавить в чёрный список" />
</form>
{/if}
</div>
<div n:if="isset($thisUser) && !$thisUser->prefersNotToSeeRating()" class="profile-hints">
{var $completeness = $user->getProfileCompletenessReport()}
@ -354,6 +367,11 @@
<div class="right_big_block">
<div class="page_info">
<div n:if="$isBlacklistedByThem AND $thisUser->isAdmin() AND $thisUser->getId() !== $user->getId()" class="user-alert">
<b>Будьте осторожны с этой информацией:</b>
<br/>
Пользователь внёс Вас в чёрный список
</div>
<div n:if="!is_null($alert = $user->getAlert())" class="user-alert">{strpos($alert, "@") === 0 ? tr(substr($alert, 1)) : $alert}</div>
{var $thatIsThisUser = isset($thisUser) && $user->getId() == $thisUser->getId()}
<div n:if="$thatIsThisUser" class="page_status_popup" id="status_editor" style="display: none;">

View file

@ -23,6 +23,7 @@ services:
- openvk\Web\Presenters\AppsPresenter
- openvk\Web\Presenters\ThemepacksPresenter
- openvk\Web\Presenters\VKAPIPresenter
- openvk\Web\Presenters\BlacklistPresenter
- openvk\Web\Models\Repositories\Users
- openvk\Web\Models\Repositories\Posts
- openvk\Web\Models\Repositories\Photos
@ -42,3 +43,4 @@ services:
- openvk\Web\Models\Repositories\Topics
- openvk\Web\Models\Repositories\Applications
- openvk\Web\Models\Repositories\ContentSearchRepository
- openvk\Web\Models\Repositories\Blacklists

View file

@ -97,6 +97,10 @@ routes:
handler: "Group->sub"
- url: "/setSub/v4/club"
handler: "Group->attend"
- url: "/removeFromBl"
handler: "Blacklist->removeFromBl"
- url: "/addToBl"
handler: "Blacklist->addToBl"
- url: "/groups/{num}/setNewOwner/{num}"
handler: "Group->changeOwner"
- url: "/comment{num}/like"