4

I have seen plenty of solutions for Magento 1.9, but I have not been able to find any conclusive way of disabling user accounts for Magento 2.

I'm developing a custom theme whose parent is Magento/blank.

I have no problem removing the login,signup,wishlist etc. from the frontend using XML and I've managed to remove any mention of customer accounts from the orders page (though I don't remember which process I followed to do so).

However, when my site loads I get an error in the console: Password fields present on an insecure (http://) page. This is a security risk that allows user login credentials to be stolen. Further, if the site is rendered as plain html then the login form is visible.

There will never be any customer accounts allowed on the store, so I'd like to remove all references to accounts on all pages.

I have followed 7ochem's answer to create a custom module for registration removal, modified for Magento 2 as follows:

app/code/[Vendor_name]/[Module_name]/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    '[Vendor_name]_[Module_name]',
    __DIR__
);

app/code/[Vendor_name]/[Module_name]/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="[Vendor_name]_[Module_name]" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Customer"/>
        </sequence>
    </module>
</config>

app/code/[Vendor_name]/[Module_name]/Plugin/RegistrationPlugin.php

namespace [Vendor_name]\[Module_name]\Plugin;
use Magento\Customer\Model\Registration;
class RegistrationPlugin
{
    /**
     * @param Registration $subject
     */
    public function afterIsAllowed(Registration $subject)
    {
        return false;
    }
}

Using the code inspector (firefox-aurora), I can see a script which contains the variable window.authenticationPopup = ..., so I presume that this module is not working properly.

EDIT:

Following JoshCarter's comments, I have added the following file:

app/code/[Vendor_name]/[Module_name]/etc/di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Customer\Model\Registration">
        <plugin name="[Vendor_name]_[Module_name]::after" type="[Vendor_name]\[Module_name]\Plugin\RegistrationPlugin"/>
    </type>
</config>

After uploading this to the server, I executed the following commands:

$ php bin/magento indexer:reindex
$ php bin/magento setup:upgrade
$ php bin/magento setup:di:compile

During compilation, I receive the following error:

[InvalidArgumentException] Plugin class [Vendor_name][Module_name]\Plugin\RegistrationPlugin doesn't exist.

10
  • 1
    Hi Joshua, have you created a di.xml as mentioned in 7ochem's answer?
    – JoshCarter
    Jan 30, 2018 at 14:29
  • 1
    di.xml isn't existent in Magento 1.x and was introduced in Magento 2.x
    – JoshCarter
    Jan 30, 2018 at 14:38
  • 1
    Thats fair enough Joshua - a lot of new concepts are introduced in M2. Looking at the new error you've got, it looks as though there must be an error with the actual plugin class (or the declaration of the class in the di.xml) - I'd run through and check the namespace & class declaration are correct in the Plugin class and that the declaration inside di.xml is correct.
    – JoshCarter
    Jan 30, 2018 at 14:51
  • 1
    Ah!! Im sure i speak for a lot of developers when i say sometimes you need to make a mistake like that to never make it again. I've added an answer now so it's up to you if you delete it. Tried to explain the di.xml file abit for you too.
    – JoshCarter
    Jan 30, 2018 at 15:03
  • 1
    Let us continue this discussion in chat.
    – JoshCarter
    Jan 30, 2018 at 15:15

1 Answer 1

4

Just wanted to add a bit of information as an answer here on di.xml, what it is, and some new concepts of Magento 2 introduced by the file. I get this doesn't completely answer the question, but Joshua looks to have missed this file out and if you read our discussion in the comments you can see that he wasn't sure of the file because he hadn't used it before.

di = Dependency Injection - It allows us to declare a whole host of new classes and class types. It might be worth having a read up on the docs for Dependency Injection by Magento

It can be included inside etc/ etc/frontend/ or etc/adminhtml

Pretty self explanatory but, classes declared in etc/frontend/ would only be executed on the frontend of the site and vise versa for etc/adminhtml - similar to the config nodes in Magento 1.x's etc/config.xml..

Inside etc/di.xml we can declare:

  • Plugins
  • Preferences
  • Types
  • Virtual Types
  • Arguments

And probably a few more things i've not mentioned. For further reading have a look at the docs page for the file itself - http://devdocs.magento.com/guides/v2.0/extension-dev-guide/build/di-xml-file.html

In your case, you're declaring a Plugin, which you could call an Interceptor, they can then intercept before/after/around any public function. This makes Magento 2.x massively flexible and really allows Magento developers to almost completely disregard Observers and class rewrites in Magento 2.

However, sometimes, theres nothing you can do but have to rewrite a class, like in Magento 1.x. For this you can use a preference in Magento 2 as described in the docs in the link above.

From the comments we've had - it looks as though you've forgot the di.xml and once you created it there's probably just a simple mistake in the Plugin class itself.

Hopefully this helps demystify the new concept of the di.xml file a bit!

EDIT:

Adding the line

<referenceBlock name="customer_form_login" remove="true"/>

to the body of default.xml removes the input fields from /customer/account/login, which is where the user is redirected if they attempt to access /customer/account/create following the addition of the module in OP.

Further to this, adding the line

<referenceBlock name="authentication-popup" remove="true"/>

removes the hidden username/password fields from pages, not including the checkout.

1
  • Thanks again! Just gonna leave this up for a little while and work out a solution for removing login so I can add it to your answer, then the green tick is yours!
    – Muckee
    Jan 30, 2018 at 15:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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