makegroupadmin.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env php
  2. <?php
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. /**
  18. * @package GNUsocial
  19. * @author Evan Prodromou <evan@status.net>
  20. * @copyright 2008, 2009 StatusNet, Inc.
  21. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  22. */
  23. define('INSTALLDIR', dirname(__DIR__));
  24. define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
  25. $shortoptions = 'g:n:';
  26. $longoptions = array('nickname=', 'group=');
  27. $helptext = <<<END_OF_MAKEGROUPADMIN_HELP
  28. makegroupadmin.php [options]
  29. makes a user the admin of a group
  30. -g --group group to add an admin to
  31. -n --nickname nickname of the new admin
  32. END_OF_MAKEGROUPADMIN_HELP;
  33. require_once INSTALLDIR.'/scripts/commandline.inc';
  34. $nickname = get_option_value('n', 'nickname');
  35. $groupname = get_option_value('g', 'group');
  36. if (empty($nickname) || empty($groupname)) {
  37. print "Must provide a nickname and group.\n";
  38. exit(1);
  39. }
  40. try {
  41. $user = User::getKV('nickname', $nickname);
  42. if (empty($user)) {
  43. throw new Exception("No user named '$nickname'.");
  44. }
  45. $group = User_group::getKV('nickname', $groupname);
  46. if (empty($group)) {
  47. throw new Exception("No group named '$groupname'.");
  48. }
  49. $member = Group_member::pkeyGet(array('group_id' => $group->id,
  50. 'profile_id' => $user->id));
  51. if (empty($member)) {
  52. $member = new Group_member();
  53. $member->group_id = $group->id;
  54. $member->profile_id = $user->id;
  55. $member->created = common_sql_now();
  56. if (!$member->insert()) {
  57. throw new Exception("Can't add '$nickname' to '$groupname'.");
  58. }
  59. }
  60. if ($member->is_admin) {
  61. throw new Exception("'$nickname' is already an admin of '$groupname'.");
  62. }
  63. $orig = clone($member);
  64. $member->is_admin = true;
  65. if (!$member->update($orig)) {
  66. throw new Exception("Can't make '$nickname' admin of '$groupname'.");
  67. }
  68. } catch (Exception $e) {
  69. print $e->getMessage() . "\n";
  70. exit(1);
  71. }