Can I check if an action requires the user to be logged in?

I’m wondering if there exists a method which accepts a controller/action and returns whether or not the current user must login to do that. I know it’s possible to do it the hard way by iterating through the access rules, but I’m wondering if there is already a method that will do it for me. I’ve search for a while now with no luck. I also don’t want to create custom roles and use checkAccess() for something as simple as this.

What I’m trying to do is add a class to any link on the page that would require the user to be logged in order to click that link. Then, I’ll use jQuery and add a click event to each of those links that presents the user with an ajax login form. Once the login is successful, I’ll go ahead and load the page the user was requesting. This will prevent me from having to load a login page for users with Javascript.

Does anyone know of such a method or can anyone recommend a way to accomplish what I’m after without manually searching through the access rules before I display each and every link?

how about redirecting to the login page url


$this->redirect('//your code here');

oh sorry i misunderstood your question…

hmmm… that’s what i was looking for earlier… and unfortunately i gave up… but then, lets just wait for other users to reply so we can see. . .

The accessRules() method is public. Have you tried accessing it with $this->

You can check if the action is declared with in_array() or something like that and than compare ===@

I haven’t tested it just some brainstorming… let me know if this helps.

The problem is that the accessRules() method doesn’t take a controller or action as the parameter, it takes an operation, and an operation must be correlated to a user role. My site isn’t complex enough to warrant user roles, logged-in and guest are all I need.

Also, the problem with the in_array() function is that if I’m currently on a page using the GroupController and I want to see if the ‘view’ action is allow to a guest in the UserController, I don’t seem to be able to instantiate a new UserController.

In other words:


$u = new UserController;

$rules = $u->accessRules();

and then in_array($rules) ...

This won’t work because apparently I don’t seem to be able to load a new controller without creating a new CWebApplication.

Any thoughts as to how this could be possible without creating a new application?

I understood from your initial description that the check would only be valid when you are within the same controller. But you may not be possible to check for different controllers unless the controller is loaded and you are performing the check for the loaded one. However, if you have are currently working in the environment of GroupController you can access the accessRules() through $this->. For example in one of your group view files print_r($this->accessRules());

Hope this helps to an extent.

Store (controller, action, NeedLogin(bool)) somewhere and access to it when creating your links.