| | 1 | | #if FAKE_XRM_EASY_2013 || FAKE_XRM_EASY_2015 || FAKE_XRM_EASY_2016 || FAKE_XRM_EASY_365 || FAKE_XRM_EASY_9 |
| | 2 | | using System; |
| | 3 | | using System.Linq; |
| | 4 | | using Microsoft.Xrm.Sdk; |
| | 5 | | using System.ServiceModel; |
| | 6 | | using Microsoft.Crm.Sdk.Messages; |
| | 7 | |
|
| | 8 | | namespace FakeXrmEasy.FakeMessageExecutors |
| | 9 | | { |
| | 10 | | public class AddUserToRecordTeamRequestExecutor : IFakeMessageExecutor |
| | 11 | | { |
| | 12 | | public bool CanExecute(OrganizationRequest request) |
| 5 | 13 | | { |
| 5 | 14 | | return request is AddUserToRecordTeamRequest; |
| 5 | 15 | | } |
| | 16 | |
|
| | 17 | | public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx) |
| 5 | 18 | | { |
| 5 | 19 | | AddUserToRecordTeamRequest addReq = (AddUserToRecordTeamRequest)request; |
| | 20 | |
|
| 5 | 21 | | EntityReference target = addReq.Record; |
| 5 | 22 | | Guid systemuserId = addReq.SystemUserId; |
| 5 | 23 | | Guid teamTemplateId = addReq.TeamTemplateId; |
| | 24 | |
|
| 5 | 25 | | if (target == null) |
| 0 | 26 | | { |
| 0 | 27 | | throw new FaultException<OrganizationServiceFault>(new OrganizationServiceFault(), "Can not add to team |
| | 28 | | } |
| | 29 | |
|
| 5 | 30 | | if (systemuserId == Guid.Empty) |
| 0 | 31 | | { |
| 0 | 32 | | throw new FaultException<OrganizationServiceFault>(new OrganizationServiceFault(), "Can not add to team |
| | 33 | | } |
| | 34 | |
|
| 5 | 35 | | if (teamTemplateId == Guid.Empty) |
| 0 | 36 | | { |
| 0 | 37 | | throw new FaultException<OrganizationServiceFault>(new OrganizationServiceFault(), "Can not add to team |
| | 38 | | } |
| | 39 | |
|
| 5 | 40 | | IOrganizationService service = ctx.GetOrganizationService(); |
| | 41 | |
|
| 5 | 42 | | Entity teamTemplate = ctx.CreateQuery("teamtemplate").FirstOrDefault(p => p.Id == teamTemplateId); |
| 5 | 43 | | if (teamTemplate == null) |
| 0 | 44 | | { |
| 0 | 45 | | throw new FaultException<OrganizationServiceFault>(new OrganizationServiceFault(), "Team template with i |
| | 46 | | } |
| | 47 | |
|
| 5 | 48 | | Entity user = ctx.CreateQuery("systemuser").FirstOrDefault(p => p.Id == systemuserId); |
| 5 | 49 | | if (user == null) |
| 0 | 50 | | { |
| 0 | 51 | | throw new FaultException<OrganizationServiceFault>(new OrganizationServiceFault(), "User with id=" + tea |
| | 52 | | } |
| | 53 | |
|
| | 54 | |
|
| 5 | 55 | | Entity team = ctx.CreateQuery("team").FirstOrDefault(p => ((EntityReference)p["teamtemplateid"]).Id == teamT |
| 5 | 56 | | if (team == null) |
| 5 | 57 | | { |
| 5 | 58 | | team = new Entity("team") |
| 5 | 59 | | { |
| 5 | 60 | | ["teamtemplateid"] = new EntityReference("teamtemplate", teamTemplateId) |
| 5 | 61 | | }; |
| 5 | 62 | | team.Id = service.Create(team); |
| 5 | 63 | | } |
| | 64 | |
|
| 5 | 65 | | Entity tm = new Entity("teammembership") |
| 5 | 66 | | { |
| 5 | 67 | | ["systemuserid"] = systemuserId, |
| 5 | 68 | | ["teamid"] = team.Id |
| 5 | 69 | | }; |
| 5 | 70 | | tm.Id = service.Create(tm); |
| | 71 | |
|
| 5 | 72 | | Entity poa = new Entity("principalobjectaccess") |
| 5 | 73 | | { |
| 5 | 74 | | ["objectid"] = target.Id, |
| 5 | 75 | | ["principalid"] = team.Id, |
| 5 | 76 | | ["accessrightsmask"] = teamTemplate.Contains("defaultaccessrightsmask") ? teamTemplate["defaultaccessrig |
| 5 | 77 | | }; |
| 5 | 78 | | poa.Id = service.Create(poa); |
| | 79 | |
|
| 5 | 80 | | ctx.AccessRightsRepository.GrantAccessTo(target, new PrincipalAccess |
| 5 | 81 | | { |
| 5 | 82 | | Principal = user.ToEntityReference(), |
| 5 | 83 | | AccessMask = (AccessRights)poa["accessrightsmask"] |
| 5 | 84 | | }); |
| | 85 | |
|
| 5 | 86 | | return new AddUserToRecordTeamResponse |
| 5 | 87 | | { |
| 5 | 88 | | ResponseName = "AddUserToRecordTeam" |
| 5 | 89 | | }; |
| 5 | 90 | | } |
| | 91 | |
|
| | 92 | | public Type GetResponsibleRequestType() |
| 3597 | 93 | | { |
| 3597 | 94 | | return typeof(AddUserToRecordTeamRequest); |
| 3597 | 95 | | } |
| | 96 | | } |
| | 97 | | } |
| | 98 | | #endif |