We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 37242 ☆ A M B ☆
    • 339 Posts
    Dear MODX people,

    I am working on a task that drives me mad, even if it sounds simple: user need be able to edit their ressources. Since days, I am reading tutorials, searching the api docs and seeking a nice way to do that.

    The target situation should be the following:

    • User can login to the manager
    • User can see only his own resources in the resources tree
    • User can edit his prefilled resource, including TVs
    • User can only see his own files for TVs of type "image"
    • User can save his resource

    As this didnt look complicated to me, I gave it a try. Created a user, placed him into a user group, gave him a role (which is definately the wrong term for what a "role" does), created a resource, placed his resource into his own resource group, created a restricted access policy, ran into a bug on the way, reported that bug, fixed the access policy, created an own media source for the user based on the filesystem.

    Unfortunately there are some things I cant set up or dont know how to manage:

    • User can see only his own resources in the resources tree - (he can see the either parent resource too or nothing)
    • User can edit the TVs of his resources (TVs can be linked to media sources, so I would need a new TV, linked to its own media source for each user)

    How can I archieve that goal? The client is already "not amused" because I cant keep the deadline sad
    I'd be happy to have:
    1) quick solution and
    2) a discussion about best practise

    Cheers!
      • 4172
      • 5,888 Posts
      would it be an option to manage (create/edit) the user-resources in its own cmp, which would only list the user's own resources and have one dynamic mediasource for all users, which points automatically to the users own directory?

      should not be that difficult to create something like that with help of MIGXdb.
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 37242 ☆ A M B ☆
        • 339 Posts
        That qould be really great. In fact, I started creating a CMP which fires a snippet, but for doing all those steps I mentioned above (Create user with all the groups and media sources).

        I will have a quick peek on the docs. Thank you for the hint!
          • 4172
          • 5,888 Posts
          this could be a start:
          http://rtfm.modx.com/extras/revo/migxdb/migxdb.tutorials/migxdb.manage-events-resources-in-a-cmp-with-help-of-migxdb

          and an example with a dynamic media-source, here resource-specific, but can, more easy than that, also be user-specific
          http://rtfm.modx.com/extras/revo/migx/migx.tutorials/migx.use-resource-specific-mediasource-and-multifile-uploader

          if you need some help to get started, let me know
            -------------------------------

            you can buy me a beer, if you like MIGX

            http://webcmsolutions.de/migx.html

            Thanks!
            • 37242 ☆ A M B ☆
            • 339 Posts
            Yeah, already reading it!

            I could really use a hint if not hand and the project is german anyway smiley
              • 3749
              • 24,544 Posts
              The secret of having a parent that they can't edit and still let them edit the child, is to put the parents in a group and give the user "load" (or possibly "load, list, and view") rights to the parent.

              The TVs can be hidden by putting them in a category and using an Elemment Category Access ACL entry to hide them (it's not the only way, but I think it's the most straightforward).
                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
                • 37242 ☆ A M B ☆
                • 339 Posts
                Hey Bob,

                I read the hint with the parent in a seperate group and also a the answer with load, list and view instead of just load. I didnt try it yet, because I didnt want to put time into a direction that might not lead to the goal.

                But I will keep this information in mind. So if I get this right, we would need three resoure groups (RG) if we use the resource tree at all, all for context mgr:

                1. all resources (permissions: be member of "admin" UG to do anything)
                2. parent folder of all the single "only me" resources (permissions: be member of "clients" UG)
                3. a single "only me" resource (permissions: be member of this RG with only this resource in it)

                I am trying to put that into one script for creating a "Single Page Admin" with only few inputs inside a form. could start like this:

                <?php
                $newname = $fullname;
                $newfullname = $fullname;
                $email = $email;
                
                // create user
                $user = $modx->newObject('modUser', array ('username'=>$newname));
                $newpass = substr(str_shuffle("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, 1).substr(md5(time()),1, 10);
                $user->set('password',md5($newpass));
                
                // create profile
                $userProfile = $modx->newObject('modUserProfile');
                $userProfile->set('fullname',$fullname);
                $userProfile->set('email',$email);
                
                $success = $user->addOne($userProfile);
                if ($success) {
                    $user->save();
                }
                
                // create own page
                $page = $this->modx->newObject('modDocument');
                
                $page->set('parent',5);
                $alias= $modx->runSnippet('makeAlias',array('title' => $fullname));
                $page->set('alias',$alias);
                $page->set('pagetitle',$fullname);
                $page->set('template',7);
                $page->set('published',0);
                $page->set('cacheable',1);
                $page->set('hidemenu',0);
                 
                $doc->setContent('<h2>Hi, my name is '.$fullname.'</h2>');
                $page->fromArray($pagedata);
                $page->save();
                $newpageid = $page->get('id');
                
                // create own resource group
                $group = $this->modx->newObject('modResourceGroup');
                $groupdata = array('name'=>'RG_'.str_replace(' ', '', $loginname,'private_memgroup'=>0,'private_webgroup'=>0);
                $group->fromArray($groupdata);
                $group->save();
                $newgroupid = $group->get('id');
                
                // put resource into own group
                $page->joinGroup($newgroupid);
                
                // set Placeholders for welcome email
                $modx->setPlaceholders(array(
                   'fullname' => $newname,
                   'email' => $email,
                   'password' => $newpass,
                ),'cuc.');
                
                // send new email to SPA
                $message = $modx->getChunk('newUserMail');
                 
                $modx->getService('mail', 'mail.modPHPMailer');
                $modx->mail->set(modMail::MAIL_BODY,$message);
                $modx->mail->set(modMail::MAIL_FROM,'[email protected]');
                $modx->mail->set(modMail::MAIL_FROM_NAME,'Guido Gallenkamp');
                $modx->mail->set(modMail::MAIL_SUBJECT,'Die Spritze immer am Mann!');
                $modx->mail->address('to',$email);
                $modx->mail->setHTML(false);
                if (!$modx->mail->send()) {
                    $modx->log(modX::LOG_LEVEL_ERROR,'An error occurred while trying to send the email: '.$modx->mail->mailer->ErrorInfo);
                }
                $modx->mail->reset();


                Id be happy if we could mak this possible like putting it into a CMP or making a MODX extra of it. I know that if will be needed smiley

                Cheers!
                  • 3749
                  • 24,544 Posts
                  Looks good.

                  Your code looks great, but you might consider putting most of it in a plugin attached to OnDocFormSave that only fires if the user is a newly created user. That way you can use the regular Create/Edit User form to fill in the user and profile data.

                  if (! $mode == modSystemEvent::MODE_NEW) {
                      return'';
                  }
                  
                  /* Your code here */



                  Unless it's your intention to let low-level users create other users. In that case, you'd want to keep going the way you're going.
                    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