We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38314
    • 45 Posts
    Hello,

    I've done a good integration of Discuss on my needs for a community website. But I'm stuck at one point and I need help from awesome Discuss's users here !

    The context :
    My users sign-in with Login. I use both email address and password to login. So email is the username and the mail. I use fullname to display the user's identity, not their email.
    Users are loggedin in Discuss by SSO (Login <-SSO-> Discuss)

    The problem is that Discuss uses the username field for the identity. So, in my case, my users have in their profile "[email protected]" instead of "Jhon Doe". Furthermore, I don't want the email to be as visible as that for privacy.

    So, after hours looking at differents solution, I thought about 2 options :
    1- tell Discuss to select fullname (of the modUser) to become the username (in disUser).
    2- OR, tell Login to use the email value and the password to login user - in that case, no more need to set the email in the username field.

    I'd prefer to find how to make the solution #1

    In core/components/discuss/elements/snippets/snippet.discussupdateprofileloader.php
    Line 54 :
    /** @var disUser $disUser */
    $disUser = $modx->getObject('disUser',array(
       'user' => $modx->user->get('id'),
    ));


    Is there something here ?

    Any help appreciated.
      • 3749
      • 24,544 Posts
      There may be an easier way, but if you can find the Tpl chunk that's displaying the name, you can change the placeholder to:

      [[!GetFullname]]


      Then create this snippet called GetFullname:


      /* GetFullname snippet */
      $output = '';
      $uid = $modx->user->get('id');
      $profile = $modx->getObject('modUserProfile', array('internalKey' => $uid));
      if ($profile) {
          $output = $profile->get('fullname');
      }
      return $output;
      


      This should be faster:
      /* GetFullname snippet (faster version) */
      $output = '';
      $uid = $modx->user->get('id');
      
      $query = $modx->newQuery('modUserProfile', array(
          'internalKey' => $uid,
      ));
      $query->select('fullname');
      return $modx->getValue($query->prepare());


        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
      • Why not simply
        [[!+modx.user.id:userinfo=`fullname`]]
          Studying MODX in the desert - http://sottwell.com
          Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
          Join the Slack Community - http://modx.org
          • 46886
          • 1,154 Posts
          Hmm this is interesting. Susan and Bob's advice always goes before mine, but I hope its ok to contribute my thoughts. My thinking is always at a less technical level (i.e. I would mostly modify the tpls to get the result I want). So I can't speak to your bit of code from the update profile loader.

          What I would want to know is where you want the name changed. The user name can appear in a lot of places, at bottom (Active Users), on the side of a thread (Who's Talking), and of course in the profile page. Plus also in some other places too, would have to think about all the places it can appear. So your thinking about telling Discuss to somehow treat the full name as the username.

          If you want this change in multiple places, I would say the easiest way would be simply to change your registration so that the email is no longer the username. That's if there isn't a special need for that setup. On mine install the user chooses their username, enters their real name first and last, and then enters their email for confirmation.

          If you want it changed in the profile page only, that's not hard, just modify the tpl. For the places I mentioned above, you can also modify the tpls but it might be a little tougher. The bottom part is wrapper.tpl and the thread view is the sidebar tpl.

          Anyway, keep us updated on this if you have time. Its interesting and if I can help more, just ask. Although it seems like you understand Discuss more than I do ;-)
            • 3749
            • 24,544 Posts
            Quote from: sottwell at Apr 17, 2015, 06:11 AM
            Why not simply
            [[!+modx.user.id:userinfo=`fullname`]]

            LOL. I spent a half-hour trying to get to that solution before writing my snippet, but never got it exactly right. My solution would be faster, but yours is whole lot more convenient. 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
              • 38314
              • 45 Posts
              Well... hum thanks a lot everyone for your answers smiley
              But, I think I didn't explain clearly, I'm going to try to be more precise (english is not my native language).

              Maybe I don't understand how Discuss and Login manage user's registration and sessions.

              # 1 : I want Login to manage my users login, not discuss. It's a community forum and all my users are registered by admin. Their email uses both for the username and the email, the fullname is the fullname (displayed for identity) ;-)
              It's just like here smiley You can see
              Squarou reply #1
              In my community website, for the moment I have
              [email protected] reply #1
              I don't want that.

              # 2 where do I want the name changed ? everywhere in Discuss database of course ! In disUser. As far as I know (correct me if I'm wrong), disUser is synchronized from modUser database isn't it ? So, at one moment, Discuss copy/update its database form modUser's database.

              # 3 now, if not too far from truth, I guess there's a hack to sync disUser form modUser, for changing the request. disUser and modUser are using the same unique key (id). And then, username is also used in disUser from modUser. If I can tell Discuss don't sync disUser's username field form modUser's username field, but use instead modUser's fullname, I'm I getting wrong ? If not, how can I do that ?

              You understand now that I want to change this relationship between 2 databases. So, everywhere in Discuss's chunk (post, thread, last active), Discuss will display John Doe instead of "[email protected]".

              Another way would be to sync Discuss's "Display name" with modx's "fullname", setting use display name by default for everyuser.

              Looking at core/components/discuss/elements/snippets/posthook.discussmergeaccount.php
              Does it the syncing ?

              Thanks for reading this, hope it's more precise now.
              Regards,
                • 3749
                • 24,544 Posts
                Yes, discuss does sync. with the regular MODX user's DB, but I think only at registration. IIRC, the disUser id is the same ID as the modUser ID and the internalKey field of the user profile.

                I think what you want is a plugin or registration postHook that will copy the fullname from the MODX user profile to the Discuss Display Name field when the user registers (though it could probably also be done when the user logs in).

                You could also create a fairly simple utility snippet to transfer that data for existing users.
                  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
                  • 38314
                  • 45 Posts
                  Quote from: BobRay at Apr 17, 2015, 10:51 PM

                  I think what you want is a plugin or registration postHook that will copy the fullname from the MODX user profile to the Discuss Display Name field when the user registers (though it could probably also be done when the user logs in).
                  Yes that's it !

                  Quote from: BobRay

                  You could also create a fairly simple utility snippet to transfer that data for existing users.
                  Sure !

                  I'll investigate this week and give feedback here.
                    • 38314
                    • 45 Posts
                    OK. Well, I found in core/components/discuss/model/discuss/discuss.class.php (line 249) that the user is imported in Discuss database if it doesn't exists..

                    /**
                         * Initializes the user, tracks their ip and activity, and loads their
                         * profile. Also loads topbar information and links.
                         *
                         * @access private
                         */
                        private function _initUser() {
                            $this->modx->lexicon->load('discuss:default');
                            /* if no user, set id to 0 */
                            $isLoggedIn = $this->modx->user->hasSessionContext($this->modx->context->get('key'));
                            if (!$isLoggedIn) {
                                $this->user = $this->modx->newObject('disUser');
                                $this->user->set('id',0);
                                $this->user->set('user',0);
                                $this->user->set('username','(anonymous)');
                                $this->user->isLoggedIn = false;
                            } else {
                                /* we are logged into MODX, check for user in Discuss */
                                $this->user = $this->modx->getObject('disUser',array('user' => $this->modx->user->get('id')));
                                if (empty($this->user)) {
                                    /* if no disUser exists but there is a modUser, import! */
                                    $profile = $this->modx->user->getOne('Profile');
                    
                                    $this->user = $this->modx->newObject('disUser');
                                    $this->user->fromArray(array(
                                        'user' => $this->modx->user->get('id'),
                                        'username' => $this->modx->user->get('username'),
                                        'password' => $this->modx->user->get('password'),
                                        'salt' => $this->modx->user->get('salt'),
                                        'synced' => true,
                                        'syncedat' => date('Y-m-d H:I:S'),
                                        'confirmed' => true,
                                        'confirmedon' => date('Y-m-d H:I:S'),
                                        'source' => 'internal',
                                        'ip' => $this->getIp(),
                                        'status' => disUser::ACTIVE,
                                    ));
                                    if ($profile) {
                                        $this->user->fromArray($profile->toArray());
                                        $fullname = $profile->get('fullname');
                                        $name = $profile->get('fullname');
                                        $name = explode(' ',$name);
                                        $this->user->fromArray(array(
                                            'name_first' => $name[0],
                                            'name_last' => isset($name[1]) ? $name[1] : '',
                                        ));
                                    }
                                    $this->user->save();
                                }
                                $this->user->init();
                            }
                        }


                    I don't understand why this does'nt work :
                    'username' => $profile->get('fullname'),

                    It should fullname to create the username of a new user isn't it ?
                    Any ideas ?
                      • 3749
                      • 24,544 Posts
                      It should, but it will only work for users that have a user profile, and filled-in full name, and are logged in to the front end when this method is called, and it will only run once for each user. If the disUser already exists, the code doesn't execute.

                      Are you sure you don't want to set the 'display_name' field instead (and set use_display_name to 1). Changing the disUser username might have side effects and your users would have to use two different usernames to log in since Discuss has it's own login system. If the username is involved in the password hash (which it might be), they won't be able to log in to Discuss at all.
                        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