Project.pm.in 5.41 KB
Newer Older
1 2 3
#!/usr/bin/perl -wT
#
# EMULAB-COPYRIGHT
Leigh B. Stoller's avatar
Leigh B. Stoller committed
4
# Copyright (c) 2005, 2006, 2007 University of Utah and the Flux Group.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# All rights reserved.
#
package Project;

use strict;
use Exporter;
use vars qw(@ISA @EXPORT);

@ISA    = "Exporter";
@EXPORT = qw ( );

# Must come after package declaration!
use lib '@prefix@/lib';
use libdb;
use libtestbed;
20
use Group;
21 22 23 24 25 26 27 28 29
use English;
use Data::Dumper;
use File::Basename;
use overload ('""' => 'Stringify');

# Configure variables
my $TB		= "@prefix@";
my $BOSSNODE    = "@BOSSNODE@";
my $CONTROL	= "@USERNODE@";
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

# Cache of instances to avoid regenerating them.
my %projects   = ();
my $debug      = 0;

# Little helper and debug function.
sub mysystem($)
{
    my ($command) = @_;

    print STDERR "Running '$command'\n"
	if ($debug);
    return system($command);
}

#
# Lookup by idx.
#
sub Lookup($$)
{
Leigh B. Stoller's avatar
Leigh B. Stoller committed
50 51
    my ($class, $token) = @_;
    my $query_result;
52 53

    # Look in cache first
Leigh B. Stoller's avatar
Leigh B. Stoller committed
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    return $projects{"$token"}
        if (exists($projects{"$token"}));

    #
    # For backwards compatability, look to see if the token is numeric
    # or alphanumeric. If numeric, assumes its an idx, otherwise a name.
    #
    if ($token =~ /^\d*$/) {
	$query_result =
	    DBQueryWarn("select * from projects where pid_idx='$token'");
    }
    elsif ($token =~ /^\w*$/) {
	$query_result =
	    DBQueryWarn("select * from projects where pid='$token'");
    }
    else {
	return undef;
    }
72 73 74 75 76 77
    
    return undef
	if (!$query_result || !$query_result->numrows);

    my $self           = {};
    $self->{'PROJECT'} = $query_result->fetchrow_hashref();
Leigh B. Stoller's avatar
Leigh B. Stoller committed
78 79 80 81
    $self->{'GROUP'}   = Group->Lookup($self->{'PROJECT'}->{'pid_idx'});

    return undef
	if (!defined($self->{'GROUP'}));
82 83 84 85

    bless($self, $class);
    
    # Add to cache. 
Leigh B. Stoller's avatar
Leigh B. Stoller committed
86
    $projects{$self->{'PROJECT'}->{'pid_idx'}} = $self;
87 88 89 90 91 92 93 94
    
    return $self;
}
# accessors
sub field($$) { return ((! ref($_[0])) ? -1 : $_[0]->{'PROJECT'}->{$_[1]}); }
sub pid($)	        { return field($_[0], "pid"); }
sub gid($)	        { return field($_[0], "gid"); }
sub pid_idx($)          { return field($_[0], "pid_idx"); }
Leigh B. Stoller's avatar
Leigh B. Stoller committed
95 96 97
sub gid_idx($)          { return field($_[0], "pid_idx"); }
sub head_uid($)         { return field($_[0], "head_uid"); }
sub head_idx($)         { return field($_[0], "head_idx"); }
98 99 100 101 102 103 104
sub created($)          { return field($_[0], "created"); }
sub description($)      { return field($_[0], "description"); }
sub expt_count($)       { return field($_[0], "expt_count"); }
sub expt_last($)        { return field($_[0], "expt_last"); }
sub wikiname($)         { return field($_[0], "wikiname"); }
sub mailman_password($) { return field($_[0], "mailman_password"); }

Leigh B. Stoller's avatar
Leigh B. Stoller committed
105 106 107 108
# These come from the group not the project.
sub unix_gid($)         { return $_[0]->{'GROUP'}->unix_gid(); }
sub unix_name($)        { return $_[0]->{'GROUP'}->unix_name(); }

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
#
# Lookup given pid For backwards compat.
#
sub LookupByPid($$)
{
    my ($class, $pid) = @_;

    my $query_result =
	DBQueryWarn("select pid_idx from projects where pid='$pid'");

    return undef
	if (! $query_result || !$query_result->numrows);

    my ($pid_idx) = $query_result->fetchrow_array();

    return Project->Lookup($pid_idx);
}

#
# Refresh a class instance by reloading from the DB.
#
sub Refresh($)
{
    my ($self) = @_;

    return -1
	if (! ref($self));

    my $pid_idx = $self->pid_idx();
    
    my $query_result =
	DBQueryWarn("select * from projects where pid_idx=$pid_idx");

    return -1
	if (!$query_result || !$query_result->numrows);

    $self->{'PROJECT'} = $query_result->fetchrow_hashref();

    return 0;
}

#
# Stringify for output.
#
sub Stringify($)
{
    my ($self) = @_;
    
    my $pid     = $self->pid();
    my $pid_idx = $self->pid_idx();

    return "[Project: $pid, IDX: $pid_idx]";
}

#
# Perform some updates ...
#
sub Update($$)
{
    my ($self, $argref) = @_;

    # Must be a real reference. 
    return -1
	if (! ref($self));

    my $pid_idx = $self->pid_idx();

    my $query = "update projects set ".
	join(",", map("$_='" . $argref->{$_} . "'", keys(%{$argref})));

    $query .= " where pid_idx='$pid_idx'";

    return -1
	if (! DBQueryWarn($query));

    return Refresh($self);
}

#
# Delete a user from the project (and all subgroups of course). No checks
# are made; that should be done higher up. Optionally return list of groups
# from which the user was deleted.
#
sub DeleteUser($$;$)
{
    my ($self, $user, $plist) = @_;
    my @grouplist = ();

    # Must be a real reference. 
    return -1
	if (! (ref($self) && ref($user)));

    my $uid_idx = $user->uid_idx();
    my $pid_idx = $self->pid_idx();
    my $gid_idx = $pid_idx;

    # Need a list of all groups for the user in this project.
    # Should probably use a "class" method in the Groups module.
    my $query_result =
	DBQueryWarn("select gid_idx from group_membership ".
		    "where pid_idx=$pid_idx and uid_idx=$uid_idx");
    return -1
	if (! $query_result);
    return 0
	if (! $query_result->numrows);

    while (my ($idx) = $query_result->fetchrow_array()) {
	# Do main group last.
	next
	    if ($idx == $gid_idx);

	my $group = Group->Lookup($idx);
	return -1
	    if (!defined($group));

	return -1
	    if ($group->DeleteMemberShip($user) < 0);

	push(@grouplist, $group);
    }

    my $group = Group->Lookup($gid_idx);
    return -1
	if (!defined($group));

    return -1
	if ($group->DeleteMemberShip($user) < 0);

    if (defined($plist)) {
	@$plist = (@grouplist, $group);
    }
    return 0;
}

# _Always_ make sure that this 1 is at the end of the file...
1;