As per the flow diagram below I want to restrict access for users outside my network. However rather then completely stopping external access I want them to be redirected to a login page where they will be required to login via google, there email address will have to be within the organisation to be granted access.

Any help/advice much appreciated.

flow diagram

  • Try adding a {% requireLogin %} tag on pages you want protected, craftcms.com/docs/templating/requirelogin – mcclaskiem Apr 19 '16 at 13:40
  • I am aware of the { requireLogin } but is there a way to do this at more of a config level? don't really want to have to add that to all the pages. Also I want to bypass the login when I am on my network (particular IP address). – luke mclean Apr 19 '16 at 13:43
  • 1
    You could try something at your _layout level: {% if not currentUser %}. Then also add some logic to look at their IP address as well and direct them accordingly. You may be better off writing this as a service and then binding it to a variable in the plug-in so you can access it in templates easier. – mcclaskiem Apr 19 '16 at 13:44

Sounds like a custom plugin is in order.

Your plugin could add a custom {% validateUser %} Twig tag. That tag would check to see if their logged in OR check craft.request.getIpAddress() and validate against your internal IP whitelist for your company. You should read the notes on that link, though, since this isn't guaranteed to be secure. It's trivial to spoof these in many circumstances.

If it passes the check, send them on their merry way to the homepage, otherwise, redirect to your social login page, have them login and send to the homepage.


Is the physical web server inside your network as well? As mcclaskiem mentioned, check to see if the IP address is within the subnet of your network and then allow access that way.

You may want to have the web server listen on another hostname that's not accessible outside the network and allow access to that vhost unconditionally (but do that at the firewall level). The would be the securest way to allow access within your network without logging in. IP addresses can be spoofed with varying threat so I'd be wary of allowing anyone to authenticate via just an IP Address if your webserver is located off-premise.

Dealing with google auth on the "other side" would obviously require a plugin for people attempting to connect outside the network.

Here's another thread for further reading.

  • 1
    lol - answered within 20 seconds of each other. – Brad Bell Apr 19 '16 at 20:26
  • Yours is better! – RitterKnight Apr 19 '16 at 20:27
  • That's definitely debatable. – Brad Bell Apr 19 '16 at 20:32

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.