<s> import re <EOL> from re import match <EOL> from gluon . contrib . login_methods . email_auth import email_auth <EOL> from gluon . tools import * <EOL> auth = Auth ( globals ( ) , db ) <EOL> auth . define_tables ( ) <EOL> auth . settings . create_user_groups = False <EOL> auth . settings . actions_disabled = [ '<STR_LIT>' ] <EOL> auth . settings . registration_requires_verification = False <EOL> auth . settings . registration_requires_approval = False <EOL> auth . settings . register_next = URL ( a = request . application , c = '<STR_LIT:default>' , f = '<STR_LIT:user>' ) <EOL> auth . settings . login_next = URL ( scheme = '<STR_LIT:http>' , a = request . application , c = '<STR_LIT:default>' , f = '<STR_LIT>' ) <EOL> auth . settings . login_methods . append ( <EOL> email_auth ( "<STR_LIT>" , "<STR_LIT>" ) ) <EOL> try : <EOL> admin_role = auth . id_group ( "<STR_LIT>" ) <EOL> dev_role = auth . id_group ( "<STR_LIT>" ) <EOL> auth_role = auth . id_group ( "<STR_LIT>" ) <EOL> writer_role = auth . id_group ( "<STR_LIT>" ) <EOL> editor_role = auth . id_group ( "<STR_LIT>" ) <EOL> except : <EOL> admin_role = None <EOL> dev_role = None <EOL> auth_role = None <EOL> writer_role = None <EOL> editor_role = None <EOL> def require_logged_in ( ) : <EOL> if not auth . user : <EOL> raise HTTP ( <NUM_LIT> , "<STR_LIT>" ) <EOL> return auth . user . id <EOL> def require_user ( owner ) : <EOL> if not auth . user : <EOL> raise HTTP ( <NUM_LIT> , "<STR_LIT>" ) <EOL> if not auth . user . id == owner : <EOL> raise HTTP ( <NUM_LIT> , "<STR_LIT>" ) <EOL> return auth . user . id <EOL> def require_role ( role ) : <EOL> if not check_role ( role ) : <EOL> raise HTTP ( <NUM_LIT> , "<STR_LIT>" ) <EOL> else : <EOL> return auth . user . id <EOL> def check_logged_in ( ) : <EOL> if not auth . user : <EOL> return False <EOL> return auth . user . id <EOL> def check_user ( user_id ) : <EOL> if not auth . user : <EOL> return False <EOL> return auth . user . id == user_id <EOL> def check_role ( role ) : <EOL> if not auth . user : <EOL> return False <EOL> if auth . has_membership ( admin_role , auth . user . id ) : <EOL> return True <EOL> if role == writer_role and auth . has_membership ( editor_role , auth . user . id ) : <EOL> return True <EOL> if not auth . has_membership ( role , auth . user . id ) : <EOL> return False <EOL> return True <EOL> def require_val ( input ) : <EOL> if not input : <EOL> raise HTTP ( <NUM_LIT> , '<STR_LIT>' ) <EOL> return input <EOL> def require_alphanumeric ( input ) : <EOL> if not match ( '<STR_LIT>' , input ) : <EOL> raise HTTP ( <NUM_LIT> , '<STR_LIT>' ) <EOL> return input <EOL> def require_text ( input ) : <EOL> if not match ( '<STR_LIT>' , input ) : <EOL> raise HTTP ( <NUM_LIT> , '<STR_LIT>' ) <EOL> return input <EOL> def require_int ( input ) : <EOL> if not match ( '<STR_LIT>' , input ) : <EOL> raise HTTP ( <NUM_LIT> , '<STR_LIT>' ) <EOL> return int ( input ) <EOL> def require_hex ( input ) : <EOL> if not match ( '<STR_LIT>' , input , re . U ) : <EOL> raise HTTP ( <NUM_LIT> , '<STR_LIT>' % input ) <EOL> return str ( input ) <EOL> def require_decimal ( input ) : <EOL> if not match ( '<STR_LIT>' , input ) : <EOL> raise HTTP ( <NUM_LIT> , '<STR_LIT>' ) <EOL> return int ( input ) <EOL> def require_color ( input ) : <EOL> from savage . graphics . color import hex_to_color <EOL> if not match ( '<STR_LIT>' , input ) : <EOL> raise HTTP ( <NUM_LIT> , '<STR_LIT>' ) <EOL> return hex_to_color ( input ) <EOL> def require_http ( input , params = True ) : <EOL> if not params : <EOL> input = input . split ( '<STR_LIT:?>' ) [ <NUM_LIT:0> ] <EOL> if not match ( '<STR_LIT>' , input ) : <EOL> input = '<STR_LIT>' + input <EOL> if input [ len ( input ) - <NUM_LIT:1> ] == '<STR_LIT:/>' : <EOL> input = input [ <NUM_LIT:0> : len ( input ) - <NUM_LIT:2> ] <EOL> return input <EOL> def require_style_attr ( input ) : <EOL> if not match ( '<STR_LIT>' , input , re . U ) : <EOL> raise HTTP ( <NUM_LIT> , "<STR_LIT>" % input ) <EOL> return input <EOL> def user_name ( id ) : <EOL> result = db ( db [ auth . settings . table_user ] . id == id ) . select ( ) . first ( ) <EOL> return result . first_name + '<STR_LIT:U+0020>' + result . last_name <EOL> def require_mongo ( ) : <EOL> if not mongo : <EOL> session [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> redirect ( URL ( r = request , c = '<STR_LIT:default>' , f = '<STR_LIT>' ) ) </s>
