We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 40359
    • 25 Posts
    ?
      • 3109 ☆ A M B ☆
      • 894 Posts
      A little more info describing your scenario might help us help you, unless this is a spambot...
        Benjamin Marte
        Interactive Media Developer
        Follow Me on Twitter | Visit my site | Learn MODX
        • 40359
        • 25 Posts
        heheh, it's not spambot... well, i'm creating some site for hotel booking, and i use login and newspublisher, but i want that every user can create only one resource...
          • 3749
          • 24,544 Posts
          Do you mean that the user can only edit a particular resource or that after creating a resource, the user can't ever create any more resources?

          If it's the second one, you could put a tag for this snippet at the top of the NewsPublisher page (above the NewsPublisher snippet tag):


          <?php
          /* Keep non-admins from creating more than one Doc */
          
          $sorryPageId = '12';    // ID of a page telling them they're out of luck.
          
          if (! $modx->user->isMember('Administrator') {
              $userId = $modx->user->get('id');
              $count = $modx->getCount('modResource', array('createdby' => $userId));
              if ($count) {  /* They've already created at least one page */
                 $url = $modx->makeUrl($sorryPageId, "", "", "full");
                 $modx->sendRedirect($url);
              }
          }


            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 42562
            • 1,145 Posts
            The NewsPublisher doubles as a Creating and Editing tool/page. When a resource has been created, will the snippet you just provided allow for Editing. Is it that smart?
              TinymceWrapper: Complete back/frontend content solution.
              Harden your MODX site by passwording your three main folders: core, manager, connectors and renaming your assets (thank me later!)
              5 ways to sniff / hack your own sites; even with renamed/hidden folders, burst them all up, to see how secure you are not.
              • 3749
              • 24,544 Posts
              No it's not. It wouldn't let a user edit their resource, but it could. I think this would do it:


              <?php
              /* Keep non-admins from creating more than one Doc */
              /* let users edit only pages they created */
               
              $sorryPageId = '12';    // ID of a page telling them they're out of luck.
              
               
              if (! $modx->user->isMember('Administrator') {
                  $userId = $modx->user->get('id');
                  if ($modx->resource->get('createdby') != $userId)) {
                      $count = $modx->getCount('modResource', array('createdby' => $userId));
                      if ($count) {  /* They've already created at least one page */
                         $url = $modx->makeUrl($sorryPageId, "", "", "full");
                         $modx->sendRedirect($url);
                      }
                  }
              }


                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 40359
                • 25 Posts
                i think this would do the job, thank you bob.
                  • 3749
                  • 24,544 Posts
                  Don't thank me until it works. wink

                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting
                    • 38783
                    • 571 Posts
                    I hope you don't mind me resurrecting this old thread. I want to do exactly this, but when I use either of the code samples above I get a blank page with no php errors displayed or logged. (they are set yo be displayed and logged on my server). I also get no errors in my MODX Log. Has anything changed in NewsPublisher over the last four years that would make this code obsolete?
                      If I help you out on these forums I would be very grateful if you would consider rating me on Trustpilot: https://uk.trustpilot.com/review/andytough.com

                      email: [email protected] | website: https://andytough.com
                      • 3749
                      • 24,544 Posts
                      TBH, I don't think that ever would have worked (though it seemed like a good idea at the time). wink
                      Even if it worked, it would have prevented users from editing their pages, as donshakespeare pointed out.

                      Department of Duh: $modx->resource is the NewsPublisher page and it's never going to have been created by a non admin.

                      This could possibly work (combined with the &ownPagesOnly property in the NP snippet tag):

                      <?php
                      /* Keep non-admins from creating more than one Doc */
                        
                      $sorryChunk = 'NpSorryOverLimit';    // Name of chunk telling them they're out of luck.
                      $pageLimit = 1;
                      
                       
                        
                      if (! $modx->user->isMember('Administrator')) {
                          /* Don't act if user is editing an existing page */
                          if (!isset($_SESSION['np_doc_to_edit'])) {
                              /* Check the user's page count */
                              $userId = $modx->user->get('id');
                              $count = (int) $modx->getCount('modResource', array('createdby' => $userId));
                              if ($count >= $pageLimit) {  /* They've already created at least one page */
                                   $msg = $modx->getChunk($sorryChunk);
                                   die ($msg);
                              }
                          }
                      }


                      npSorryOverLimit chunk:

                      <h3>Sorry, you're limited to creating a single page, and you've already done that<h3>


                      If the output is too ugly, you could redirect to a "Sorry" page, as in the original. Let me know if this works and I'll consider building it into NewsPublisher.


                        Did I help you? Buy me a beer
                        Get my Book: MODX:The Official Guide
                        MODX info for everyone: http://bobsguides.com/modx.html
                        My MODX Extras
                        Bob's Guides is now hosted at A2 MODX Hosting