2

Can't really find much useful in the way of googling.

I have a special content case on my site where I have created a page template which displays a series of blocks in my theme code. To apply this template to this particular page, I made a custom node type for it. However, this content is (and should be) restricted only to users with a certain permission, which I accomplished via the Content Access plugin.

I want to use wkhtmltopdf to output a PDF of that page for internal use. The only way I think I can get the node instance to allow wkhtmltopdf to view it is to run wkhtmltopdf with the --custom-header flag with an Authorization header. I need this to happen once per day, and was thinking of setting up a cron task to run wkhtmltopdf nightly on a Linux system (so far I have just been testing it manually on a Windows system where the node had no permission requirements and everything seems fine).

I know that enabling basic auth for certain paths is a thing I can do using a plugin, but this content is not defined by a plugin and is just a node I created using the Drupal interface.

Is this possible, or do I have to tear it down and start over and build a new custom plugin?

3

Probably there is no one here, that has tried the method you've described. I would start with a custom entity, use the basic auth or create a new authentication provider and apply this for the entity route. You can do this with drupal console within a minute.

And you don't need contrib code to configure access for the custom entity, because here you can do what you want in core. I wouldn't use an Access plugin from contrib, until there is a stable release.

  • I think I'm going to go with this option. Unfortunately now I am having problems getting the code to output an actual HTML document instead of a div snippet. Going to make a separate thread about this. – saramm1 Dec 19 '16 at 19:40
2

Solution 1 - override the node access controller

The node entity type, defines the node access controller. This is done through annotation (system that allow you to define things through comments.)

If you use hook_entity_type_alter you can change the node access controller to a subclass you make and implement any kind of logic.

Solution 2 - use basic auth on all pages

Is there any reason why you shouldn't be able to authenticate with basic auth on all pages. Drupal has this feature OOB from the basic_auth module, so you simply could turn it on. I don't see that being a security info as you would need to pass credentials on all requests (instead of logging in once and using a cookie).

You need to enable for global use, look the cookie authenticator:

user.authentication.cookie:
  class: Drupal\user\Authentication\Provider\Cookie
  arguments: ['@session_configuration', '@database']
  tags:
    - { name: authentication_provider, provider_id: 'cookie', priority: 0, global: TRUE }

Solution 3 - generate a cookie used for requests

Another solution would be to login with Drupal (simply post the login form and get the cookie in return) and you could just add the cookie in all requests. That would provide you access. The services modules in Drupal core might help making some of these things a bit easier.

  • Thank you for the answer. #2 seems acceptable, but embarrassingly enough I can't figure out how to do it. I have the module enabled but don't see this option anywhere to turn it on, and basic auth is only working on modules where I've explicitly specified it in the routing.yml. Every guide I found is talking about the REST module and I don't think that's what I'm looking for. Am I missing something? – saramm1 Dec 19 '16 at 16:41
  • @saramm1 Seems like the basic auth isn't global by default, so looks like you need to add that information in the service definition, or create your own service using the same class. – googletorp Dec 19 '16 at 16:56
  • Hmm, I think I'm in way over my head here. I don't understand what a drupal service is or how to create one. – saramm1 Dec 20 '16 at 16:07
  • @saramm1 A service is just a reusable class which does something, like cookie auth handling, basic auth handling etc. What makes it a service is a few lines of yml which defines a name, class location, other services that the service depend on and some metadata. When you make it a service other services can use it, or by giving it tags you it will automatically be used for some things. How to alter service: drupal.org/docs/8/api/services-and-dependency-injection/… – googletorp Dec 20 '16 at 21:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.