Forum

Thread tagged as: Question, Runway, Members

Members Access by Email or Tag

Hi,

Hoping someone can shed some light for me.

I have a requirement for a secure client area that will allow specific clients to log in and see only their documents.

I have this working with the current set up.

  • Collection named "Clients"
  • Template for client has a field with ID "emailWithAccess" and a repeater for associated documents
  • I create a member with the same email address as the associated client in the client collection.
  • Once a member is logged in I get their email using perch_member_get('email') and assign it to a variable ($clientEmail) I then filter my client collection on the "emailWithAccess" field matching this $clientEmail variable.

I would like to add a repeater for emails with access and filter my client collection by checking against this repeater as opposed to a single field. Is there way of doing this? Can I loop the values of the repeater into an array and use that as a comma delimited list for the "in" filter?

Secondary to this, am I overcomplicating this? Is there a way of getting a members tag using a perch_member_get('tag') or similar. I tried this and it didn't work. I could then add a field to the client collection that relates to this member tag and use this as my collection filter?

Many thanks

Lee

Lee Goodman

Lee Goodman 0 points

  • 6 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Can you show me your diagnostics?

Hi Drew,

The client area works using the solution I implemented above, but I am wondering if I can pull out the tags assigned to a member using a perch_member_get('tag') or similar, if so how would I do this?

If not can I loop the values of a repeater so that I may add a list of email addresses with access as proposed above, or is there a simpler solution that I am missing?

Thanks

Lee

I've also tried adding a field called "clientTag" to the client collection, this is then set to the same value as the tag assigned to the member and then to loop the collection and return the clientTag but I am not sure how to access this returned value. I've also tried using Members API but the member object doesn't seem to have a "tag" property?


perch_collection('clients', array( 'each' => function($client) { return $client; print_r($client['clientTag']); }, ));

Any guidance would be appreciated.

Many thanks

Drew McLellan

Drew McLellan 2638 points
Perch Support

Of course. Can you show me your diagnostics?

Drew McLellan

Drew McLellan 2638 points
Perch Support

The problem with your code sample is that you're returning the value form the function and then using print_r(). The print_r() will never run as the function has already returned.

Hi Drew,

I think I'm confusing things, I think I need to rephrase the question.

I am trying to create a client area so that different clients can see their documents. Client A logs in and see's Client A documents. Client B logs in and sees Client B documents.

I have this working by creating a client collection, the client has an email address. This email address corresponds to an email address assigned to the client in the members app. When the client logs in I get their email address using perch_member_get('email') and assign it to a variable. I then use this variable as the filter for the collection comparing it to the email address field in the client collection item.

I was looking into ways of making this more flexible and allowing multile email addresses for the same client, by maybe retrieving the tag assigned to the logged in member and somehow comparing that to an associated tag field in the client collection item.

I wasn't sure if

  • I can retrieve the tag from a logged in member, I've looked in to the API and couldn't see it.

  • How I would then compare this to a "client tag" field in my collection. I tried to use "each" callback in the collection but how do you access anything returned from the callback to be used in code after the loop, if that makes sense?

For reference I tried accessing member info as follows

       $memberID = perch_member_get('id'); // get our Member ID


                    $Member = $Members->find($memberID); // Retrieve our Member object by passing memberID
                    $member_details = $Member->to_array();
                    print_r($member_details);



perch_collection('clients', array( 'each' => function($client) { process as necessary, then return the modified array return $client['clientTag']; }, ));

and I wanted to do some kind of matching of the member tag against clientTag and then show the relevant client documents by filtering the collection by clientTag. Admittedly I am not a PHP expert so I wasn't sure if my approach makes sense or I am completely bonkers!

Many thanks

Drew McLellan

Drew McLellan 2638 points
Perch Support

In an each callback, you need to return the item you were given. So in this instance where you've been handed $client, you need to modify it and then return the entire $client item. Returning $client['clientTag'] will result in all the other properties not being available in the template.

Thanks Drew, may sound like a complete noobie question but how do I access the $client item once it's been returned from the loop?

I was getting an undefined error when I tried to use it in any code outside of the each callback.

Drew McLellan

Drew McLellan 2638 points
Perch Support

You can't use it outside the callback - it's out of scope by that point.

The purpose of the callback is to be able to modify the result after it's been retrieved but before it's been templated.

Ok thanks Drew, it's ok I have another solution I've added a custom field "Company Name" to the members area which I can access via the "perch_member_get" method, and then I filter against that in the client collection. I haven't tested fully but seems to fit my requirement of one client (company) to many members relationship .

Just one last question, I am assuming I can apply the same principle to filter custom content in Perch as opposed to a collection, I've had a request for exactly the same functionality on a Perch site :-)

I'm going to test it myself on my dev site but thought I'd ask as I know custom content behaves slightly differently, I would have to generate the content via a page function first.

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, I think, although I'm not sure which part is the question!

hah - the question is does the same principle apply to custom content :-)

But I should stop being lazy and test it!

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, they work pretty much the same from userland.