rockstardev
rockstardev

Reputation: 13537

Creating an Admin Only Form in YII?

I know how to create a standard form, as you would for a contact form or any other user input form. However, I wish to create a form that allows the user to create a "Product" on our site. The problem is, this form must only be accessible by administrators. How do I limit this?

Upvotes: 0

Views: 205

Answers (2)

christian
christian

Reputation: 558

why dont you check the rbac in its main controller ? something like this:

public function YourAdminOnlyController(){
   public function init(){
     if(Yii::app()->user->checkAccess('your_role_admin')){
     }else
     $this->redirect(array('/site/message','txt'=>'access denied. only admins.'));
   }
}

this is a simple way to do that, maybe not using init(), but my intention is to transmit the main idea. Also you can do that simply using accessControl rules too.

Upvotes: 1

cetver
cetver

Reputation: 11829

class SomeController extends Controller {
   public function filters()
    {
        return array(
            'accessControl',
        );
    }

    public function accessRules()
    {
        return array(
            array('allow',
                'actions' => array('actions_with_public_access'),
                'users' => array('*'),
            ),
            array('allow',
                'actions' => array('action_with_form'),
                'roles' => array('admin')

            ),
            array('deny',
                'users' => array('*'),
            ),
        );
    }

}

Upvotes: 2

Related Questions