package ca.intelliware.ihtsdo.mlds.service;

import org.apache.commons.lang.Validate;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import ca.intelliware.ihtsdo.mlds.domain.Member;
import ca.intelliware.ihtsdo.mlds.security.AuthoritiesConstants;

@Service
public class CurrentSecurityContext {

	public String getCurrentUserName() {
		SecurityContext securityContext = SecurityContextHolder.getContext();
		return securityContext.getAuthentication().getName();
	}

	public boolean isAdmin() {
		return hasRole(AuthoritiesConstants.ADMIN);
	}

	private boolean hasRole(String role) {
		SecurityContext securityContext = SecurityContextHolder.getContext();
		for (GrantedAuthority authority : securityContext.getAuthentication().getAuthorities()) {
			if (role.equals(authority.getAuthority())) {
				return true;
			}
		}
		return false;
	}
	
	public boolean isUser() {
		return hasRole(AuthoritiesConstants.USER);
	}

	public boolean isStaff() {
		return hasRole(AuthoritiesConstants.STAFF);
	}

	public boolean isStaffOrAdmin() {
		return isStaff() || isAdmin();
	}

	public boolean isStaffFor(Member member) {
		Validate.notNull(member);
		return hasRole(AuthoritiesConstants.staffRoleForMember(member.getKey()));
	}

	public String getStaffMemberKey() {
		if (isAdmin()) {
			return Member.KEY_IHTSDO;
		} else if (isStaff()) {
			for (GrantedAuthority authority : SecurityContextHolder.getContext().getAuthentication().getAuthorities()) {
				String a = authority.getAuthority();
				if (a.startsWith(AuthoritiesConstants.STAFF) &&
					!a.equals(AuthoritiesConstants.STAFF)) {
					return a.substring(AuthoritiesConstants.STAFF.length()+1); // +1 for underscore
				}
			}
		}
		throw new IllegalStateException("User does not represent member");
	}
	
}
@@ -597,16 +597,70 @@
 text();%0A
+%09%09if (securityContext.getAuthentication() != null) %7B%0A%09
 %09%09return
@@ -703,24 +703,70 @@
 .getName();%0A
+%09%09%7D else %7B%0A%09%09%09return %22MISSING PRINCIPAL%22;%0A%09%09%7D%0A
 %09%7D%0A%0A%09public 
@@ -947,16 +947,70 @@
 text();%0A
+%09%09if (securityContext.getAuthentication() != null) %7B%0A%09
 %09%09for (G
@@ -1096,16 +1096,17 @@
 )) %7B%0A%09%09%09
+%09
 if (role
@@ -1142,24 +1142,25 @@
 y())) %7B%0A%09%09%09%09
+%09
 return true;
@@ -1160,16 +1160,22 @@
 n true;%0A
+%09%09%09%09%7D%0A
 %09%09%09%7D%0A%09%09%7D

Make the CurrentSecurityContext more resilient to null getAuthentication()