Jump to content
Larry Ullman's Book Forums

Creating Custom Views For Different Users


Recommended Posts

I played around with this for some time and couldn't get it to work properly. It removes "Delete" button for both authenticated users:

 

                array(
                    'class'=>'CButtonColumn',
                        'buttons'=>array
                        (
                            'delete'=>array
                            (
                                'visible'=>"(Yii::app()->user->checkAccess('demo')) ? true : false;",
                            )
                        )
                    ),

 

My access rules are:

 

    public function accessRules()
    {
        return array(
            array('allow',  // allow all users to perform 'index' and 'view' actions
                'actions'=>array('index','view'),
                'users'=>array('*'),
            ),
                        array('deny', // can't create, update, delete
                                'actions'=>array('create','update','delete'),
                                'users'=>array('demo'),
                        ),
            array('allow', // can create, update, delete
                'actions'=>array('create','update','delete'),
                'users'=>array('admin'),
            ),
            array('allow', // can access CGridView
                'actions'=>array('admin'),
                'users'=>array('admin','demo'),
            ),
            array('deny',  // deny all users
                'users'=>array('*'),
            ),
        );
    }

 

So, the CGridView shows for both users, which is what I want, but the "Delete" button doesn't appear for both users which is what I don't want. The good thing though is that I'm not getting errors with this code. :) I'll keep working with what you gave me though.

Link to post
Share on other sites

The checkAccess() method performs an access check for the user we both should be using either Yii::app()->user->Name or Yii::app()->user->Id to find the actual username of the logged in user for static authentication values which you have. I hope for both of our sakes we have the correct answer this time. :D

'visible'=>"(Yii::app()->user->Name==='admin') ? true : false;",

This was actually the first time i had to go into investigate CWebUser a little more, i didn't understand my self why static actually work but now i do. I am using database authentication with my website but i truly understand why you are using static values, if i were in your situation i would do the same.

Link to post
Share on other sites

Looks like that's the one!! :) Yea!! I was actually using Yii::app()->user->name == 'admin' about a week ago, but it didn't work because I didn't kow the correct syntax, and it was erroring out so I abandoned it.  The key was the "", the === and the ternary. I can't find this anywhere I was looking. Thanks Edward. Here's the whole thing as intended:

 

                array(
                    'class'=>'CButtonColumn',
                        'buttons'=>array
                        (
                            'delete'=>array
                            (
                                'visible'=>"(Yii::app()->user->name==='admin') ? true : false;",
                            ),
                            'update'=>array
                            (
                                'visible'=>"(Yii::app()->user->name==='admin') ? true : false;",
                            )
                        )
                ),

 

Now, any authenticated user, other than admin will only see a "View" button--admin sees "View", "Update" and "Delete". I can retire this project now. Thanks again.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...