For a project I needed to check which rights a user has in a database and on documents. The UserBean in the Extension Library did not seem to match my needs since that the rights of the current effective user against the database from which the code is called. In my case the documents reside in different databases than the one serving the Xpages.
Second stop was the UserBean class written by Oliver Busse. This allows me to change the effective username againt a given name.
Name name = session.createName(“a string here”);
The same principle goes for the database.
this.aclLevel = session.getDatabase(“a string here”,”a string here”).queryAccess(this.userNameCanonical);
The aclPriviliges are no part of the original code so I added something as followed:
public final List<String> aclPriviliges = new ArrayList<String>();
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”);
}
}…
I registered my class as a Managed Bean and I invoke it from the beforepageload event:
<xp:this.beforePageLoad><![CDATA[#{javascript:UserInfo.init(“location of my nsf”,”Ja user name here”);}]]></xp:this.beforePageLoad>
With this I can use it in the same way Oliver demonstrated in his snippet. But I can also check the acl Priviliges:
<xp:div>
<xp:label value=”ACL Priviliges:”></xp:label>
<xp:label value=”#{javascript:UserInfo.aclPriviliges}”></xp:label>
</xp:div>
Next I wanted to check if a user can edit a document or not. This is not that easy as it might seem. This code is what I took as starting-point. It became this piece of code:
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;
}
Unfortunately I cannot set the username, so it runs against against the effective username 😦
I tried to achieve the same functionality with the lock function for documents (which you have to enable on database level) but it fails. Even a given user with Reader access was able to lock a document (name added to $Writers field)
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;
}
I assume I am doing something wrong but I am not sure what. If you happen to know what then drop a comment.
Happy development 🙂
Add 20 years of experience to your workforce
You can 20 years of experience within IBM Notes and Web development to your workforce by hiring me.
Interested? Read my curriculum vitae on LinkedIn: http://www.linkedin.com/in/patrickkwinten and get in contact.
I am happy to work WITH you !