Help Understanding GetUserId()

Just looking for a little guidance on exactly what this is and it works.

For example, lets say that a page is being looked at anonymously, if they enter some information in a form and click submit, and I run a save action with GetUserId() in it, will I return anything?

Or in another instance, if I allow someone to "sign up" for a website, they are NOT a registered user, and they are NOT in the Users Entity in the System eSpace (they are at my page anonymously, entering information into a form).  If I want them to be able to add themselves to the Users Entity, I have to first grant them UserManagerRole, which requires a UserId() input parameter.  But because this person, at the time of sign-up, has no User Role in my system, they have no User.Id.... what do I use for the UserId() input parameter in UserManagerRole?  If I use the GetUserId() function, what will be returned?

And then when it comes time to revoke UserManagerRole after they are in my Users entity, should I use the newly created User.Id as the UserId() input in RevokeUserManagerRole, or do I need to use GetUserId() again?  Meaning the GetUserId() and the User.Id are NOT the same value?

Hi Rob!  Great questions!  There are a couple of questions in your post, so let me make sure I've got them:

1) If a page is being browsed anonymously and you allow them to save the form the GetUserId() will not return anything, because they aren't a) created and b) authenticated.  So this would work for forms where they are requesting information and you have a choice later to sign them up based on info in the form (email address for example).

2)  The registration part is kind of a two part thing - first you have to create the User, then grant that user a Role.  I've done this a bunch of different ways depending on where they are, but in the simplest form, I would create a basic Role in the eSpace and then when the User hits Submit, build the logic to validate the form, create the user (which will return the User.Id) then Grant the basic Role.  Under no circumstance should you ever grant a user the UserManager role.

3) If in the future you need to revoke them (or remove them) then you would revoke the role and then delete the user.

I haven't looked at this, but it sounds like it might help:

Hope this helps!


Thanks for the help.

I think my confusion is that while I was actually using that user registration flow you linked to, I also looked at the How2UserRegistration example

And if you look at that example a couple things stand out.  1) I see they use the GetUserId() in the flow, but again if you're not registered or assigned any sort of role (you're just information in a form and you've clicked submit), what ID is being assigned when GetUserId() is being called?

and 2) This example they grant the UserManagerRole using GetUserId(), add the person to the Users Entity, then immediately revoke UserManagerRole after the User is added to the database.  Would this be poor practice?  Thing is I don't see a way around doing it the way they do.