2

I'm using modX Revo with plugins Login and Babel. As there was wrote at Babel manual I made 2 contexts for two languages, used TV params and wrote contextSwitch plugin. It is mostly like plugin from example at the manual. After that I put plugin on event HandleRequest.

Everything is working except switching context after login: if user is authorized, modX shows only main page, the others show 404 page. I tried to rummage in the code and found out that method switchContext of model modx (line 1843 of /core/model/modx/modx.class.php) returns false.

  public function switchContext($contextKey, $reload = false) {
        $switched= false;
        if ($this->context->key != $contextKey) {
            $switched= $this->_initContext($contextKey, $reload); // RETURNS FALSE
            if ($switched) {
                if (is_array($this->config)) {
                    $this->setPlaceholders($this->config, '+');
                }
            }
        }
        return $switched;
    }

It happens, because context can't be initiated (method _initContext of the same class at the same file). Here context is correctly creating, when we ask to switch to it, but $this->context->checkPolicy('load') returns false (near 2169 line of the same file).

protected function _initContext($contextKey, $regenerate = false) {
    // HERE IS EVERYTHING ALLRIGHT
    $initialized= false;
    $oldContext = is_object($this->context) ? $this->context->get('key') : '';
    if (isset($this->contexts[$contextKey])) {
        $this->context= & $this->contexts[$contextKey];
    } else {
        $this->context= $this->newObject('modContext');
        $this->context->_fields['key']= $contextKey;
    }
    if ($this->context) { //HERE TRUE
        if (!$this->context->prepare((boolean) $regenerate)) { // HERE TRUE
            $this->log(modX::LOG_LEVEL_ERROR, 'Could not prepare context: ' . $contextKey);
        } else {
            if ($this->context->checkPolicy('load')) { // HERE FALSE - MODX CAN'T DO IT
                // .. SOME OTHER modX CODE

So after that I stopped to rummage at the core. May be somebody already saw something like that or know modx Revo core good to answer why modx can't switch context when user is logged in?

P.s. I tried to use another event for context switch plugin - but of course this event is the most correct for the plugin. And I'm accenting that modx can't switch context only while user is logged in!

UPDATE

I tried to edit some permissions. But nothing has happend (I cleared cash and relogged all users). Here is screeshot of context permissions (sorry for nonEnglish, however it should be clear): enter image description here

UPDATE 2 Permissions were wrong: they have to be all 9999 and "list, veiw, load". But the same time I can't log in while i'm at second context as before I couldn't. Now I tried to find out is it the same problem or not.

UPDATE 3 The second problem with login is solvineg very easy: in snippet params there should be &contexts=web,eng

Ist
  • 23
  • 1
  • 10

1 Answers1

3

but $this->context->checkPolicy('load') returns false (near 2169 line of the same file).

Well, there's your answer! The user needs at least "load" permission for the context in order to switch to it.

Go to Security > Access Policies > right click the user group and choose to update it. In the Context Access tab make sure all front-end facing contexts are listed there with preferably the "Load, List & View" access policy. May want to do that for the Administrator user group first to prevent getting locked out of it yourself, as well as the (anonymous) group which is used for not logged in users.

I think, though not 100% sure without seeing more about your specific setup, you didn't gave the user group access to the second context which would explain why it does work for anonymous users.

Mark Hamstra
  • 692
  • 1
  • 4
  • 16
  • Thank you! But it seems to not working or I'm doing something wrong. Here is screen from context editing form (sorry for nonEnglish interface, however it shpould be clear): http://habrastorage.org/storage2/19e/7c9/d17/19e7c9d1746481d7e17d4511147793d6.gif – Ist Mar 18 '12 at 20:02
  • You've got the rank (third column) set to 0 for the "Users" group, which is the highest (it sounds like the world upside down, I know) so chances are your user doesn't have that high of a rank within the user group. Unless you use different roles within user goups I'd advise to keep it simple and give all the permissions a rank of 9999, which is the lowest. – Mark Hamstra Mar 18 '12 at 20:15
  • Thanks a lot. Now it is working if I'm loging in on one context, and switch language. But when I'm triing to log in on "eng." context nothing happens. May be it is kind of the same problem, mey be not - when permissions were wrong I couldn't log in too. – Ist Mar 18 '12 at 20:23
  • In your Login snippet make sure to set &contexts=`web,eng` so it right away logs you in to both contexts. If the contexts are on different (sub)domains, the user might not be sharing the session in the first place and see it as an anonymous user. – Mark Hamstra Mar 18 '12 at 20:55