0

I have a website where a user can have multiple roles (usually a user will have multiple roles). What I am trying to do is simply add and remove user roles.

The html side is pretty simple. I have a select 2 multiselect box that loads all the possible users roles. I select or un-select roles and then submit it.

The form simply has 2 variables; username and role(s)

I have a simply for loop with if statements but I appear to be having 2 problems. 1 the if statements are never 'True' for some reason. The 2nd problem is the for loop appears to be exiting it evaluates 1 variable instead of actually looping through all of them.

    formroleadmin = AdminUserRoles()  # if roles administered
    if formroleadmin.roles.data and formroleadmin.validate(): #if form is submitted
        # ar = all roles er = existing roles ur = updated roles
        allroles = usersRolesNames.query.all() #query all possible roles
        usr = formroleadmin.username.data #submitted user name
        ur = formroleadmin.roles.data #load the submitted roles
        er = usersRoles.query.filter_by(username_fk=usr).all() #get current user roles
        for ar in allroles: #loop through all possible roles
            if ar in ur:  # if the role is a submitted role
                if ar not in er:  # if role does not exist add it #if role is not assigned
                    db.session.add(usersRoles(username_fk=usr, role_fk=ar)) #add user
                    db.session.commit()
            elif ar not in ur:  # remove it if exists #if not a submitted role
                if ar in er: # remove if user is assigned role remove permission
                    db.session.filter_by(username_fk=usr, role_fk=ar).delete

examples of content

allroles = ['Admin', 'Completions', 'Completions RW', 'Operations'
ur = ['Completions', 'Operations']

so basic sequence I am trying to do is iterate through allroles (ar) and test each value against ur. if the role does or does not exist then it would be added, removed, or passed etc.

Jeff Bluemel
  • 476
  • 9
  • 22
  • Can you clarify which statements are never true, and try printing the contents of allroles to make sure it is an iterable with more than one member. – Logan Bertram Mar 30 '18 at 15:21
  • @LoganBertram I added some data samples and hopefully some more clarification. for example, if I use Admin as the first role in ur when I iterate it, it would be if 'Admin' in ['Admin', 'Operations'] (), but that would return false. – Jeff Bluemel Mar 30 '18 at 15:57
  • 1
    Besides the abstract samples, can you load debugging info, logs, or what methods you've used to try and solve the problem? As it is, this looks like it should work, so there is clearly something else going on. Try logging the variables at multiple points and see if anything is happening that you don't expect. – Logan Bertram Mar 30 '18 at 17:07
  • 1
    If you start the flask app using the built-in server, then when an error is thrown, the browser will offer access to the interactive debug console. If you are using some other method, then `print(allroles)`, `print(ur)`, etc. Then you should see the output in stdout or stderr. A bit about the built-in debugger: http://werkzeug.pocoo.org/docs/0.14/debug/ – Logan Bertram Mar 30 '18 at 18:25
  • @LoganBertram alright, your tip on logging helped. I have never really used logging. Honesty the fix seems pretty odd. if ar in ur: changed to if str(ar) in ur: etc. – Jeff Bluemel Mar 30 '18 at 18:51
  • I'm doing this using IIS. I guess I should try using the built-in server. One thing I have been frustrated with is restarting the stupid server EVERY time I make a code change. – Jeff Bluemel Mar 30 '18 at 19:03
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/167893/discussion-between-logan-bertram-and-jeff-b). – Logan Bertram Mar 30 '18 at 19:05

1 Answers1

0

This was resolved by adding str() around the variable that was iterated. @LoganBertram gave me the suggestions to get this error tracked down. Updated code listed below (the logging and troubleshooting was done in iPython).

Logging was added as such

import logging
logging.basicConfig(level=logging.DEBUG)

for u in ar:
    if u in ur:
        logging.debug('found u {} ur {}'.format(u, ur))
    else:
        logging.debug('not found u {} ur {}'.format(u, ur))

DEBUG:root:not found u Admin ur ['Admin', 'Completions']
DEBUG:root:not found u Completions ur ['Admin', 'Completions']

I changed the code to this;

for u in ar:
    if str(u) in ur:
        logging.debug('found u {} ur {}'.format(u, ur))
    else:
        logging.debug('not found u {} ur {}'.format(u, ur))

and got this result

DEBUG:root:found u Admin ur ['Admin', 'Completions']
DEBUG:root:found u Completions ur ['Admin', 'Completions']
Jeff Bluemel
  • 476
  • 9
  • 22