I have two tables:
nets_permissions
user_id INT NOT NULL,
network_id BIGINT UNSIGNED NOT NULL,
perm INT(3) NOT NULL,
PRIMARY KEY(user_id, network_id)
devices_permissions
user_id INT NOT NULL,
network_id BIGINT UNSIGNED NOT NULL,
device_id INT UNSIGNED NOT NULL,
perm INT(3) NOT NULL,
PRIMARY KEY(user_id, network_id, device_id),
The nets_permissions table has permissions for each user. A user can have a different perm values: 1 for read permission, 2 for write permission, 4 for read+commands permission. The net admin have a perm value of 3.
Same thing for the second table, but this time for the devices that are in the specific net. The users are added to this table when the admin of the net and devices give them a permission. But the admin of the net is not registered in this table. He is only in the nets_permission table with perm=3.
I have to create a query that select the permission of the current user in an handler of Tornado Web Server. The code is in this form:
# Retrieve the current user
usr = self.get_current_user()
usr_id = usr['id']
self.lock_tables("read", ['nets_permissions as n, devices_permissions as d'])
usrperm = self.db.query("SELECT * FROM nets_permissions as n
LEFT OUTER JOIN devices_permissions as d
ON n.network_id = d.network_id WHERE d.user_id=%s
AND d.device_id=%s",
int(usr_id), sens.id);
self.unlock_tables()
In usr_id I retrieve is the id of the current user.
The current user can be the admin of the net (so he is only in the table nets_permissions with perm=3) or a user with some permission on the specific device (so he is only in the devices_permissions table with some value for perm).
In the query result ‘userperm’ I would use an integer to compare after in the html page with a permission value and to show or not something to the user.
Sorry for my English but is difficult explain for me the problem. I don’t know I can structure the query to obtain this result I want.
Thank you very much for your help.
However, it looks like there’s a problem with your data model. If a device has no users with permissions to the device, there won’t be a row in
devices_permissions, so we won’t be able to find itsnetwork_id. You need to normalize this better: there should be adevice_networktable to relate them.