profile_defs.php 11 KB
Newer Older
1
2
3
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
<?php
#
# Copyright (c) 2006-2014 University of Utah and the Flux Group.
# 
# {{{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 Profile
{
    var	$profile;
    var $project;

    #
    # Constructor by lookup on unique index.
    #
33
34
    function Profile($token, $version = null) {
	$safe_profileid = addslashes($token);
Leigh B Stoller's avatar
Leigh B Stoller committed
35
	
36
	if (preg_match("/^\w+\-\w+\-\w+\-\w+\-\w+$/", $token)) {
37
38
39
40
41
	    #
	    # First look to see if the uuid is for the profile itself,
	    # which means current version. Otherwise look for a
	    # version with the uuid.
	    #
42
	    $query_result =
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
		DBQueryWarn("select i.*,v.*,i.uuid as profile_uuid ".
			    "  from apt_profiles as i ".
			    "left join apt_profile_versions as v on ".
			    "     v.profileid=i.profileid and ".
			    "     v.version=i.version ".
			    "where i.uuid='$token' and v.deleted is null");

	    if (!$query_result || !mysql_num_rows($query_result)) {
		$query_result =
		    DBQueryWarn("select i.*,v.*,i.uuid as profile_uuid ".
				"  from apt_profile_versions as v ".
				"left join apt_profiles as i on ".
				"     v.profileid=i.profileid ".
				"where v.uuid='$token' and ".
				"      v.deleted is null");
	    }
Leigh B Stoller's avatar
Leigh B Stoller committed
59
	}
60
	elseif (is_null($version)) {
Leigh B Stoller's avatar
Leigh B Stoller committed
61
	    $query_result =
62
63
		DBQueryWarn("select i.*,v.*,i.uuid as profile_uuid ".
			    "  from apt_profiles as i ".
64
65
66
67
			    "left join apt_profile_versions as v on ".
			    "     v.profileid=i.profileid and ".
			    "     v.version=i.version ".
			    "where i.profileid='$safe_profileid'");
Leigh B Stoller's avatar
Leigh B Stoller committed
68
	}
69
70
71
	else {
	    $safe_version = addslashes($version);
	    $query_result =
72
73
	        DBQueryWarn("select i.*,v.*,i.uuid as profile_uuid ".
			    "  from apt_profile_versions as v ".
74
75
76
77
78
			    "left join apt_profiles as i on ".
			    "     i.profileid=v.profileid ".
			    "where v.profileid='$safe_profileid' and ".
			    "      v.version='$safe_version' and ".
			    "      v.deleted is null");
Leigh B Stoller's avatar
Leigh B Stoller committed
79
	}
80
81
82
83
84
85
86
87
88
89
90
91
92
93
	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->profile = null;
	    return;
	}
	$this->profile = mysql_fetch_array($query_result);

	# Load lazily;
	$this->project    = null;
    }
    # accessors
    function field($name) {
	return (is_null($this->profile) ? -1 : $this->profile[$name]);
    }
    function name()	    { return $this->field('name'); }
94
95
    function profileid()    { return $this->field('profileid'); }
    function version()      { return $this->field('version'); }
96
97
98
99
100
    function creator()	    { return $this->field('creator'); }
    function creator_idx()  { return $this->field('creator_idx'); }
    function pid()	    { return $this->field('pid'); }
    function pid_idx()	    { return $this->field('pid_idx'); }
    function created()	    { return $this->field('created'); }
101
102
    function published()    { return $this->field('published'); }
    function deleted()	    { return $this->field('deleted'); }
103
    function uuid()	    { return $this->field('uuid'); }
104
    function profile_uuid() { return $this->field('profile_uuid'); }
105
106
107
108
    function ispublic()	    { return $this->field('public'); }
    function shared()	    { return $this->field('shared'); }
    function listed()	    { return $this->field('listed'); }
    function rspec()	    { return $this->field('rspec'); }
109
110
    function locked()	    { return $this->field('status'); }
    function status()	    { return $this->field('locked'); }
111
112
    function parent_profileid()    { return $this->field('parent_profileid'); }
    function parent_version()      { return $this->field('parent_version'); }
113
114
115
116
117

    # Private means only in the same project.
    function IsPrivate() {
	return !($this->ispublic() || $this->shared());
    }
118
119
120
121
122
123
124
    
    # Hmm, how does one cause an error in a php constructor?
    function IsValid() {
	return !is_null($this->profile);
    }

    # Lookup up a single profile by idx. 
125
126
    function Lookup($token, $version = null) {
	$foo = new Profile($token, $version);
127
128
129
130
131
132
133
134

	if ($foo->IsValid()) {
            # Insert into cache.
	    return $foo;
	}	
	return null;
    }

135
    function LookupByName($project, $name, $version = null) {
136
	$pid = $project->pid();
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
	$safe_name = addslashes($name);

	if (preg_match("/^\w+\-\w+\-\w+\-\w+\-\w+$/", $name)) {
	    return Profile::Lookup($name);
	}
	elseif (is_null($version)) {
	    $query_result =
		DBQueryWarn("select i.profileid,i.version ".
			    "  from apt_profiles as i ".
			    "left join apt_profile_versions as v on ".
			    "     v.profileid=i.profileid and ".
			    "     v.version=i.version ".
			    "where i.pid='$pid' and ".
			    "      i.name='$safe_name'");
	}
	else {
	    $safe_version = addslashes($version);
	    $query_result =
		DBQueryWarn("select i.profileid,i.version ".
			    "  from apt_profiles as i ".
			    "left join apt_profile_versions as v on ".
			    "     v.profileid=i.profileid ".
			    "where i.pid='$pid' and ".
			    "      i.name='$safe_name' and ".
			    "      v.version='$safe_version'");
	}
163
164
	if ($query_result && mysql_num_rows($query_result)) {
	    $row = mysql_fetch_row($query_result);
165
	    return Profile::Lookup($row[0], $row[1]);
166
167
168
	}
	return null;
    }
169

170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
    #
    # Lookup the most recently published version of a profile.
    #
    function LookupMostRecentPublished() {
	$profileid = $this->profileid();

	$query_result = 
	    DBQueryWarn("select version from apt_profile_versions as v ".
			"where v.profileid='$profileid' and ".
			"      published is not null and ".
			"      deleted is null ".
			"order by published desc limit 1");

	if (!$query_result || !mysql_num_rows($query_result)) {
	    return null;
	}
	$row = mysql_fetch_row($query_result);
	return Profile::Lookup($profileid, $row[0]);
    }

190
191
192
193
194
195
196
    #
    # Refresh an instance by reloading from the DB.
    #
    function Refresh() {
	if (! $this->IsValid())
	    return -1;

197
198
	$profileid = $this->profileid();
	$version   = $this->version();
199
200

	$query_result =
201
202
	    DBQueryWarn("select * from apt_profile_versions ".
			"where profileid='$profileid' and version='$version'");
203
204
205
206
207
208
209
210
211
212
    
	if (!$query_result || !mysql_num_rows($query_result)) {
	    $this->profile    = NULL;
	    $this->project    = null;
	    return -1;
	}
	$this->profile    = mysql_fetch_array($query_result);
	$this->project    = null;
	return 0;
    }
Leigh B Stoller's avatar
Leigh B Stoller committed
213
214

    #
215
    # URL. To the specific version of the profile.
Leigh B Stoller's avatar
Leigh B Stoller committed
216
217
    #
    function URL() {
218
	global $APTBASE, $ISVSERVER;
Leigh B Stoller's avatar
Leigh B Stoller committed
219
220
221
	
	$uuid = $this->uuid();

222
223
224
	if ($this->ispublic()) {
	    $pid  = $this->pid();
	    $name = $this->name();
225
	    $vers = $this->version();
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
	    if ($ISVSERVER)
		return "$APTBASE/p/$pid/$name/$vers";
	    return "$APTBASE/instantiate.php?profile=$name".
		"&project=$pid&version=$vers";
	}
	else {
	    if ($ISVSERVER)
		return "$APTBASE/p/$uuid";	    
	    return "$APTBASE/instantiate.php?profile=$uuid";
	}
    }
    # And the URL of the profile itself.
    function ProfileURL() {
	global $APTBASE;
	
	$uuid = $this->profile_uuid();

	if ($this->ispublic()) {
	    $pid  = $this->pid();
	    $name = $this->name();
	    if ($ISVSERVER)
		return "$APTBASE/p/$pid/$name";
	    return "$APTBASE/instantiate.php?profile=$name&project=$pid";
249
250
	}
	else {
251
252
253
	    if ($ISVSERVER)
		return "$APTBASE/p/$uuid";	    
	    return "$APTBASE/instantiate.php?profile=$uuid";
254
	}
Leigh B Stoller's avatar
Leigh B Stoller committed
255
    }
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330

    #
    # Is this profile the highest numbered version.
    # 
    function IsHead() {
	$profileid = $this->profileid();

	$query_result =
	    DBQueryWarn("select max(version) from apt_profile_versions ".
			"where profileid='$profileid'");
	if (!$query_result || !mysql_num_rows($query_result)) {
	    return -1;
	}
	$row = mysql_fetch_row($query_result);
	return ($this->version() == $row[0] ? 1 : 0);
    }
    #
    # Does this profile have more then one version (history).
    # 
    function HasHistory() {
	$profileid = $this->profileid();

	$query_result =
	    DBQueryWarn("select count(*) from apt_profile_versions ".
			"where profileid='$profileid'");
	if (!$query_result || !mysql_num_rows($query_result)) {
	    return -1;
	}
	$row = mysql_fetch_row($query_result);
	return ($row[0] > 1 ? 1 : 0);
    }
    #
    # A profile can be published if it is a > version then the most
    # recent published profile. 
    # 
    function CanPublish() {
	$profileid = $this->profileid();

	# Already published. Might support unpublish at some point.
	if ($this->published())
	    return 0;

	$query_result =
	    DBQueryWarn("select version from apt_profile_versions ".
			"where profileid='$profileid' and ".
			"      published is not null ".
			"order by version desc limit 1");
	if (!$query_result || !mysql_num_rows($query_result)) {
	    return -1;
	}
	$row = mysql_fetch_row($query_result);
	$vers = $row[0];
	
	return ($this->version() > $row[0] ? 1 : 0);
    }
    #
    # A profile can be modified if it is a >= version then the most
    # recent published profile. 
    # 
    function CanModify() {
	$profileid = $this->profileid();

	$query_result =
	    DBQueryWarn("select version from apt_profile_versions ".
			"where profileid='$profileid' and ".
			"      published is not null ".
			"order by version desc limit 1");
	if (!$query_result || !mysql_num_rows($query_result)) {
	    return -1;
	}
	$row = mysql_fetch_row($query_result);
	$vers = $row[0];
	
	return ($this->version() >= $row[0] ? 1 : 0);
    }
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
    #
    # Has a profile been instantiated?
    #
    function HasActivity() {
	$profileid = $this->profileid();

	$query_result =
	    DBQueryWarn("select count(h.uuid) from apt_instance_history as h ".
			"where h.profile_id='$profileid'");

	if (!$query_result) {
	    return 0;
	}
	if (mysql_num_rows($query_result)) {
	    $row = mysql_fetch_row($query_result);
	    if ($row[0] > 0) {
		return 1;
	    }
	}
	$query_result =
	    DBQueryWarn("select count(uuid) from apt_instances ".
			"where profile_id='$profileid'");

	if (!$query_result) {
	    return 0;
	}
	if (mysql_num_rows($query_result)) {
	    $row = mysql_fetch_row($query_result);
	    if ($row[0] > 0) {
		return 1;
	    }
	}
	return 0;
    }
365
366
367
368
369
370
371
372

    #
    # Permission check; does user have permission to instantiate the
    # profile. At the moment, view/instantiate are the same.
    #
    function CanInstantiate($user) {
	$profileid = $this->profileid();

373
374
	if ($this->shared() || $this->ispublic() ||
	    $this->creator_idx() == $user->uid_idx()) {
375
376
377
	    return 1;
	}
	# Otherwise a project membership test.
378
	$project = Project::Lookup($this->pid_idx());
379
380
381
382
383
384
385
386
387
388
389
390
	if (!$project) {
	    return 0;
	}
	$isapproved = 0;
	if ($project->IsMember($user, $isapproved) && $isapproved) {
	    return 1;
	}
	return 0;
    }
    function CanView($user) {
	return $this->CanInstantiate($user);
    }
391
392
}
?>