dataset_defs.php 5.85 KB
Newer Older
1 2
<?php
#
3
# Copyright (c) 2006-2015 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 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 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
# 
# {{{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;

    #
    # 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);
    }

    # 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
87
    function idx()           { return $this->field("idx"); }
88 89 90 91 92 93 94
    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
95 96
    function gid()           { return $this->pid(); }
    function aggregate_urn() { return $this->field("aggregate_urn"); }
97
    function remote_urn()    { return $this->field("remote_urn"); }
98
    function remote_url()    { return $this->field("remote_url"); }
99 100
    function type()          { return $this->field("type"); }
    function fstype()        { return $this->field("fstype"); }
101 102 103
    function created()       { return NullDate($this->field("created")); }
    function expires()       { return NullDate($this->field("expires")); }
    function last_used()     { return NullDate($this->field("last_used")); }
104 105 106 107
    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"); }
108 109
    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
110 111
    function ispublic()      { return $this->field("public"); }
    function shared()        { return $this->field("shared"); }
Leigh B Stoller's avatar
Leigh B Stoller committed
112
    function islocal()       { return 0; }
113 114 115 116 117

    #
    # This is incomplete.
    #
    function AccessCheck($user, $access_type) {
118 119 120 121 122 123 124 125 126 127 128 129 130 131
        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();
132 133 134 135 136 137 138 139 140
        #
        # Admins do whatever they want.
        # 
	if (ISADMIN()) {
	    return 1;
	}
	if ($this->creator_uid() == $user->uid()) {
	    return 1;
	}
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
        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;
	}
171 172
	return 0;
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
173 174 175 176 177

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

    function deleteCommand() {
	return  "webmanage_dataset delete " . $this->pid() . "/" . $this->id();
    }
    function grantCommand() {
	return  "webmanage_dataset modify ";
    }
193
}
194

195
?>