I assume you understand SQL and database concepts such as 1 to 1, 1 to Many and Many to Many relationships.
I think there are 2 parts of your question above.
1. We need to make sure a user is registered in the database and allowed to execute commands with correct username and password
2. We need to make sure he/she only execute queries or update on his/her data, not data belongs or linked to another user
Let say the data looks like below.
users table
| id | name | password |
|---|
| 1 | Alice | xxxxxx |
| 2 | Bob | yyyy |
animals table
| id | name |
|---|
| 1 | Cat |
| 2 | Dog |
| 3 | Echidna |
| 4 | Frog |
user_animals table
[CODE lang="sql" title="SQL1a"]SELECT a.* FROM users u
JOIN user_animals ua ON ua.user_id = u.id
JOIN animals a ON ua.animal_id = a.id
WHERE u.name = 'Alice' AND u.password = 'xxxxxx'[/CODE]
This query will return 2 animals (Cat, Echidna) linked to Alice
[CODE lang="sql" title="SQL1b"]SELECT a.* FROM users u
JOIN user_animals ua ON ua.user_id = u.id
JOIN animals a ON ua.animal_id = a.id
WHERE u.name = 'Bob' AND u.password = 'yyyy'[/CODE]
This query will return 1 animal (Dog) linked to Bob
If either one of the users or let say Oscar provides wrong username and password, no results are return.
[CODE lang="b4x" title="SQL2a"]UPDATE animals
SET name = 'Hedgehog' WHERE id = 3
AND id IN (SELECT a.id FROM animals a
JOIN user_animals ua ON ua.animal_id = a.id
JOIN users u ON ua.user_id = u.id
WHERE u.name = 'Alice' AND u.password = 'xxxxxx')[/CODE]
When Alice provides the correct password and the animal id she is updating is linked to her then the update will take place.
[CODE lang="b4x" title="SQL2b"]UPDATE animals
SET name = 'Gorilla' WHERE id = 3
AND id IN (SELECT a.id FROM animals a
JOIN user_animals ua ON ua.animal_id = a.id
JOIN users u ON ua.user_id = u.id
WHERE u.name = 'Bob' AND u.password = 'yyyy')[/CODE]
When Bob provides the correct password but the animal id is not linked to him then the database is not updated.
We can add above queries into config.properties of jRDC2 server as:
B4X:
sql.select_user_animals = SELECT a.* FROM users u \
JOIN user_animals ua ON ua.user_id = u.id \
JOIN animals a ON ua.animal_id = a.id \
WHERE u.name = ? AND u.password = ?
sql.update_user_animal = UPDATE animals \
SET name = ? WHERE id = ? \
AND id IN (SELECT a.id FROM animals a \
JOIN user_animals ua ON ua.animal_id = a.id \
JOIN users u ON ua.user_id = u.id \
WHERE u.name = ? AND u.password = ?)
This is just a simple example. There are always room for improvement.