private void DoUpdate(IPermissable updatedEntity, Resource resourceToUpdate, Resource targetParentResource, Resource previousParentResource)
 {
     if (resourceToUpdate == null)
     {
         throw new ModelNotFoundException(String.Format("The resource with foreign id [{0}] and resource type id [{1}] was not found.  It should have been registered when it was created.",
                                                        updatedEntity.GetId(),
                                                        updatedEntity.GetPermissableType().GetResourceTypeId()));
     }
     if (updatedEntity.GetParentId().HasValue&& targetParentResource == null)
     {
         throw new ModelNotFoundException(String.Format("The parent resource with foreign id [{0}] and resource type id [{1}] was not found.  It should have been registered when it was created.",
                                                        updatedEntity.GetParentId().Value,
                                                        updatedEntity.GetParentPermissableType().GetResourceTypeId()));
     }
     RemoveFromCache(resourceToUpdate);
     RemoveFromCache(targetParentResource);
     RemoveFromCache(updatedEntity);
     RemoveFromCache(previousParentResource);
     if (targetParentResource == null)
     {
         resourceToUpdate.ParentResource   = null;
         resourceToUpdate.ParentResourceId = null;
     }
     else if (resourceToUpdate.ResourceId != targetParentResource.ResourceId)
     {
         resourceToUpdate.ParentResource   = targetParentResource;
         resourceToUpdate.ParentResourceId = targetParentResource.ResourceId;
     }
 }
        private Resource AddResourceToCAM(IPermissable permissable, Resource parentResource)
        {
            logger.Info("Adding the permissable entity with id [{0}] and permissable type [{1}] to CAM.", permissable.GetId(), permissable.GetPermissableType());
            var resource = new Resource
            {
                ForeignResourceId = permissable.GetId(),
                ResourceTypeId    = permissable.GetPermissableType().GetResourceTypeId()
            };

            if (parentResource != null)
            {
                resource.ParentResourceId = parentResource.ResourceId;
                resource.ParentResource   = parentResource;
                logger.Info("Setting parent resource to existing resource with id [{0}] to new cam resource.", parentResource.ResourceId);
            }
            else if (permissable.GetParentId().HasValue&& parentResource == null)
            {
                logger.Info("Parent resource does not exist.  Adding to CAM.");
                var newParentResource = new Resource
                {
                    ForeignResourceId = permissable.GetParentId().Value,
                    ResourceTypeId    = permissable.GetParentPermissableType().GetResourceTypeId()
                };
                resource.ParentResource = newParentResource;
                newParentResource.ChildResources.Add(resource);
                Context.Resources.Add(newParentResource);
            }
            Context.Resources.Add(resource);
            return(resource);
        }
 /// <summary>
 /// Removes the permissable entity and its parent from the resource cache.
 /// </summary>
 /// <param name="permissable">The permissable to entity to remove from the cache.</param>
 public void RemoveFromCache(IPermissable permissable)
 {
     if (permissable != null)
     {
         cache.Remove(GetKey(permissable.GetId(), permissable.GetPermissableType().GetResourceTypeId()));
         if (permissable.GetParentId().HasValue)
         {
             cache.Remove(GetKey(permissable.GetParentId().Value, permissable.GetParentPermissableType().GetResourceTypeId()));
         }
     }
 }
 /// <summary>
 /// Updates the given permissable entity in CAM by updating it's parent resource if necessary.
 /// </summary>
 /// <param name="updatedEntity">The updated entity.</param>
 /// <returns>The task.</returns>
 public void OnUpdated(IPermissable updatedEntity)
 {
     if (updatedEntity.IsExempt())
     {
         logger.Info("Permissable entity with id [{0]} and permissable type [{1}] was exempt from cam.", updatedEntity.GetId(), updatedEntity.GetPermissableType());
     }
     else
     {
         var      resource               = ResourceQueries.CreateGetResourceByForeignResourceIdQuery(this.Context, updatedEntity.GetId(), updatedEntity.GetPermissableType().GetResourceTypeId()).FirstOrDefault();
         Resource targetParentResource   = null;
         Resource previousParentResource = null;
         if (updatedEntity.GetParentId().HasValue)
         {
             targetParentResource = ResourceQueries.CreateGetResourceByForeignResourceIdQuery(this.Context, updatedEntity.GetParentId().Value, updatedEntity.GetParentPermissableType().GetResourceTypeId()).FirstOrDefault();
         }
         if (resource != null && resource.ParentResourceId.HasValue)
         {
             previousParentResource = Context.Resources.Find(resource.ParentResourceId.Value);
         }
         DoUpdate(updatedEntity, resource, targetParentResource, previousParentResource);
         this.Context.SaveChanges();
     }
 }
        /// <summary>
        /// Handles a single added permissable entity by adding it as a cam resource.
        /// </summary>
        /// <param name="addedEntity">The added entity.</param>
        public async Task <AddedPermissableEntityResult> OnAddedAsync(IPermissable addedEntity)
        {
            Contract.Requires(addedEntity != null, "The added entity must not be null.");
            if (addedEntity.IsExempt())
            {
                logger.Info("Permissable entity with id [{0]} and permissable type [{1}] was exempt from cam.", addedEntity.GetId(), addedEntity.GetPermissableType());
                return(null);
            }
            else
            {
                var resourceTypeName = ResourceType.GetStaticLookup(addedEntity.GetPermissableType().GetResourceTypeId());
                Contract.Assert(resourceTypeName != null, "The resource type should be recognized.");
                var resourceTypePermissions = await GetResourcePermissionsAsync(resourceTypeName.Value, null);

                var roles = await CreateGetAllRolesQuery().ToListAsync();

                var existingResource = await ResourceQueries.CreateGetResourceByForeignResourceIdQuery(this.Context, addedEntity.GetId(), addedEntity.GetPermissableType().GetResourceTypeId())
                                       .FirstOrDefaultAsync();

                Resource parentResource = null;
                if (addedEntity.GetParentId().HasValue)
                {
                    parentResource = await ResourceQueries.CreateGetResourceByForeignResourceIdQuery(
                        this.Context,
                        addedEntity.GetParentId().Value,
                        addedEntity.GetParentPermissableType().GetResourceTypeId())
                                     .FirstOrDefaultAsync();
                }

                var addedPermissableEntityResult = DoOnAdded(addedEntity: addedEntity,
                                                             existingResource: existingResource,
                                                             parentResource: parentResource,
                                                             resourceTypePermissions: resourceTypePermissions,
                                                             roles: roles);
                logger.Info("Saving cam model context changes.");
                await this.Context.SaveChangesAsync();

                return(addedPermissableEntityResult);
            }
        }