ChangeGroup.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * This file is part of the ForkBB <https://github.com/forkbb>.
  4. *
  5. * @copyright (c) Visman <mio.visman@yandex.ru, https://github.com/MioVisman>
  6. * @license The MIT License (MIT)
  7. */
  8. declare(strict_types=1);
  9. namespace ForkBB\Models\User;
  10. use ForkBB\Models\Action;
  11. use ForkBB\Models\Forum\Forum;
  12. use ForkBB\Models\User\User;
  13. use InvalidArgumentException;
  14. use RuntimeException;
  15. class ChangeGroup extends Action
  16. {
  17. /**
  18. * Обновляет группу указанных пользователей
  19. */
  20. public function changeGroup(int $newGroupId, User ...$users): void
  21. {
  22. $newGroup = $this->c->groups->get($newGroupId);
  23. if (
  24. null === $newGroup
  25. || $newGroup->groupGuest
  26. ) {
  27. throw new InvalidArgumentException('Expected group number');
  28. }
  29. $ids = [];
  30. $moderators = [];
  31. $adminPresent = $newGroup->groupAdmin;
  32. $unverPresent = false;
  33. foreach ($users as $user) {
  34. if ($user->isGuest) {
  35. throw new RuntimeException('Guest can not change group');
  36. }
  37. if (
  38. 1 !== $newGroup->g_moderator
  39. && $user->isAdmMod
  40. ) {
  41. $moderators[$user->id] = $user;
  42. }
  43. if ($user->isAdmin) {
  44. $adminPresent = true;
  45. }
  46. if ($user->isUnverified) {
  47. $unverPresent = true;
  48. }
  49. $ids[] = $user->id;
  50. $user->__group_id = $newGroupId;
  51. }
  52. if (! empty($moderators)) {
  53. $root = $this->c->forums->get(0); //???? вызов от группы админов?
  54. if ($root instanceof Forum) {
  55. foreach ($this->c->forums->depthList($root, 0) as $forum) {
  56. $forum->modDelete(...$moderators);
  57. $this->c->forums->update($forum);
  58. }
  59. }
  60. }
  61. $vars = [
  62. ':new' => $newGroupId,
  63. ':ids' => $ids,
  64. ];
  65. $query = 'UPDATE ::users
  66. SET group_id = ?i:new
  67. WHERE id IN (?ai:ids)';
  68. $this->c->DB->exec($query, $vars);
  69. if ($adminPresent) {
  70. $this->c->admins->reset();
  71. }
  72. if ($unverPresent) {
  73. $this->c->stats->reset();
  74. }
  75. }
  76. }