Tell if user logged in to edit/dashboard in Concrete5 from block
Asked Answered
N

4

5

I'm writing a block and want to be able to tell if the user is logged in to the dashboard when the block is being viewed. I'm imagining there should be something like isAdminArea() or isEditing() but haven't been able to find it on my own accord so far!

Nudity answered 22/4, 2013 at 4:40 Comment(0)
A
6

If you're using Concrete5.6.0 or higher, you can do this:

global $cp;
$canViewToolbar = (isset($cp) && is_object($cp) && $cp->canViewToolbar());
if ($canViewToolbar) {
    //do something...
}

If you're doing this in a theme template or block view, you can leave out a lot of that junk and just do this:

global $cp;
if ($cp->canViewToolbar()) {
    //do something...
}

But if you're doing this in a package controller or during a system event, you'll want to use my first example (to make sure the global "Collection Permissions" object exists).

If you're running a version of Concrete5 that's older than 5.6, use this code:

global $cp;
$canViewToolbar = (isset($cp) && ($cp->canWrite() || $cp->canAddSubContent() || $cp->canAdminPage() || $cp->canApproveCollection()));
if ($canViewToolbar) {
    //do something...
}
Arvillaarvin answered 22/4, 2013 at 16:44 Comment(0)
H
1

To improve on the above: whilst global is a valid construct in PHP, it's use really should be avoided. It's lazy and dangerous. The correct way to access the active permission model in the C5 architecture is

$cp = new Permissions(Page::getCurrentPage());

if ($cp->canViewToolbar()) {
  ...
}
Hitandrun answered 26/5, 2014 at 7:22 Comment(0)
D
0

If you're simply interested in the edit mode, this will work:

 <?php 
 if ($c->isEditMode()) { ?>
    //do something
 <?php } ?>
Doghouse answered 22/4, 2013 at 5:25 Comment(1)
This only tells you if the user is currently editing the page, not whether they're logged in or not.Arvillaarvin
M
0

This should be it

$u = new User();
if($u->IsLoggedIn()){
  //logged in
}

if it doesnt work, at the top you might need

global $u

Mainz answered 8/5, 2014 at 21:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.