Learning XPages Part 32 : Using Roles To Make Fields Editable

Now that we can switch our page into Edit
Mode you’ll see that all of the fields are editable. In most applications
you may not want this so we can disable the ability to edit certain fields.
Start by opening the content_Person custom control again.

Lets disable a couple of the fields
that we don’t want the end user changing. Select the ‘FirstName’ field
and look at the properties. In the ‘Edit Box’ properties you will see a
‘Read Only’ tickbox. Turning this on will make the field uneditable. Nice
and simple for this particular field.

A picture named M2

You may also notice that the ‘Read-Only’
tickbox has a diamond beside it. You can use this to create a computed
result to determine if the field should be read only or not.

Lets say that we don’t want the end
user editing their Job Title but we do want somebody with the ‘PhonebookEditor’
role in the ACL to be able to update peoples job titles. First of all click
on the ‘JobTitle’ field in the design and then in the properties click
the diamond for the ‘Read-Only’ flag and select compute value.  In
the script editor that appears we need to enter in the code to see if the
currently logged in person has the role or not.  Here’s the code I’m
using :



var
s1 =
context.getUser().getRoles();

var
s2 =
"{PhonebookEditor}";

if(@Contains(s1,
s2) == @True())


        return
false;

else

        return
true;



NOTE : Replace curly brackets with square
brackets in the above code.

This very simple code could be reduced
in size but I’ve left it expanded so you can see how it is working, I am
setting up two variables and then doing an @contains to see if the role
exists inside the current users .getRoles value. This would be very similar
to how you would do it on a form in traditional notes client development.

In the next part I’ll add some validation
to the fields that can be edited so make sure the person enters in valid
data.

Advertisement
Tagged with: , , ,
Posted in None
3 comments on “Learning XPages Part 32 : Using Roles To Make Fields Editable
  1. Dude if all these great posts are penance for some sin, it must have been one helluva sin.Sorry for the interruption. Please continue…

    Like

  2. Erik Brooks says:

    What I want to know is, why wasn’t @UserRoles implemented in xPages? It seems like it would be really simple for that to be equivalent to context.getUser().getRoles()…..

    There’s many @functions like that — seemingly simple to implement, but they weren’t.

    Like

  3. Devin Olson says:

    @2 – I agree. I just realized today that @IsDocBeingEdited isn’t implemented either -grrrrr.

    Like

Comments are closed.

Archives
%d bloggers like this: