You can add the supplied authentication model to your Bootcamp application and use it to control which ViewModels logged-in users and administrators can open.
This is Chapter 11 of the MDriven Bootcamp. Continue from Chapter 10, or return to Chapter 1 if you need to restart.
What you will build
In this chapter, you merge the SysUserAuthentication model and then use its types to:
- keep the current user in a singleton object;
- add common creation and update information through a default superclass;
- register and log in users in the local Turnkey application;
- restrict a ViewModel to logged-in users; and
- restrict SysUser administration to administrators.
The example uses the CarSeeker ViewModel as a page that requires a login and the SysUser autoforms as pages that require administrator access.
Merge the authentication model
- In the MDriven wiki, open Model Examples and download the SysUserAuthentication model.
- Open your Bootcamp model in MDriven Designer.
- Open and merge the downloaded model into your model.
- Save the merged model.
The merged model contains an authentication package, including SysSingleton, SysSuperClass, and SysUser.
Understand the singleton and current user
A singleton is a class for which the running system uses one shared object. In this model, SysSingleton provides that shared object, and its CurrentUser association identifies the user currently logged in.
- Select
SysSingletonin the class diagram. - In the object inspector, verify that
IsSingletonis set. - Select the
CurrentUserassociation onSysSingleton. - Verify that
Persistentis false.
A non-persistent association is transient: it represents runtime state and is not stored as persistent data. Here, the current login is runtime state rather than a permanent property of a user object.
Test the singleton in the prototyper
- Start the system prototyper with XML persistence.
- Open the debugger.
- Enter the following OCL expression and execute it with F5:
SysSingleton.oclSingleton
You should receive an object. This is the shared SysSingleton object used by the running prototype.
Use SysSuperClass as the default superclass
SysSuperClass provides common behavior for classes in a package. Inspect its OnCreate and OnUpdate definitions and use the debugger to observe when they run. In this chapter, you use it to make a change timestamp available on Person.
- Select
SysSuperClassin the diagram. - Inspect its
OnCreateandOnUpdatesettings. - Verify that
SysSuperClassis the default superclass ofAuthenticationPackage. - Set the default superclass of
Package1toSysSuperClass. - Refresh the model or restart the debugger.
- Inspect
Person. Verify that it now has theChangeTimeattribute.
A default superclass applies inheritance to the classes in the package. For example, after setting Package1's default superclass, Person inherits the common members supplied by SysSuperClass.
Verify inheritance with OCL
In the debugger, execute each expression below. oclIsTypeOf checks an exact type, oclIsKindOf checks a type relationship, and superTypes returns the superclass types.
| Expression | Expected result |
|---|---|
SysSingleton.oclSingleton.oclIsTypeOf(SysSuperClass)
|
True (shown as a check mark) |
SysSingleton.oclSingleton.oclIsKindOf(SysSuperClass)
|
True (shown as a check mark) |
SysSingleton.superTypes
|
SysSuperClass
|
Update existing Person objects
Existing Person objects need a Guid value for the later exercises.
- In the debugger, change the expression mode from OCL to an Action expression by selecting Action.
- Enter the following expression:
Person.allinstances->collect(p|p.Guid:=Guid.NewGuid)
- Execute it with F5.
- Verify that each Person now has a new Guid value.
- Save the data.
- Save the Person objects and verify that
ChangeTimeupdates.
The expression collects all Person instances and assigns a newly generated Guid to each object.
Register and inspect a user
- Start the Local Turnkey Prototyper.
- Attempt to log in with any user.
- Register a new user with an email address and password.
- Return to the debugger and execute:
SysUser.allinstances
Verify that the registration created a SysUser object.
Restrict CarSeeker to logged-in users
An access group is a named condition that controls whether a ViewModel or action is viable and enabled. The supplied IsLoggedIn access group is used to make CarSeeker available only after a successful login.
- Open the ViewModel Editor.
- Select the
CarSeekerViewModel. - Set its AccessGroups value to
IsLoggedIn. - Save the model.
- Test the web application while logged out. Verify that
CarSeekeris not available. - Log in and verify that
CarSeekerbecomes available. - In the top menu, select the AccessGroup tool button next to the Actions Editor button.
- Inspect the expressions that define
IsLoggedInand confirm how they determine the logged-in state.
Create an administrator access group
Create an IsAdministrator access group for views that expose user administration.
- In the AccessGroup editor, add an access group named
IsAdministrator. - Set both its Enable Expression and Viable Expression to:
SysSingleton.oclSingleton.CurrentUser.IsAdmin=true
- Save the model.
This condition evaluates the IsAdmin property of the user associated with SysSingleton.oclSingleton.CurrentUser.
Protect the SysUser autoforms
- Refresh AutoForms and verify that autoforms are created for
SysUser. - Open
AutoFormSysUserin the ViewModel Editor. - Set its access group to
IsAdministrator. - Save and test the web application as a non-administrator.
- Verify that access is denied when the user attempts to open the protected view.
Create an AccessDenied destination
Create a ViewModel named AccessDenied to explain that the user does not have permission to access the requested view.
- Create a new ViewModel named
AccessDenied. - Add a row by double-clicking in the ViewModel editor.
- Configure the row to present an access-denied message.
- Save the model.
The exact row expression or presentation configuration for this message must match the intended Bootcamp model configuration.
Make an existing user an administrator
- Set the access group of
AutoFormSysUserSeekertoIsAdministrator. - In the debugger, re-read the model.
- Execute
SysUser.allinstancesand save the result context. - Open the SysUser Seeker, select a user, select the
IsAdmincheck box, and save. - Return to the web application and verify that this administrator can open
AutoFormSysUserSeeker.
Verify access with two users
- Create another user, for example
b@b.se. - Log in as the new user.
- Attempt to open
AutoFormSysUserSeeker. - Verify that the non-administrator cannot open it and is directed to the
AccessDeniedViewModel.
You now have two distinct authorization rules: IsLoggedIn protects CarSeeker, while IsAdministrator protects user-administration views. The next chapter applies these rules to control editing and visibility in the car application.
