updated comments

This commit is contained in:
2021-08-11 17:44:19 +10:00
parent 5258a7c915
commit b21d4f78e5

38
main.py
View File

@@ -26,12 +26,11 @@ hostname = socket.gethostname()
print( "Running on: {}".format( hostname) ) print( "Running on: {}".format( hostname) )
app = Flask(__name__) app = Flask(__name__)
### what is this value? I gather I should change it?
app.config['SQLALCHEMY_DATABASE_URI'] = DB_URL app.config['SQLALCHEMY_DATABASE_URI'] = DB_URL
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['ENV'] = os.environ['FLASK_ENV'] app.config['ENV'] = os.environ['FLASK_ENV']
app.config.from_mapping( SECRET_KEY=b'\xd6\x04\xbdj\xfe\xed$c\x1e@\xad\x0f\x13,@G') app.config.from_mapping( SECRET_KEY=b'\xe9\xaf\xe1b\xe9mg\xbe\\\x90\x1f(\xf8Kp\xce91\x17\x14lJ\x9e0')
# ldap config vars: (the last one is required, or python ldap freaks out) # ldap config vars: (the last one is required, or python ldap freaks out)
app.config['LDAP_HOST'] = 'mara.ddp.net' app.config['LDAP_HOST'] = 'mara.ddp.net'
@@ -44,14 +43,14 @@ app.config['LDAP_BIND_USER_DN'] = None
app.config['LDAP_BIND_USER_PASSWORD'] = None app.config['LDAP_BIND_USER_PASSWORD'] = None
app.config['LDAP_GROUP_OBJECT_FILTER'] = '(objectclass=posixGroup)' app.config['LDAP_GROUP_OBJECT_FILTER'] = '(objectclass=posixGroup)'
db = SQLAlchemy(app) db = SQLAlchemy(app) # create the (flask) sqlalchemy connection
ma = Marshmallow(app) ma = Marshmallow(app) # set up Marshmallow - data marshalling / serialising
Bootstrap(app) Bootstrap(app) # set up Bootstrap - used in flask-forms (TODO: CONFIRM THIS IS NEEDED - sometimes I do boostrap by hand anyway)
login_manager = LoginManager(app) # Setup a Flask-Login Manager login_manager = LoginManager(app) # Setup a Flask-Login Manager
ldap_manager = LDAP3LoginManager(app) # Setup a LDAP3 Login Manager. ldap_manager = LDAP3LoginManager(app) # Setup a LDAP3 Login Manager.
login_manager.login_view = "login" # default login route, failed with url_for, so hard-coded login_manager.login_view = "login" # default login route, failed with url_for, so hard-coded
################################# Now, import non-book classes ################################### ################################# Now, import separated class files ###################################
from ai import aistats from ai import aistats
from settings import Settings from settings import Settings
from files import Entry, GetJM_Message, ClearJM_Message from files import Entry, GetJM_Message, ClearJM_Message
@@ -75,23 +74,8 @@ app.jinja_env.globals['LocationIcon'] = LocationIcon
app.jinja_env.globals['StoragePathNames'] = StoragePathNames app.jinja_env.globals['StoragePathNames'] = StoragePathNames
# Declare an Object Model for the user, and make it comply with the
# flask-login UserMixin mixin.
class User(UserMixin):
def __init__(self, dn, username, data):
self.dn = dn
self.username = username
self.data = data
def __repr__(self):
return self.dn
def get_id(self):
return self.dn
# Declare a User Loader for Flask-Login. # Declare a User Loader for Flask-Login.
# Simply returns the User if it exists in our 'database', otherwise # Returns the User if it exists in our 'database', otherwise returns None.
# returns None.
@login_manager.user_loader @login_manager.user_loader
def load_user(id): def load_user(id):
pau=PAUser.query.filter(PAUser.dn==id).first() pau=PAUser.query.filter(PAUser.dn==id).first()
@@ -99,8 +83,7 @@ def load_user(id):
# Declare The User Saver for Flask-Ldap3-Login # Declare The User Saver for Flask-Ldap3-Login
# This method is called whenever a LDAPLoginForm() successfully validates. # This method is called whenever a LDAPLoginForm() successfully validates.
# Here you have to save the user, and return it so it can be used in the # store the user details / session in the DB if it is not in there already
# login controller.
@ldap_manager.save_user @ldap_manager.save_user
def save_user(dn, username, data, memberships): def save_user(dn, username, data, memberships):
pau=PAUser.query.filter(PAUser.dn==dn).first() pau=PAUser.query.filter(PAUser.dn==dn).first()
@@ -122,6 +105,9 @@ def main_page():
return render_template("base.html") return render_template("base.html")
# route for the login page/box
# POST is when user submits pwd & uses flask-login to hit ldap, validate pwd
# if valid, then we save user/session into the DB via login_user() -> calls save_user()
@app.route('/login', methods=['GET', 'POST']) @app.route('/login', methods=['GET', 'POST'])
def login(): def login():
# Instantiate a LDAPLoginForm which has a validator to check if the user # Instantiate a LDAPLoginForm which has a validator to check if the user
@@ -135,9 +121,7 @@ def login():
print( f"WARNING: Detected special LDAP chars in username: {request.form['username']}") print( f"WARNING: Detected special LDAP chars in username: {request.form['username']}")
return redirect('/login') return redirect('/login')
if form.validate_on_submit(): if form.validate_on_submit():
# Successfully logged in, We can now access the saved user object # Successfully logged in, We can now access the saved user object via form.user.
# via form.user.
print( f"form user = {form.user}" )
login_user(form.user, remember=True) # Tell flask-login to log them in. login_user(form.user, remember=True) # Tell flask-login to log them in.
next = request.args.get("next") next = request.args.get("next")
if next: if next: