Please upgrade here. These earlier versions are no longer being updated and have security issues.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Conditional display of login/logout/register links

edited November 2010 in Vanilla 2.0 - 2.8
I am heavily modifying the default vanilla theme and I need to know how to show a register link only if the user is a guest or a profile link only if the user is logged in, etc.

Essentially I want to make my own version of the "Guest Box" (the box that allows guests to sign in or register) in the header.

Any help would be greatly appreciated :)

Comments

  • These lines in default.master.php should be useful:

    $Session = Gdn::Session(); if ($this->Menu) { $this->Menu->AddLink('Dashboard', T('Dashboard'), '/dashboard/settings', array('Garden.Settings.Manage')); // regarding this -- it checks for Garden.Settings.Manage permissions -- only admins have that, so only admins get to see this button $Authenticator = Gdn::Authenticator(); if ($Session->IsValid()) { //if the Session is VALID -- this is what you are interested in $Name = $Session->User->Name; $CountNotifications = $Session->User->CountNotifications; if (is_numeric($CountNotifications) && $CountNotifications > 0) $Name .= ' <span>'.$CountNotifications.'</span>'; $this->Menu->AddLink('User', $Name, '/profile/{UserID}/{Username}', array('Garden.SignIn.Allow'), array('class' => 'UserNotifications')); $this->Menu->AddLink('SignOut', T('Sign Out'), $Authenticator->SignOutUrl(), FALSE, array('class' => 'NonTab SignOut')); } else { // GUEST $Attribs = array(); if (C('Garden.SignIn.Popup') && strpos(Gdn::Request()->Url(), 'entry') === FALSE) $Attribs['class'] = 'SignInPopup'; $this->Menu->AddLink('Entry', T('Sign In'), $Authenticator->SignInUrl($this->SelfUrl), FALSE, array('class' => 'NonTab'), $Attribs); }

    Let me know if something is unclear.

    /cd
  • Just what I was looking for. Thanks cd
  • Hey @cdavid, even if I delete the entire Menu div, the menu still displays. How can I edit the menu?

    I also tried to print the signing link elsewhere using this syntax:

    <?php echo Anchor(T('Sign In'), Gdn::Authenticator()->SignInUrl($this->_Sender->SelfUrl), 'class'.(SignInPopup() ? ' SignInPopup' : '')); ?>

    ..but the redirect URL doesn't show up when you sign in. What am I doing wrong and is there any easier way to print these links?

    Thanks, cd
  • Or is it caching the template file? Now I'm not seeing any changes that I'm making.
  • The menu can be edited via the
    $this->Menu entries. The list of chained available functions is in applications/dashboard/modules/class.menumodule.php

    I haven't tried but maybe this works
    echo Anchor(T('Sign In'), Gdn::Authenticator()->SignInUrl($this->_Sender->SelfUrl), 'Button'.(SignInPopup() ? ' SignInPopup' : ''));

    /cd
  • Hmm... You are using the PHP file right? default.master.php ... because I don't know my way around TPL...

    /cd
  • Fixed everything except for the non-included redirect link when using the sign in button outside of the menu div.

    Yup I'm using the PHP file.

    Any ideas?
  • For me, that line works. Where exactly are you trying to put the button, maybe some excerpt of the code might be good?

    /cd
  • Thanks again for taking the time to help! Here's the code:

    <ul> <?php $Session = Gdn::Session(); if ($Session->IsValid()) { ?> <li class="accounts hello">Hello, <?php $Name = $Session->User->Name; $CountNotifications = $Session->User->CountNotifications; if (is_numeric($CountNotifications) && $CountNotifications > 0) $Name .= ' <span>'.$CountNotifications.'</span>'; echo Anchor(T($Name), '/profile/', 'class'); ?> <ul> <li><a href="/community/profile">Profile</a></li> <li><?php echo Anchor(T('Sign out'), Gdn::Authenticator()->SignOutUrl(), 'class'); ?></li> </ul> </li> <?php } else { ?> <li class="accounts"><?php echo Anchor(T('Sign In'), Gdn::Authenticator()->SignInUrl($this->_Sender->SelfUrl), 'class'.(SignInPopup() ? ' SignInPopup' : '')); ?> &nbsp;|&nbsp; <?php $Url = Gdn::Authenticator()->RegisterUrl($this->_Sender->SelfUrl); if(!empty($Url)) echo ' '.Anchor(T('Join'), $Url, 'class'); ?></li> <?php }; ?>
  • Btw 2k views? We're popular haha
  • I don't get it... it works for me... I add this exact fragment in <div id="Body"> and this is what I get... http://yfrog.com/9fscreenshotflp which is the normal thing. Maybe you have a broken CSS or something that doesn't show it (try changing the theme?).

    Or am I misunderstanding the problem? For you the "Sign In" doesn't show up, right? Does the "Join" appear?

    /cd
  • Sorry I must have not been clear. I mean that the url that the user should be redirected to after signing in (the page that he/she is currently on) is not being attached to the end of the singin url. So it just redirect the user back to the homepage instead of the page they were in.

    To be more specific, the 'target' variable in the url does not have a value

    eg. "http://mysite.com/community/discussions/entry/signin?target="
  • I think that it's not a bug in the theme (does it work with any of the provided themes?), but rather a bug in the code... Can you please try changin to another theme and tell me if you get redirected?

    If you do get redirected, there might be other things that you do in your theme, that breaks it.

    /cd
Sign In or Register to comment.