Tell me more ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

If I know the name of a certain profile, I would like to obtain CRUD permission settings for each object in the profile using Apex.

I see that there is an sObject called Profile, but this only reveals the boolean values of permissions such as Edit Case Comments, or Transfer Cases etc.

There is another sObject called ObjectPermissions which you can get this info from, but this is only for Permission Sets and not for Profiles.

I would like to determine whether a certain profile can read and edit Case records just as the ObjectPermissions sObject allows you to do for Permission Sets.

Is this possible?

Thanks

share|improve this question

3 Answers

up vote 9 down vote accepted

Actually, the PermissionSet SObject has a field in it called IsOwnedByProfile. Take a look at this blog post by Adam Torman, http://blogs.developerforce.com/engineering/2012/06/using-soql-to-determine-your-users-permissions-2.html:

This field determines whether a permission set is a custom one or if it is parented by a profile. This is made possible because for every profile, there is one underlying permission set. That way, permissions layer equally across permission sets without having to treat a profile differently.

So, if you want to find the CRUD permissions of a profile you can do something like this:

SELECT Id, SObjectType, PermissionsRead, PermissionsCreate
FROM ObjectPermissions
WHERE parentid in (select id from permissionset where
PermissionSet.Profile.Name = 'System Administrator')
share|improve this answer
Nice one Daniel, I was searching for this myself, since the question relates to know what is set on a profile rather than the current users permissions. Was about to break out the Metadata API from Apex! ;-) – Andrew Fawcett Oct 28 '12 at 20:19
That's great. Thanks Daniel. I wondered why I was getting so many permission sets returned when I was playing around with some SOQL queries before. I will try and put this to good use. – Joe Oct 29 '12 at 21:01
Awesome Daniel! Thanks for the call out as well!! Please keep in mind that accessing PermissionSetAssignment, PermissionSet, ObjectPermissions, FieldPermissions, or SetupEntityAccess for a Profile is read-only - you can't update these permissions or assignments using Permission Sets that are owned by profiles. – Adam Torman Nov 30 '12 at 0:36

Doing this is pretty much possible both in Apex and Visualforce code. The platform gives rich describe information and shorthands which check the current user's CRUD or FLS permissions.

Here are few code snippets

To know if few fields are accessible

<apex:outputText value="{!accountBillingAddress}" rendered="{!AND($ObjectType.Account.fields.BillingCity.Accessible, $ObjectType.Account.fields.BillingState.Accessible)}"/> 

To check the same in Apex code

     if (Schema.sObjectType.Account.fields.BillingCity.isAccessible()
          &&
        Schema.sObjectType.Account.fields.BillingState.isAccessible()  
        ){ }

Similarly individual objects can be checked as well, for ex:

<apex:relatedList list="Contacts" rendered="{!$ObjectType.Case.accessible}"/>

This guide has lot of rich details about secure coding guidelines for further reading: http://wiki.developerforce.com/page/Secure_Coding_Guideline

Source : VF Dev guide (http://www.salesforce.com/us/developer/docs/pages/Content/pages_controller_std_checking_accessibility.htm) and Developer force WIKI

share|improve this answer
Describe calls (from Apex or VF) work only for current $User. Given that OP is talking about name of given profile I don't think he wants to use it only for current user... – eyescream Oct 28 '12 at 19:37

sObject Describe Result Methods and Describe Field Result Methods is very helpful.

Here is the guide for this .

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_sobject_describe.htm

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_fields_describe.htm

Here are the some of the methods that can be helpful for Object Type

Schema.DescribeSObjectResult R = Account.SObjectType.getDescribe();
boolean canAcess=R.isAccessible;
boolean canCreate=R.isCreateable;
boolean candelete=R.isDeletable;
boolean canUpdate=R.isUpdateable;

Here are some how you may use for the fields

Schema.DescribeFieldResult F = Account.AccountNumber.getDescribe();
boolean canAccess=F.isAccessible;
boolean CanUpdate=F.isUpdateable;
share|improve this answer
Thanks for your answer. I have read into the Apex Describe Information methods and they look very useful, but I am really looking to be able to determine the CRUD permissions of a Profile rather than the running user. I could select a user from each of the profiles in an app, but I think it is possible for an application not to have any users assigned to it. – Joe Oct 28 '12 at 18:05

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.