Пример #1
0
        public void DataAccessAlreadyExist()
        {
            var dbRes = ClientSvc.CreateResourceAccess(_clientVm.Id, ResourceVm.Id, 0, ClientResourceAccessStatus.Pending);

            _clientResourceAccessVm = dbRes.Resource;

            Assert.AreEqual(dbRes.Message, ResourceManagerMessages.Error.RESOURCE_ALREADY_EXISTS);
        }
Пример #2
0
        public void CreateResourceAccess()
        {
            if (_clientVm == null)
            {
                Assert.Fail("FindById: Test Failed to create a new client.");
            }

            var dbRes = ClientSvc.CreateResourceAccess(_clientVm.Id, ResourceVm.Id, 0, ClientResourceAccessStatus.Pending);

            _clientResourceAccessVm = dbRes.Resource;

            Assert.IsTrue(dbRes.IsSuccess);
        }
Пример #3
0
        /// <summary>
        /// Create a new resource access for the client.
        /// </summary>
        /// <param name="clientId">Client Id</param>
        /// <param name="resourceId">Resouce Id</param>
        /// <param name="resourceValue">Resource Value</param>
        /// <param name="status">Client Resource Access Status</param>
        /// <param name="claims">List of Claim Ids</param>
        /// <returns><![CDATA[ (ClientResourceAccessVm Resource, bool IsSuccess, String Message) ]]></returns>
        public (ClientResourceAccessVm Resource, bool IsSuccess, String Message) CreateResourceAccess(int clientId, int resourceId, int resourceValue, ClientResourceAccessStatus status, int[] claims = null)
        {
            try
            {
                // Check to make sure that the client does exists in the database.
                var dbClient = ClientDataAccess.Client.Find(f => f.Id == clientId);
                if (dbClient == null)
                {
                    return(null, false, ResourceManagerMessages.Error.CLIENT_NOT_FOUND);
                }

                // Check to make sure that the resource manager does exists in the database.
                var dbRes = ResourceDataAccess.ResourceManager.Find(f => f.Id == resourceId);
                if (dbRes == null)
                {
                    return(null, false, ResourceManagerMessages.Error.RESOURCE_NOT_FOUND);
                }

                // Check to make sure that the resource access with the same resource value does not exists.
                var dbResVal = ClientDataAccess.ClientResourceAccess.Find(f => f.ResourceId == resourceId && f.ResourceValue == resourceValue);
                if (dbResVal != null)
                {
                    return(null, false, ResourceManagerMessages.Error.RESOURCE_ALREADY_EXISTS);
                }


                /* --------------------------------------------------------
                 * If the caller passesa list of claim Ids
                 * check to see if all the claim ids exists in the database
                 * before creating a new record in the database.
                 * --------------------------------------------------------- */
                if (claims != null && claims.Length != 0)
                {
                    var hasInvalidClaim = ResourceDataAccess.ResourceClaim.Any(f => f.ResourceId == resourceId && !claims.Contains(f.Id));
                    if (hasInvalidClaim)
                    {
                        return(null, false, ResourceManagerMessages.Error.CLAIM_UPDATE_NOT_FOUND);
                    }
                }

                var dbResAccess = (new ClientResourceAccessVm()
                {
                    Status = status
                }).ToEntityCreate(clientId, resourceId, resourceValue);
                dbResAccess = ClientDataAccess.ClientResourceAccess.Create(dbResAccess);

                if (claims != null)
                {
                    foreach (var id in claims)
                    {
                        ClientDataAccess.ClientResourceAccessClaim.Create(
                            new ClientResourceAccessClaim()
                        {
                            ResourceClaimId        = id,
                            ClientResourceAccessId = dbResAccess.Id,
                            Access = ClientResourceClaimsAccess.Allow
                        }
                            );
                    }
                }

                ClientDataAccess.Save();

                var resAccess = new ClientResourceAccessVm(dbResAccess);
                return(resAccess, true, ResourceManagerMessages.Success.ACCESS_CREATED);
            }
            catch (DbEntityValidationException ex)
            {
#if (DEBUG)
                // for debuging entity framework
                foreach (var error in ex.EntityValidationErrors.SelectMany(valError => valError.ValidationErrors))
                {
                    Console.WriteLine(error.ErrorMessage);
                }
#endif
                throw;
            }
            catch
            {
                throw;
            }
        }