user-management icon indicating copy to clipboard operation
user-management copied to clipboard

Can i use your module to make menu items appear?

Open arnoschrijver80 opened this issue 9 years ago • 8 comments

Hello,

Can i use your module to make menu items appear? For example: I want to hide the "Application Management" complete if no admin role: echo Nav::widget([ 'options' => ['class' => 'navbar-nav navbar-right'], 'items' => [ ['label' => 'Home', 'url' => ['/site/index']], ['label' => 'About', 'url' => ['/site/about'], ], ['label' => 'Contact', 'url' => ['/site/contact'], ],

        ['label' => 'Application Management', 'url' => ['#'], 'visible' => 0,
            'items' => [
            //Manage Users

            ['label' => 'Users Management',],
            ['label' => 'Edit Users','url' => ['/user-management/user/index']],
            ['label' => 'Edit Roles','url' => ['/user-management/role/index']],
            ['label' => 'Edit Permissions','url' => ['/user-management/permission/index']],
            ['label' => 'Edit Permission Groups','url' => ['/user-management/auth-item-group/index']],

            //Manage Companies
            ['label' => 'Company Management'],
            ['label' => 'Edit Companies', 'url' => ['/user/admin']],
            ['label' => 'Create New Company', 'url' => ['/user/admin/create']],

        ],


        ],
        Yii::$app->user->isGuest ?
            ['label' => 'Login', 'url' => ['/user-management/auth/login']] :
            [
                'label' => 'Logout (' . Yii::$app->user->username . ')',
                'url' => ['/user-management/auth/logout'],
                'linkOptions' => ['data-method' => 'post']
            ],
    ],
]);

arnoschrijver80 avatar Dec 02 '15 15:12 arnoschrijver80

I'm not sure, but you might look at GhostNav and related functions that are provided. The do a lot automatically. To use it just change Nav::widget([ to GhostNav::widget([

Their are other GhostXXX functions that may also do what you need.

Sandy

sganz avatar Dec 02 '15 22:12 sganz

    echo GhostNav::widget([
        'options' => ['class' => 'navbar-nav navbar-right'],
        'encodeLabels'=>false, // don't encode stuff in the label, needed for UserManagementModule::menuItems()

        'items' => [
            ['label' => 'Equalizer', 'url' => ['/equalizer']],
            ['label' => 'Reviews', 'url' => ['/reviews']],
            ['label' => 'Recipe', 'url' => ['/recipes']],
            ['label' => 'About', 'url' => ['/site/about']],
            ['label' => 'Contact', 'url' => ['/site/contact']],

            Yii::$app->user->isGuest ?
                ['label' => 'Login', 'url' => ['/user-management/auth/login']] : 
                ['label' => 'Logout (' . Yii::$app->user->identity->username . ')', 'url' => ['/user-management/auth/logout']],
            [
             'label' => 'Admin',
             'items'=>  array_merge(UserManagementModule::menuItems(), [ ['label'=>'Change Password', 'url'=>['/user-management/auth/change-own-password']]]),
            ],

            /*           
            Yii::$app->user->isGuest ?
                ['label' => 'Login', 'url' => ['/site/login']] :
                [
                    'label' => 'Logout (' . Yii::$app->user->identity->username . ')',
                    'url' => ['/site/logout'],
                    'linkOptions' => ['data-method' => 'post']
                ],*/
        ],
    ]);

This is my simple menu bar with some of the integration. Might not be exactly what you need but maybe some help.

sganz avatar Dec 02 '15 22:12 sganz

I allready considdered that, but thanks anyway. I need for example that: ['label' => 'Users Management',], ['label' => 'Edit Users','url' => ['/user-management/user/index']], //Visible to admin ['label' => 'Edit Roles','url' => ['/user-management/role/index']], //Visible to admin ['label' => 'Edit Permissions','url' => ['/user-management/permission/index']], //Visible to superadmin ['label' => 'Edit Permission Groups','url' => ['/user-management/auth-item-group/index']], //Visible to superadmin

arnoschrijver80 avatar Dec 03 '15 14:12 arnoschrijver80

Look at these static functions -

User::hasRole($roles, $superAdminAllowed = true) User::hasPermission($permission, $superAdminAllowed = true)

They look like they might give you the ability to check for what you need. Also if you look at the code in them they will also give you some clue as to what it's looking at. I would expect you would just need to set the $role to what you are checking.

Also this is in the code to see if a user is superadmin -

return Yii::$app->user->identity->superadmin == 1;

Sandy

sganz avatar Dec 03 '15 17:12 sganz

sganz gave you nice example.

GhostNav automatically hide items if user has no access to it's route. If items have some sub-items, it will be hidden if user has no access to none of it's children.

Although you can override visibility settings by 'visibility' attribute

webvimark avatar Dec 03 '15 18:12 webvimark

Briljant examples, tested it and works, thanks. @webvimark i've seen you're example on adding a profile table but i want to use it for profile and customer. I was always building apps on Yii1.x but Yii2 is a different ballgame.

Like issue #96 Can you assist me in expanding you briljant module?

arnoschrijver80 avatar Dec 03 '15 22:12 arnoschrijver80

@arnoschrijver80 what exactly do you need ?

webvimark avatar Dec 10 '15 07:12 webvimark

@webvimark : An expension of your module with the folowing info (sql) I wan't to be able to link a user to an exsisting or new customer (linked via user_profile table or perhaps a new junction table). A customer can have more users. Or if you have other suggestions. New customers have to sign up and customer created if not exist The admins need to be able to create and change accordingly.

I'm struggeling with the new YII2 relations.

part of database.txt

Any help would be appreciated.

arnoschrijver80 avatar Dec 10 '15 12:12 arnoschrijver80