SysUser is the model class you use to represent an authenticated end user in a Turnkey application; this page is for modelers who need to add, identify, and extend application users.
Use SysUser for application identity
SysUser is part of the user-login pattern used by MDriven Turnkey applications. The pattern combines:
- SysUser â the user identity, including the username and authentication-related data.
- SysSingleton â the singleton through which the application can identify the current user.
- SysSession â a record of login activity, including the user agent and IP address.
Use SysUser when an action must be associated with the person who is currently signed in. For example, an expression can obtain the current user's username through the current user exposed by SysSingleton:
SysSingleton.oclSingleton.CurrentUser.UserNameThe availability of CurrentUser is useful when, for example, you need to record who performed an action or show the signed-in user's identity in a ViewModel.
Add the authentication pattern
The SysUser class is supplied by the SysUserAuthentication pattern. This pattern integrates registration and sign-in into a Turnkey application and covers both authentication and authorization:
- Authentication establishes who the user is.
- Authorization controls what the user is permitted to do.
To add the pattern to a model:
- Open the model in MDriven Designer.
- Open TK Live View.
- Merge the SysUserAuthentication pattern into the model.
- Review the merged classes and use SysUser as the application's end-user class.
For a new application, Turnkey sample Empty start model provides a starting model containing an index page and the SysUser structure.
SysUser data in the authentication pattern
When you use the SysUserAuthentication pattern, SysUser includes the following authentication-related attributes.
| Member | Type | Purpose |
|---|---|---|
isAdmin
|
Boolean?
|
Identifies a user with administrator permissions. |
PasswordHash
|
String?
|
Stores the password hash rather than a plain-text password. |
SecurityStamp
|
String?
|
Invalidates sessions and cookies when the user resets a password or adds an external login. |
SendMeNews
|
Boolean?
|
Records whether the user subscribes to the newsletter. |
UserName
|
String
|
The user's unique username. |
The pattern also defines password-related operations on SysUser:
| Operation | Arguments | Result and use |
|---|---|---|
HashPassword
|
pwd:String
|
Returns a password hash for the supplied password. |
SetPassword
|
pwd:String
|
Sets PasswordHash by using HashPassword.
|
Do not store a user's password in a separate plain-text attribute. Use the password hashing approach described in Hash and Validate Passwords. In .NET Core, SysUser must also have an optional Email:String? attribute when using those password methods; otherwise the Turnkey log reports that SysUser has no member named Email.
Show the signed-in user
The main menu can show the logged-in user's UserName. You can show a more meaningful identity by setting the DefaultStringRepresentation of SysUser in the model.
For example, if SysUser is associated with a Person, the representation can show the person's first and last name when available, and fall back to the username otherwise:
if self.Person->isempty then
self.UserName
else
self.Person.FirstName+' '+self.Person.LastName
endifSee Logged in Person presentation for the complete example, including how to include information from the person's house.
Extend login capabilities
Use the related pattern classes and packages instead of adding parallel user-login structures.
| Need | Use |
|---|---|
| Track login activities, user agents, and IP addresses | SysSession |
| Support Google, Open ID, Microsoft, or Facebook sign-in | SysUserAuthentication and its SysExternalLogin class
|
| Assign roles used with access groups and ViewModel permissions | SysUserAuthentication and its SysRole class
|
| Let users reset passwords | Password Reset Package |
| Charge an authenticated end user or present account top-up UI | Charge end user |
When an external-login user becomes stuck on the registration page, investigate stale or incorrect SysUser and SysExternalLogin objects. Follow the cleanup and restart guidance in External login screen problem.
