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