Can this user Edit this document?

Sometimes, we develop applications in Notes. Well, most of my time I do. And sometimes, I find that I just cant figure something out. I'm convinced that there's some magic-bullet function that allows us to quickly do something. For instance, today, I wanted to show something on a web or notes form, but only if the user was allowed to edit this form. Now, remember the first rule of lazy programming: Do as little as possible. So after quite un-lazily searching for stuff, I chatted with Julian and he mentioned that he had to code something like this too. Horrible, I thought. Oh well.





So lets create a field using @formula, which will give us "1" for 'this user is allowed to edit this document', otherwise "0"




rem {if he's lower than author, say no. Higher than author, say 'yes' };

thislevel := @userAccess(DbName);

@if(thisLevel < 3; @return("0"); thisLevel > 3; @return ("1"); "");





rem { Lets get a list of allowed authors for this document } ;

allowedAuthors := @name([Canonicalize]; @Author);

@if (allowedAuthors *= @UserNamesList; @return("1"); "");



Rem {default to 'no' };

"0";



So whats the magic ?

  • @userNamesList returns all the users name variants, as well as his hierachical name wildcards, a global wildcard and (on a server) all the groups he belongs to - including nesting, and all the roles for this database. Its like a babel fish. One suspects this is exactly how the database itself works out permissions. Its far too useful to exist otherwise!

  • The *= (permuted equality) operator compares every permutation of both lists and if one or more pair is equal, returns true. Pass in two lists and if one item is in both sides - bingo.

  • @Author returns a list of all allowed authors for this form. Watch out as its not in canonicalized format.

  • @return allows me to bale out as quickly as possible without having to evaluate the rest of the formula.



So there you have it. 15 years into notes and this still made me stop and think. Mostly think:

  • Formula language is dammed powerful.

  • I wish Formula language had a debugger!