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