Assigning UserRole through Microflow on Register process?

-1
Hi there my fellow Mendixers! I’m quite new in Mendix, but I’m loving it. I’ve done some of the beginner and rapid course, but am stuck at understanding one of the things that may be fundamental in the Standard Mendix Security process. Here’s what I did: I have created Registration pages for two different types of users for my application - Customer and Business Owner. Upon registration I am using microflows to save the data (FirstName, LastName, Email, Password) to the database after they successfully register. Now I am trying to use a log-in page with the Mendix Authentication ‘Sign In’  Widget - and if I understood correctly - this widget uses User-Roles to check the authentication for the log in. I have indeed created the required User-Roles in my security setup of course, but I am not sure how to add them to the newly registered users.   My questions: I want to use the registration microflows that I already have and simply add logic to it which assigns an existing user-role to the newly registered user so that I can use that to compare with when they try to log in. It has to happen automatically under the hood upon successful registration as there is no Administrator who will be doing this for the users afterwards. Is there a way to do this directly in the microflow? Another thing I wonder is whether the “Sign In Authentication Widget” can be used without using the provided Account Entity? I’ve read that this entity is involved everywhere in the authentication process using user-roles. Are they dependencies of each other? If so, could someone explain how they are related?   Hopefully someone can push me in the right direction so that I can move on my with my awesome application idea. Cheers!
asked
4 answers
0

With regards to your first question, yes, that is possible. The approach might be a bit unfamiliar for the average developer but once I explain it, it will make sense.

User roles are objects in your database of type UserRole. They are created automatically under the hood once your app is deployed for the first time and every time you change or add user roles. To assign a user role is to assign the UserRole object to the user. So all you need to do is retrieve it from the database and make the appropriate association. But how do I get my User user role you might ask? There Mendix provides a useful symbol in XPath that you can use to filter. You would use the XPath expression below.

 

The answer to your second question is no, but you have authentication widgets in your Page toolbox that will work without a data view.

answered
0

Also have a look at ‘Base application’: baseapplication-sandbox.mxapps.io/. Using that module as your starter app will save you days or weeks development and save you from a lot of pitfalls and you will be working on your app instead of on basic functions that have been created millions of times already.

answered
0

Hi @Lennaert

Thanks for your quick reply.

My NewUser Entity has the following attributes:

  • - FirstName
  • - LastName
  • - EmailAddress
  • - Password
  • - UserRole (this one is not visible in the registration form of course.)

 

At the moment, in my Registration microflow I do the following:

  1. I check all the input fields that can be filled in at Registration.
  2. Provide feedback messages if needed.
  3. I do a final check on validity of all data entered.
  4. If everything is valid, I retrieve the 'Customer' user role as per your example.
  5. Now I want to assign this retrieved user role data to the UserRole attribute of my NewUser, but not sure how. Do I make a variable and assign the retrieved value to that first in order to assign it to my object, or maybe do a Variable Change accordingly? Or do I make an association in my Data Domain between NewUser and UserRole and assign the UserRole value in that way? Or is there some completely other way to do this?
  6. When everything is done, I commit my NewUser to the database along with the data.

 

Sorry for the long hassle, I'm just trying to figure out how these things work in Mendix properly without getting confused on the amount of options to do the same thing.

Cheers!

 

answered
0

Hello Nikola,

user roles are created automatically in project once your app is running so you will need to retrieve it from the database and make relationship association between required entities .mendix provides a useful symbol XPATH that you can use to filter. [id = ‘[%UserRole_Administration%]’] use this XPATH expression.

answered