Added email notifications
This commit is contained in:
@@ -1,10 +1,11 @@
|
||||
from ..modules import db
|
||||
from ..modules import db, mail
|
||||
from ..tools.encryption import decrypt, encrypt
|
||||
from ..tools.logs import write
|
||||
|
||||
from flask import flash, jsonify, session
|
||||
from flask.helpers import url_for
|
||||
from flask_login import current_user, login_user, logout_user, UserMixin
|
||||
from flask_mail import Message
|
||||
from werkzeug.security import check_password_hash, generate_password_hash
|
||||
|
||||
import secrets
|
||||
@@ -52,15 +53,44 @@ class User(UserMixin, db.Model):
|
||||
|
||||
def get_email(self): return decrypt(self.email)
|
||||
|
||||
def register(self, notify:bool=False):
|
||||
def register(self, notify:bool=False, password:str=None):
|
||||
self.generate_id()
|
||||
users = User.query.all()
|
||||
for user in users:
|
||||
if user.get_username() == self.get_username(): return False, f'Username {self.get_username()} already in use.'
|
||||
if user.get_email() == self.get_email(): return False, f'Email address {self.get_email()} already in use.'
|
||||
self.set_password(password=password)
|
||||
db.session.add(self)
|
||||
db.session.commit()
|
||||
write('users.log', f'User \'{self.get_username()}\' was created with id \'{self.id}\'.')
|
||||
if notify:
|
||||
email = Message(
|
||||
subject='RefTest | Registration Confirmation',
|
||||
recipients=[self.email],
|
||||
body=f"""
|
||||
Hello {self.get_username()},\n\n
|
||||
You have been registered as an administrator on the SKA RefTest App!\n\n
|
||||
You can access your account using the username '{self.get_username()}'\n\n
|
||||
Your password is as follows:\n\n
|
||||
{password}\n\n
|
||||
You can log in to the admin console via the following URL, where you can administer the test or change your password:\n\n
|
||||
{url_for('admin._home', _external=True)}\n\n
|
||||
Have a nice day!\n\n
|
||||
SKA Refereeing
|
||||
""",
|
||||
html=f"""
|
||||
<p>Hello {self.get_username()},</p>
|
||||
<p>You have been registered as an administrator on the SKA RefTest App!</p>
|
||||
<p>You can access your account using the username '{self.get_username()}'</p>
|
||||
<p>Your password is as follows:</p>
|
||||
<strong>{password}</strong>
|
||||
<p>You can log in to the admin console via the following URL, where you can administer the test or change your password:</p>
|
||||
<p><a href='{url_for('admin._home', _external=True)}'>{url_for('admin._home', _external=True)}</a></p>
|
||||
<p>Have a nice day!</p>
|
||||
<p>SKA Refereeing</p>
|
||||
"""
|
||||
)
|
||||
mail.send(email)
|
||||
return True, f'User {self.get_username()} was created successfully.'
|
||||
|
||||
def login(self, remember:bool=False):
|
||||
@@ -80,6 +110,39 @@ class User(UserMixin, db.Model):
|
||||
self.reset_token = secrets.token_urlsafe(16)
|
||||
self.verification_token = secrets.token_urlsafe(16)
|
||||
db.session.commit()
|
||||
email = Message(
|
||||
subject='RefTest | Password Reset',
|
||||
recipients=[self.get_email()],
|
||||
body=f"""
|
||||
Hello {self.get_username()},\n\n
|
||||
This email was generated because we received a request to reset the password for your administrator account for the SKA RefTest app.\n\n
|
||||
If you did not make this request, please ignore this email.\n\n
|
||||
If you did make this request, then you have two options to recover your account.\n\n
|
||||
Your password has been reset to the following:\n\n
|
||||
{new_password}\n\n
|
||||
You may use this to log back in to your account, and subsequently change your password to something more suitable.\n\n
|
||||
Alternatively, you may visit the following private link using your unique token to override your password. Copy and paste the following link in a web browser. Please note that this token is only valid once:\n\n
|
||||
{url_for('admin_auth.reset_gateway', token = self.reset_token, verification = self.verification_token, _external = True)}\n\n
|
||||
Hopefully, this should enable access to your account once again.\n\n
|
||||
Have a nice day!\n\n
|
||||
SKA Refereeing
|
||||
""",
|
||||
html=f"""
|
||||
<p>Hello {self.get_username()},</p>
|
||||
<p>This email was generated because we received a request to reset the password for your administrator account for the SKA RefTest app.</p>
|
||||
<p>If you did not make this request, please ignore this email.</p>
|
||||
<p>If you did make this request, then you have two options to recover your account.</p>
|
||||
<p>Your password has been reset to the following:</p>
|
||||
<strong>{new_password}</strong>
|
||||
<p>You may use this to log back in to your account, and subsequently change your password to something more suitable.</p>
|
||||
<p>Alternatively, you may visit the following private link using your unique token to override your password. Copy and paste the following link in a web browser. Please note that this token is only valid once:</p>
|
||||
<p><a href='{url_for('admin_auth.reset_gateway', token = self.reset_token, verification = self.verification_token, _external = True)}'>{url_for('admin_auth.reset_gateway', token = self.reset_token, verification = self.verification_token, _external = True)}</a></p>
|
||||
<p>Hopefully, this should enable access to your account once again.</p>
|
||||
<p>Have a nice day!</p>
|
||||
<p>SKA Refereeing</p>
|
||||
"""
|
||||
)
|
||||
mail.send(email)
|
||||
print('Password', new_password)
|
||||
print('Reset Token', self.reset_token)
|
||||
print('Verification Token', self.verification_token)
|
||||
@@ -92,16 +155,69 @@ class User(UserMixin, db.Model):
|
||||
|
||||
def delete(self, notify:bool=False):
|
||||
username = self.get_username()
|
||||
email_address = self.get_email()
|
||||
db.session.delete(self)
|
||||
db.session.commit()
|
||||
message = f'User \'{username}\' was deleted by \'{current_user.get_username()}\'.'
|
||||
write('users.log', message)
|
||||
if notify:
|
||||
email = Message(
|
||||
subject='RefTest | Account Deletion',
|
||||
recipients=[email_address],
|
||||
bcc=[current_user.get_email()],
|
||||
body=f"""
|
||||
Hello {username},\n\n
|
||||
Your administrator account for the SKA RefTest App, as well as all data associated with the account, have been deleted by {current_user.get_username()}.\n\n
|
||||
If you believe this was done in error, please contact them immediately.\n\n
|
||||
If you would like to register to administer the app, please ask an existing administrator to create a new account.\n\n
|
||||
Have a nice day!\n\n
|
||||
SKA Refereeing
|
||||
""",
|
||||
html=f"""
|
||||
<p>Hello {username},</p>
|
||||
<p>Your administrator account for the SKA RefTest App, as well as all data associated with the account, have been deleted by {current_user.get_username()}.</p>
|
||||
<p>If you believe this was done in error, please contact them immediately.</p>
|
||||
<p>If you would like to register to administer the app, please ask an existing administrator to create a new account.</p>
|
||||
<p>Have a nice day!</p>
|
||||
<p>SKA Refereeing</p>
|
||||
"""
|
||||
)
|
||||
mail.send(email)
|
||||
return True, message
|
||||
|
||||
def update(self, password:str=None, email:str=None, notify:bool=False):
|
||||
if not password and not email: return False, 'There were no changes requested.'
|
||||
if password: self.set_password(password)
|
||||
old_email = self.get_email()
|
||||
if email: self.set_email(email)
|
||||
db.session.commit()
|
||||
write('system.log', f'Information for user {self.get_username()} has been updated by {current_user.get_username()}.')
|
||||
if notify:
|
||||
message = Message(
|
||||
subject='RefTest | Account Update',
|
||||
recipients=[email],
|
||||
bcc=[old_email,current_user.get_email()],
|
||||
body=f"""
|
||||
Hello {self.get_username()},\n\n
|
||||
Your administrator account for the SKA RefTest App has been updated by {current_user.get_username()}.\n\n
|
||||
Your new account details are as follows:\n\n
|
||||
Email: {email}\n
|
||||
Password: {password if password else '<same as old>'}\n\n
|
||||
You can update your email address and password by logging in to the admin console using the following URL:\n\n
|
||||
{url_for('admin._home', _external=True)}\n\n
|
||||
Have a nice day!\n\n
|
||||
SKA Refereeing
|
||||
""",
|
||||
html=f"""
|
||||
<p>Hello {self.get_username()},</p>
|
||||
<p>Your administrator account for the SKA RefTest App has been updated by {current_user.get_username()}.</p>
|
||||
<p>Your new account details are as follows:</p>
|
||||
<p>Email: {email} <br/> Password: <strong>{password if password else '<same as old>'}</strong></p>
|
||||
<p>You can update your email address and password by logging in to the admin console using the following URL:</p>
|
||||
<p><a href='{url_for('admin._home', _external=True)}'>{url_for('admin._home', _external=True)}</a></p>
|
||||
<p>Have a nice day!</p>
|
||||
<p>SKA Refereeing</p>
|
||||
"""
|
||||
)
|
||||
mail.send(message)
|
||||
return True, f'Account {self.get_username()} has been updated.'
|
||||
|
Reference in New Issue
Block a user