PHP code example of marshmallow / nova-user-groups

1. Go to this page and download the library: Download marshmallow/nova-user-groups library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

marshmallow / nova-user-groups example snippets


namespace App\Models;

use Marshmallow\NovaUserGroups\Traits\HasUserGroup;

class User extends Authenticatable
{
    use HasUserGroup;

    // ...
}


namespace App\Nova;

use Laravel\Nova\Resource as NovaResource;
use Laravel\Nova\Http\Requests\NovaRequest;
use Marshmallow\NovaUserGroups\Traits\UserGroupResource;

abstract class Resource extends NovaResource
{
    use UserGroupResource;

    // ...
}

// config/nova-user-groups.php
return [
    'user_methods' => [
        // ...
        'impersonate' => 'Is allowed to impersonate users',
    ],
];

// app/models/user.php

namespace App\Models;

class User extends Authenticatable
{
    /**
     * Please not that the methods must start with `may` and
     * then start with a capital letter.
     */
    public function mayImpersonate()
    {
        return $this->allowedToRunMethod('impersonate');
    }
}

namespace App\Providers;

// ..
use Marshmallow\NovaUserGroups\Traits\UserGroupNovaServiceProvider;

class NovaServiceProvider extends NovaApplicationServiceProvider
{
    use UserGroupNovaServiceProvider;

    // ...

    protected function cards()
    {
        return $this->canSeeCards([
            // Your cards go here.
        ]);
    }

    protected function dashboards()
    {
        return $this->canSeeDashboards([
            // Your cards go here.
        ]);
    }

    protected function tools()
    {
        return $this->canSeeTools([
            // Your cards go here.
        ]);
    }
}

/**
 * Bootstrap any application services.
 *
 * @return void
 */
public function boot()
{
    NovaUserGroups::$userModel = User::class;
    NovaUserGroups::$userGroupModel = \Marshmallow\NovaUserGroups\Models\UserGroup::class;
    NovaUserGroups::$novaResourceModel = \Marshmallow\NovaUserGroups\Models\NovaResource::class;
    NovaUserGroups::$novaResourceActionModel = \Marshmallow\NovaUserGroups\Models\NovaResourceAction::class;

    NovaUserGroups::$userResource = \App\Nova\User::class;
    NovaUserGroups::$novaResource = \Marshmallow\NovaUserGroups\Nova\NovaResource::class;
    NovaUserGroups::$novaResourceAction = \Marshmallow\NovaUserGroups\Nova\NovaResourceAction::class;
}


return [

    /*
    |--------------------------------------------------------------------------
    | Allowed Admin Groups
    |--------------------------------------------------------------------------
    |
    | This is a list of groups on which the methods will be defined.
    |
    */
    'groups' => [
        'admin' => 'Administrator',
        'super-admin' => 'SuperAdministrator',
    ],

    /*
    |--------------------------------------------------------------------------
    | Allowed Methods
    |--------------------------------------------------------------------------
    |
    | This is a list of allowed methods per group
    |
    */
    'methods' => [
        'admin' => [
            'viewNova',
        ],
        'super-admin' => [
            'viewNova',
            'viewTelescope',
            'viewHorizon'
        ]
    ],
];

## Commands

bash
php artisan vendor:publish --provider="Marshmallow\NovaUserGroups\NovaUserGroupsServiceProvider" --tag="nova-user-groups-migrations"
php artisan migrate
bash
php artisan user-groups:install

php artisan user-groups:policies
bash
php artisan user-groups:install -s