-1

In my project i have 2 users Organiser and User with 2 different tables.

I want to define rules for Event table for both users. For example if Organiser is logged in which i can see as Yii::$app->user->isGuest and if User is logged in which i can see as Yii::$app->users->isGuest and i want to define separate rules for both users.

if Organiser logged in than only he can view that entire section and not user and same if user is logged in than he can only create the list of users.

If any visitor clicks on Create an Event than they should redirect to Organiser Login or if they click on Create user than they should redirect to User Login. I have defined the rules like below but when it always redirect to Organiser Login and not the user login.

public function behaviors()
{

    return [
        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'delete' => ['post'],
            ],
        ],
        'access' => [
            'class' => AccessControl::className(),
            'only' => ['index', 'view','create','update','delete'],
            'rules' => [
                [
                    'actions' => ['index','view'],
                    'allow' => true,
                    'roles' => ['?'],
                ],
                [
                    'actions' => ['create','update','delete'],
                    'allow' => true,
                    'roles' => ['@'],
                ],
                [
                    'allow' => false,
                    'verbs' => ['POST']
                ],
            ],
        ],
    ];
}

How can i define the rules based on which user is logged in???

Mike Ross
  • 2,753
  • 3
  • 38
  • 81
  • 2
    I suggest yoy an evaluation of RBAC and AuthManager. You should assign role to the User or Organiser and then assign permissione based on this role.. – scaisEdge Nov 12 '15 at 07:46

1 Answers1

1

I agree with scaisEdge that using rbac is a much better solution but if you really want to define a rule based on a user name it is possible. If you wanted user with username "testuser" to be the only one to access the "testPage" action of your controller, you could add something like the following to the rules array:

[
    'actions' => ['test-page'],
    'allow' => true,
    'matchCallback' => function ($rule, $action)
    {
        if (Yii::$app->user->isGuest)
            return false;

        return (Yii::$app->user->identity->username == 'testuser');
    }
],
dataskills
  • 561
  • 6
  • 15