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 1/7] 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()}
diff --git a/Web/Presenters/templates/User/View.xml b/Web/Presenters/templates/User/View.xml index 5a3881fa..f046d7a9 100644 --- a/Web/Presenters/templates/User/View.xml +++ b/Web/Presenters/templates/User/View.xml @@ -368,9 +368,9 @@
- Будьте осторожны с этой информацией: + {admin_privacy_warning}:
- Пользователь внёс Вас в чёрный список + {_user_blacklisted_you}
{strpos($alert, "@") === 0 ? tr(substr($alert, 1)) : $alert}
{var $thatIsThisUser = isset($thisUser) && $user->getId() == $thisUser->getId()} diff --git a/Web/di.yml b/Web/di.yml index 8503a266..29cb1f2b 100644 --- a/Web/di.yml +++ b/Web/di.yml @@ -43,4 +43,4 @@ services: - openvk\Web\Models\Repositories\Topics - openvk\Web\Models\Repositories\Applications - openvk\Web\Models\Repositories\ContentSearchRepository - - openvk\Web\Models\Repositories\Blacklists \ No newline at end of file + - openvk\Web\Models\Repositories\Blacklists diff --git a/Web/routes.yml b/Web/routes.yml index 716dd81c..1f90f1d8 100644 --- a/Web/routes.yml +++ b/Web/routes.yml @@ -98,9 +98,9 @@ routes: - url: "/setSub/v4/club" handler: "Group->attend" - url: "/removeFromBl" - handler: "Blacklist->removeFromBl" + handler: "Blacklist->removeFromBlacklist" - url: "/addToBl" - handler: "Blacklist->addToBl" + handler: "Blacklist->addToBlacklist" - url: "/groups/{num}/setNewOwner/{num}" handler: "Group->changeOwner" - url: "/comment{num}/like" diff --git a/locales/ru.strings b/locales/ru.strings index 0e7b7806..2743d186 100644 --- a/locales/ru.strings +++ b/locales/ru.strings @@ -1111,6 +1111,8 @@ "admin_commerce_disabled" = "Коммерция отключена системным администратором"; "admin_commerce_disabled_desc" = "Настройки ваучеров и подарков будут сохранены, но не будут оказывать никакого влияния."; +"admin_privacy_warning" = "Будьте осторожны с этой информацией"; + /* Paginator (deprecated) */ "paginator_back" = "Назад"; @@ -1174,3 +1176,8 @@ "cookies_popup_content" = "Все дети любят печенье, поэтому этот веб-сайт использует Cookies для того, чтобы идентифицировать вашу сессию и ничего более. Ознакомьтесь с нашей политикой конфиденциальности для получения дополнительной информации."; "cookies_popup_agree" = "Согласен"; + +/* Blacklist */ + +"blacklist" = "Чёрный список"; +"user_blacklisted_you" = "Пользователь внёс Вас в чёрный список."; \ No newline at end of file From c111bd9847b50ce144c9ad94b8d15f8a6b313248 Mon Sep 17 00:00:00 2001 From: n1rwana <93197434+n1rwana@users.noreply.github.com> Date: Sun, 4 Sep 2022 00:30:06 +0300 Subject: [PATCH 6/7] Update en.strings --- locales/en.strings | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/locales/en.strings b/locales/en.strings index a0825392..aa840738 100644 --- a/locales/en.strings +++ b/locales/en.strings @@ -1062,6 +1062,8 @@ "admin_commerce_disabled" = "Commerce has been disabled by the system administrator"; "admin_commerce_disabled_desc" = "The voucher and gift settings will be saved, but will have no effect."; +"admin_privacy_warning" = "Be careful with this information"; + /* Paginator (deprecated) */ "paginator_back" = "Back"; @@ -1115,3 +1117,8 @@ "cookies_popup_content" = "All kids love cookie, so this website uses Cookies to identify your session and nothing more. Check our privacy policy for more information."; "cookies_popup_agree" = "Accept"; + +/* Blacklist */ + +"blacklist" = "Blacklist"; +"user_blacklisted_you" = "This user has blacklisted you."; \ No newline at end of file From b1848c3afa54db761bd03a100602e02fc18c6faa Mon Sep 17 00:00:00 2001 From: n1rwana <93197434+n1rwana@users.noreply.github.com> Date: Mon, 5 Sep 2022 22:30:01 +0300 Subject: [PATCH 7/7] ok 2.0 --- Web/Presenters/BlacklistPresenter.php | 5 ++--- locales/en.strings | 2 ++ locales/ru.strings | 3 +++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/Web/Presenters/BlacklistPresenter.php b/Web/Presenters/BlacklistPresenter.php index ede24240..37ed36ec 100644 --- a/Web/Presenters/BlacklistPresenter.php +++ b/Web/Presenters/BlacklistPresenter.php @@ -26,7 +26,7 @@ final class BlacklistPresenter extends OpenVKPresenter $record->setCreated(time()); $record->save(); - $this->flashFail("succ", "Успех", $target->getCanonicalName() . " занесён в чёрный список."); + $this->flashFail("succ", tr("success"), tr("user_blacklisted", $target->getCanonicalName())); } function renderRemoveFromBlacklist(): void @@ -35,10 +35,9 @@ final class BlacklistPresenter extends OpenVKPresenter $this->assertUserLoggedIn(); $record = $this->blacklists->getByAuthorAndTarget($this->user->identity->getId(), $this->postParam("id")); - //$record = new BlacklistItem(DB::i()->getContext()->table("blacklists")->where([ "author" => $this->user->identity->getId(), "target" => ])->fetch()); $name = $record->getTarget()->getCanonicalName(); $record->delete(false); - $this->flashFail("succ", "Успех", "$name удалён из чёрного списка."); + $this->flashFail("succ", tr("success"), tr("user_removed_from_the_blacklist", $name)); } } diff --git a/locales/en.strings b/locales/en.strings index cf6a53b0..017420ec 100644 --- a/locales/en.strings +++ b/locales/en.strings @@ -1145,6 +1145,8 @@ "blacklist" = "Blacklist"; "user_blacklisted_you" = "This user has blacklisted you."; +"user_blacklisted" = "$1 has been blacklisted" +"user_removed_from_the_blacklist" = "$1 has been removed from the blacklist." /* Away */ diff --git a/locales/ru.strings b/locales/ru.strings index 6081fdfe..960714d4 100644 --- a/locales/ru.strings +++ b/locales/ru.strings @@ -1187,6 +1187,7 @@ "edit_action" = "Изменить"; "transfer" = "Передать"; "close" = "Закрыть"; +"success" = "Успех"; "warning" = "Внимание"; "question_confirm" = "Это действие нельзя отменить. Вы действительно уверены в том что хотите сделать?"; @@ -1204,6 +1205,8 @@ "blacklist" = "Чёрный список"; "user_blacklisted_you" = "Пользователь внёс Вас в чёрный список."; +"user_blacklisted" = "$1 занесён в чёрный список." +"user_removed_from_the_blacklist" = "$1 удалён из чёрного списка." /* Away */