Module in yii2 and relationship to existing model



  • Question related to development https://github.com/yiisoft/yii2/blob/master/docs/guide-ru/structure-modules.md Total yii2

    Let's say I have a module. Блог Authorized users can create blog posts. In order to perform this function, I understand that I need to create a model in the module. User and on its basis, to exercise copyright, delineation of rights, etc.

    Now let's say I want to put this module on the website.yii2 Annex, which has already implemented its own authorization and is a model User

    The question is, in fact, whether the module should be developed Блог Is there any way that this situation could be envisaged so that it can work with the user model already available on the website? I mean, the user authorises on my website, and now he's also allowed to create posts in the blog.



  • You can use the web application component in your module. http://www.yiiframework.com/doc-2.0/yii-web-application.html#$user-detail - it provides a set of standard methods that cover most of the model tasks, such as:

    • \Yii::$app->user->isGuest - whether the current user is authorised
    • \Yii::$app->user->id - ID of current user
    • \Yii::$app->user->identity -- specific copy of the class describing the current user
    • etc.

    If you want to minimize the use of a parent ' s application in the module, it's possible to make a component. user in the configuration of its module:

    public class BlogModule extends Module 
    {
        public $user = 'user';
    
    /**
     * @return \yii\web\User
     */
    public function getUser()
    {
        return \Yii::$app->{$this->user};
    }        
    

    }

    Then in the code, you can approach the component. user Here we go.

    BlogModule::getInstance()->getUser()->isGuest

    Demarcation of rights, you can make in your module completely independent (referring to ID users) or also use, for example authManager or add it to the installation of the module:

    public class BlogModule extends Module
    {
    /* @var string $authManager AuthManager component name in parent application */
    public $authManager = 'authManager';

    /**
     * @return \yii\rbac\DbManager
     */
    public function getAuthManager()
    {
        return \Yii::$app->{$this->authManager};
    }       
    

    }

    Then you can, for example, in the migrations of your module, describe the creation of all the roles, permits and rules you need. Verification of access rights will lead to a simple type of challenge:

    BlogModule::getInstance()->getUser()->can('editPost', ['id' => $postId]);

    In most cases, the configuration of your module doesn't even need to be directly listed. user and authManageras their standard names are already in default values.

    The alternative is to create in the module their own abstractions and to require their application, but it is unlikely that this would be appropriate in the case of model tasks such as authentication and distribution of access rights.

    p.s. to statistic, recommend that the data link component should also be provided in the module settings along the same pattern.

    p.p.s. getInstance The module has not only been operated in the context of counterallers (e.g., if you plan to use models or components from the module directly), it is necessary to add uploading the module into the module. bootstrap parent application (in configuration).




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2