userrole.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 = 'i:n:r:d';
  22. $longoptions = array('id=', 'nickname=', 'role=', 'delete');
  23. $helptext = <<<END_OF_USERROLE_HELP
  24. userrole.php [options]
  25. modifies a role for the given user
  26. Available roles: owner moderator administrator sandboxed silenced deleted
  27. -d --delete delete the role
  28. -i --id ID of the user
  29. -n --nickname nickname of the user
  30. -r --role role to add (or delete)
  31. END_OF_USERROLE_HELP;
  32. require_once INSTALLDIR.'/scripts/commandline.inc';
  33. if (have_option('i', 'id')) {
  34. $id = get_option_value('i', 'id');
  35. $profile = Profile::getKV('id', $id);
  36. if (empty($profile)) {
  37. print "Can't find user with ID $id\n";
  38. exit(1);
  39. }
  40. } else if (have_option('n', 'nickname')) {
  41. $nickname = get_option_value('n', 'nickname');
  42. $user = User::getKV('nickname', $nickname);
  43. if (empty($user)) {
  44. print "Can't find user with nickname '$nickname'\n";
  45. exit(1);
  46. }
  47. $profile = $user->getProfile();
  48. if (empty($profile)) {
  49. print "User with ID $id has no profile\n";
  50. exit(1);
  51. }
  52. } else {
  53. print "You must provide either an ID or a nickname.\n";
  54. exit(1);
  55. }
  56. $role = get_option_value('r', 'role');
  57. if (empty($role)) {
  58. print "You must provide a role.\n";
  59. exit(1);
  60. }
  61. if (have_option('d', 'delete')) {
  62. print "Revoking role '$role' from user '$profile->nickname' ($profile->id)...";
  63. try {
  64. $profile->revokeRole($role);
  65. print "OK\n";
  66. } catch (Exception $e) {
  67. print "FAIL\n";
  68. print $e->getMessage();
  69. print "\n";
  70. }
  71. } else {
  72. print "Granting role '$role' to user '$profile->nickname' ($profile->id)...";
  73. try {
  74. $profile->grantRole($role);
  75. print "OK\n";
  76. } catch (Exception $e) {
  77. print "FAIL\n";
  78. print $e->getMessage();
  79. print "\n";
  80. }
  81. }