makegroupadmin.php 2.5 KB

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