| | 1 | | using Microsoft.Crm.Sdk.Messages; |
| | 2 | | using Microsoft.Xrm.Sdk; |
| | 3 | | using System; |
| | 4 | | using System.Collections.Generic; |
| | 5 | | using System.Linq; |
| | 6 | |
|
| | 7 | | namespace FakeXrmEasy.Permissions |
| | 8 | | { |
| | 9 | | public class AccessRightsRepository : IAccessRightsRepository |
| | 10 | | { |
| | 11 | | protected Dictionary<EntityReference, List<PrincipalAccess>> _accessRights; |
| | 12 | |
|
| 4270 | 13 | | public AccessRightsRepository() |
| 4270 | 14 | | { |
| | 15 | | //One record might be accessed from many security principals |
| 4270 | 16 | | _accessRights = new Dictionary<EntityReference, List<PrincipalAccess>>(); |
| 4270 | 17 | | } |
| | 18 | |
|
| | 19 | | /// <summary> |
| | 20 | | /// Grants the specified rights to the security principal (user or team) for the specified record |
| | 21 | | /// </summary> |
| | 22 | | /// <param name="er"></param> |
| | 23 | | /// <param name="pa"></param> |
| | 24 | | public void GrantAccessTo(EntityReference er, PrincipalAccess pa) |
| 118 | 25 | | { |
| 118 | 26 | | List<PrincipalAccess> accessList = GetAccessListForRecord(er); |
| 154 | 27 | | PrincipalAccess paMatch = accessList.Where(p => p.Principal.Id == pa.Principal.Id).SingleOrDefault(); |
| 118 | 28 | | if (paMatch == null) |
| 112 | 29 | | accessList.Add(pa); |
| 118 | 30 | | } |
| | 31 | |
|
| | 32 | | /// <summary> |
| | 33 | | /// Modify access on a specific record |
| | 34 | | /// </summary> |
| | 35 | | /// <param name="er">The entity for which we are modifying permissions</param> |
| | 36 | | /// <param name="pa">The permissions to overwrite</param> |
| | 37 | | public void ModifyAccessOn(EntityReference er, PrincipalAccess pa) |
| 12 | 38 | | { |
| 12 | 39 | | List<PrincipalAccess> accessList = GetAccessListForRecord(er); |
| 18 | 40 | | PrincipalAccess paMatch = accessList.Where(p => p.Principal.Id == pa.Principal.Id).SingleOrDefault(); |
| 12 | 41 | | if (paMatch != null) |
| 6 | 42 | | { |
| 6 | 43 | | accessList[accessList.IndexOf(paMatch)] = pa; |
| 6 | 44 | | } |
| | 45 | | else |
| 6 | 46 | | { |
| 6 | 47 | | accessList.Add(pa); |
| 6 | 48 | | } |
| 12 | 49 | | } |
| | 50 | |
|
| | 51 | | /// <summary> |
| | 52 | | /// Retrieves the RetrievePrincipalAccessResponse for the specified security principal (user or team) and record |
| | 53 | | /// </summary> |
| | 54 | | /// <param name="er"></param> |
| | 55 | | /// <param name="principal"></param> |
| | 56 | | public RetrievePrincipalAccessResponse RetrievePrincipalAccess(EntityReference er, EntityReference principal) |
| 106 | 57 | | { |
| 106 | 58 | | List<PrincipalAccess> accessList = GetAccessListForRecord(er); |
| 243 | 59 | | PrincipalAccess pAcc = accessList.Where(pa => pa.Principal.Id == principal.Id).SingleOrDefault(); |
| 106 | 60 | | RetrievePrincipalAccessResponse resp = new RetrievePrincipalAccessResponse(); |
| | 61 | |
|
| 106 | 62 | | if (pAcc != null) |
| 71 | 63 | | resp.Results["AccessRights"] = pAcc.AccessMask; |
| | 64 | |
|
| 106 | 65 | | return resp; |
| 106 | 66 | | } |
| | 67 | |
|
| | 68 | | /// <summary> |
| | 69 | | /// Retrieves the list of permitted security principals (user or team) that have access to the given record |
| | 70 | | /// </summary> |
| | 71 | | /// <param name="er"></param> |
| | 72 | | /// <returns></returns> |
| | 73 | | public RetrieveSharedPrincipalsAndAccessResponse RetrieveSharedPrincipalsAndAccess(EntityReference er) |
| 36 | 74 | | { |
| 36 | 75 | | List<PrincipalAccess> accessList = GetAccessListForRecord(er); |
| 36 | 76 | | RetrieveSharedPrincipalsAndAccessResponse resp = new RetrieveSharedPrincipalsAndAccessResponse(); |
| 36 | 77 | | resp.Results["PrincipalAccesses"] = accessList.ToArray(); |
| 36 | 78 | | return resp; |
| 36 | 79 | | } |
| | 80 | |
|
| | 81 | | /// <summary> |
| | 82 | | /// Revokes the specified rights to the security principal (user or team) for the specified record |
| | 83 | | /// </summary> |
| | 84 | | /// <param name="er"></param> |
| | 85 | | /// <param name="pa"></param> |
| | 86 | | public void RevokeAccessTo(EntityReference er, EntityReference principal) |
| 17 | 87 | | { |
| 17 | 88 | | List<PrincipalAccess> accessList = GetAccessListForRecord(er); |
| | 89 | |
|
| 80 | 90 | | for (int x = accessList.Count - 1; x >= 0; x--) |
| 23 | 91 | | { |
| 23 | 92 | | PrincipalAccess pa = accessList[x]; |
| 23 | 93 | | if (pa.Principal.Id == principal.Id) |
| 17 | 94 | | accessList.RemoveAt(x); |
| 23 | 95 | | } |
| 17 | 96 | | } |
| | 97 | |
|
| | 98 | | /// <summary> |
| | 99 | | /// Retrieves all principals (security principals) who have any access to the specified record |
| | 100 | | /// </summary> |
| | 101 | | /// <param name="er"></param> |
| | 102 | | public void GetAllPrincipalAccessFor(EntityReference er) |
| 0 | 103 | | { |
| 0 | 104 | | throw new NotImplementedException(); |
| | 105 | | } |
| | 106 | |
|
| | 107 | | /// <summary> |
| | 108 | | /// Fetches the List<PrincipalAccess> for the given EntityReference |
| | 109 | | /// </summary> |
| | 110 | | /// <param name="er"></param> |
| | 111 | | private List<PrincipalAccess> GetAccessListForRecord(EntityReference er) |
| 289 | 112 | | { |
| 289 | 113 | | List<PrincipalAccess> accessList = null; |
| 289 | 114 | | if (!_accessRights.TryGetValue(er, out accessList)) |
| 94 | 115 | | { |
| 94 | 116 | | accessList = new List<PrincipalAccess>(); |
| 94 | 117 | | _accessRights.Add(er, accessList); |
| 94 | 118 | | } |
| | 119 | |
|
| 289 | 120 | | return accessList; |
| 289 | 121 | | } |
| | 122 | | } |
| | 123 | | } |