Project

General

Profile

« Previous | Next » 

Revision 6225

-use every Subject in the session (alt Ids and Group membership)
-consolidate to single isAuthorized method

View differences:

src/edu/ucsb/nceas/metacat/dataone/CNodeService.java
131 131
		String guid = pid.getValue();
132 132
		
133 133
		// are we allowed to do this?
134
		if (!hasPermission(session, pid, Permission.CHANGE_PERMISSION)) {
134
		if (!isAuthorized(session, pid, Permission.CHANGE_PERMISSION)) {
135 135
			throw new NotAuthorized("4881", Permission.CHANGE_PERMISSION + " not allowed by " + subject.getValue() + " on " + guid);	
136 136
		}
137 137
		
......
184 184
		String guid = pid.getValue();
185 185
		
186 186
		// are we allowed to do this?
187
		if (!hasPermission(session, pid, Permission.WRITE)) {
187
		if (!isAuthorized(session, pid, Permission.WRITE)) {
188 188
			throw new NotAuthorized("4720", Permission.WRITE + " not allowed by " + subject.getValue() + " on " + guid);	
189 189
		}
190 190
		
......
285 285
		// get the system metadata
286 286
		String guid1 = pidOfSubject.getValue();
287 287
		// are we allowed to do this?
288
		if (!hasPermission(session, pidOfSubject, Permission.READ)) {
288
		if (!isAuthorized(session, pidOfSubject, Permission.READ)) {
289 289
			throw new NotAuthorized("4881", Permission.READ + " not allowed on " + guid1);	
290 290
		}
291 291
		
......
378 378
		String guid = pid.getValue();
379 379
		
380 380
		// are we allowed to do this?
381
		if (!hasPermission(session, pid, Permission.READ)) {
381
		if (!isAuthorized(session, pid, Permission.READ)) {
382 382
			throw new NotAuthorized("4720", Permission.READ + " not allowed on " + guid);	
383 383
		}
384 384
		
......
659 659
		String guid = pid.getValue();
660 660
		
661 661
		// are we allowed to do this?
662
		if (!hasPermission(session, pid, Permission.CHANGE_PERMISSION)) {
662
		if (!isAuthorized(session, pid, Permission.CHANGE_PERMISSION)) {
663 663
			throw new NotAuthorized("4440", "not allowed by " + subject.getValue() + " on " + guid);	
664 664
		}
665 665
		
src/edu/ucsb/nceas/metacat/dataone/D1NodeService.java
24 24
package edu.ucsb.nceas.metacat.dataone;
25 25

  
26 26
import java.io.InputStream;
27
import java.util.ArrayList;
27 28
import java.util.Calendar;
28 29
import java.util.Date;
29 30
import java.util.List;
......
40 41
import org.dataone.service.types.AccessRule;
41 42
import org.dataone.service.types.Event;
42 43
import org.dataone.service.types.Identifier;
44
import org.dataone.service.types.Group;
43 45
import org.dataone.service.types.Log;
44 46
import org.dataone.service.types.LogEntry;
45 47
import org.dataone.service.types.NodeReference;
46 48
import org.dataone.service.types.Permission;
49
import org.dataone.service.types.Person;
47 50
import org.dataone.service.types.Session;
48 51
import org.dataone.service.types.Subject;
49 52
import org.dataone.service.types.SystemMetadata;
......
281 284
		String guid = pid.getValue();
282 285
		
283 286
		// are we allowed to do this?
284
		if (!hasPermission(session, pid, Permission.CHANGE_PERMISSION)) {
287
		if (!isAuthorized(session, pid, Permission.CHANGE_PERMISSION)) {
285 288
			throw new NotAuthorized("4420", "not allowed by " + subject.getValue() + " on " + guid);	
286 289
		}
287 290
		
......
329 332

  
330 333
		boolean allowed = false;
331 334
		
332
		// get the subject
333
		Subject subject = session.getSubject();
335
		// get the subjects from the session
336
		List<Subject> subjects = new ArrayList<Subject>();
337
		subjects.add(session.getSubject());
338
		for (Person p: session.getSubjectList().getPersonList()) {
339
			subjects.add(p.getSubject());
340
		}
341
		for (Group g: session.getSubjectList().getGroupList()) {
342
			subjects.add(g.getSubject());
343
		}
334 344
		
335 345
		// get the system metadata
336 346
		String guid = pid.getValue();
......
342 352
		}
343 353
		List<AccessRule> allows = systemMetadata.getAccessPolicy().getAllowList();
344 354
		for (AccessRule accessRule: allows) {
345
			if (accessRule.getSubjectList().contains(subject)) {
346
				allowed = accessRule.getPermissionList().contains(permission);
347
				if (allowed) {
348
					break;
355
			for (Subject subject: subjects) {
356
				if (accessRule.getSubjectList().contains(subject)) {
357
					allowed = accessRule.getPermissionList().contains(permission);
358
					if (allowed) {
359
						break;
360
					}
349 361
				}
350 362
			}
351 363
		}
352 364
		
353 365
		// TODO: throw or return?
354 366
		if (!allowed) {
355
			throw new NotAuthorized("1820", permission + "not allowed by " + subject.getValue() + " on " + guid);
367
			throw new NotAuthorized("1820", permission + "not allowed on " + guid);
356 368
		}
357 369
		return allowed;
358 370
	}
359
	
360
	protected boolean hasPermission(Session session, Identifier pid, Permission permission)
361
	  throws ServiceFailure, InvalidToken, NotFound, NotAuthorized,
362
	  NotImplemented, InvalidRequest {
363 371

  
364
		boolean allowed = false;
365
		
366
		// get the subject
367
		Subject subject = session.getSubject();
368
		
369
		// get the system metadata
370
		String guid = pid.getValue();
371
		SystemMetadata systemMetadata = null;
372
		try {
373
			systemMetadata = IdentifierManager.getInstance().getSystemMetadata(guid);
374
		} catch (McdbDocNotFoundException e) {
375
			throw new NotFound("1800", "No record found for: " + guid);
376
		}
377
		List<AccessRule> allows = systemMetadata.getAccessPolicy().getAllowList();
378
		for (AccessRule accessRule: allows) {
379
			if (accessRule.getSubjectList().contains(subject)) {
380
				allowed = accessRule.getPermissionList().contains(permission);
381
				if (allowed) {
382
					break;
383
				}
384
			}
385
		}
386

  
387
		return allowed;
388
	}
389

  
390 372
	/* End methods common to CNAuthorization and MNAuthorization APIs */
391 373
	
392 374
	/**

Also available in: Unified diff