1
votes

I have a scenario that I need to change some fields of an entity which user doesn't have access at all. I'd like to know if there is some way to in the moment that my plugin business logic is running I increment to the calling user permission the permission to update the entity and after that I take it out from him.

Access Right Employee: has no acces upon Account at all (Bryan's current Access Right). Access Right Manager: has access to update Account.

Scenario: Every time "Bryan" creates an opportunity runs a plugin on create of it that increment in one a field named new_count in the whole accounts of the system. (I know this cenario is stupid without any sense, but anyway). So we put a plugin on create of the lead. Bryan creates a lead, in plugin I add his Access Rights with Manager just to run the plugin, after the update I take it away from him.

was I clear? If so, I'm hoping for an answer!

Thanks

2

2 Answers

3
votes

You have two options

  1. In plugin registration tool, register your plugin to run in System user context

  2. Or in plugin, create web service with null as userid.

IOrganizationService adminservice = serviceFactory.CreateOrganizationService(null);

All calls to adminservice methods will be executed as SystemUser with administrator permissions, so you can update any field on any entity regardless current users permissions.

0
votes

I've finally found out a solution to my issue.

To get it done properly I had to do the following steps.

  1. I Create both services: IOrganizationService administratorService = serviceFactory.CreateOrganizationService(null);(Thanks MarioZG);

IOrganizationService service = serviceFactory.CreateOrganizationService(context.UserId);

  1. Create a team which has Administrator's permission

  2. In the beginning of my plugin using that "administratorService" I assign the calling user to that team.

  3. I do whatever I want using the calling user (using my IOrganizationService named service).

  4. Finally I take of the user from the team.