ConceptHMI Web Help
Defining and managing user rights in an application
Introduction

When developing an application, certain functionalities are intended for different use cases in different situations. It can depend on the state of the application, the state of an external peripheral, the access level of the current user and many other reasons. This chapter will explain in details how to take advantage of the user management system brought by Concept.

How to define custom rights for an application

The rights of an application are created with an enumeration as shown in the example below. As said above, a right represents an access to a specific functionality of the application.

public enum UserRights
{
    Recipe, Execution, Configuration, Settings
}

These rights should then be included into the RightRepository. The method below needs to be called only once at the start of the application.

private void InitializeUserRight()
{
    RightRepository.Instance.Initialize(typeof(UserRights));
}

Generating rights configuration file

In order to save the rights configuration of an application, the content of the RightRepository is automatically written to a .cxml file. This file is automatically generated when an application with its rights defined is started for the first time. This file can be found here C:\Users\UserName\AppData\Roaming\CompanyName\ApplicationName\Rights.

CompanyName and ApplicationName are retrieved from the assembly information of the application.

AssemblyInfo

Giving access to functionalities based on user rights

Once the rights of the application have been registered, access to functionalities can be set, based on the rights of the current user. Below are examples of how to do it in C# and XAML.

C# use

if(RightRepository.Instance.CurrentUser.HasRight(UserRights.Execution.ToString()))
    // Access functionality

XAML use

To give access to a functionality in XAML, the Markup Extensionn RightsExtension can be used. It will work both on Visibility and boolean properties.

The table below describes the default behavior of RightsExtension.

Current user has access Visibility property Boolean property
True Visible True
False Collapsed False
The VisibilityWhenNotValid property can be used to change the visibility when the current user does not have rights.
<Button Visibility="{concept:Rights Configuration, VisibilityWhenNotValid=Hidden}"/>
<Button IsEnabled="{concept:Rights Configuration}"/>

Creating a custom behavior

For more extensibility, the Converter property can be used to define a custom behavior, as shown in the example below.

Changing control content based on current user rights

The purpose of this example is to define a different content based on the rights of the current user. To do so, the converter below is used.

public class IsUserAllowedToContentConverter : BaseConverterMarkupExtension
{
    public object ContentIfAllowed { get; set; }
    public object ContentIfDisallowed { get; set; }
    public override object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        bool isAllowed = (bool)value;
        return isAllowed ? ContentIfAllowed : ContentIfDisallowed;
    }
}

And here is how it can be used.

<ContentControl Content="{concept:Rights Execution, Converter={local:IsUserAllowedToContentConverter ContentIfAllowed={StaticResource TextBlockIfAllowed}, ContentIfDisallowed={StaticResource TextBlockIfDisallowed}}}"/>
Managing users and groups

Using ready-to-use editor directly in application

To edit users and groups directly in an application, ConceptRightsView can be used as shown below.

<concept:ConceptRightsView/>

With this control users and groups can be added, removed or edited as shown below.

User edition

User edition

User edition

When editing a user the following properties can be changed:

Group edition

Group edition

Group edition

When editing a group the following properties can be changed:

Using external rights configurator

If a choice has been made not to include the rights configurator directly into the application, oStudio can be used to manage them.

To do so a ConceptEditor project is needed.

ConceptEditor project creation

ConceptEditor project creation

The rights file generated by the application has to be opened.

Open generated rights file

Open generated rights file

Once the file has been opened, the edition screen appears, as shown below.

Rights file edition with oStudio

Rights file edition with oStudio

For more information about users and groups edition, see chapters user edition and group edition.
Setting up user authentication

Using built-in login control

To manage login and logout of users, ConceptLoginView can be used.

<concept:ConceptLoginView/>
ConceptLoginView

ConceptLoginView

This control will automatically check the password and set the CurrentUser property of the RightRepository to the selected user. If the property IsUserVisible has been set to false on a user, he will have to log in by clicking on the button Other User..., the control below will then appear.

Login as other user

Login as other user

Extending user management system

Depending on the application, it might be helpful to gather more information about a user or a group of users. To do so, the classes User and Group can be inherited to create custom classes containing additional information and behaviors, as shown in the example below.

Creating custom User class

This example explains a way of extending the User class in order to know the dominant hand of a user.

Here is how to inherit from User to create a new class containing user's dominant hand information.

public class ApplicationUser : User
{
    public HandSide DominantHand { get; set; }
}

public enum HandSide { Right, Left }
Going through the model

Below are the main components necessary to set up user management in an application.

RightRepository UML

The RightRepository class is a singleton which encapsulates the users (BaseUser), the groups of users (BaseUserGroup) and the rights (Right). A user and a group of users can both have rights. A right represents an access to a specific functionality. The RightRepository keeps a reference to the current user of the application, that is the user who is logged in.

The user's rights are always applicable even if the group he is part of does not have them. On the other hand a user can benefit of all the rights of his group.

In order to easily define and use Concept users management in an application, the views ConceptLoginView and ConceptRightsView have been created.

See Also

Reference

 

 


© 2016 Objectis

Send Feedback