Summary

Class:FakeXrmEasy.Permissions.AccessRightsRepository
Assembly:FakeXrmEasy
File(s):C:\code\jordimontana82\fake-xrm-easy\FakeXrmEasy.Shared\Permissions\AccessRightsRepository.cs
Covered lines:53
Uncovered lines:2
Coverable lines:55
Total lines:123
Line coverage:96.3%
Branch coverage:100%

Metrics

MethodCyclomatic ComplexitySequence CoverageBranch Coverage
.ctor()1100100
GrantAccessTo(...)2100100
ModifyAccessOn(...)2100100
RetrievePrincipalAccess(...)2100100
RetrieveSharedPrincipalsAndAccess(...)1100100
RevokeAccessTo(...)3100100
GetAllPrincipalAccessFor(...)100
GetAccessListForRecord(...)2100100

File(s)

C:\code\jordimontana82\fake-xrm-easy\FakeXrmEasy.Shared\Permissions\AccessRightsRepository.cs

#LineLine coverage
 1using Microsoft.Crm.Sdk.Messages;
 2using Microsoft.Xrm.Sdk;
 3using System;
 4using System.Collections.Generic;
 5using System.Linq;
 6
 7namespace FakeXrmEasy.Permissions
 8{
 9    public class AccessRightsRepository : IAccessRightsRepository
 10    {
 11        protected Dictionary<EntityReference, List<PrincipalAccess>> _accessRights;
 12
 427013        public AccessRightsRepository()
 427014        {
 15            //One record might be accessed from many security principals
 427016            _accessRights = new Dictionary<EntityReference, List<PrincipalAccess>>();
 427017        }
 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)
 11825        {
 11826            List<PrincipalAccess> accessList = GetAccessListForRecord(er);
 15427            PrincipalAccess paMatch = accessList.Where(p => p.Principal.Id == pa.Principal.Id).SingleOrDefault();
 11828             if (paMatch == null)
 11229                accessList.Add(pa);
 11830        }
 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)
 1238        {
 1239            List<PrincipalAccess> accessList = GetAccessListForRecord(er);
 1840            PrincipalAccess paMatch = accessList.Where(p => p.Principal.Id == pa.Principal.Id).SingleOrDefault();
 1241             if (paMatch != null)
 642            {
 643                accessList[accessList.IndexOf(paMatch)] = pa;
 644            }
 45            else
 646            {
 647                accessList.Add(pa);
 648            }
 1249        }
 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)
 10657        {
 10658            List<PrincipalAccess> accessList = GetAccessListForRecord(er);
 24359            PrincipalAccess pAcc = accessList.Where(pa => pa.Principal.Id == principal.Id).SingleOrDefault();
 10660            RetrievePrincipalAccessResponse resp = new RetrievePrincipalAccessResponse();
 61
 10662             if (pAcc != null)
 7163                resp.Results["AccessRights"] = pAcc.AccessMask;
 64
 10665            return resp;
 10666        }
 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)
 3674        {
 3675            List<PrincipalAccess> accessList = GetAccessListForRecord(er);
 3676            RetrieveSharedPrincipalsAndAccessResponse resp = new RetrieveSharedPrincipalsAndAccessResponse();
 3677            resp.Results["PrincipalAccesses"] = accessList.ToArray();
 3678            return resp;
 3679        }
 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)
 1787        {
 1788            List<PrincipalAccess> accessList = GetAccessListForRecord(er);
 89
 8090             for (int x = accessList.Count - 1; x >= 0; x--)
 2391            {
 2392                PrincipalAccess pa = accessList[x];
 2393                 if (pa.Principal.Id == principal.Id)
 1794                    accessList.RemoveAt(x);
 2395            }
 1796        }
 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)
 0103        {
 0104            throw new NotImplementedException();
 105        }
 106
 107        /// <summary>
 108        /// Fetches the List&lt;PrincipalAccess&gt; for the given EntityReference
 109        /// </summary>
 110        /// <param name="er"></param>
 111        private List<PrincipalAccess> GetAccessListForRecord(EntityReference er)
 289112        {
 289113            List<PrincipalAccess> accessList = null;
 289114             if (!_accessRights.TryGetValue(er, out accessList))
 94115            {
 94116                accessList = new List<PrincipalAccess>();
 94117                _accessRights.Add(er, accessList);
 94118            }
 119
 289120            return accessList;
 289121        }
 122    }
 123}