1

I want to use the UID to get a user from a url and don't want to use the ID as it is guessable. I know I can do craft()->users->getUserByUid($uid); but after getting the uid, the uid is not on the user model which is kinda a pain.

For example what if i wanted the uid for the currently logged in user?

2

Currently, Craft doesn't expose the UID in any of its models.

Kind of a pain, but the only thing I can think of as as a workaround is to write a plugin that has a UserModel with the UID and mimics whatever methods from UsersService that you need.

  • thanks brad, kinda a pain but yeah ill do a workaround. – Luke Holder Nov 26 '14 at 5:53
  • Entirely possible we expose UID to the UserModel at some point, too. Will have to think about any side-effects from doing so. – Brad Bell Nov 26 '14 at 5:55
  • i understand security concerns... just as a little background on what I am building: a link in an email that contains the users UID within the link as well as a one time use code to log the user in directly from the email link using userSession->impersonate(). I just wanted to use the UID and Code not the userId and Code - as the userId is guessable. – Luke Holder Nov 26 '14 at 5:58
2

If you want to get a user model based on UID you can do this.

For current user..

  1. Get current user $currentUser = craft()->userSession->getUser();
  2. Get user record $currentUserRecord = $this->_getUserRecordById($currentUser->id);
  3. Add this private function (copied from UserService.php). This will pull the user record by calling _getUserRecordById function

    private function _getUserRecordById($userId)
    {
      $userRecord = UserRecord::model()->findById($userId);
      if (!$userRecord) {
        throw new Exception(Craft::t('No user exists with the ID “{id}”.', array('id' => $userId)));
    }
    
    return $userRecord;
    }
    
  4. Now get user UID $currentUser->uid

So it'll be similar for any other user. Assuming you have a user id (I'll use 32 for example)

  1. Get user record $userRecord = $this->_getUserRecordById('32');
  2. Now get user UID $userRecord->uid

my two cents.

  • Its a possible solution but the resulting record is not an element and has no ability to access user custom fields. – Luke Holder Sep 28 '15 at 13:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.