def del_user_attribute(env, username=None, authenticated=1, attribute=None): """Delete one or more Trac user attributes for one or more users.""" columns = [] constraints = [] if username is not None: columns.append('sid') constraints.append(username) if authenticated is not None: columns.append('authenticated') constraints.append(as_int(authenticated, 0, min=0, max=1)) if attribute is not None: columns.append('name') constraints.append(attribute) if len(columns) > 0: where_stmt = ''.join(['WHERE ', '=%s AND '.join(columns), '=%s']) else: where_stmt = '' sql = """ DELETE FROM session_attribute %s """ % where_stmt sql_args = tuple(constraints) with env.db_transaction as db: cursor = db.cursor() cursor.execute(sql, sql_args)
def del_user_attribute(env, username=None, authenticated=1, attribute=None, db=None): """Delete one or more Trac user attributes for one or more users.""" columns = [] constraints = [] if username is not None: columns.append('sid') constraints.append(username) if authenticated is not None: columns.append('authenticated') constraints.append(as_int(authenticated, 0, min=0, max=1)) if attribute is not None: columns.append('name') constraints.append(attribute) if len(columns) > 0: where_stmt = ''.join(['WHERE ', '=%s AND '.join(columns), '=%s']) else: where_stmt = '' sql = """ DELETE FROM session_attribute %s """ % where_stmt sql_args = tuple(constraints) db = _get_db(env, db) cursor = db.cursor() cursor.execute(sql, sql_args) db.commit()
def get_user_attribute(env, username=None, authenticated=1, attribute=None, value=None): """Return user attributes.""" ALL_COLS = ('sid', 'authenticated', 'name', 'value') columns = [] constraints = [] if username is not None: columns.append('sid') constraints.append(username) if authenticated is not None: columns.append('authenticated') constraints.append(as_int(authenticated, 0, min=0, max=1)) if attribute is not None: columns.append('name') constraints.append(attribute) if value is not None: columns.append('value') constraints.append(to_unicode(value)) sel_columns = [col for col in ALL_COLS if col not in columns] if len(sel_columns) == 0: # No variable left, so only COUNTing is as a sensible task here. sel_stmt = 'COUNT(*)' else: if 'sid' not in sel_columns: sel_columns.append('sid') sel_stmt = ','.join(sel_columns) if len(columns) > 0: where_stmt = ''.join(['WHERE ', '=%s AND '.join(columns), '=%s']) else: where_stmt = '' sql = """ SELECT %s FROM session_attribute %s """ % (sel_stmt, where_stmt) sql_args = tuple(constraints) with env.db_query as db: cursor = db.cursor() cursor.execute(sql, sql_args) rows = cursor.fetchall() if rows is None: return {} res = {} for row in rows: if sel_stmt == 'COUNT(*)': return [row[0]] res_row = {} res_row.update(zip(sel_columns, row)) # Merge with constraints, that are constants for this SQL query. res_row.update(zip(columns, constraints)) account = res_row.pop('sid') authenticated = res_row.pop('authenticated') # Create single unique attribute ID. m = md5() m.update(''.join([account, str(authenticated), res_row.get('name')]).encode('utf-8')) row_id = m.hexdigest() if account in res: if authenticated in res[account]: res[account][authenticated].update({ res_row['name']: res_row['value'] }) res[account][authenticated]['id'].update({ res_row['name']: row_id }) else: res[account][authenticated] = { res_row['name']: res_row['value'], 'id': {res_row['name']: row_id} } # Create account ID for additional authentication state. m = md5() m.update(''.join([account, str(authenticated)]).encode('utf-8')) res[account]['id'][authenticated] = m.hexdigest() else: # Create account ID for authentication state. m = md5() m.update(''.join([account, str(authenticated)]).encode('utf-8')) res[account] = {authenticated: {res_row['name']: res_row['value'], 'id': {res_row['name']: row_id}}, 'id': {authenticated: m.hexdigest()}} return res
def get_user_attribute(env, username=None, authenticated=1, attribute=None, value=None, db=None): """Return user attributes.""" ALL_COLS = ('sid', 'authenticated', 'name', 'value') columns = [] constraints = [] if username is not None: columns.append('sid') constraints.append(username) if authenticated is not None: columns.append('authenticated') constraints.append(as_int(authenticated, 0, min=0, max=1)) if attribute is not None: columns.append('name') constraints.append(attribute) if value is not None: columns.append('value') constraints.append(to_unicode(value)) sel_columns = [col for col in ALL_COLS if col not in columns] if len(sel_columns) == 0: # No variable left, so only COUNTing is as a sensible task here. sel_stmt = 'COUNT(*)' else: if 'sid' not in sel_columns: sel_columns.append('sid') sel_stmt = ','.join(sel_columns) if len(columns) > 0: where_stmt = ''.join(['WHERE ', '=%s AND '.join(columns), '=%s']) else: where_stmt = '' sql = """ SELECT %s FROM session_attribute %s """ % (sel_stmt, where_stmt) sql_args = tuple(constraints) db = _get_db(env, db) cursor = db.cursor() cursor.execute(sql, sql_args) rows = cursor.fetchall() if rows is None: return {} res = {} for row in rows: if sel_stmt == 'COUNT(*)': return [row[0]] res_row = {} res_row.update(zip(sel_columns, row)) # Merge with constraints, that are constants for this SQL query. res_row.update(zip(columns, constraints)) account = res_row.pop('sid') authenticated = res_row.pop('authenticated') # Create single unique attribute ID. m = md5() m.update(''.join([account, str(authenticated), res_row.get('name')]).encode('utf-8')) row_id = m.hexdigest() if account in res: if authenticated in res[account]: res[account][authenticated].update({ res_row['name']: res_row['value'] }) res[account][authenticated]['id'].update({ res_row['name']: row_id }) else: res[account][authenticated] = { res_row['name']: res_row['value'], 'id': {res_row['name']: row_id} } # Create account ID for additional authentication state. m = md5() m.update(''.join([account, str(authenticated)]).encode('utf-8')) res[account]['id'][authenticated] = m.hexdigest() else: # Create account ID for authentication state. m = md5() m.update(''.join([account, str(authenticated)]).encode('utf-8')) res[account] = {authenticated: {res_row['name']: res_row['value'], 'id': {res_row['name']: row_id}}, 'id': {authenticated: m.hexdigest()}} return res