Last active
September 20, 2016 14:38
-
-
Save PatrickKwinten/8fa2c2163675b1391df883bc02102c9c to your computer and use it in GitHub Desktop.
java class to get information from a user for IBM Notes Domino XPages
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.quintessens.utils; | |
import java.io.Serializable; | |
import java.util.ArrayList; | |
import java.util.Enumeration; | |
import java.util.List; | |
import java.util.Vector; | |
import javax.faces.context.FacesContext; | |
import org.quintessens.comments.utils.JSFUtil; | |
import com.ibm.domino.xsp.module.nsf.NotesContext; | |
import lotus.domino.Database; | |
import lotus.domino.Document; | |
import lotus.domino.Name; | |
import lotus.domino.NotesException; | |
import lotus.domino.Session; | |
public class UserInfo implements Serializable{ | |
private static final long serialVersionUID = 1L; | |
private final List<String> aclLevelNames = new ArrayList<String>(); | |
private int aclLevel; | |
private String aclLevelName; | |
private String userNameCommon; | |
private String userNameAbbreviated; | |
private String userNameCanonical; | |
private String userRoles; | |
private String emailAdress; | |
private String mailFilePath; | |
private String activeDb; | |
public final List<String> aclPriviliges = new ArrayList<String>(); | |
public void init(String databaseName, String userName) throws NotesException{ | |
Session session = null; | |
session = getCurrentSession(); | |
try{ | |
Database db = session.getDatabase("", databaseName); | |
this.activeDb = databaseName; | |
Name name = session.createName(userName); | |
System.out.println(name.getCommon()); | |
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"); | |
this.aclLevel = db.queryAccess(name.getCanonical()); | |
this.aclLevelName = aclLevelNames.get(this.aclLevel); | |
this.userNameCommon = name.getCommon(); | |
this.userNameAbbreviated = name.getAbbreviated(); | |
this.userNameCanonical = name.getCanonical(); | |
this.emailAdress = (String) session.evaluate( | |
"@NameLookup( [Exhaustive] ; \"" | |
+ this.userNameCanonical | |
+ "\"; \"InternetAddress\")").elementAt(0); | |
this.mailFilePath = (String) session.evaluate( | |
"@NameLookup( [Exhaustive] ; \"" | |
+ this.userNameCanonical | |
+ "\"; \"MailFile\")").elementAt(0); | |
this.userRoles = this.implode(db.queryAccessRoles(this.userNameCanonical)); | |
int accPriv = db.queryAccessPrivileges(this.userNameCanonical); | |
if ((accPriv & db.DBACL_CREATE_DOCS) > 0){ | |
if (!aclPriviliges.contains("DBACL_CREATE_DOCS")){ | |
aclPriviliges.add("DBACL_CREATE_DOCS"); | |
} | |
} | |
if ((accPriv & db.DBACL_DELETE_DOCS) > 0){ | |
if (!aclPriviliges.contains("DBACL_DELETE_DOCS")){ | |
aclPriviliges.add("DBACL_DELETE_DOCS"); | |
} | |
} | |
if ((accPriv & db.DBACL_CREATE_PRIV_AGENTS) > 0){ | |
if (!aclPriviliges.contains("DBACL_CREATE_PRIV_AGENTS")){ | |
aclPriviliges.add("DBACL_CREATE_PRIV_AGENTS"); | |
} | |
} | |
if ((accPriv & db.DBACL_CREATE_PRIV_FOLDERS_VIEWS) > 0){ | |
if (!aclPriviliges.contains("DBACL_CREATE_PRIV_FOLDERS_VIEWS")){ | |
aclPriviliges.add("DBACL_CREATE_PRIV_FOLDERS_VIEWS"); | |
} | |
} | |
if ((accPriv & db.DBACL_CREATE_SHARED_FOLDERS_VIEWS) > 0){ | |
if (!aclPriviliges.contains("DBACL_CREATE_SHARED_FOLDERS_VIEWS")){ | |
aclPriviliges.add("DBACL_CREATE_SHARED_FOLDERS_VIEWS"); | |
} | |
} | |
if ((accPriv & db.DBACL_CREATE_SCRIPT_AGENTS) > 0){ | |
if (!aclPriviliges.contains("DBACL_CREATE_SCRIPT_AGENTS")){ | |
aclPriviliges.add("DBACL_CREATE_SCRIPT_AGENTS"); | |
} | |
} | |
if ((accPriv & db.DBACL_READ_PUBLIC_DOCS) > 0){ | |
if (!aclPriviliges.contains("DBACL_READ_PUBLIC_DOCS")){ | |
aclPriviliges.add("DBACL_READ_PUBLIC_DOCS"); | |
} | |
} | |
if ((accPriv & db.DBACL_WRITE_PUBLIC_DOCS) > 0){ | |
if (!aclPriviliges.contains("DBACL_WRITE_PUBLIC_DOCS")){ | |
aclPriviliges.add("DBACL_WRITE_PUBLIC_DOCS"); | |
} | |
} | |
if ((accPriv & db.DBACL_REPLICATE_COPY_DOCS) > 0){ | |
if (!aclPriviliges.contains("DBACL_REPLICATE_COPY_DOCS")){ | |
aclPriviliges.add("DBACL_REPLICATE_COPY_DOCS"); | |
} | |
} | |
} | |
catch (NotesException e) { | |
e.printStackTrace(); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} | |
public static Session getCurrentSession() { | |
FacesContext context = FacesContext.getCurrentInstance(); | |
return (Session) context.getApplication().getVariableResolver().resolveVariable(context, "session"); | |
} | |
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 boolean canWriteDocument(String docId, String userName) { | |
boolean canWrite = false; | |
Session session = null; | |
session = getCurrentSession(); | |
try { | |
Database db = session.getDatabase("", this.activeDb); | |
if (db.isDocumentLockingEnabled()) { | |
//Document locking is enabled | |
Document doc = db.getDocumentByUNID(docId); | |
if (null != doc){ | |
if (doc.lock(userName)) { | |
canWrite = true; | |
doc.unlock(); | |
} | |
} | |
} else { | |
//Document locking is NOT enabled | |
} | |
} catch (NotesException e) { | |
// fail silently | |
e.printStackTrace(); | |
} | |
return canWrite; | |
} | |
public boolean canEdit(String docId){ | |
boolean canEdit = false; | |
NotesContext ctx = new NotesContext(null).getCurrent(); | |
Session session = null; | |
session = getCurrentSession(); | |
try { | |
Database db = session.getDatabase("", this.activeDb); | |
Document doc = db.getDocumentByUNID(docId); | |
if(null != doc){ | |
canEdit = ctx.isDocEditable(doc); | |
} | |
} catch (NotesException e) { | |
// TODO Auto-generated catch block | |
e.printStackTrace(); | |
} | |
return canEdit; | |
} | |
public int getAclLevel() { | |
return aclLevel; | |
} | |
public void setAclLevel(int aclLevel) { | |
this.aclLevel = aclLevel; | |
} | |
public List<String> getAclLevelNames() { | |
return aclLevelNames; | |
} | |
public String getAclLevelName() { | |
return aclLevelName; | |
} | |
public void setAclLevelName(String aclLevelName) { | |
this.aclLevelName = aclLevelName; | |
} | |
public String getUserNameCommon() { | |
return userNameCommon; | |
} | |
public void setUserNameCommon(String userNameCommon) { | |
this.userNameCommon = userNameCommon; | |
} | |
public String getUserNameAbbreviated() { | |
return userNameAbbreviated; | |
} | |
public void setUserNameAbbreviated(String userNameAbbreviated) { | |
this.userNameAbbreviated = userNameAbbreviated; | |
} | |
public String getUserNameCanonical() { | |
return userNameCanonical; | |
} | |
public void setUserNameCanonical(String userNameCanonical) { | |
this.userNameCanonical = userNameCanonical; | |
} | |
public String getUserRoles() { | |
return userRoles; | |
} | |
public void setUserRoles(String userRoles) { | |
this.userRoles = userRoles; | |
} | |
public String getEmailAdress() { | |
return emailAdress; | |
} | |
public void setEmailAdress(String emailAdress) { | |
this.emailAdress = emailAdress; | |
} | |
public String getMailFilePath() { | |
return mailFilePath; | |
} | |
public void setMailFilePath(String mailFilePath) { | |
this.mailFilePath = mailFilePath; | |
} | |
public List<String> getAclPriviliges() { | |
return aclPriviliges; | |
} | |
public String getActiveDb() { | |
return activeDb; | |
} | |
public void setActiveDb(String activeDb) { | |
this.activeDb = activeDb; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment