Loading app/main.py +18 −23 Original line number Diff line number Diff line Loading @@ -31,15 +31,8 @@ class MyHandler(BaseHTTPRequestHandler): postvars['displayname'], postvars['email_quota'], postvars['fallback_email'], postvars['nextcloud_quota']) def create_account(self, target_email: str, # was $resultmail password: str, # was $pw password_confirm: str, # was $pw2 displayname: str, # was $name email_quota: str, # was $quota fallback_email: str, # new parameter nextcloud_quota: str # new parameter ): def create_account(self, target_email: str, password: str, password_confirm: str, displayname: str, email_quota: str, fallback_email: str, nextcloud_quota: str): target_username = target_email.split('@')[0] with open('forbidden_usernames') as f: if target_username in f.read().splitlines(): Loading @@ -51,32 +44,34 @@ class MyHandler(BaseHTTPRequestHandler): ssh.set_missing_host_key_policy(paramiko.MissingHostKeyPolicy) ssh.connect(hostname='postfixadmin', username='pfexec', password=os.environ['POSTFIXADMIN_SSH_PASSWORD']) stdin, stdout, stderr = ssh.exec_command(f'/postfixadmin/scripts/postfixadmin-cli mailbox view "{target_email}"') stdin, stdout, stderr = ssh.exec_command( f'/postfixadmin/scripts/postfixadmin-cli mailbox view "{target_email}"') if b"error: the email is not valid!" not in stderr.read().lower(): data = json.dumps({'success': True, 'message': 'username_taken'}) self.respond(403, data) return ssh.exec_command(f'/postfixadmin/scripts/postfixadmin-cli mailbox add "{target_email}" ' + stdin, stdout, stderr = ssh.exec_command( f'/postfixadmin/scripts/postfixadmin-cli mailbox add "{target_email}" ' + f'--password "{password}" --password2 {password_confirm} --name "{displayname}" ' + f'--quota {email_quota} --active 1 --welcome-mail 0') print(stdout.read()) print(stderr.read()) print(f'exit code: {stdout.channel.recv_exit_status()}') # TODO: handle errors # Edit nextcloud account, set quota and email auth = HTTPBasicAuth(os.environ['NEXTCLOUD_ADMIN_USER'], os.environ['NEXTCLOUD_ADMIN_PASSWORD']) url = f'https://drive.{os.environ["DOMAIN"]}/ocs/v1.php/cloud/users/' + target_email headers = {'OCS-APIRequest': 'true'} r = requests.put(url, data={'key': 'email', 'value': fallback_email}, headers=headers, auth=auth) print(r.text) print(f'status code: {r.status_code}') # TODO: check status code r = requests.put(url, data={'key': 'quota', 'value': nextcloud_quota}, headers=headers, auth=auth) print(r.text) print(f'status code: {r.status_code}') # TODO: check status code r1 = requests.put(url, data={'key': 'email', 'value': fallback_email}, headers=headers, auth=auth) r2 = requests.put(url, data={'key': 'quota', 'value': nextcloud_quota}, headers=headers, auth=auth) print(r1.text) print(f'status code: {r1.status_code}') print(r2.text) print(f'status code: {r2.status_code}') if r1.status_code != 200 or r2.status_code != 200: data = json.dumps({'success': False, 'message': 'internal_error'}) self.respond(403, data) return self.respond(200, json.dumps({'success': True})) Loading Loading
app/main.py +18 −23 Original line number Diff line number Diff line Loading @@ -31,15 +31,8 @@ class MyHandler(BaseHTTPRequestHandler): postvars['displayname'], postvars['email_quota'], postvars['fallback_email'], postvars['nextcloud_quota']) def create_account(self, target_email: str, # was $resultmail password: str, # was $pw password_confirm: str, # was $pw2 displayname: str, # was $name email_quota: str, # was $quota fallback_email: str, # new parameter nextcloud_quota: str # new parameter ): def create_account(self, target_email: str, password: str, password_confirm: str, displayname: str, email_quota: str, fallback_email: str, nextcloud_quota: str): target_username = target_email.split('@')[0] with open('forbidden_usernames') as f: if target_username in f.read().splitlines(): Loading @@ -51,32 +44,34 @@ class MyHandler(BaseHTTPRequestHandler): ssh.set_missing_host_key_policy(paramiko.MissingHostKeyPolicy) ssh.connect(hostname='postfixadmin', username='pfexec', password=os.environ['POSTFIXADMIN_SSH_PASSWORD']) stdin, stdout, stderr = ssh.exec_command(f'/postfixadmin/scripts/postfixadmin-cli mailbox view "{target_email}"') stdin, stdout, stderr = ssh.exec_command( f'/postfixadmin/scripts/postfixadmin-cli mailbox view "{target_email}"') if b"error: the email is not valid!" not in stderr.read().lower(): data = json.dumps({'success': True, 'message': 'username_taken'}) self.respond(403, data) return ssh.exec_command(f'/postfixadmin/scripts/postfixadmin-cli mailbox add "{target_email}" ' + stdin, stdout, stderr = ssh.exec_command( f'/postfixadmin/scripts/postfixadmin-cli mailbox add "{target_email}" ' + f'--password "{password}" --password2 {password_confirm} --name "{displayname}" ' + f'--quota {email_quota} --active 1 --welcome-mail 0') print(stdout.read()) print(stderr.read()) print(f'exit code: {stdout.channel.recv_exit_status()}') # TODO: handle errors # Edit nextcloud account, set quota and email auth = HTTPBasicAuth(os.environ['NEXTCLOUD_ADMIN_USER'], os.environ['NEXTCLOUD_ADMIN_PASSWORD']) url = f'https://drive.{os.environ["DOMAIN"]}/ocs/v1.php/cloud/users/' + target_email headers = {'OCS-APIRequest': 'true'} r = requests.put(url, data={'key': 'email', 'value': fallback_email}, headers=headers, auth=auth) print(r.text) print(f'status code: {r.status_code}') # TODO: check status code r = requests.put(url, data={'key': 'quota', 'value': nextcloud_quota}, headers=headers, auth=auth) print(r.text) print(f'status code: {r.status_code}') # TODO: check status code r1 = requests.put(url, data={'key': 'email', 'value': fallback_email}, headers=headers, auth=auth) r2 = requests.put(url, data={'key': 'quota', 'value': nextcloud_quota}, headers=headers, auth=auth) print(r1.text) print(f'status code: {r1.status_code}') print(r2.text) print(f'status code: {r2.status_code}') if r1.status_code != 200 or r2.status_code != 200: data = json.dumps({'success': False, 'message': 'internal_error'}) self.respond(403, data) return self.respond(200, json.dumps({'success': True})) Loading