Create a user corresponding to each record in a table
In this article, we’ll show how to automatically create a user account for each record in a table. This is useful when you have a table of users and you want to create a corresponding user account for each user in the membership_users
table.
For example, let’s say you have an application for managing rental units, and you have a table named tenants
that contains the details of each tenant. You want to create a user account for each tenant in the membership_users
table. This way, each tenant can log in to the application and view their details, rent payments, submit maintenance requests, etc.
Another example, which we’ll be using below, is a customers
table. We’ll show how to create a user account for each customer. This would allow each customer to log in to the application and view their orders, invoices, etc.
User tables
In order to be able to create a user account for each record in a table, you need to have a table in your AppGini application that contains the details of each user. This table should contain at least an email
field, which will be used as the username for the user account. It’s also recommended to set this email field as unique in the table structure.
You also need to create a user group in the admin area that corresponds to the table. For example, if you have a customers
table, you should create a user group named Customers
,
with the appropriate permissions. Those permissions should allow users in this group to typically view their own records in specific tables, and possibly edit them.
Adding a new record to the users table
The screenshot below shows the message displayed after adding a new customer to the customers table. The message indicates that a user account has been created for the customer. It includes the username, which is the same as the customer email, an initial strong password, and the user group to which the user account was added.
Showing the status of an existing user
When you open an existing customer record in the customers table, the status message at the top of the page shows that a user account already exists for this customer. It includes the username, which is the same as the customer email, and a link to view the user details in the admin area.
Clicking the View button from the status message opens the user details in admin area:
Part of the user details visible in the admin area is the comment field, which indicates the customer ID corresponding to this user account, as well as the initial password that was set when the user account was created.
Code snippets for automatic user creation
tablename_dv
code
In the generated hooks/tablename.php
file, add the following code line inside the tablename_dv()
function,
where tablename
is the name of the table for which you want to create user accounts.
|
|
Replace tablename
with the actual name of the table, for example, customers
or tenants
.
The above line checks if an existing table record is being viewed in the detail view (dv) page. If so, it calls the createUserIfNotExists()
function to create a user account for this record if it doesn’t already exist. The function also displays a notification message indicating the status of the user account creation.
Tip: If you have multiple types of users in your application, you can add the above line for each user table in the
tablename_dv()
function. For example, if you want to create users for bothcustomers
andemployees
tables, you can follow the above step for both tables. And you need to have aCustomers
user group and anEmployees
user group pre-defined in the admin area.
The createUserIfNotExists()
function in hooks/__bootstrap.php
Add the following function to the hooks/__bootstrap.php
file. If the file doesn’t exist, create it in the hooks
folder.
|
|
Let’s go through the above function step by step (you might skip this if you’re not interested in the details):
- The function
createUserIfNotExists()
takes two arguments:$tablename
and$id
.$tablename
is the name of the table for which you want to create user accounts, and$id
is the ID of the record in this table. - The function first fetches the record data from the table using the
getRecord()
function. - It then checks if the email field in the record is not empty and is a valid email address. If not, it displays an error message and returns.
- It then checks if there is a matching user account in the
membership_users
table for this email address. If a matching user account is found, it displays a message indicating that the user account already exists and returns. - If no matching user account is found, the function generates a random initial password and inserts a new record in the
membership_users
table. The new record includes the email address as the username, the generated password, the user group ID corresponding to the table, and some other fields. - Finally, the function displays a success message indicating that the user account has been created and returns.
Troubleshooting, when things don’t work as expected
If you encounter any issues while implementing the above steps, here are some tips to help you troubleshoot:
- Check the
email
field in the table: Make sure the table for which you want to create user accounts has anemail
field that contains valid email addresses. The email field should be set as unique in the table structure. - Check the user group in the admin area: Make sure you have created a user group in the admin area that corresponds to the table. The user group should have the appropriate permissions to view and edit records in the table.
- Check the
tablename_dv()
function: Make sure you have added the line to call thecreateUserIfNotExists()
function in thetablename_dv()
function in thehooks/tablename.php
file, wheretablename
is the name of the table.
Conclusion
In this article, we showed how to automatically create a user account for each record in a table. This can be useful when you have a table of users and you want to create corresponding user accounts for each user in the membership_users
table. By following the steps outlined in this article, you can automate the process of creating user accounts for users in your application, making it easier for them to log in and access their data.