4

I am looking to have all users that sign in have their name converted(Transformed) to uppercase in the DB records or before it hits the DB.

Currently I am using LDAP. So people are free to use sign ins like this:

['PREACTIVE','Preactive','preactive','PREactive','preACTIVE']

Each one of those will get a new Auth_User.id # and a DB correlation of db.auth_membership.id for each variation even though LDAP doesn't care about case. With 100+ users with each users having ~20 entries each for AD groups assignments makes it really messy.

Here is the code that I am currently using:

    ## create all tables needed by auth if not custom tables
    auth.define_tables(username=True)

    auth.settings.create_user_groups=False

    # all we need is login
    auth.settings.actions_disabled=['register','change_password','request_reset_password','retrieve_username','profile']

    # you don't have to remember me
    auth.settings.remember_me_form = True

    # ldap authentication and not save password on web2py
    from gluon.contrib.login_methods.ldap_auth import ldap_auth
    auth.settings.login_methods = [ldap_auth(mode='ad',
       manage_groups= True,
       db = db,
       manage_user = True,
       username_attrib = 'sAMAccountName',
       user_firstname_attrib = 'cn:1',
       user_lastname_attrib = 'sn',
       user_mail_attrib = 'mail',
       group_name_attrib = 'cn',
       group_member_attrib = 'member',
       group_filterstr = 'objectClass=Group',
       server='corp.server.com',
       base_dn='DC=corp,DC=server,DC=com')]

Referenced from: {http://www.web2pyslices.com/slice/show/1468/how-to-set-up-web2py-ldap-with-windows-active-directory , http://www.web2pyslices.com/slice/show/1715/authentication-and-group-control-with-active-directory-ldap , http://www.web2pyslices.com/slice/show/1476/ldap-auth-with-allowed-groups-and-manage-groups}

View from DB(CSV):

auth_user.id,auth_user.first_name,auth_user.last_name,auth_user.email,auth_user.username,auth_user.password,auth_user.registration_key,auth_user.reset_password_key,auth_user.registration_id
1,Pre,Active,p@gmail.com,Preactive,,,,Preactive
2,Pre,Active,p@gmail.com,PREACTIVE,,,,PREACTIVE
3,Pre,Active,p@gmail.com,PREactive,,,,PREactive
4,Pre,Active,p@gmail.com,preACTIVE,,,,preACTIVE

EDIT1: Added this to db.py to try and grab the vars before it hits DB I/O. But it creates two entries in DB. One uppercase with no association with LDAP groups and such. And the Second that is with the what the user actually typed in with the association to LDAP groups.

def login_upper(form):
    form.vars.username = form.vars.username.upper()
    return form

auth.settings.login_onvalidation = login_upper
auth.settings.profile_onvalidation = login_upper

EDIT2:

Edit1 Abandoned.

Modified code in ldap_auth.py(note learned I have to relaunch web2py.exe to see changes made to ..\Web2Py\gluon\contrib\login_methods*)

    if ldap_mode == 'ad':
        # Microsoft Active Directory
        if '@' not in username:
            domain = []
            for x in ldap_basedn.split(','):
                if "DC=" in x.upper():
                    domain.append(x.split('=')[-1])
            username = "%s@%s" % (username, '.'.join(domain))
        username_bare = username.split("@")[0].upper()
        con.set_option(ldap.OPT_PROTOCOL_VERSION, 3)

was: username_bare = username.split("@")[0]

Now: username_bare = username.split("@")[0].upper()

and the when the user logs in the case corrected entry has the AD groups attached but the current user logged in the web2py sessions is the case sensitive entry that got pushed into the DB.

EDIT3:

Looks like I cannot modify after Auth() starts takes control of the data in the username field. So I need to figure out how to modify the login form variables before Auth() is called. So that all subsequent fields will follow the same case and not create additional DB entries....... Plugging away at testing code and Googling.....

Edit4: (FINAL)

FIXED IT!!!!

def user():
    if request.args(0) == 'login' and request.post_vars.username:
        request.post_vars.username = request.vars.username = request.post_vars.username.upper() # changes field to all uppercase
    return dict(form=auth())

Now user can type their name however they like(caSewiSe) and it will catch and transform before hitting Auth and only one entry in db.auth_user.id

Thanks to a 2012 post from Marin Pranjić: https://groups.google.com/forum/#!topic/web2py/Tdse7GDwJ28

RESOLVED!!!!!!

Preactive
  • 310
  • 1
  • 13
  • So you just want `['PREACTIVE','Preactive','preactive','PREactive','preACTIVE']` transformed to : `PREACTIVE PREACTIVE PREACTIVE PREACTIVE PREACTIVE` – ZdaR May 28 '15 at 15:42
  • 1
    Can you not use `username.upper()` before saving? – kylieCatt May 28 '15 at 15:42
  • so when I do `username_attrib.upper() = 'sAMAccountName',` I get error `keyword can't be an expression`. then when I tried `username_attrib = 'sAMAccountName'.upper(),` I get no error but also no modified entry in the DB. So I am at 8th try so `preactiVe` now. just moving one letter at a time to caps to generate a new db entry. – Preactive May 28 '15 at 16:06
  • @anmol_uppal what I am looking for is no matter how they type their username I will only have 1 entry. they log in 5 times `['PREACTIVE','Preactive','preactive','PREactive','preACTIVE']` and `db.auth_user.username` and `db.auth_user.registration_id` only show `['PREACTIVE']` – Preactive May 28 '15 at 16:08
  • CSV of db: auth_user.id,auth_user.first_name,auth_user.last_name,auth_user.email,auth_user.username,auth_user.password,auth_user.registration_key,auth_user.reset_password_key,auth_user.registration_id 1,Pre,Active,p@gmail.com,Preactive,,,,Preactive 2,Pre,Active,p@gmail.com,PREACTIVE,,,,PREACTIVE 3,Pre,Active,p@gmail.com,PREactive,,,,PREactive 4,Pre,Active,p@gmail.com,preACTIVE,,,,preACTIVE – Preactive May 28 '15 at 16:23
  • @IanAuld I updated my post to show output from my database in CSV – Preactive May 28 '15 at 18:22
  • I got the DB to record closer to what I wanted but it still generates records and memberships for the typed in username and not the modified one. See Edit at the bottom of my OP: – Preactive May 29 '15 at 15:21

1 Answers1

2

Edit4: Fixed the issue: The solution

def user():
    if request.args(0) == 'login' and request.post_vars.username:
        request.post_vars.username = request.vars.username = request.post_vars.username.upper() # changes field to all uppercase
    return dict(form=auth())

So it was a part to what IanAuld said but it was WHERE to put it was key.

Preactive
  • 310
  • 1
  • 13