Giter Site home page Giter Site logo

vuthuan / customergroupaccess-mage2 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from fontis/customergroupaccess-mage2

0.0 1.0 0.0 11 KB

Library for implementing customer group based access in Magento 2 extensions

License: Open Software License 3.0

PHP 100.00%

customergroupaccess-mage2's Introduction

Fontis Magento 2 Customer Group Access

This is a standalone package designed to help extensions limit access to functionality such as payment methods to certain customer groups.

This is NOT a Magento 2 extension. It will not do anything by itself. It is intended for developers to use while implementing extensions.

The recommended way to install this package is to make your extension depend on this package in its composer.json file. To do so, add the following line to the require section of the file:

{
    ...
    "require": {
        ...
        "fontis/customergroupaccess-mage2": "1.0.*",
        ...
    },
    ...
}

Once you've added this package as a dependency, follow these instructions for actually using the package within your extension:

  1. Add two settings to your system.xml file, using the relevant source models:
  • This customer_group_access with Fontis\CustomerGroupAccess\Source\CustomerGroupAccess
  • That customer_group with Fontis\CustomerGroupAccess\Source\CustomerGroup
  1. In your di.xml file, define a virtualType. The configPathPrefix should be the section and group IDs that contain the settings you added above.

    <virtualType name="myModuleMyFeatureCustomerGroupAccessCheckFactory" type="Fontis\CustomerGroupAccess\AccessCheckFactory">
        <arguments>
            <argument name="configPathPrefix" xsi:type="string">my_settings_section/my_settings_group</argument>
        </arguments>
    </virtualType>

    Using this example, the full path to one of the settings would be like so:

    my_settings_section/my_settings_group/customer_group_access
    
  2. Use the virtualType to configure the model where your access checks need to be performed:

    <type name="MyNamespace\MyModule\Model\MyFeature\MyModel">
        <arguments>
            <argument name="accessCheckFactory" xsi:type="object">myModuleMyFeatureCustomerGroupAccessCheckFactory</argument>
        </arguments>
    </type>
  3. In your model's constructor, accept this class as one of the arguments:

    Fontis\CustomerGroupAccess\AccessCheckFactory
    
  4. When you actually need to perform the access check, use the factory to create an instance of the access check class. If you need to perform the check in the context of a store that isn't the current store, you'll need to pass in a store ID:

    $accessCheck = $this->accessCheckFactory->create(array("storeId" => $this->getStore()));
  5. Perform the check by calling the check() method and passing it a customer group ID. If it returns true, they're allowed to use whatever it is you're offering. If it returns false, they aren't. Simple!

customergroupaccess-mage2's People

Contributors

chnorton avatar vuthuan avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.