Tutorial - Advanced Security - Static User Level Security

In this tutorial we will show you how to setup static User Level Security. We will use the demo database for demonstration.

User Level
User Level Security secures data at table level. Each user level is granted with specific permissions to tables in the database. Users with different access levels are restricted with different add/copy, list/search/view, delete and edit rights.

There are 2 types of User Level Security:

Static User Levels The User Levels and permissions are defined in the project and the User Levels are not to be changed after script generation.
Dynamic User Levels The User Levels and permissions are defined in 2 tables in the database, the User Levels can be changed with the generated scripts.

In this tutorial we use Static User Level Security.

The "employees" table and "orders" table in the demo will be used in this example.

Fields in Table "employees"

Fields in Table "orders"

Steps to Setup Static User level Security

1. Open DB AppMaker

Open DB AppMaker and connect to the demo database.

2. Set up User Levels

Click on the Security tab, there are two sections for the login process:

Administrator Login
If you enable this option, a hard-coded Administrator account will be generated which has all access right to all tables.

Use Existing Table
Enable this option to set up the user levels. You should select the security table and the corresponding User Name and Password fields.

To set up the user levels, click on the Advanced button. A popup window will appear. Click on the User Levels button. Select the User Level Field.

Important The User Level field must be of integer data type. Non integer fields will not be seen in the User Level Field combobox.

There are three built-in user levels:

Anonymous The built-in user level for the anonymous user (i.e. users that have not logged in). The User Level ID of Anonymous is -2.
Administrator The built-in user level that has all permissions plus the privileges to modify User IDs and User Levels. Its permissions are same as that of the hard-coded Administrator. The User Level ID of Administrator is -1.
Default The built-in default user level for logged in user. The User Level ID of Default is 0. Since User Level field is an integer field, if you set a default value of 0 for this field, this user level will become the default user level for the user after registration and before the Administrator assigning another higher user level.

Click to add a new user level. Enter the description, and default permissions. Click OK to finish.

For each user level, you can set refine the permission for different tables/views. Click OK to finish.

If you go to Field Setup Page now and view the Edit Tag for the User Level Field, you should see that the Edit Tag has been setup as "SELECT" and the user levels have been added automatically in the value list:


3. Generate scripts

Go to the Generate tab, click the Generate button to generate scripts.

4. Run the application

To assign different user level for the users, login as Administrator and go to the user table (the "employees" table in this case). You'll find that the Edit Tag of the User Level Field is setup as "SELECT" (combobox) and the combobox is populated with the user levels we defined above automatically.

We assign a password and the user level "Sales" to the employee #1 (the employee with EmployeeID equals 1). Then we logout.

To see the Advanced Security works, login as employee #1 using "nancy" as user name and "1234" as password.

According to the user level defined by us, users with "Sales" level has view and add permissions to the "orders" table only. They are not allowed to update or delete records. Employee #1 belongs to the "Sales" level, so you can view the records, you can see the "+" button at the bottom. If you slide the item, you can only see the "Copy" button, but not the "Edit" and "Delete" buttons.

  

 

Notes
  1. User Level Security settings are generated in a file named userlevelsettings.php. After changing permissions, you must generate that file again.
  2. The permissions are stored in session variables after login. After changing permissions, users need to logout (to destroy sessions) and then login again for the new permssions to be effective.
  3. Users have no right to change his own user level. Only Administrator can change an user's user level. If you want to assign user levels in the generated scripts, a hard-coded administrator login account must also be created. Alternatively, an user must be assigned with the Administrator level. However, you may still need to use the hard-coded Administrator Login to log in and assign user levels to users initially. Of course, you can also modify data in your database directly. The value of the Administrator level is -1.
  4. Since User Level works at table level only, if an user has permissions to the User Table, he/she may be able to modify personal information (including user level and user id) of other users. Therefore you should not expose the User Table to normal users. If you want to expose the User Table and restrict users to access their own data only, you need to use User ID Security, which controls permissions at record level. (See Advanced Security - User ID Security)
  5. There are two types of Advanced Security - User ID Security and User Level Security. User ID Security secures data at record level; User Level Security secures data at table level. They can work independently or work together. (See Advanced Security - User ID Security)

 

Also See

Advanced Security - User ID Security
Advanced Security - Dynamic User Level Security

 

 ©2016-2021 e.World Technology Ltd. All rights reserved.