alan

isLoggedin() and isSuperuser() doing nothing (as if commented out) in a PW3 site - did I miss something?

Recommended Posts

Sorry folks if this is obvious to most and I missed something in the docs or here in the forum, but, I have cheerfully used code such as:

if($user->isLoggedin()) {
  echo " loggedIn";
} else {
  echo " notLoggedIn";
}

in PW 2.x sites.

Now on a v3 site I am finding even the most basic test to see if the viewer is logged in or `isSuperuser()` is not giving an error but simply doing nothing - not evaluating.

For example, the top of a typical template reads:

<?php namespace ProcessWire;

			if($user->isLoggedin()) {
				echo " loggedIn";
			} else {
				echo " notLoggedIn";
			}

I have `debug` turned on.

Is there something obvious I have missed, perhaps a step required for v3 that's not required for v2 to allow access to $user data?

Thanks for any pointers.

Share this post


Link to post
Share on other sites

Nothing different from PW2 to 3 really. This code is even in the default install themes and working normally.

  • Like 1

Share this post


Link to post
Share on other sites

Thanks @Soma for confirming that.

I'm confused in that case as I have a site where this test is failing.

I'll go back and re-re-check to see if I am missing something else.., thanks again for the sanity check 👍

Share this post


Link to post
Share on other sites

Thanks @Mustafa-Online - I was pretty certain it was good, now the worse bit, working out what else I have here that's causing this ;)

  • Like 1

Share this post


Link to post
Share on other sites

Check $user with var_dump if it has the expected user. My guess is that you overwrite $user somewhere. Where do you have this code?

  • Like 2

Share this post


Link to post
Share on other sites

Thanks @tpr, I wondered if I had too and so previously had done a `print_r($user)` and got a dump that looked convincingly like an array of user info, so I assumed I had not overwritten it. But I now see I get the same dumped output whether logged in or not... So that seems to suggest it's not null, is an array but looks like maybe it's an array of stuff that is not the $user object!

Thanks for the pointer, I will go back and look harder at that now and report back here, cheers!

Share this post


Link to post
Share on other sites

:-[ Oh nos... I was convinced I had ruled out any chance it could be session data, browser cache, etc, for example opening the same URL in a browser I don't use for dev. But I think it was somehow polluted session from using the CodeKit url `http://myMacName.local:5757/` rather than the LAMP url that I tell CodeKit is where my site is running in one tab, and the LAMP (non-CodeKit) url in another tab.

tl;dr: $user may not behave as expected when previewing on a CodeKit URL, or if you do and another tab is open at the non-CodeKit URL.

Takeaway, check this first and/or if I insist on keeping both Admin and end-user view open, at different URLs, use two different browsers.

Share this post


Link to post
Share on other sites
2 hours ago, alan said:

I wondered if I had too and so previously had done a `print_r($user)` and got a dump that looked convincingly like an array of user info

 

Sorry if it seems like I am promoting Tracy here, but if you were using her, you could just do this in the Console panel and you'd instantly see if $user is an object and what the name of that user is. 

59ea34e926131_ScreenShot2017-10-20at10_38_31AM.png.e392b8063aa383e3962d26fdfffa0253.png

  • Like 3
  • Thanks 1

Share this post


Link to post
Share on other sites

@adrian not at all! I love being reminded of Tracy.

Off to go look it up again. I am embarrassed to say I looked briefly when you released Tracy and I think I saw some dependancy of mine that meant a slight question and I never went further.

I am very impressed to see that you can do per your screenshot, clearly I am a total newb' in some respects, when it comes to debugging, I will turn on, er, `debug` and that's about as far as I get, then it's adding `echo` here and there to see stuff.

tl;dr I am eXCITEd to go look at your module!

Thanks! 👍

  • Like 1

Share this post


Link to post
Share on other sites
7 minutes ago, adrian said:

Sorry if it seems like I am promoting Tracy here

Developing a website or whatever without Tracy is for masochists! Sorry I can't resist! :lol:

  • Like 5

Share this post


Link to post
Share on other sites

o_O
s t u n n e d
to see what I have been missing by not using Tracy.

THANK you again @adrian for the post and for Tracy! ^_^

  • Like 2

Share this post


Link to post
Share on other sites
51 minutes ago, adrian said:

Sorry if it seems like I am promoting Tracy here, but if you were using her

Never considered Tracy as a she :). But yeah, its a girl's name isn't it.

I'm gonna call Tracy a `she` from now on.

  • Like 5
  • Haha 1

Share this post


Link to post
Share on other sites

Been meaning to do it for awhile, this reminded me. Gonna go and install her now.

  • Like 1

Share this post


Link to post
Share on other sites

One of those times that even though I'm pretty pleased with my progress, a quick scroll in there tells me I've only just got over a few small rocks at the bottom of the massive PHP mountain.

59ea4d861f15e_ScreenShot2017-10-20at20_19_51.thumb.png.71d180eba0f693917d168e9b9638c908.png

Do you leave this on when a site goes live or uninstall it?

Share this post


Link to post
Share on other sites

Actually @alan - I should note that in some instances using the Console panel for this purpose may not achieve what you are looking for. If your template had overridden $user somewhere you might be better off doing bd($user) or bd($user->name) just before that isLoggedin() check so you know you are getting the value that is being used right there in your code.

You can do this in your code editor, or you can even do it in the Template Editor panel and use the "Test" button - that way no changes are even made to your template files, so no need to remove debug statements when you're done. Have a read here: https://processwire.com/blog/posts/introducing-tracy-debugger/#template-editor-panel

  • Like 1

Share this post


Link to post
Share on other sites

 

7 minutes ago, SamC said:

One of those times that even though I'm pretty pleased with my progress, a quick scroll in there tells me I've only just got over a few small rocks at the bottom of the massive PHP mountain.

Firstly, sorry about the number of settings in Tracy - that isn't a sign of your lack of PHP knowledge, but rather a sign of my indecisiveness :)

 

7 minutes ago, SamC said:

Do you leave this on when a site goes live or uninstall it?

Really this is up to you. I leave it running - it's not accessible to regular users anyway. In production mode it can send you emails when it logs errors. But if you want you can uncheck the "Enable Tracy" option so there is no load at all from it, although it's not noticeable in production mode anyway.

  • Like 2

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By matsn0w
      Hey all,
      I am working on a website and I want to style the login page, but I'm a bit confused. 
      I want either the existing login page styled in my own way using some CSS (I guess I prefer that) or I want to create a custom page with a form to login. (Which I could style too).
      I used the code from Ryan and Renobird posted here - which works great - but that doesn't replace the original login page. 
      Is there a way to some sort of 'disable' the original login?
      I hope my question is clear and thanks in advance,
      matsn0w
    • By Lex Sanchez
      Hi everyone:
      I do not know if someone before using ProcessWire with AWS CloudFront, currently I have problems with the login, it does not work for any reason, when I check in the logs generated by ProcessWire, it only indicates This request was aborted because it appears to be forged. (in /wire/core/SessionCSRF.php line 190).
      I have allowed CloudFront to forward all headers, cookies and allow all methods (GET, POST, PUT).
      When I perform the same process from the ip server if it works or from the balancer.
    • By flydev
      OAuth2Login for ProcessWire
      A Module which give you ability to login an existing user using your favorite thrid-party OAuth2 provider (i.e. Facebook, GitHub, Google, LinkedIn, etc.)..
      You can login from the backend to the backend directly or render a form on the frontend and redirect the user to a choosen page.
      Built on top of ThePhpLeague OAuth2-Client lib.
      Registration is not handled by this module but planned.
       
      Howto Install
      Install the module following this procedure:
       - http://modules.processwire.com/modules/oauth2-login/
       - https://github.com/flydev-fr/OAuth2Login
      Next step, in order to use a provider, you need to use Composer to install each provider
      ie: to install Google, open a terminal, go to your root directory of pw and type the following command-line: composer require league/oauth2-google
      Tested providers/packages :
          Google :  league/oauth2-google     Facebook: league/oauth2-facebook     Github: league/oauth2-github     LinkedIn: league/oauth2-linkedin
      More third-party providers are available there. You should be able to add a provider by simply adding it to the JSON config file.

      Howto Use It
      First (and for testing purpose), you should create a new user in ProcessWire that reflect your real OAuth2 account information. The important informations are, Last Name, First Name and Email. The module will compare existing users by firstname, lastname and email; If the user match the informations, then he is logged in.
      ie, if my Google fullname is John Wick, then in ProcessWire, I create a new user  Wick-John  with email  johnwick@mydomain.com
      Next step, go to your favorite provider and create an app in order to get the ClientId and ClientSecret keys. Ask on the forum if you have difficulties getting there.
      Once you got the keys for a provider, just paste it into the module settings and save it. One or more button should appear bellow the standard login form.
      The final step is to make your JSON configuration file.
      In this sample, the JSON config include all tested providers, you can of course edit it to suit your needs :
      { "providers": { "google": { "className": "Google", "packageName": "league/oauth2-google", "helpUrl": "https://console.developers.google.com/apis/credentials" }, "facebook": { "className": "Facebook", "packageName": "league/oauth2-facebook", "helpUrl": "https://developers.facebook.com/apps/", "options": { "graphApiVersion": "v2.10", "scope": "email" } }, "github": { "className": "Github", "packageName": "league/oauth2-github", "helpUrl": "https://github.com/settings/developers", "options": { "scope": "user:email" } }, "linkedin": { "className": "LinkedIn", "packageName": "league/oauth2-linkedin", "helpUrl": "https://www.linkedin.com/secure/developer" } } }  
      Backend Usage
      In ready.php, call the module :
      if($page->template == 'admin') { $oauth2mod = $modules->get('Oauth2Login'); if($oauth2mod) $oauth2mod->hookBackend(); }  
      Frontend Usage
      Small note: At this moment the render method is pretty simple. It output a InputfieldForm with InputfieldSubmit(s) into wrapped in a ul:li tag. Feedbacks and ideas welcome!
      For the following example, I created a page login and a template login which contain the following code :
      <?php namespace ProcessWire; if(!$user->isLoggedin()) { $options = array( 'buttonClass' => 'my_button_class', 'buttonValue' => 'Login with {provider}', // {{provider}} keyword 'prependMarkup' => '<div class="wrapper">', 'appendMarkup' => '</div>' ); $redirectUri = str_lreplace('//', '/', $config->urls->httpRoot . $page->url); $content = $modules->get('Oauth2Login')->config( array( 'redirect_uri' => $redirectUri, 'success_uri' => $page->url ) )->render($options); }
      The custom function lstr_replace() :
      /* * replace the last occurence of $search by $replace in $subject */ function str_lreplace($search, $replace, $subject) { return preg_replace('~(.*)' . preg_quote($search, '~') . '~', '$1' . $replace, $subject, 1); }  
      Screenshot
       



    • By ridgedale
      Reference: PW 3.0.62 and uikit3 based site using the Regular-Master profile.
      I have a table that needs some of its content to be hidden. I've tried applying the following classes and styles to <tr>, <td> and <a> elements all without success:
      class="hidden" class="uk-hidden" class="uk-invisible" style="display:none" style="visibility:none" style="visibility:collapse" <-- only applicable to rows in this case Is there any way to allow a user to hide content? Any assistance would be appreciated.
    • By dragan
      If I have two PW sites that sit in separate folders, I can't be logged-in in both sites.
      e.g.
      site.com/project-a/pw-admin-slug/
      site.com/project-b/pw-admin-slug/
      If I login to project-a, then also login to project-b, get back to the first site, I have to login again.
      Is the cookie / session mechanism storing my domain? If it does, and it's meant to be some sort of security enhancement, it should not check my domain, but root-URL of the PW-installation. (strangely, this doesn't happen on localhost)
      Is it possible to prevent that behavior? Often I have two sites open (e.g. check to see if I have the same CKEditor setup and quickly copy and paste it, or copy a user-role)