dataset_defs.php 7.22 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2006-2018 University of Utah and the Flux Group.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#

class Dataset
{
    var	$dataset;
28
    var $project;
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

    #
    # Constructor by lookup on unique index.
    #
    function Dataset($token) {
	$safe_token = addslashes($token);
	$query_result = null;

	if (preg_match("/^\d+$/", $token)) {
	    $query_result =
		DBQueryWarn("select * from apt_datasets ".
			    "where idx='$safe_token'");
	}
	elseif (preg_match("/^\w+\-\w+\-\w+\-\w+\-\w+$/", $token)) {
	    $query_result =
		DBQueryWarn("select * from apt_datasets ".
			    "where uuid='$safe_token'");
	}
	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->dataset = NULL;
	    return;
	}
	$this->dataset = mysql_fetch_array($query_result);
52 53
	# Load lazily;
	$this->project    = null;
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
    }

    # Hmm, how does one cause an error in a php constructor?
    function IsValid() {
	return !is_null($this->dataset);
    }

    # Lookup by idx.
    function Lookup($token) {
	$foo = new Dataset($token);

	if (! $foo->IsValid()) {
	    return null;
	}
	return $foo;
    }
    # Lookup by name in a project
    function LookupByName($project, $name) {
	$pid       = $project->pid();
	$safe_name = addslashes($name);
	
	$query_result =
	    DBQueryFatal("select idx from apt_datasets ".
			 "where pid='$pid' and dataset_id='$safe_name'");

	if (mysql_num_rows($query_result) == 0) {
	    return null;
	}
	$row = mysql_fetch_array($query_result);
	return Dataset::Lookup($row["idx"]);
    }

    # accessors
    function field($name) {
	return (is_null($this->dataset) ? -1 : $this->dataset[$name]);
    } 
Leigh B Stoller's avatar
Leigh B Stoller committed
90
    function idx()           { return $this->field("idx"); }
91 92 93 94 95 96 97
    function dataset_id()    { return $this->field("dataset_id"); }
    function id()            { return $this->field("dataset_id"); }
    function creator_uid()   { return $this->field("creator_uid"); }
    function owner_uid()     { return $this->field("creator_uid"); }
    function uuid()          { return $this->field("uuid"); }
    function pid()           { return $this->field("pid"); }
    function pid_idx()       { return $this->field("pid_idx"); }
Leigh B Stoller's avatar
Leigh B Stoller committed
98 99
    function gid()           { return $this->pid(); }
    function aggregate_urn() { return $this->field("aggregate_urn"); }
100
    function remote_urn()    { return $this->field("remote_urn"); }
101
    function remote_url()    { return $this->field("remote_url"); }
102 103
    function type()          { return $this->field("type"); }
    function fstype()        { return $this->field("fstype"); }
104
    function created()       { return NullDate($this->field("created")); }
105
    function updated()       { return NullDate($this->field("updated")); }
106 107
    function expires()       { return NullDate($this->field("expires")); }
    function last_used()     { return NullDate($this->field("last_used")); }
108 109 110 111
    function state()	     { return $this->field("state"); }
    function size()	     { return $this->field("size"); }
    function locked()	     { return $this->field("locked"); }
    function locker_pid()    { return $this->field("locker_pid"); }
112 113
    function read_access()   { return $this->field("read_access"); }
    function write_access()  { return $this->field("write_access"); }
Leigh B Stoller's avatar
Leigh B Stoller committed
114 115
    function ispublic()      { return $this->field("public"); }
    function shared()        { return $this->field("shared"); }
116
    function webtask_id()    { return $this->field('webtask_id'); }
Leigh B Stoller's avatar
Leigh B Stoller committed
117
    function islocal()       { return 0; }
118 119 120 121 122

    #
    # This is incomplete.
    #
    function AccessCheck($user, $access_type) {
123 124 125 126 127 128 129 130 131 132 133 134 135 136
        global $LEASE_ACCESS_READINFO;
        global $LEASE_ACCESS_MODIFYINFO;
        global $LEASE_ACCESS_READ;
        global $LEASE_ACCESS_MODIFY;
        global $LEASE_ACCESS_DESTROY;
        global $LEASE_ACCESS_MIN;
        global $LEASE_ACCESS_MAX;
	global $TBDB_TRUST_USER;
	global $TBDB_TRUST_GROUPROOT;
	global $TBDB_TRUST_LOCALROOT;

	$mintrust = $LEASE_ACCESS_READINFO;
        $read_access  = $this->read_access();
        $write_access = $this->write_access();
137 138 139 140 141 142 143 144 145
        #
        # Admins do whatever they want.
        # 
	if (ISADMIN()) {
	    return 1;
	}
	if ($this->creator_uid() == $user->uid()) {
	    return 1;
	}
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
        if ($read_access == "global") {
            if ($access_type == $LEASE_ACCESS_READINFO) {
                return 1;
            }
        }
        if ($write_access == "creator") {
            if ($access_type > $LEASE_ACCESS_READINFO) {
                return 0;
            }
        }
	$pid    = $this->pid();
	$gid    = $this->gid();
	$uid    = $user->uid();
	$uid_idx= $user->uid_idx();
	$pid_idx= $user->uid_idx();
	$gid_idx= $user->uid_idx();
        
        #
        # Otherwise must have proper trust in the project.
        # 
	if ($access_type == $LEASE_ACCESS_READINFO) {
	    $mintrust = $TBDB_TRUST_USER;
	}
	else {
	    $mintrust = $TBDB_TRUST_LOCALROOT;
	}
	if (TBMinTrust(TBGrpTrust($uid, $pid, $gid), $mintrust) ||
	    TBMinTrust(TBGrpTrust($uid, $pid, $pid), $TBDB_TRUST_GROUPROOT)) {
	    return 1;
	}
176 177
	return 0;
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
178 179 180 181 182

    #
    # Form a URN for the dataset.
    #
    function URN() {
183
        return $this->remote_urn();
Leigh B Stoller's avatar
Leigh B Stoller committed
184
    }
185 186 187 188 189 190
    function URL() {
        if ($this->type() != "imdataset") {
            return null;
        }
        return $this->remote_url();
    }
191

192 193 194 195 196
    function deleteCommand($webtask) {
        $command = " webmanage_dataset " .
                 "-t " . $webtask->task_id() . " delete " .
                 $this->pid() . "/" . $this->id();
        return $command;
197
    }
198 199 200 201
    function grantCommand($webtask) {
        $command = " webmanage_dataset " .
                 "-t " . $webtask->task_id() . " modify ";
        return $command;
202
    }
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223

    # Grab the webtask. Backwards compat mode, see if there is one associated
    # with the object, use that. Otherwise create a new one.
    function WebTask() {
        if ($this->webtask_id()) {
            return WebTask::Lookup($this->webtask_id());
        }
        $webtask = WebTask::LookupByObject($this->uuid());
        if (!$webtask) {
            $webtask = WebTask::CreateAnonymous();
            if (!$webtask) {
                return null;
            }
        }
        $idx  = $this->idx();
        $webtask_id = $webtask->task_id();
        DBQueryFatal("update apt_datasets set ".
                         "  webtask_id='$webtask_id' ".
                         "where idx='$idx'");
        return $webtask;
    }
224 225 226 227 228 229 230 231 232 233 234 235 236

    function Project() {
	$pid = $this->pid();

	if ($this->project)
	    return $this->project;

	$this->project = Project::Lookup($pid);
	if (! $this->project) {
	    TBERROR("Could not lookup project $pid!", 1);
	}
	return $this->project;
    }
237
}
238

239
?>