All new accounts created on Gitlab now require administrator approval. If you invite any collaborators, please let Flux staff know so they can approve the accounts.

lease_defs.php 5.53 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
# 
# {{{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/>.
# 
# }}}
#

25 26 27
define("GLOBAL_PERM_ANON_RO_IDX",  1);
define("GLOBAL_PERM_USER_RO_IDX",  2);

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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
class Lease
{
    var	$lease;
    var $attributes;

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

	if (preg_match("/^\d+$/", $token)) {
	    $query_result =
		DBQueryWarn("select * from project_leases ".
			    "where lease_idx='$safe_token'");
	}
	elseif (preg_match("/^\w+\-\w+\-\w+\-\w+\-\w+$/", $token)) {
	    $query_result =
		DBQueryWarn("select * from project_leases ".
			    "where uuid='$safe_token'");
	}
	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->lease = NULL;
	    return;
	}
	$this->lease = mysql_fetch_array($query_result);
	$lease_idx   = $this->lease_idx();

	#
	# Load the attributes.
	#
	$query_result =
	    DBQueryWarn("select attrkey,attrval ".
			"  from lease_attributes ".
			"  where lease_idx='$lease_idx'");
	if (!$query_result) {
	    $this->lease = NULL;
	    return;
	}
	$attrs = array();

	while ($row = mysql_fetch_array($query_result)) {
	    $key = $row["attrkey"];
	    $val = $row["attrval"];
	    $attrs[$key] = $val;
	}
	$this->attributes = $attrs;
    }

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

    # Lookup.
    function Lookup($token) {
	$foo = new Lease($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 lease_idx from project_leases ".
			 "where pid='$pid' and lease_id='$safe_name'");

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

    # accessors
    function field($name) {
	return (is_null($this->lease) ? -1 : $this->lease[$name]);
    } 
    function lease_idx()     { return $this->field("lease_idx"); }
    function idx()           { return $this->field("lease_idx"); }
    function lease_id()      { return $this->field("lease_id"); }
    function id()            { return $this->field("lease_id"); }
    function owner_uid()     { return $this->field("owner_uid"); }
    function uuid()          { return $this->field("uuid"); }
    function pid()           { return $this->field("pid"); }
119
    function gid()           { return $this->field("gid"); }
120 121
    function lease_type()    { return $this->field("type"); }
    function type()          { return $this->field("type"); }
122 123 124 125 126
    function inception()     { return NullDate($this->field("inception")); }
    function created()       { return $this->inception(); }
    function lease_end()     { return NullDate($this->field("lease_end")); }
    function expires()       { return $this->lease_end(); }
    function last_used()     { return NullDate($this->field("last_used")); }
127 128 129 130 131 132 133 134 135 136 137 138
    function state()	     { return $this->field("state"); }
    function locked()	     { return $this->field("locked"); }
    function locker_pid()    { return $this->field("locker_pid"); }

    function attribute($key) {
	if (array_key_exists($key, $this->attributes)) {
	    return $this->attributes[$key];
	}
	return null;
    }
    function size()	{ return $this->attribute("size"); }
    function fstype()	{ return $this->attribute("fstype"); }
139
    function islocal()  { return 1; }
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

    #
    # This is incomplete.
    #
    function AccessCheck($user, $access_type) {
        #
        # Admins do whatever they want.
        # 
	if (ISADMIN()) {
	    return 1;
	}
	if ($this->owner_uid() == $user->uid()) {
	    return 1;
	}
	return 0;
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
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
    function read_access() {
        $lease_idx = $this->lease_idx();
        $perm_idx  = GLOBAL_PERM_ANON_RO_IDX;

        $query_result =
            DBQueryFatal("select * from lease_permissions ".
                         "where lease_idx='$lease_idx' and ".
                         "      permission_idx='$perm_idx'");
        
	if (mysql_num_rows($query_result) == 0) {
	    return "project";
	}
        else {
            return "global";
        }
    }
    function write_access() {
        $lease_idx = $this->lease_idx();
        $pid       = $this->pid();

        $query_result =
            DBQueryFatal("select * from lease_permissions ".
                         "where lease_idx='$lease_idx' and ".
                         "      permission_id='$pid' and allow_modify=1");
        
	if (mysql_num_rows($query_result)) {
	    return "project";
	}
        else {
            return "creator";
        }
Leigh B Stoller's avatar
Leigh B Stoller committed
188 189
    }

190 191 192 193 194 195
    #
    # Form a URN for the dataset.
    #
    function URN() {
	global $OURDOMAIN;
	
196
	return "urn:publicid:IDN+${OURDOMAIN}+dataset+" . $this->idx();
197 198
    }
    
199 200
}
?>