/*
 * $Id: HasAdminPrivilegesTag.java,v 1.6 2005/08/09 19:05:11 jonyp Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: HasAdminPrivilegesTag.java,v $
 * Revision 1.6  2005/08/09 19:05:11  jonyp
 * added new value attribute
 *
 * Revision 1.5  2004/06/27 17:39:29  marbobo
 * + Quellcode maintenance
 *
 * Revision 1.4  2003/12/24 10:12:04  bigmac
 * Projektweite Import-Optimierung
 *
 * Revision 1.3  2003/08/02 16:13:37  bigmac
 * *** empty log message ***
 *
 * Revision 1.2  2003/07/23 10:45:33  marbobo
 * -unnötige Importanweisungen entfernt
 *
 */
package vkc.view.tag;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.tagext.TagSupport;

import vkc.db.entities.VKCUser;
import vkc.util.ServletUtils;
import vkc.view.WebShare;


/**
 * Tag zur Überprüfung der Adminrechte eines Users
 *
 * @author marbobo
 */
public class HasAdminPrivilegesTag extends TagSupport {
    private String userId = null;
    private String value = null;

	/**
	 * Konstruktor
	 */
    public HasAdminPrivilegesTag() {
        super();
    }

	/**
	 * Diese Methode überprüft ob der eingeloggte Benutzer 
	 * Adminrecte besitzt oder nicht
	 */
    private boolean hasAdminPrivileges() throws JspTagException {
        VKCUser loggedUser = new VKCUser();

        if (userId == null) {
            VKCUser user = ServletUtils.getUser((HttpServletRequest)pageContext.getRequest());

            if (user == null)
                throw new JspTagException(
                        "VKCUser in session scope not found! (KEY=" +
                        WebShare.USER_KEY + ")");

            userId = user.getUserId().toString();
            loggedUser = user;
        } else {
            loggedUser = ServletUtils.getUser((HttpServletRequest)pageContext.getRequest());
        }

		return loggedUser.hasAdminPrivileges();
    }

    /**
     * Aufruf der Methode am Anfang des PermissionTags
     */
    public int doStartTag() throws JspException {
        if (this.hasAdminPrivileges() && (value == null || "true".equals(value)))
            return (EVAL_BODY_INCLUDE);

        return (SKIP_BODY);
    }

    /**
     * Aufruf der Methode am Ende des PermissionTags
     */
    public int doEndTag() throws JspException {
        return EVAL_PAGE;
    }

    public String getValue() {
		return value;
	}

    public void setValue(String value) {
		this.value = value;
	}
}