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