It depends how complex your parent - child relation will be.
The best solution would be is to write a custom plugin or module for that, with records and rules, some controllers ...etc
My solution would be the following
Allow public registration
Create two user groups
:
Set the default user group to Parent
Make sure the user group Parent
has the permission:
Assign User Groups -> Assign users to "Child" checked
Create a field group
with one field in it:
- parentId > number (more precisely int)
Assign this field group to the users
Create a front-end user registration form for the parents so they can register.
And then you will need one more form for the parents, so they can register their children.For this, the parents are required to be logged in, otherwise it won't work.
You can check this with
{% if not craft.app.user.isGuest %}
{# Your child registration form #}
<input type="hidden" id="parentId" name="parentId" value="{{ craft.app.user.id }}">
{% else %}
{# The user is not logged in #}
{% endif %}
This way you can:
- Differentiate Parent users from children using:
{% set userGroups = craft.app.user.identity.getGroups() %}
- Get the logged in child's parent user object using:
{% set parent = craft.users().id( craft.app.user.identity.parentId ).one()%}
- Get the logged in parents children
{% set children = craft.users().parentId( craft.app.user.id ).all() %}