<s> import re <EOL> import base64 <EOL> import dbutils <EOL> import gitutils <EOL> import auth <EOL> import mailutils <EOL> import textutils <EOL> import configuration <EOL> from operation import ( Operation , OperationResult , OperationError , <EOL> OperationFailure , Optional , User ) <EOL> class SetFullname ( Operation ) : <EOL> def __init__ ( self ) : <EOL> Operation . __init__ ( self , { "<STR_LIT>" : int , <EOL> "<STR_LIT:value>" : str } ) <EOL> def process ( self , db , user , user_id , value ) : <EOL> if user . id != user_id : <EOL> Operation . requireRole ( db , "<STR_LIT>" , user ) <EOL> if not value . strip ( ) : <EOL> raise OperationError ( "<STR_LIT>" ) <EOL> db . cursor ( ) . execute ( "<STR_LIT>" , ( value . strip ( ) , user_id ) ) <EOL> db . commit ( ) <EOL> return OperationResult ( ) <EOL> class SetGitEmails ( Operation ) : <EOL> def __init__ ( self ) : <EOL> Operation . __init__ ( self , { "<STR_LIT>" : User , <EOL> "<STR_LIT:value>" : [ str ] } ) <EOL> def process ( self , db , user , subject , value ) : <EOL> if user != subject : <EOL> Operation . requireRole ( db , "<STR_LIT>" , user ) <EOL> for address in value : <EOL> if not address . strip ( ) : <EOL> raise OperationError ( "<STR_LIT>" ) <EOL> if address . count ( "<STR_LIT:@>" ) != <NUM_LIT:1> : <EOL> raise OperationError ( "<STR_LIT>" ) <EOL> cursor = db . cursor ( ) <EOL> cursor . execute ( "<STR_LIT>" , ( subject . id , ) ) <EOL> current_addresses = set ( address for ( address , ) in cursor ) <EOL> new_addresses = set ( address . strip ( ) for address in value ) <EOL> for address in ( current_addresses - new_addresses ) : <EOL> cursor . execute ( "<STR_LIT>" , <EOL> ( subject . id , address ) ) <EOL> for address in ( new_addresses - current_addresses ) : <EOL> cursor . execute ( "<STR_LIT>" , <EOL> ( subject . id , address ) ) <EOL> db . commit ( ) <EOL> return OperationResult ( ) <EOL> class ChangePassword ( Operation ) : <EOL> def __init__ ( self ) : <EOL> Operation . __init__ ( self , { "<STR_LIT>" : Optional ( User ) , <EOL> "<STR_LIT>" : Optional ( str ) , <EOL> "<STR_LIT>" : str } ) <EOL> def process ( self , db , user , new_pw , subject = None , current_pw = None ) : <EOL> if subject is None : <EOL> subject = user <EOL> cursor = db . cursor ( ) <EOL> if user != subject : <EOL> Operation . requireRole ( db , "<STR_LIT>" , user ) <EOL> elif current_pw is None : <EOL> cursor . execute ( "<STR_LIT>" , ( subject . id , ) ) <EOL> if cursor . fetchone ( ) [ <NUM_LIT:0> ] is not None : <EOL> raise OperationFailure ( code = "<STR_LIT>" , <EOL> title = "<STR_LIT>" , <EOL> message = "<STR_LIT>" ) <EOL> if current_pw is not None : <EOL> try : auth . checkPassword ( db , subject . name , current_pw ) <EOL> except auth . WrongPassword : <EOL> raise OperationFailure ( code = "<STR_LIT>" , <EOL> title = "<STR_LIT>" , <EOL> message = "<STR_LIT>" ) <EOL> if not new_pw : <EOL> raise OperationFailure ( code = "<STR_LIT>" , <EOL> title = "<STR_LIT>" , <EOL> message = "<STR_LIT>" ) <EOL> cursor . execute ( "<STR_LIT>" , <EOL> ( auth . hashPassword ( new_pw ) , subject . id ) ) <EOL> db . commit ( ) <EOL> return OperationResult ( ) <EOL> def sanitize ( self , value ) : <EOL> sanitized = value . copy ( ) <EOL> if "<STR_LIT>" in value : <EOL> sanitized [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> sanitized [ "<STR_LIT>" ] = "<STR_LIT>" <EOL> return sanitized <EOL> def checkEmailAddressSyntax ( address ) : <EOL> return bool ( re . match ( r"<STR_LIT>" , address ) ) <EOL> def sendVerificationMail ( db , user , email_id = None ) : <EOL> cursor = db . cursor ( ) <EOL> if email_id is None : <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( user . id , ) ) <EOL> email_id , = cursor . fetchone ( ) <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( email_id , ) ) <EOL> email , verification_token = cursor . fetchone ( ) <EOL> if verification_token is None : <EOL> verification_token = auth . getToken ( encode = base64 . b16encode ) <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( verification_token , email_id ) ) <EOL> if configuration . base . ACCESS_SCHEME == "<STR_LIT:http>" : <EOL> protocol = "<STR_LIT:http>" <EOL> else : <EOL> protocol = "<STR_LIT>" <EOL> administrators = dbutils . getAdministratorContacts ( db , indent = <NUM_LIT:2> ) <EOL> if administrators : <EOL> administrators = "<STR_LIT>" % administrators <EOL> else : <EOL> administrators = "<STR_LIT:.>" <EOL> recipients = [ mailutils . User ( user . name , email , user . fullname ) ] <EOL> subject = "<STR_LIT>" % email <EOL> body = textutils . reflow ( """<STR_LIT>""" % { "<STR_LIT>" : configuration . base . HOSTNAME , <EOL> "<STR_LIT:username>" : user . name , <EOL> "<STR_LIT:email>" : email , <EOL> "<STR_LIT>" : "<STR_LIT>" % ( protocol , configuration . base . HOSTNAME ) , <EOL> "<STR_LIT>" : verification_token , <EOL> "<STR_LIT>" : administrators } ) <EOL> mailutils . sendMessage ( recipients , subject , body ) <EOL> class RequestVerificationEmail ( Operation ) : <EOL> def __init__ ( self ) : <EOL> Operation . __init__ ( self , { "<STR_LIT>" : int } ) <EOL> def process ( self , db , user , email_id ) : <EOL> cursor = db . cursor ( ) <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( email_id , ) ) <EOL> row = cursor . fetchone ( ) <EOL> if not row : <EOL> raise OperationFailure ( <EOL> code = "<STR_LIT>" , <EOL> title = "<STR_LIT>" , <EOL> message = "<STR_LIT>" ) <EOL> user_id , email , verified = row <EOL> if verified is True : <EOL> raise OperationFailure ( <EOL> code = "<STR_LIT>" , <EOL> title = "<STR_LIT>" , <EOL> message = "<STR_LIT>" ) <EOL> if user != user_id : <EOL> Operation . requireRole ( db , "<STR_LIT>" , user ) <EOL> user = dbutils . User . fromId ( db , user_id ) <EOL> sendVerificationMail ( db , user , email_id ) <EOL> db . commit ( ) <EOL> return OperationResult ( ) <EOL> class DeleteEmailAddress ( Operation ) : <EOL> def __init__ ( self ) : <EOL> Operation . __init__ ( self , { "<STR_LIT>" : int } ) <EOL> def process ( self , db , user , email_id ) : <EOL> cursor = db . cursor ( ) <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( email_id , ) ) <EOL> row = cursor . fetchone ( ) <EOL> if not row : <EOL> raise OperationFailure ( <EOL> code = "<STR_LIT>" , <EOL> title = "<STR_LIT>" , <EOL> message = "<STR_LIT>" ) <EOL> subject_id , = row <EOL> if user != subject_id : <EOL> Operation . requireRole ( db , "<STR_LIT>" , user ) <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( subject_id , ) ) <EOL> emails = dict ( cursor ) <EOL> if len ( emails ) > <NUM_LIT:1> and emails [ email_id ] : <EOL> raise OperationFailure ( <EOL> code = "<STR_LIT>" , <EOL> title = "<STR_LIT>" , <EOL> message = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( subject_id , email_id ) ) <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( email_id , ) ) <EOL> db . commit ( ) <EOL> return OperationResult ( ) <EOL> class SelectEmailAddress ( Operation ) : <EOL> def __init__ ( self ) : <EOL> Operation . __init__ ( self , { "<STR_LIT>" : int } ) <EOL> def process ( self , db , user , email_id ) : <EOL> cursor = db . cursor ( ) <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( email_id , ) ) <EOL> row = cursor . fetchone ( ) <EOL> if not row : <EOL> raise OperationFailure ( <EOL> code = "<STR_LIT>" , <EOL> title = "<STR_LIT>" , <EOL> message = "<STR_LIT>" ) <EOL> user_id , = row <EOL> if user != user_id : <EOL> Operation . requireRole ( db , "<STR_LIT>" , user ) <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( email_id , user_id ) ) <EOL> db . commit ( ) <EOL> return OperationResult ( ) <EOL> class AddEmailAddress ( Operation ) : <EOL> def __init__ ( self ) : <EOL> Operation . __init__ ( self , { "<STR_LIT>" : User , <EOL> "<STR_LIT:email>" : str } ) <EOL> def process ( self , db , user , subject , email ) : <EOL> if not checkEmailAddressSyntax ( email ) : <EOL> raise OperationFailure ( <EOL> code = "<STR_LIT>" , <EOL> title = "<STR_LIT>" , <EOL> message = "<STR_LIT>" ) <EOL> if user != subject : <EOL> Operation . requireRole ( db , "<STR_LIT>" , user ) <EOL> cursor = db . cursor ( ) <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( subject . id , email ) ) <EOL> if cursor . fetchone ( ) : <EOL> raise OperationFailure ( <EOL> code = "<STR_LIT>" , <EOL> title = "<STR_LIT>" , <EOL> message = "<STR_LIT>" ) <EOL> if user . hasRole ( db , "<STR_LIT>" ) : <EOL> verified = None <EOL> elif configuration . base . VERIFY_EMAIL_ADDRESSES : <EOL> verified = False <EOL> else : <EOL> verified = None <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( subject . id , email , verified ) ) <EOL> email_id , = cursor . fetchone ( ) <EOL> if verified is False : <EOL> sendVerificationMail ( db , subject , email_id ) <EOL> if subject . email is None : <EOL> cursor . execute ( """<STR_LIT>""" , <EOL> ( email_id , subject . id ) ) <EOL> db . commit ( ) <EOL> return OperationResult ( ) </s>
