Jump to content

find pages with upper level parents that have x field value


PHPSpert
 Share

Recommended Posts

I have a page architecture like this:

account-data

 - 1010 (account, owner=clinton, assigned_users=brian)

   - Some update

   - Some project

      - Some task

 - 1020 (account, owner=jack, assigned_users=brian,clinton)

 - 1030 (account, owner=tom, assigned_users=clinton)

 - 1040 (account, owner=jordan, assigned_users=brian)

What I want to do is give viewing permissions (for updates, projects and tasks) to users only if they are either assigned or own the parent account.

I need to find out a clean way of doing this. For example, I could do:

foreach($pages->find("template=task") as $item) { if($item->parents()->find("template=account,user|assigned_user={$user}")) ...

But looping through all of the items is a pretty heavy task.

Sometimes it has to go a few levels before finding the parent with the user info.

Is there a better way of doing this?

Link to comment
Share on other sites

Ok, here's another idea:

let's say we're looking for "update" pages assigned to or owned by $user:

function findUserPages($user, $template) {

return $pages->find("template=account,user|assigned_user={$user}")->find("template={$template}");

}

$pages_found = findUserPages($user, "update");

Link to comment
Share on other sites

I'm confused because not sure if your definition of 'user' here is a ProcessWire User, or you are maintaining your own system of users? To me it looks like you are maintaining your own user system, but not sure? This would affect the answer. I think we also need a little more context about where this code is going. Is this simply to check access on a page being viewed, or is this compiling/aggregating all access information to display somewhere? I'm guessing this is to aggregate somewhere since you mentioned looping through all the items? One thing that might be worth looking at is the PageEditPerUser module, which I think does some of what you are trying to accomplish here. 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...