|  |  | 1 |  | using Microsoft.Crm.Sdk.Messages; | 
|  |  | 2 |  | using Microsoft.Xrm.Sdk; | 
|  |  | 3 |  | using System; | 
|  |  | 4 |  | using System.Linq; | 
|  |  | 5 |  |  | 
|  |  | 6 |  | namespace FakeXrmEasy.FakeMessageExecutors | 
|  |  | 7 |  | { | 
|  |  | 8 |  |     public class WhoAmIRequestExecutor : IFakeMessageExecutor | 
|  |  | 9 |  |     { | 
|  |  | 10 |  |         public bool CanExecute(OrganizationRequest request) | 
|  | 30 | 11 |  |         { | 
|  | 30 | 12 |  |             return request is WhoAmIRequest; | 
|  | 30 | 13 |  |         } | 
|  |  | 14 |  |  | 
|  |  | 15 |  |         public OrganizationResponse Execute(OrganizationRequest request, XrmFakedContext ctx) | 
|  | 30 | 16 |  |         { | 
|  | 30 | 17 |  |             var req = request as WhoAmIRequest; | 
|  |  | 18 |  |  | 
|  | 30 | 19 |  |             var callerId = ctx.CallerId.Id; | 
|  |  | 20 |  |  | 
|  | 30 | 21 |  |             var results = new ParameterCollection { | 
|  | 30 | 22 |  |               { "UserId", callerId } | 
|  | 30 | 23 |  |             }; | 
|  |  | 24 |  |  | 
|  | 30 | 25 |  |             var user = ctx.CreateQuery("systemuser") | 
|  | 30 | 26 |  |                           .Where(u => u.Id == callerId) | 
|  | 30 | 27 |  |                           .SingleOrDefault(); | 
|  |  | 28 |  |  | 
|  | 54 | 29 |  |             if(user != null) { | 
|  | 24 | 30 |  |               var buId = GetBusinessUnitId(user); | 
|  | 24 | 31 |  |               results.Add("BusinessUnitId", buId); | 
|  |  | 32 |  |  | 
|  | 24 | 33 |  |               var orgId = GetOrganizationId(ctx, user, buId); | 
|  | 24 | 34 |  |               results.Add("OrganizationId", orgId); | 
|  | 24 | 35 |  |             } | 
|  |  | 36 |  |  | 
|  | 30 | 37 |  |             var response = new WhoAmIResponse | 
|  | 30 | 38 |  |             { | 
|  | 30 | 39 |  |                 Results = results | 
|  | 30 | 40 |  |             }; | 
|  | 30 | 41 |  |             return response; | 
|  | 30 | 42 |  |         } | 
|  |  | 43 |  |  | 
|  |  | 44 |  |         public Type GetResponsibleRequestType() | 
|  | 4270 | 45 |  |         { | 
|  | 4270 | 46 |  |             return typeof(WhoAmIRequest); | 
|  | 4270 | 47 |  |         } | 
|  |  | 48 |  |  | 
|  | 24 | 49 |  |         private static Guid GetBusinessUnitId(Entity user) { | 
|  | 24 | 50 |  |           var buRef = user.GetAttributeValue<EntityReference>("businessunitid"); | 
|  | 24 | 51 |  |           var buId = buRef != null ? buRef.Id : Guid.Empty; | 
|  | 24 | 52 |  |           return buId; | 
|  | 24 | 53 |  |         } | 
|  |  | 54 |  |  | 
|  | 24 | 55 |  |         private static Guid GetOrganizationId(XrmFakedContext ctx, Entity user, Guid buId) { | 
|  | 24 | 56 |  |           var orgId = user.GetAttributeValue<Guid?>("organizationid") ?? Guid.Empty; | 
|  | 36 | 57 |  |           if(orgId == Guid.Empty) { | 
|  | 12 | 58 |  |             var bu = ctx.CreateQuery("businessunit") | 
|  | 12 | 59 |  |                         .Where(b => b.Id == buId) | 
|  | 12 | 60 |  |                         .SingleOrDefault(); | 
|  | 12 | 61 |  |             var orgRef = bu.GetAttributeValue<EntityReference>("organizationid"); | 
|  | 12 | 62 |  |             orgId = orgRef?.Id ?? Guid.Empty; | 
|  | 12 | 63 |  |           } | 
|  |  | 64 |  |  | 
|  | 24 | 65 |  |           return orgId; | 
|  | 24 | 66 |  |         } | 
|  |  | 67 |  |  | 
|  |  | 68 |  |     } | 
|  |  | 69 |  | } |