[SalesForce] How to check other user’s CRUD permission for an object (APEX)

I want to find out, if another user has READ permission on a specific object (I don't care about record access/sharing). Is there a way to accomplish this via APEX?

I do NOT want to find out if the running user has access to the object, since that is rather straight forward.

My use case is: I want to assign ownership of custom object records to users based on their properties. Unfortunately a user can only own a record if he has at least read access to the object. If I try to assign the owner regardless, I'll get an exception.

Also this must be applicable in a non-test context, so. runAs() won't do.

My question is: How can I check read (or CRUD in general) access to objects for other users?

Any help is appreciated.

Best Answer

You can try like below

String desireUserId = UserInfo.getUserId(); // or pass any other user id.
User u = [select profileId from user where id =: desireUserId];
String profileId = u.profileId;

List<ObjectPermissions> 0bjPermissionList = [SELECT Id, SObjectType, 
PermissionsRead FROM ObjectPermissions WHERE parentid in (select id from 
permissionset where PermissionSet.Profile.Id=: profileId)];

Before assigning, chedk if that user has PermissoinsRead field is true.