+ Start a Discussion
sgkmillssgkmills 

Controller Extension seems to be running in user mode not system mode

All,

 

I have written a controller extension which is extending the standard controller of an object, say abc__c.  Within the controller extension, I have declared the class with the keyword 'without sharing'.  In the abc__c object, there is a field that only Admins can update, call it PRM__c.  Within the controller extension, I write to the the PRM__c field and then try to update the abc__c record. 

 

To test the code, I login as a standard user and click the button that kicks off the controller extension class.  When the update abc__c code executes, I get a validation error stating that the update failed.  The validation rule checks the PRM__c field == True and $Profile.Name <> "System Administrator".  When I look in the debug logs, I see that the $Profile.Name is the value of the standard user 'SU' (who I am currently logged in as), but I was expecting it to be 'System Administrator' since the APEX code should be running in System Mode. An excerpt from my log is below:

 

 

|VALIDATION_FORMULA|PRM__c = True && 
$Profile.Name <> "System Administrator"|Primary__c=1 , $Profile.Name=SU
17:59:06.150 (150752000)|VALIDATION_FAIL
17:59:06.150 (150803000)|CODE_UNIT_FINISHED|Validation:abc:a0l50000009lmGk
17:59:06.152 (152315000)|FATAL_ERROR|System.DmlException: Update failed. First exception on row 0 with id a0l50000009lmGk; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION,....

 

I also looked in the the Visual Force Developer's Guide and on Pg. 11 it states the below:

 

Because standard controllers execute in user mode, in which the permissions, field-level security, and sharing rules of the
current user are enforced, extending a standard controller allows you to build a Visualforce page that respects user permissions.
Although the extension class executes in system mode, the standard controller executes in user mode. As with custom
controllers, you can specify whether a user can execute methods in a controller extension based on the user's profile.
Note: Although custom controllers and controller extension classes execute in system mode and thereby ignore
profile-based permissions and field-level security, you can choose whether they respect a user's organization-wide
defaults, role hierarchy, and sharing rules by using the with sharing keywords in the class definition. For information,
see “Using the with sharing or without sharing Keywords” in the Force.com Apex Code Developer's Guide

 

So is the problem that even though my controller extension is set up with 'without sharing', the standard controller, abc__c, runs in user mode?  Consequently, the field PRM__c is read_only for the standard user profile (SU), therefore the update in the controller extension to the PRM__c field in the abc__c object fails!

 

If this is the case, how can I allow the controller extension to update the PRM__c field?  If it isn't, has anyone come across this situation and has a solution?

 

Thanks in advance.

Best Answer chosen by Admin (Salesforce Developers) 
Starz26Starz26

The issue is your validation rule...

 

While the extension WILL return records to which the user typically cannot see, it does not mean it will allow them to Perform DML on records that violate teh OWD and CRUD.

 

While the page displays records that the user normally would not have access to the validation rule continues to enforce the OWD and CRUD as it is not part of the class.

 

This Line $Profile.Name <> "System Administrator" explicitly states to check the profile of the user and that does not change just because the class is without sharing.

 

You will need to modify the the field level security to only allow that field to be visible to the system admin. The remove the profile= from the validation rule. Then you will be able to update it in your controller...

 

 

All Answers

Starz26Starz26

The issue is your validation rule...

 

While the extension WILL return records to which the user typically cannot see, it does not mean it will allow them to Perform DML on records that violate teh OWD and CRUD.

 

While the page displays records that the user normally would not have access to the validation rule continues to enforce the OWD and CRUD as it is not part of the class.

 

This Line $Profile.Name <> "System Administrator" explicitly states to check the profile of the user and that does not change just because the class is without sharing.

 

You will need to modify the the field level security to only allow that field to be visible to the system admin. The remove the profile= from the validation rule. Then you will be able to update it in your controller...

 

 

This was selected as the best answer
sgkmillssgkmills

Thank you for the prompt response.  I did some more testing to get clarity and indeed everything you stated was correct.  Thanks again for the answer and pointing me in the right direction.:smileywink: 

 

I marked your answer as the Accepted Solution so others can benefit from it.

G!R!G!R!

Hi Starz26,

 

I am having confusion with system modes and user modes in standard controller , extension controller and cutome controller

 

after refering docs and forums...

 

Standard controller runs in "user mode" where it respects all user permissions

1. what user permission does it respects..is it only sharing rules set by current running user or...all object level and field level permission aswell ?

 

Custome controllers ans extension controllers runs in "system mode" i.e it does not respect user permssion and users can see all data(objects,fields and records)

 

But in docs..it says by extending standard controller..we can build VF page that displays all the records that user will not have access normally.  ( normally --- I think may be in user mode.)

 

Does it mean by using extension class for standard controller....the standard controller turns in "system mode". If not how can we access all records if standard controller still in "user mode" itself

 

 

thanks in advance