diff --git a/Web/Models/Repositories/ChandlerGroups.php b/Web/Models/Repositories/ChandlerGroups.php new file mode 100644 index 00000000..4f895aea --- /dev/null +++ b/Web/Models/Repositories/ChandlerGroups.php @@ -0,0 +1,48 @@ +context = DB::i()->getContext(); + $this->groups = $this->context->table("chandlergroups"); + $this->members = $this->context->table("chandleraclrelations"); + $this->perms = $this->context->table("chandleraclgroupspermissions"); + } + + function get(string $UUID): ?ActiveRow + { + return $this->groups->where("id", $UUID)->fetch(); + } + + function getList(): \Traversable + { + foreach($this->groups as $group) yield $group; + } + + function getMembersById(string $UUID): \Traversable + { + foreach($this->members->where("group", $UUID) as $member) + yield (new Users)->getByChandlerUser( + new ChandlerUser($this->context->table("chandlerusers")->where("id", $member->user)->fetch()) + ); + } + + function getUsersMemberships(string $UUID): \Traversable + { + foreach($this->members->where("user", $UUID) as $member) yield $member; + } + + function getPermissionsById(string $UUID): \Traversable + { + foreach($this->perms->where("group", $UUID) as $perm) yield $perm; + } +} diff --git a/Web/Models/Repositories/ChandlerUsers.php b/Web/Models/Repositories/ChandlerUsers.php new file mode 100644 index 00000000..dbe0197a --- /dev/null +++ b/Web/Models/Repositories/ChandlerUsers.php @@ -0,0 +1,39 @@ +context = DB::i()->getContext(); + $this->users = $this->context->table("chandlerusers"); + } + + private function toUser(?ActiveRow $ar): ?ChandlerUser + { + return is_null($ar) ? NULL : (new User($ar))->getChandlerUser(); + } + + function get(int $id): ?ChandlerUser + { + return (new Users)->get($id)->getChandlerUser(); + } + + function getById(string $UUID): ?ChandlerUser + { + return new ChandlerUser($this->users->where("id", $UUID)->fetch()); + } + + function getList(int $page = 1): \Traversable + { + foreach($this->users as $user) + yield new ChandlerUser($user); + } +} diff --git a/Web/Presenters/AdminPresenter.php b/Web/Presenters/AdminPresenter.php index ceda6f6b..b66b651a 100644 --- a/Web/Presenters/AdminPresenter.php +++ b/Web/Presenters/AdminPresenter.php @@ -1,7 +1,7 @@ users = $users; $this->clubs = $clubs; $this->vouchers = $vouchers; $this->gifts = $gifts; $this->bannedLinks = $bannedLinks; + $this->chandlerGroups = $chandlerGroups; parent::__construct(); } @@ -62,7 +64,9 @@ final class AdminPresenter extends OpenVKPresenter $this->notFound(); $this->template->user = $user; - + $this->template->c_groups_list = (new ChandlerGroups)->getList(); + $this->template->c_memberships = $this->chandlerGroups->getUsersMemberships($user->getChandlerGUID()); + if($_SERVER["REQUEST_METHOD"] !== "POST") return; @@ -78,8 +82,13 @@ final class AdminPresenter extends OpenVKPresenter $user->changeEmail($this->postParam("email")); if($user->onlineStatus() != $this->postParam("online")) $user->setOnline(intval($this->postParam("online"))); $user->setVerified(empty($this->postParam("verify") ? 0 : 1)); + if($this->postParam("add-to-group")) { + $query = "INSERT INTO `chandleraclrelations` (`user`, `group`) VALUES ('" . $user->getChandlerGUID() . "', '" . $this->postParam("add-to-group") . "')"; + DatabaseConnection::i()->getConnection()->query($query); + } $user->save(); + break; } } @@ -447,4 +456,95 @@ final class AdminPresenter extends OpenVKPresenter $this->redirect("/admin/bannedLinks"); } + + function renderChandlerGroups(): void + { + $this->template->groups = (new ChandlerGroups)->getList(); + + if($_SERVER["REQUEST_METHOD"] !== "POST") + return; + + $req = "INSERT INTO `chandlergroups` (`name`) VALUES ('" . $this->postParam("name") . "')"; + DatabaseConnection::i()->getConnection()->query($req); + } + + function renderChandlerGroup(string $UUID): void + { + $DB = DatabaseConnection::i()->getConnection(); + + if(is_null($DB->query("SELECT * FROM `chandlergroups` WHERE `id` = '$UUID'")->fetch())) + $this->flashFail("err", tr("error"), tr("c_group_not_found")); + + $this->template->group = (new ChandlerGroups)->get($UUID); + $this->template->mode = in_array( + $this->queryParam("act"), + [ + "main", + "members", + "permissions", + "removeMember", + "removePermission", + "delete" + ]) ? $this->queryParam("act") : "main"; + $this->template->members = (new ChandlerGroups)->getMembersById($UUID); + $this->template->perms = (new ChandlerGroups)->getPermissionsById($UUID); + + if($this->template->mode == "removeMember") { + $where = "`user` = '" . $this->queryParam("uid") . "' AND `group` = '$UUID'"; + + if(is_null($DB->query("SELECT * FROM `chandleraclrelations` WHERE " . $where)->fetch())) + $this->flashFail("err", tr("error"), tr("c_user_is_not_in_group")); + + $DB->query("DELETE FROM `chandleraclrelations` WHERE " . $where); + $this->flashFail("succ", tr("changes_saved"), tr("c_user_removed_from_group")); + } elseif($this->template->mode == "removePermission") { + $where = "`model` = '" . trim(addslashes($this->queryParam("model"))) . "' AND `permission` = '". $this->queryParam("perm") ."' AND `group` = '$UUID'"; + + if(is_null($DB->query("SELECT * FROM `chandleraclgroupspermissions WHERE $where`"))) + $this->flashFail("err", tr("error"), tr("c_permission_not_found")); + + $DB->query("DELETE FROM `chandleraclgroupspermissions` WHERE $where"); + $this->flashFail("succ", tr("changes_saved"), tr("c_permission_removed_from_group")); + } elseif($this->template->mode == "delete") { + $DB->query("DELETE FROM `chandlergroups` WHERE `id` = '$UUID'"); + $DB->query("DELETE FROM `chandleraclgroupspermissions` WHERE `group` = '$UUID'"); + $DB->query("DELETE FROM `chandleraclrelations` WHERE `group` = '$UUID'"); + + $this->flashFail("succ", tr("changes_saved"), tr("c_group_removed")); + } + + if ($_SERVER["REQUEST_METHOD"] !== "POST") return; + + $req = ""; + + if($this->template->mode == "main") + if($this->postParam("delete")) + $req = "DELETE FROM `chandlergroups` WHERE `id`='$UUID'"; + else + $req = "UPDATE `chandlergroups` SET `name`='". $this->postParam('name') ."' , `color`='". $this->postParam("color") ."' WHERE `id`='$UUID'"; + + if($this->template->mode == "members") + if($this->postParam("uid")) + if(!is_null($DB->query("SELECT * FROM `chandleraclrelations` WHERE `user` = '" . $this->postParam("uid") . "'"))) + $this->flashFail("err", tr("error"), tr("c_user_is_already_in_group")); + + $req = "INSERT INTO `chandleraclrelations` (`user`, `group`, `priority`) VALUES ('". $this->postParam("uid") ."', '$UUID', 32)"; + + if($this->template->mode == "permissions") + $req = "INSERT INTO `chandleraclgroupspermissions` (`group`, `model`, `permission`, `context`) VALUES ('$UUID', '". trim(addslashes($this->postParam("model"))) ."', '". $this->postParam("permission") ."', 0)"; + + $DB->query($req); + $this->flashFail("succ", tr("changes_saved")); + } + + function renderChandlerUser(string $UUID): void + { + if(!$UUID) $this->notFound(); + + $c_user = (new ChandlerUsers())->getById($UUID); + $user = $this->users->getByChandlerUser($c_user); + if(!$user) $this->notFound(); + + $this->redirect("/admin/users/id" . $user->getId()); + } } diff --git a/Web/Presenters/templates/Admin/@layout.xml b/Web/Presenters/templates/Admin/@layout.xml index 8367e709..788bba8d 100644 --- a/Web/Presenters/templates/Admin/@layout.xml +++ b/Web/Presenters/templates/Admin/@layout.xml @@ -60,6 +60,14 @@ {_admin_banned_links} +
+ Chandler +
+
{_admin_services}
diff --git a/Web/Presenters/templates/Admin/ChandlerGroup.xml b/Web/Presenters/templates/Admin/ChandlerGroup.xml new file mode 100644 index 00000000..3c9ae709 --- /dev/null +++ b/Web/Presenters/templates/Admin/ChandlerGroup.xml @@ -0,0 +1,177 @@ +{extends "@layout.xml"} + +{block title} + {$group->name} +{/block} + +{block heading} + {_c_groups} + » {$group->name} +{/block} + +{block content} + {var $isMain = $mode === 'main'} + {var $isPermissions = $mode === 'permissions'} + {var $isMembers = $mode === 'members'} + + {if $isMain} +
+ +
+
+ + +
+
+ + +
+
+ + +
+
+
+ + +
+
+
+
+ {elseif $isMembers} +
+ + + + + + + + + + + + + + + + + + + + + + + + +
IDUUID{_admin_name}{_gender}{_admin_shortcode}{_registration_date}{_admin_actions}
{$user->getId()}{$user->getChandlerGUID()} + + + {$user->getCanonicalName()} + + + + {$user->getCanonicalName()} + + {_admin_banned} + {$user->isFemale() ? tr("female") : tr("male")}{$user->getShortCode() ?? "(" . tr("none") . ")"}{$user->getRegistrationTime()} + + {_delete} + + + {_edit} + + {if $thisUser->getChandlerUser()->can("substitute")->model('openvk\Web\Models\Entities\User')->whichBelongsTo(0)} + + + + {/if} +
+ +
+ {elseif $isPermissions} +
+ + + + + + + + + + + + + + + + +
{_c_model}{_c_permission}{_admin_actions}
{$perm->model}{$perm->permission} + + {_edit} + +
+
+ {/if} +{/block} diff --git a/Web/Presenters/templates/Admin/ChandlerGroups.xml b/Web/Presenters/templates/Admin/ChandlerGroups.xml new file mode 100644 index 00000000..488e7bea --- /dev/null +++ b/Web/Presenters/templates/Admin/ChandlerGroups.xml @@ -0,0 +1,59 @@ +{extends "@layout.xml"} + +{block title} + {_c_groups} +{/block} + +{block heading} + {_c_groups} +{/block} + +{block content} +
+
+ +
+ +
+ + +
+
+
+
+ + + + + + + + + + + + + + + +
ID{_admin_title}{_admin_actions}
+ {$group->id} + + + {$group->name} + + + + {_edit} + + + {_c_permissions} + + + {_members} + + + {_delete} + +
+{/block} diff --git a/Web/Presenters/templates/Admin/User.xml b/Web/Presenters/templates/Admin/User.xml index e0f4d905..46044a7a 100644 --- a/Web/Presenters/templates/Admin/User.xml +++ b/Web/Presenters/templates/Admin/User.xml @@ -68,6 +68,43 @@ +
+

{_c_groups}

+
+
+ + + + + + + + + + + + + + + + +
ID{_admin_actions}
+ {$membership->group} + + + {_c_remove_from_group} + +
+
+
diff --git a/Web/Presenters/templates/Admin/Users.xml b/Web/Presenters/templates/Admin/Users.xml index f538d7ad..8194f79a 100644 --- a/Web/Presenters/templates/Admin/Users.xml +++ b/Web/Presenters/templates/Admin/Users.xml @@ -21,6 +21,7 @@ ID + UUID {_admin_name} {_gender} {_admin_shortcode} @@ -31,6 +32,7 @@ {$user->getId()} + {$user->getChandlerGUID()} diff --git a/Web/di.yml b/Web/di.yml index 152e5db1..3363c5de 100644 --- a/Web/di.yml +++ b/Web/di.yml @@ -47,4 +47,5 @@ services: - openvk\Web\Models\Repositories\ContentSearchRepository - openvk\Web\Models\Repositories\Aliases - openvk\Web\Models\Repositories\BannedLinks + - openvk\Web\Models\Repositories\ChandlerGroups - openvk\Web\Presenters\MaintenancePresenter diff --git a/Web/routes.yml b/Web/routes.yml index ec7fe02d..55d73abb 100644 --- a/Web/routes.yml +++ b/Web/routes.yml @@ -323,6 +323,12 @@ routes: handler: "VKAPI->tokenLogin" - url: "/admin/sandbox" handler: "About->sandbox" + - url: "/admin/chandler/groups" + handler: "Admin->chandlerGroups" + - url: "/admin/chandler/groups/{slug}" + handler: "Admin->chandlerGroup" + - url: "/admin/chandler/users/{slug}" + handler: "Admin->chandlerUser" - url: "/internal/wall{num}" handler: "Wall->wallEmbedded" - url: "/robots.txt" diff --git a/locales/en.strings b/locales/en.strings index 7ccedb95..132ffe8f 100644 --- a/locales/en.strings +++ b/locales/en.strings @@ -1186,6 +1186,28 @@ "url_is_banned_title" = "Link to a suspicious site"; "url_is_banned_proceed" = "Follow the link"; +/* Chandler */ + +"c_user_removed_from_group" = "The user has been removed from the group"; +"c_permission_removed_from_group" = "The permission has been removed from the group"; +"c_group_removed" = "The group has been deleted."; +"c_groups" = "Chandler Groups"; +"c_users" = "Chandler Users"; +"c_group_permissions" = "Permissions"; +"c_group_members" = "Members"; +"c_model" = "Model"; +"c_permission" = "Permission"; +"c_permissions" = "Permissions"; +"c_color" = "Color"; +"add" = "Add"; +"c_edit_groups" = "Edit Groups"; +"c_user_is_not_in_group" = "The relationship between the user and the group was not found."; +"c_permission_not_found" = "The relationship between the permission and the group was not found."; +"c_group_not_found" = "The group was not found."; +"c_user_is_already_in_group" = "This user is already a member of this group."; +"c_add_to_group" = "Add to group"; +"c_remove_from_group" = "Remove from group"; + /* Maintenance */ "global_maintenance" = "Undergoing maintenance"; diff --git a/locales/ru.strings b/locales/ru.strings index a3838ba0..e62048ed 100644 --- a/locales/ru.strings +++ b/locales/ru.strings @@ -1244,6 +1244,28 @@ "url_is_banned_title" = "Ссылка на подозрительный сайт"; "url_is_banned_proceed" = "Перейти по ссылке"; +/* Chandler */ + +"c_user_removed_from_group" = "Пользователь был удалён из группы"; +"c_permission_removed_from_group" = "Право было удалено из группы"; +"c_group_removed" = "Группа была удалена."; +"c_groups" = "Группы Chandler"; +"c_users" = "Пользователи Chandler"; +"c_group_permissions" = "Права"; +"c_group_members" = "Участники"; +"c_model" = "Модель"; +"c_permission" = "Право"; +"c_permissions" = "Права"; +"c_color" = "Цвет"; +"add" = "Добавить"; +"c_edit_groups" = "Редактировать группы"; +"c_user_is_not_in_group" = "Связь пользователя и группы не найдена."; +"c_permission_not_found" = "Связь права и группы не найдена."; +"c_group_not_found" = "Группа не найдена."; +"c_user_is_already_in_group" = "Этот пользователь уже включён в эту группу."; +"c_add_to_group" = "Добавить в группу"; +"c_remove_from_group" = "Исключить из группы"; + /* Maintenance */ "global_maintenance" = "Технические работы";