Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter option not working on menus with user role permissions #308

Open
mtkumar82 opened this issue Aug 16, 2023 · 5 comments
Open

Filter option not working on menus with user role permissions #308

mtkumar82 opened this issue Aug 16, 2023 · 5 comments

Comments

@mtkumar82
Copy link

Hi,

I am using your extension to create menu in my application and did all the settings as per extension settings, I want to show menu's in my application based on user permissions set by Admin user for each user role, I am trying to use Filter function of menu, But its not working in my application below is code of my middleware.

namespace Modules\AmazonRepricer\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
//use Symfony\Component\HttpFoundation\Response;
use Auth;
//use Modules\AmazonRepricer\Http\Controllers\AmazonRepricerItemsController;
//use Modules\AmazonRepricer\Http\Controllers\AmazonDefectiveProductController;
use app\Models\User;

class AdminMenus
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle(Request $request, Closure $next)
{
//if (Auth::guard('admin')->check() && (Auth::guard('admin')->user()->role_id == 1 || Auth::guard('admin')->user()->role_id == 4)) {

        if (Auth::guard('admin')->check() ) {          

        if (\Menu::exists('AdminMenu')) {
            
            $navbar = \Menu::get('AdminMenu')->sortBy('order')
            ->filter(function($item){
                if(User::get()->can( $item->data('permission'))) {
                    return true;
                }
                return false;
              });
        } else {
            $navbar = \Menu::make('AdminMenu', function () { 
            })
            ->filter(function($item) { die("sdsdf");
                if(User::get()->can( $item->data('permission'))) {
                    return true;
                }
                return false;
              })
              ->sortBy('order');
        }

        /* 
        ->filter(function($item){
                if(User::get()->can( $item->data('permission'))) {
                    return true;
                }
                return false;
              })

        */

        $navbar->group(['prefix' => 'admin'], function ($navbars) {
            $navbars->add('Repricer Tools', 'items', ['action' => 'AmazonRepricerItems@index', 'class' => 'navbar navbar-items'])->nickname('amazon_repricer')
            ->data('permission', 'manage_users')
            ->data('order', 2)->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/menu_repricer_icon.png').'" alt=""></i>')
                ->link->href('#')
                ->attr(['data-bs-toggle' => 'dropdown']);
            $navbars->get('amazon_repricer')->add('Items', 'items', ['action' => 'AmazonRepricerItems@index'])->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/dropdown_items_icon.png').'" alt=""></i>');
            $navbars->get('amazon_repricer')->add('No Repricer Items', 'noactiveoffers', ['action' => 'AmazonRepricerItems@noactiveoffers'])->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_repricer_icon3.png').'" alt=""></i>');
            $navbars->get('amazon_repricer')->add('No Offersitems', 'nooffersitems', ['action' => 'AmazonRepricerItems@nooffersitems'])->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_repricer_icon4.png').'" alt=""></i>');

            $navbars->get('amazon_repricer')->add('Latestproduct', 'latestproduct', ['action' => 'AmazonRepricerItems@latestproduct'])->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_repricer_icon4.png').'" alt=""></i>');
            $navbars->get('amazon_repricer')->add('Manage Bookmarks', 'bookamrks', ['action' => 'AmazonRepricerBookmarks@index'])->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_repricer_icon6.png').'" alt=""></i>');
            $navbars->get('amazon_repricer')->add('Manage Categories', 'offer_category', ['action' => 'AmazonOfferCategory@index'])->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_repricer_icon6.png').'" alt=""></i>');

            $navbars->get('amazon_repricer')->add('Dashboard', 'dashboard', ['action' => 'AmazonRepricerDashboard@index'])->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_repricer_icon7.png').'" alt=""></i>');
            $navbars->get('amazon_repricer')->add('Manage Sales Limit', 'managesalelimit', ['action' => 'AmazonRepricerDashboard@setsaleslimit'])->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_repricer_icon8.png').'" alt=""></i>');
            $navbars->get('amazon_repricer')->add('Manage Address Book', 'addressbook', ['action' => 'AmazonAddressbook@index'])->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_repricer_icon9.png').'" alt=""></i>');

            $navbars->get('amazon_repricer')->add('Reset Notification Subscription', 'resetsubscription', ['action' => 'AmazonSubscriptionReset@index'])->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_repricer_icon10.png').'" alt=""></i>');
            $navbars->get('amazon_repricer')->add('Amazon Package Templates', 'amazon_package', ['action' => 'AmazonPackage@index'])->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_repricer_icon11.png').'" alt=""></i>');
            $navbars->get('amazon_repricer')->add('Cronjobs Progress', 'amazon_cronjob', ['action' => 'AmazonCronjob@index'])->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_repricer_icon12.png').'" alt=""></i>');

            $navbars->get('amazon_repricer')->add('Removal Cart History', 'removeplancart', ['action' => 'AmazonRepricerRemoveplancart@view_remove_carts'])->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_repricer_icon13.png').'" alt=""></i>');
            $navbars->get('amazon_repricer')->add('Create New ASIN', 'create_asin', ['action' => 'AmazonCreateAsin@create_product'])->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_repricer_icon14.png').'" alt=""></i>');
            $navbars->get('amazon_repricer')->add('Manage New Created ASIN', 'manage_create_asin', ['action' => 'AmazonCreateAsin@index'])->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_repricer_icon15.png').'" alt=""></i>');


            $navbars->add('Reports', 'repricer_reports', ['action' => 'AmazonRepricerShipmentplan@index', 'class' => 'navbar navbar-items'])->data("order","3")->nickname('repricer_reports')
                ->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/menu_report_icon.png').'" alt=""></i>')
                ->link->href('#')->attr('data-bs-toggle', 'dropdown');

            $navbars->get('repricer_reports')->add('Shipment Plan', 'shipmentplan', ['action' => 'AmazonRepricerShipmentplan@index'])->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_repricer_icon17.png').'" alt=""></i>');
            $navbars->get('repricer_reports')->add('Seller Feedbacks', 'viewfeedback', ['action' => 'AmazonRepricerDashboard@feedback'])->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_repricer_icon18.png').'" alt=""></i>');

            $navbars->get('repricer_reports')->add('Sales', 'sales_reports', ['action' => 'AmazonRepricerDashboard@viewsale'])->nickname('sales_reports')
                ->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_repricer_icon19.png').'" alt=""></i>')
                ->link->href('#')->attr('data-bs-toggle', 'dropdown');
            // Sales Report Sub Menu 
            $navbars->get('sales_reports')->add('View Sales', 'viewsale', ['action' => 'AmazonRepricerDashboard@viewsale'])->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_repricer_icon20.png').'" alt=""></i>');

            // financial_reports

            $navbars->get('repricer_reports')->add('Financial', 'financial_reports', ['action' => 'AmazonFinancialGroup@index'])->nickname('financial_reports')
                ->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_repricer_icon21.png').'" alt=""></i>')
                ->link->href('#')
                ->attr(['data-bs-toggle' => 'dropdown']);
            $navbars->get('financial_reports')->add('Financial Group Data', 'financegroup', ['action' => 'AmazonFinancialGroup@index'])->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_repricer_icon18.png').'" alt=""></i>');

            //removal_reports
            $navbars->get('repricer_reports')->add('Removals', 'removal_reports', ['action' => 'AmazonRemovalReport@index'])->nickname('removal_reports')
                ->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_repricer_icon23.png').'" alt=""></i>')
                ->link->href('#')->attr('data-bs-toggle', 'dropdown');

            $navbars->get('removal_reports')->add('Removal Plan Report', 'removal_report', ['action' => 'AmazonRemovalReport@index'])->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_repricer_icon24.png').'" alt=""></i>');
            $navbars->get('removal_reports')->add('Recomanded Removal', 'recomanded_removal', ['action' => 'AmazonRecomandedRemoval@index'])->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_repricer_icon25.png').'" alt=""></i>');
            $navbars->get('removal_reports')->add('Removal Shipment Detail Data', 'removalshipment', ['action' => 'AmazonRemovalShipmentReport@index'])->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_repricer_icon26.png').'" alt=""></i>');


            //inventory_reports
            $navbars->get('repricer_reports')->add('Inventory', 'inventory_reports', ['action' => 'AmazonInventoryData@index'])->nickname('inventory_reports')
                ->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_repricer_icon27.png').'" alt=""></i>')
                ->link->href('#')->attr('data-bs-toggle', 'dropdown');

            $navbars->get('inventory_reports')->add('Country Inventory Data', 'inventory_country_data', ['action' => 'AmazonInventoryData@index'])->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_repricer_icon9.png').'" alt=""></i>');
            $navbars->get('inventory_reports')->add('Inventory Summary', 'inventory_summary', ['action' => 'AmazonInventorySummary@index'])->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_repricer_icon30.png').'" alt=""></i>');
            $navbars->get('inventory_reports')->add('Monthly Inventory Data', 'inventory_monthly_data', ['action' => 'AmazonInventoryMonthlyData@index'])->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_repricer_icon31.png').'" alt=""></i>');
            $navbars->get('inventory_reports')->add('Inventory Adjustment', 'inventory_adjustment', ['action' => 'AmazonInventoryAdjustment@index'])->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_repricer_icon29.png').'" alt=""></i>');
            $navbars->get('inventory_reports')->add('Ledger Summary', 'ledger_summery', ['action' => 'AmazonLedgerSummery@index'])->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_repricer_icon24.png').'" alt=""></i>');
            $navbars->get('inventory_reports')->add('Defective Product', 'defective_product', ['action' => 'AmazonDefectiveProduct@index'])->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_repricer_icon26.png').'" alt=""></i>');
            $navbars->get('inventory_reports')->add('Reimbursement Report', 'reimbursement', ['action' => 'AmazonReimbursement@index'])->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_reimbursement_icon27.png').'" alt=""></i>');
            $navbars->get('inventory_reports')->add('FBA Daily Inventory History', 'inventory_current_data', ['action' => 'AmazonInventoryCurrentData@index'])->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_current_icon1.png').'" alt=""></i>');
            $navbars->get('inventory_reports')->add('Customer Returns Data', 'customer_return', ['action' => 'CustomerReturn@index'])->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/customer_return_icon1.png').'" alt=""></i>');
            $navbars->get('inventory_reports')->add('Inventory Arrival Data', 'inventory_receipt', ['action' => 'AmazonInventoryReceipt@index'])->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_inventory_receipt_icon1.png').'" alt=""></i>');

            // order_reports
            $navbars->get('repricer_reports')->add('Orders', 'order_reports', ['action' => 'LastUpdatedOrder@index'])->nickname('order_reports')
                ->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_repricer_icon23.png').'" alt=""></i>')
                ->link->href('#')->attr('data-bs-toggle', 'dropdown');
            $navbars->get('order_reports')->add('Last Updated Orders', 'last_updated_order', ['action' => 'LastUpdatedOrder@index'])->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/last_updated_order_icon1.png').'" alt=""></i>');
            $navbars->get('order_reports')->add('Order saletax report', 'order_saletax_report', ['action' => 'OrderReport@index'])->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/order_report_icon2.png').'" alt=""></i>');

            // fba_inventory_reports
            $navbars->get('repricer_reports')->add('FBA Fulfilment', 'fba_inventory_reports', ['action' => 'AmazonRepricerDashboard@inventorydata'])->nickname('fba_inventory_reports')
                ->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_repricer_icon23.png').'" alt=""></i>')
                ->link->href('#')->attr('data-bs-toggle', 'dropdown');

            $navbars->get('fba_inventory_reports')->add('FBA Fulfilment Shipments', 'inventorydata', ['action' => 'AmazonRepricerDashboard@inventorydata'])->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_repricer_icon14.png').'" alt=""></i>');
            $navbars->get('fba_inventory_reports')->add('FBA Fulfilment Removals Orders', 'removalorder', ['action' => 'AmazonRepricerDashboard@viewremovalorder'])->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_repricer_icon15.png').'" alt=""></i>');
            // custom_inventory_reports
            $navbars->get('repricer_reports')->add('Custom Inventory Data', 'custom_inventory_report', ['action' => 'CustomInventoryReport@index'])->prepend('<i class="nav-icon"><img src="'.asset('css/images/icons/amazon_repricer_icon24.png').'" alt=""></i>');
        });
    }
    return $next($request);
}

}

Please check help me where I am making the mistake and its not working for me?

Thanks

@mtkumar82
Copy link
Author

@lavary , @cgrice , @uyab , @afraca , @DMeganoski , Can you please help me to short out this issue?

@afraca
Copy link
Contributor

afraca commented Aug 16, 2023

I'm sorry @mtkumar82 , it's been 3 years since I last used this and do not have the means to help you with this.
One piece of advice I can give you: instead of saying "it's not working" be more precise. What is the expected behaviour and what is it that you see?

@mtkumar82
Copy link
Author

Hello @afraca ,

Thanks for your quick message, Actually when I am using filter function there , and trying to add some echo statement there with die, Its not doing anything there. I actually want to show menus, based on user role and permissions. For user role and permissions I am using spatie/laravel-permission extension,

Please let me know if you understand my problem?

Thanks

@DMeganoski
Copy link
Contributor

@mtkumar82 Issues is not really the place to ask this, you'd be better on stack overflow or a forum for laravel like laracasts. Github issues is more to report a bug in the repository, this is a bug in your app.

But I will throw you a bone and tell you what your problem likely is. Your filter function is using User::get()->can() (which I would expect would throw an error as get should return an instance of the Builder class) when it should be auth()->user()->can()

@mtkumar82
Copy link
Author

Hello @DMeganoski , Thanks for your reply, But I short out that issue, I need to put that filter function in end of menus collections like below.

$navbar->filter(function($navbar) {
$userId =Auth::guard('admin')->user()->id;
$user = User::find($userId);
$userRole = $user->role_id; // Role id 1 is for admin user

            if($user->can( $navbar->data('permission')) || $userRole==1) {
                return true;
            }
            return false;
          });

Sharing it here, May be someone else can get help from this.

Thanks again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants