From aaad80f4f86e54dc0a3b703c036cd0d282eee4ad Mon Sep 17 00:00:00 2001 From: n1rwana <93197434+n1rwana@users.noreply.github.com> Date: Sat, 27 Aug 2022 18:31:02 +0300 Subject: [PATCH] Blacklist --- Web/Models/Entities/BlacklistItem.php | 31 ++++++++++++++ Web/Models/Entities/User.php | 13 +++++- Web/Models/Repositories/Blacklists.php | 37 +++++++++++++++++ Web/Presenters/BlacklistPresenter.php | 43 +++++++++++++++++++ Web/Presenters/NotesPresenter.php | 8 +++- Web/Presenters/PhotosPresenter.php | 19 +++++++-- Web/Presenters/UserPresenter.php | 35 +++++++++++++--- Web/Presenters/VideosPresenter.php | 14 +++++-- Web/Presenters/templates/User/Settings.xml | 48 +++++++++++++++++++++- Web/Presenters/templates/User/View.xml | 18 ++++++++ Web/di.yml | 2 + Web/routes.yml | 4 ++ 12 files changed, 255 insertions(+), 17 deletions(-) create mode 100644 Web/Models/Entities/BlacklistItem.php create mode 100644 Web/Models/Repositories/Blacklists.php create mode 100644 Web/Presenters/BlacklistPresenter.php diff --git a/Web/Models/Entities/BlacklistItem.php b/Web/Models/Entities/BlacklistItem.php new file mode 100644 index 00000000..fcc19452 --- /dev/null +++ b/Web/Models/Entities/BlacklistItem.php @@ -0,0 +1,31 @@ +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); + } +} \ No newline at end of file diff --git a/Web/Models/Entities/User.php b/Web/Models/Entities/User.php index 44db0cd9..d96e3a4b 100644 --- a/Web/Models/Entities/User.php +++ b/Web/Models/Entities/User.php @@ -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: @@ -1017,6 +1023,11 @@ class User extends RowModel { return (bool) $this->getRecord()->activated; } + + function isAdmin(): bool + { + return $this->getChandlerUser()->can("access")->model("admin")->whichBelongsTo(NULL); + } use Traits\TSubscribable; } diff --git a/Web/Models/Repositories/Blacklists.php b/Web/Models/Repositories/Blacklists.php new file mode 100644 index 00000000..cf267afe --- /dev/null +++ b/Web/Models/Repositories/Blacklists.php @@ -0,0 +1,37 @@ +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; + } +} \ No newline at end of file diff --git a/Web/Presenters/BlacklistPresenter.php b/Web/Presenters/BlacklistPresenter.php new file mode 100644 index 00000000..74ec4a37 --- /dev/null +++ b/Web/Presenters/BlacklistPresenter.php @@ -0,0 +1,43 @@ +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 удалён из чёрного списка."); + } +} \ No newline at end of file diff --git a/Web/Presenters/NotesPresenter.php b/Web/Presenters/NotesPresenter.php index 363d814c..d634631f 100644 --- a/Web/Presenters/NotesPresenter.php +++ b/Web/Presenters/NotesPresenter.php @@ -1,6 +1,6 @@ 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); diff --git a/Web/Presenters/PhotosPresenter.php b/Web/Presenters/PhotosPresenter.php index eacf76d4..508ecd51 100644 --- a/Web/Presenters/PhotosPresenter.php +++ b/Web/Presenters/PhotosPresenter.php @@ -1,7 +1,7 @@ 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; @@ -157,7 +165,10 @@ 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]); diff --git a/Web/Presenters/UserPresenter.php b/Web/Presenters/UserPresenter.php index 6871b90e..d8fe419c 100644 --- a/Web/Presenters/UserPresenter.php +++ b/Web/Presenters/UserPresenter.php @@ -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"; @@ -468,6 +486,11 @@ final class UserPresenter extends OpenVKPresenter $this->template->qrCodeType = substr($qrCode[0], 5); $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(); diff --git a/Web/Presenters/VideosPresenter.php b/Web/Presenters/VideosPresenter.php index e7b24344..dd4586a9 100644 --- a/Web/Presenters/VideosPresenter.php +++ b/Web/Presenters/VideosPresenter.php @@ -1,7 +1,7 @@ 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(); diff --git a/Web/Presenters/templates/User/Settings.xml b/Web/Presenters/templates/User/Settings.xml index f5125d8c..ff02fd06 100644 --- a/Web/Presenters/templates/User/Settings.xml +++ b/Web/Presenters/templates/User/Settings.xml @@ -12,6 +12,7 @@ {var $isFinance = $mode === 'finance'} {var $isFinanceTU = $mode === 'finance.top-up'} {var $isInterface = $mode === 'interface'} +{var $isBlackList = $mode === 'blacklist'}
@@ -26,6 +27,9 @@ +
@@ -655,7 +659,49 @@ - + + {elseif $isBlackList} + {if $count < 1} + {include "../components/nothing.xml"} + {/if} +
+ + + + + + + + +
+ + Фотография пользователя + + + + + {$item->getTarget()->getCanonicalName()} + + + +
+ + + + + + + +
Дата добавления:{$item->getCreationDate()}
+
+
{/if}
diff --git a/Web/Presenters/templates/User/View.xml b/Web/Presenters/templates/User/View.xml index a2b8cb0d..5a3881fa 100644 --- a/Web/Presenters/templates/User/View.xml +++ b/Web/Presenters/templates/User/View.xml @@ -139,6 +139,19 @@ {/if} {/if} {tr("followers", $user->getFollowersCount())} + {if $isBlacklistedThem} + + {else} + + {/if}
{var $completeness = $user->getProfileCompletenessReport()} @@ -354,6 +367,11 @@
+
+ Будьте осторожны с этой информацией: +
+ Пользователь внёс Вас в чёрный список +
{strpos($alert, "@") === 0 ? tr(substr($alert, 1)) : $alert}
{var $thatIsThisUser = isset($thisUser) && $user->getId() == $thisUser->getId()}