VisualForce – How to detect if a user has write permissions for a field

I would like to display different elements to a user depending on whether they have write permissions for a certain field, or just read permissions.

Is it possible to detect user permissions for a field simply on a visualforce page?

Thanks

Answer

You have two options here….

  1. Visualforce Components. By using the apex:inputField and apex:outputField components to accept and display data to the user, the platform will automatically respect the current users Field Level Security. Hiding the field if it is not visible to them or making it read only if they cannot edit it. This is the easiest way to implement field level security in your pages.
  2. Visualforce $ObjectType.. You can also access this information using the $ObjectType global variable, as describe here. There is also a very good Wiki article showing more examples here (see ‘Manual CRUD and FLS Enforcement in VisualForce’). By removing the ‘get’ or ‘is’ prefix you can access any of the properties on the DescribeFieldResult object this way. A basic example is this.

    apex:outputText value=”{!contactName}” rendered=”{!$ObjectType.Contact.fields.Name.Accessible}”

  3. Apex. Although not strictly part of the Salesforce Security Review (at present). It is recommended that you check field level security in your Apex code as well. Though for complex code bases this is nearly impossible to maintain and keep up to date IMHO (some better platform support is needed here). In the meantime you can utilise the Apex Describe support to check this dynamically and if needed expose this information as bindings from your controller for which you can apply to the ‘rendered’ attribute on your components. Refer to this Wiki article for more information (see ‘Manual CRUD and FLS Enforcement in Apex Controllers’)

Attribution
Source : Link , Question Author : Joe , Answer Author : Andrew Fawcett

Leave a Comment