Jump to content

The Frayed Ends of Sanity

  • Posts

  • Joined

  • Last visited

Everything posted by The Frayed Ends of Sanity

  1. Just an update to my code. After looking through the forum for a few hours I changed it to the following which I hope is more efficient. function permissionCheck($content, $type) { $user = wire('user'); $fields = wire('fields'); switch ($type) { case "view": $fieldName="content_view"; break; case "comment": $fieldName="content_comment"; break; case "post": $fieldName="content_post"; break; case "delete": $fieldName="content_delete"; break; case "pinned": $fieldName="content_pinned"; break; } // Find only the parent pages that have the $fieldName attached to their template $templateWithField = $fields->get($fieldName)->getFieldgroups()->implode('|', 'name'); $pagesWithField = $content->parents("template=$templateWithField"); // Check the current $content page also, if it has the $fieldName, append it to the $pagesWithField array. if($content->hasField($fieldName)) $pagesWithField = $pagesWithField->append($content); // If the $pagesWithField array contains pages, check for the $user roles within the $fieldName if($pagesWithField->count > 0) { $i = 0; foreach ($pagesWithField as $item) { if($item->hasField($fieldName)) { if($item->$fieldName->has('id=' . $user->roles)) { $i++; } } } // Only return true if the $user has a role for all the pages within the $pagesWithField array if($pagesWithField->count == $i) return true; } return false; }
  2. Thanks a lot for the feedback @Robin S Of course you are totally right about returning false by default, that was bad practice on my part. Providing I haven't missed something silly then I guess my code is good? I'm not sure about how efficient it is, but I doubt it would cause too many issues? The other suggestion you made seems good, but for this project I need to limit all permission in the PW admin. Thanks again
  3. Hi everyone. I was wondering if the intelligent people on the forum could help me check over a piece of code related to access options. It's based on the template access options, but I need more access options than view/edit etc so I decided to create this so please don't laugh too hard. First of all, this will all be related to the front-end. I plan on restricting the PW admin area to the superuser only so normal users will be unable to change these settings. Basically I have Page Reference fields attached to certain templates. The Page Reference fields are defined as checkboxes and are linked to user roles. As an example, one of the fields could be called content_pinned. I could then create a new page using the template with that field attached and then my method would determine if a member is allowed to pin a topic for example via the front-end. Other Page Reference fields can be added to the templates at any time, for example content_delete, content_comment. My method would then check these permissions for the desired output. Examples of calling the method.... $deleteCheck = permissionCheck($page, "delete"); if($deleteCheck === false) { // The user can't delete this content, so maybe we could display a disabled delete button } else { // The user has a role that is allowed to delete this content so we can now add a delete button and some code to // allow them to delete it.... } OR $commentCheck = permissionCheck($page, "comment"); if($commentCheck === false) { // The user can't comment on this content, so display a message telling them how useless they are ;) } else { // The user has a role that is allowed to comment on this content so we can now add a shiny new editor for them // to use } Below is the method I came up with. It seems to work well, but I'm just a tad worried I have left in a loophole or something stupid. Also, it's worth noting that this method is designed to check parent pages etc for permissions as well. This will allow me to create categories. If the parent category doesn't allow the user to do something but the current page does, it should take this into account and still disallow the action. This allows me to disable certain permissions at a category level. function permissionCheck($content, $type) { $user = wire('user'); switch ($type) { case "view": $fieldName="content_view"; break; case "comment": $fieldName="content_comment"; break; case "post": $fieldName="content_post"; break; case "delete": $fieldName="content_delete"; break; case "pinned": $fieldName="content_pinned"; break; } // Check if the $content page has the $fieldName field. If it does, check that the user's role // is selected - if not, return false if($content->hasField($fieldName)) { if(!$content->$fieldName->has('id=' . $user->roles)) { return false; } } // Get all the parents for the $content page $contentParents = $content->parents(); foreach($contentParents as $contentParent) { // Find out how many parent pages have the $fieldName field if($contentParent->hasField($fieldName)) { // If the parent page has the field, check it has the user's role selected $rolesField = $contentParent->$fieldName; if(!$rolesField->has("id=" . $user->roles)) { return false; } } } return true; } Thanks for your time.
  4. This all looks interesting as usual. Thanks Can someone explain what the following means for PW? Preferably with an example of how it would be used?
  5. I really really appreciate the solutions you guys are churning out. Thanks for sparing your time. I have been reading up a bit more and decided to have a bash at something myself. I wanted an * placed before the labels of required Text and Textarea fields. So I came up with the following loop that works but I'm worried I've missed something. foreach ($form->children as $field) { // loop form fields if($field instanceof InputfieldText || $field instanceof InputfieldTextarea) { if($field->required === "required") { $field->label = '* ' . $field->label; } } } Does this look good to you? Thanks again
  6. Thanks a lot everyone it means a lot. I am not sure where you lot get this information from as I am new to all this but I do have one more related question for which I don't know where to look. If we go back to the buildLoginForm hook we find the following for the submit button..... /** @var InputfieldSubmit $submitField */ $submitField = $this->modules->get('InputfieldSubmit'); $submitField->attr('name', 'login_submit'); $submitField->attr('value', $this->_('Login')); // Login form: submit login button $submitField->appendMarkup = $this->wire('session')->CSRF->renderInput(); $form->add($submitField); Now again I have no idea how to change the login button name from the API. I know it can be done via the translation method above, but how would I do it via the API? I've tried many things and it all ends in failure. I have the following code so far, but have no idea how to dig deeper.... $wire->addHookAfter('LoginRegister::buildLoginForm', function($event) { $form = $event->return; $form->description = false; // Remove the description $event->return = $form; }); Thanks again and sorry to be a pain
  7. Hi @bernhard I honestly didn't think about the css method, and I was thinking more along the api etc. If I hook into buildRegisterForm() how would I actually remove the description of the form? Thanks
  8. Thanks for that lengthy reply. I did actually find the code you linked to, but It was the reference to the actual H2 heading I couldn't find. So it appears there is no easy way of changing that heading tag at the moment or indeed removing it from our markup? It seems we can only change the actual text? In reference to the profile image (avatar) I was referring more to the fact of extending the module to allow the use of an image field. Currently it's not supported, so I was fishing around for ideas of the best way to achieve this from the front end. Thanks again!
  9. Hello everyone, I have some questions regarding some basic customisation for the new LoginRegister module markup. If you look at the demo registration page you will notice it has the header of Register for an account which if you look at the source code equates to: <h2>Register for an account</h2> Now I've looked through the actual module code and I can't find where the H2 heading comes from, or how to change it? I'd like to be able to change the heading size, text and also to have the ability of not outputting the heading at all. But how do I do that? I'm just generally at a loss as to how I can take control of the form output. Also, profiles nowadays usually have an avatar or picture attached, so what's the best way to go about that if I already have an image field attached to the profile. Thanks in advance.
  • Create New...