JaasBean.java 5.36 KB
/*
 * Copyright Codecrew Ry
 * 
 * All rights reserved.
 * 
 * This license applies to any software containing a notice placed by the 
 * copyright holder. Such software is herein referred to as the Software. 
 * This license covers modification, distribution and use of the Software. 
 * 
 * Any distribution and use in source and binary forms, with or without 
 * modification is not permitted without explicit written permission from the 
 * copyright owner. 
 * 
 * A non-exclusive royalty-free right is granted to the copyright owner of the 
 * Software to use, modify and distribute all modifications to the Software in 
 * future versions of the Software. 
 * 
 */
package fi.codecrew.moya.beans;

import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Vector;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.ejb.Stateless;

import fi.codecrew.moya.utilities.UserLoginUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import fi.codecrew.moya.AuthenticationResult;
import fi.codecrew.moya.MoyaRealmBeanRemote;
import fi.codecrew.moya.beans.auth.AuthenticationFormat;
import fi.codecrew.moya.beans.auth.BasicAuthPBean;
import fi.codecrew.moya.beans.auth.NormalAuthPBean;
import fi.codecrew.moya.beans.auth.RestMacAuthPBean;
import fi.codecrew.moya.enums.BortalApplication;
import fi.codecrew.moya.enums.apps.IAppPermission;
import fi.codecrew.moya.enums.apps.SpecialPermission;
import fi.codecrew.moya.enums.apps.UserPermission;
import fi.codecrew.moya.facade.EventUserFacade;
import fi.codecrew.moya.model.ApplicationPermission;
import fi.codecrew.moya.model.EventUser;
import fi.codecrew.moya.model.LanEvent;
import fi.codecrew.moya.model.Role;

/**
 * Session Bean implementation class SessionHandlerBean
 */
@Stateless(mappedName = MoyaRealmBeanRemote.REMOTE_BEAN_NAME)
public class JaasBean implements MoyaRealmBeanRemote, JaasBeanLocal {

	private static final Logger logger = LoggerFactory.getLogger(JaasBean.class);

	@EJB
	private UserBean userbean;
	@EJB
	private PermissionBean permbean;
	@EJB
	private EventBean eventbean;

	@EJB
	private RestBean restbean;

	@EJB
	private EventBean eventorgbean;
	@EJB
	private EventUserFacade eventUserFacade;

	// public static void foo()
	// {
	// if (user != null) {
	// LanEvent event = eventbean.getCurrentEvent();
	// eventUser = new EventUser(user, event, null);
	// // eventUser.setCreator(eventUser);
	// eventUserFacade.create(eventUser);
	// eventUserFacade.flush();
	// eventUser.setCreator(eventUser);
	// }
	// }

	public static enum UserType {
		USER, REST
	}

	@EJB
	private BasicAuthPBean restauthBean;
	@EJB
	private RestMacAuthPBean restMacAuthBean;
	@EJB
	private NormalAuthPBean normalAuthBean;

	@PostConstruct
	public void initAuthformats() {
		authFormats = Arrays.asList(restauthBean, restMacAuthBean, normalAuthBean);
	}

	private List<AuthenticationFormat> authFormats;

	@Override
	public AuthenticationResult authUsername(String jaasUsername, String password) {

		AuthenticationResult ret = null;
		for (AuthenticationFormat auth : authFormats) {
			ret = auth.authenticate(jaasUsername, password);
			if (ret != null) {
				break;
			}
		}
		return ret;

	}

	@Override
	public Enumeration<String> getGroupNames(String jaasUsername, String usertype) {
		String user = UserLoginUtils.getUsernameFromJaasString(jaasUsername);
		LanEvent event = eventbean.getEventForHostname(UserLoginUtils.getDomainFromJaasString(jaasUsername));

		EventUser usr = eventUserFacade.findByLogin(user.toLowerCase().trim(), event);
		HashSet<String> roleset = new HashSet<>();
		roleset.add(UserPermission.ANYUSER.getFullName());

		if (usr == null) {
			usr = permbean.getAnonEventUser(event);
			roleset.add(SpecialPermission.ANONYMOUS.name());
		}

		if (usertype != null) {
			try {
				switch (UserType.valueOf(usertype)) {
				case REST:
					roleset.add(SpecialPermission.REST.name());
					break;
				case USER:
					break;
				default:
					throw new RuntimeException("Unknown user type: " + usertype);
				}
			} catch (Exception t) {
				logger.warn("UserType authentication " + usertype, t);
			}
		}

		if (!usr.getUser().isAnonymous()) {
			// all logged in users should be able to logout :)
			roleset.add(UserPermission.LOGOUT.name());
			roleset.add(SpecialPermission.USER.name());
		}
		// TODO: EI NÄIN!!!!! Superadmin ei saa kaikkia oikkia!!

		if (usr.getUser().isSuperadmin()) {
			for (BortalApplication app : BortalApplication.values()) {
				for (IAppPermission perm : app.getPermissions()) {
					roleset.add(perm.getFullName());
				}
			}
			roleset.add(SpecialPermission.SUPERADMIN.name());
		} else {

			Set<Role> usrroles = userbean.localFindUsersRoles(usr);
			for (Role role : usrroles) {
				for (ApplicationPermission apperm : role.getPermissions()) {
					roleset.add(apperm.getPermission().getFullName());
				}
			}

			if (event != null && !usr.getUser().isAnonymous() && event.getDefaultRole() != null) {
				for (ApplicationPermission apperm : event.getDefaultRole().getPermissions()) {
					roleset.add(apperm.getPermission().getFullName());
				}
			}
		}

		Vector<String> retvect = new Vector<String>();
		retvect.addAll(roleset);
		logger.debug("group names for user {}: {}", user, retvect);

		return retvect.elements();
	}

	@Override
	public Enumeration<String> getGroupNames(String username) {
		return getGroupNames(username, null);
	}


}