makegroupadmin.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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', dirname(__DIR__));
  21. define('PUBLICDIR', INSTALLDIR . DIRECTORY_SEPARATOR . 'public');
  22. $shortoptions = 'g:n:';
  23. $longoptions = array('nickname=', 'group=');
  24. $helptext = <<<END_OF_MAKEGROUPADMIN_HELP
  25. makegroupadmin.php [options]
  26. makes a user the admin of a group
  27. -g --group group to add an admin to
  28. -n --nickname nickname of the new admin
  29. END_OF_MAKEGROUPADMIN_HELP;
  30. require_once INSTALLDIR.'/scripts/commandline.inc';
  31. $nickname = get_option_value('n', 'nickname');
  32. $groupname = get_option_value('g', 'group');
  33. if (empty($nickname) || empty($groupname)) {
  34. print "Must provide a nickname and group.\n";
  35. exit(1);
  36. }
  37. try {
  38. $user = User::getKV('nickname', $nickname);
  39. if (empty($user)) {
  40. throw new Exception("No user named '$nickname'.");
  41. }
  42. $group = User_group::getKV('nickname', $groupname);
  43. if (empty($group)) {
  44. throw new Exception("No group named '$groupname'.");
  45. }
  46. $member = Group_member::pkeyGet(array('group_id' => $group->id,
  47. 'profile_id' => $user->id));
  48. if (empty($member)) {
  49. $member = new Group_member();
  50. $member->group_id = $group->id;
  51. $member->profile_id = $user->id;
  52. $member->created = common_sql_now();
  53. if (!$member->insert()) {
  54. throw new Exception("Can't add '$nickname' to '$groupname'.");
  55. }
  56. }
  57. if ($member->is_admin) {
  58. throw new Exception("'$nickname' is already an admin of '$groupname'.");
  59. }
  60. $orig = clone($member);
  61. $member->is_admin = 1;
  62. if (!$member->update($orig)) {
  63. throw new Exception("Can't make '$nickname' admin of '$groupname'.");
  64. }
  65. } catch (Exception $e) {
  66. print $e->getMessage() . "\n";
  67. exit(1);
  68. }