All blog posts, code samples and downloads licensed under Apache License 2.0.
Close

A little UserBean

Oliver Busse on 04/17/2013 19:54:21 CEDT, filed under Java 

Just another bean to retrieve common information from the current user.

package com.ulc;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Vector;

import javax.faces.context.FacesContext;

import lotus.domino.Name;
import lotus.domino.NotesException;
import lotus.domino.Session;

public class UserBean {

	private final List<String> aclLevelNames = new ArrayList<String>();

	private Session session = null;
	private int aclLevel;
	private String aclLevelName;
	private String userNameCommon;
	private String userNameAbbreviated;
	private String userNameCanonical;
	private String userRoles;

	private String emailAdress;
	private String mailFilePath;

	public UserBean() {
		this.init();
	}

	public void init() {
		session = getCurrentSession();

		try {

			aclLevelNames.add("ACL.LEVEL_NO_ACCESS");
			aclLevelNames.add("ACL.LEVEL_DEPOSITOR");
			aclLevelNames.add("ACL.LEVEL_READER");
			aclLevelNames.add("ACL.LEVEL_AUTHOR");
			aclLevelNames.add("ACL.LEVEL_EDITOR");
			aclLevelNames.add("ACL.LEVEL_DESIGNER");
			aclLevelNames.add("ACL.LEVEL_MANAGER");

			Name name = session.createName(session.getEffectiveUserName());
			this.userNameCommon = name.getCommon();
			this.userNameAbbreviated = name.getAbbreviated();
			this.userNameCanonical = name.getCanonical();
			this.emailAdress = (String) session.evaluate(
					"@NameLookup( [Exhaustive] ; \""
							+ session.getEffectiveUserName()
							+ "\"; \"InternetAddress\")").elementAt(0);
			this.mailFilePath = (String) session.evaluate(
					"@NameLookup( [Exhaustive] ; \""
							+ session.getEffectiveUserName()
							+ "\"; \"MailFile\")").elementAt(0);
			this.aclLevel = session.getCurrentDatabase().queryAccess(
					this.userNameCanonical);
			this.aclLevelName = aclLevelNames.get(this.aclLevel);
			this.userRoles = this.implode(session.getCurrentDatabase()
					.queryAccessRoles(this.userNameCanonical));

			System.out.println(this.userNameCanonical);

		} catch (NotesException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String getUserNameAbbreviated() {
		return userNameAbbreviated;
	}

	public String getUserNameCanonical() {
		return userNameCanonical;
	}

	public String getAclLevelName() {
		return aclLevelName;
	}

	public String getEmailAdress() {
		return emailAdress;
	}

	public String getMailFilePath() {
		return mailFilePath;
	}

	public int getAclLevel() {
		return aclLevel;
	}

	public String getUserNameCommon() {
		return userNameCommon;
	}

	public String getUserRoles() {
		return userRoles;
	}

	private String implode(Vector v) {
		StringBuilder builder = new StringBuilder();
		Enumeration e = v.elements();
		while (e.hasMoreElements()) {
			builder.append(e.nextElement().toString());
		}

		return builder.toString();
	}

	public boolean hasRole(String roleName) {
		return (this.userRoles.indexOf(roleName) != -1);
	}

	public static Session getCurrentSession() {
		FacesContext context = FacesContext.getCurrentInstance();
		return (Session) context.getApplication().getVariableResolver()
				.resolveVariable(context, "session");
	}
}

Tagged with bean user roles acl