Jump to content

Recommended Posts

Posted

Hi guys

I'm attempting to do something new (to me) in PW and a search is coming up blank. I suspect I'm using the wrong search terms.

I'd like to create a simple content block that is only shown to distinct PW users. The overall page itself would be public and viewable by anyone but there's a particular DIV which only shows to PW user X or Y.

Can someone point me in the right direction? I thought this would be session based but having searched the forums and Google I'm coming up with results on hiding back-end content etc instead of front in within public pages.

Cheers

P

Posted

I would use permissions or take a shurtcut and use roles for that. The shortest route is to use user, but I think it is messy even in your situation, since you have at least two users that can see the content ("user X or Y").

- So first create (and publish) new permission, lets call it "view-hidden"

- Create new role (or use existing role) and add the new permission to all roles that should be able to view hidden content

- Now make sure that user X and Y have roles that have "view-hidden" permission

Then on your template code:

if ($user->hasPermission("view-hidden")) {
  echo "This is hidden content block";
}

If you took the shortcut with using roles directly, then it would be:

if ($user->hasRole("admin")) {
  echo "This is hidden content block";
}
  • Like 2
Posted

Thanks apesia. So simple!

Now that I know where to look, I notice that here is a $user->name property. Can't I do this?

if ($user->name("Peter")) {
echo "Hidden content";
}

or refer to a user by pageID

if ($user->id("41")) {
echo "Hidden content";
}

I'm not at my installation right now.

Posted

You could check by name or id (or any custom field also), but those are properties, not methods. So correct syntax would be:

if ($user->name == "Peter") {
  echo "Hidden content";
}

// or 

if ($user->id == 41) {
  echo "Hidden content";
}

But as I said earlier, checking only by user is a shortcut I wouldn't take even on smallest sites:

  • There is nothing on the admin site to tell the difference that some users are handled differently
  • If you need to add or remove this hidden content from some user, it is only possible through code

So I strongly suggest using permission or role for this purpose.

Posted

I was wondering what you use the $permissions API for?
I was trying to do if($permissions->get("page-edit")) { // content here }  however it was $user->hasPermission("page-edit") I soon figured out. 

Posted

I was wondering what you use the $permissions API for?

You can build logic upon a permission.

if ($user->hasPermission('just-a-permission')) {
    // do something
}
Every user has role. (A Role extend Page, so behaves like a page)

Every role can have multiple permissions (A Permission extends Page, so behaves like a page)

Every user can have multiple roles.

When you ask $user->hasPermission('something'), ProcessWire will loop all roles and see if one of those roles has that permission. And it returns a boolean true or false on it's findings.

  • Like 2

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.
×
×
  • Create New...