** I created two different forms (form1admin for admin, and form2users for regular users). How do I route newly create users to the right form? Is there a proper way of doing this? **
Here is an example:
A new user creates a login and I enable it as Admin from my end in the data tables.
Another user creates a login and I enabled their account as a User. How do I tell the system to show one form to USER and a different view/form to Admin?
I am really looking for guidance and direction here.
The basic idea is you use the information in your data tables to make the decision. For example, I have a boolean column in my Users table named admin. So when I need to make the decision, I use something like:
user = anvil.users.get_user()
if user['admin']:
show_admin_form()
else:
show_user_form()
This is where it starts to get a little more complicated. The easy, plain Anvil, answer is the open_form function described in the docs: Anvil Docs | Navigation
The more complicated answer is to use the Anvil Extras routing module to do your routing. It has a ton of features, including giving each form a distinct URL, so users can bookmark a particular form, automatic caching of forms, near-automatic handling of making sure users are logged in with the correct permissions, etc.
But setting up an app for Anvil Extras routing is more complicated. Hereâs a sample app that uses it: Anvil | Login
If you are a bit security conscious like me, youâll bury part of the redirect in server code.
So, you can add a role column to your Users table, then on your client side something like:
a = anvil.server.call('vet')`
open_form(a)
Then over in your server code:
@anvil.server.callable
def vet():
role = anvil.users.get_user()['role']
if role == "Admin":
a = "AdminForm"
elif role == "User":
a = "UserForm"
else:
a = "NoCredsForm"
return a
At least thatâs what my brain has done, I canât vouch for the underlying sanity.
Unfortunately this wonât offer any additional security. Any determined person can get through this easily. An additional server call will also slow down your app.
As has been discussed many times on the forum, thereâs simply no way for you to prevent access to a particular form. Instead validate all calls to server.
So I used your example to create a boolean in my data table named âAdminâ (true/false).
I used the code below. I placed a checkmark under my user so I should be admin. So, in theory, its suppose to only show the admin page, however, it is showing first the admin page, then the employee page. What am I missing?
user = anvil.users.get_user()
if user['Admin']:
open_form('admin_haga_system')
else:
open_form('employee_haga_system')
Python is fine with if user['Admin']:, you do not need to specifically compare it to True or False. The use in a conditional implies a comparison with True.
Also, be careful when copying code from the forum. Anything not between backticks will have quotes messed up, which is what happened with your syntax error.
You have the code to change the form inside the __init__ method. open_form doesnât work well inside __init__. Try putting the code into a form_show event handler instead.
Edit: or, you can put the code into a startup module, instead of a startup form. That way no form gets initially shown.
At a loss for you, never had the issue and canât see a logical reason why it would happen. Looked at your clone link and can only think you should use a form_show event as @jshaffstall says. Over time Iâve developed an uncouth preference for roles rather than bools for admin, so tend to stick to explicit truths in code - makes it easier to deal with âTruthyâ values but drives other devs to distraction when they see it.
You have the code to change the form inside the __init__ method. open_form doesnât work well inside __init__ . Try putting the code into a form_show event handler instead.
I think this is what you meant right?
If so, I added the code in the function form_show
def form_show(self, **event_args):
user = anvil.users.get_user()
if user['Admin']:
open_form('admin_haga_system')
else:
open_form('employee_haga_system')
At this point though, it shows the correct form page âadmin_haga_systemâ only after it shows âemployee_haga_systemâ . I think it is because the form_show() is in my main form. Do you agree? How do I make it run this argument right after the login and before the main app loads? I think that might be the issue.
Since Anvil added the ability to have startup modules, you could also use one of those instead of having a blank form whose only purpose is to be shown briefly. Either a startup module or blank startup form will have the same effect.
in case it would be 3 roles, using a text type colum instead of a boolean colum in the users table with the âadminâ âofficeâ and âuserâ roles, how should i do it?
I have 3 forms for the 3 type of users, so i made an admin colum and an office colum, should i do something like this?
user = anvil.users.get_user()
if user[âadminâ]:
open_form(âregistro_listocarâ)
else:
if user[âofficeâ]:
open_form(âregistro_listocar_officeâ)
else:
open_form(âregistro_listocar_userâ)