<p>Currently, Craft doesn't expose the UID in any of its models.</p>

<p>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.</p>

CC BY-SA 3.0
2014-11-26T05:53:35.510Z
thanks brad, kinda a pain but yeah ill do a workaround.
Luke Holder
CC BY-SA 3.0
2014-11-26T05:55:26.150Z
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
CC BY-SA 3.0
2014-11-26T05:58:21.277Z
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
CC BY-SA 3.0
2014-11-26T04:47:19.017Z
2014-11-26T05:12:04.390Z
2014-11-26T05:12:04.390Z
Brad Bell
<p>If you want to get a user model based on UID you can do this. </p>

<p>For current user..</p>

<ol>
<li>Get current user <code>$currentUser = craft()-&gt;userSession-&gt;getUser();</code></li>
<li>Get user record <code>$currentUserRecord = $this-&gt;_getUserRecordById($currentUser-&gt;id);</code></li>
<li><p>Add this private function (copied from UserService.php). This will pull the user record by calling <code>_getUserRecordById</code> function</p>

<pre><code>private function _getUserRecordById($userId)
{
  $userRecord = UserRecord::model()-&gt;findById($userId);
  if (!$userRecord) {
    throw new Exception(Craft::t('No user exists with the ID “{id}”.', array('id' =&gt; $userId)));
}

return $userRecord;
}
</code></pre></li>
<li>Now get user UID <code>$currentUser-&gt;uid</code></li>
</ol>

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

<ol>
<li>Get user record <code>$userRecord = $this-&gt;_getUserRecordById('32');</code></li>
<li>Now get user UID <code>$userRecord-&gt;uid</code></li>
</ol>

<p>my two cents.</p>

CC BY-SA 3.0
2015-09-28T13:03:13.997Z
Its a possible solution but the resulting record is not an element and has no ability to access user custom fields.
Luke Holder
CC BY-SA 3.0
2015-09-24T21:03:31.440Z
2015-09-24T22:34:10.043Z
2015-09-24T22:34:10.043Z
Vadim Goncharov
<p>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 <code>craft()-&gt;users-&gt;getUserByUid($uid);</code> but after getting the uid, the uid is not on the user model which is kinda a pain.</p>

<p>For example what if i wanted the uid for the currently logged in user?</p>

<usermodel>
Getting the UID of a user model
# Getting the UID of a user model

(Asked by: username_0 on 2014-11-26)

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?

**Tags:** usermodel


---

## 2 answers

### Answer by username_1 on 2014-11-26. Score: 2 | Accepted answer

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.

> **Comments:**
> **username_0**: thanks brad, kinda a pain but yeah ill do a workaround.  
> **username_1**: Entirely possible we expose UID to the UserModel at some point, too. Will have to think about any side-effects from doing so.  
> **username_0**: 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.  

---

### Answer by username_2 on 2015-09-24. Score: 2

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

Get current user $currentUser = craft()->userSession->getUser();
Get user record $currentUserRecord = $this->_getUserRecordById($currentUser->id);
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;
}

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)

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

my two cents.

> **Comments:**
> **username_0**: Its a possible solution but the resulting record is not an element and has no ability to access user custom fields.  

---

