diff --git a/app.py b/app.py index 66c8044..521b560 100644 --- a/app.py +++ b/app.py @@ -24,6 +24,7 @@ config = { "measurementId": os.environ.get('measurementId'), } firebase = pyrebase.initialize_app(config) +db = firebase.database() auth = firebase.auth() tz = pytz.timezone('Asia/Taipei') @@ -52,22 +53,38 @@ def index(): if request.method == 'GET': if check_login_status(): return render_template('login.html') - return redirect('/manage') + return redirect('/select') elif request.method == 'POST': - email = request.form['username'] + "@group-attendance.fhjh.tp.edu.tw" + email = request.form['username'] if check_login_status(): try: if (verify_recaptcha("")): user = auth.sign_in_with_email_and_password( email, request.form['password']) - print("Login SUCC:", email, flush=True) - session['is_logged_in'] = True - session['email'] = user['email'] - session['uid'] = user['localId'] - session['token'] = user['idToken'] - session['refreshToken'] = user['refreshToken'] - session['loginTime'] = datetime.now(tz) - return redirect('/manage') + usrData = db.child("Users").child(user['localId']).child("permission").get( + user['idToken']).val() + if (usrData == 'realPerson'): + print("RealPerson Login SUCC:", email, flush=True) + session['is_logged_in'] = True + session['email'] = user['email'] + session['uid'] = user['localId'] + session['token'] = user['idToken'] + session['refreshToken'] = user['refreshToken'] + session['loginTime'] = datetime.now(tz) + return redirect('/select') + if (usrData == 'admin'): + print("Admin Login SUCC:", email, flush=True) + session['subuser_type'] = 'admin' + session['is_logged_in'] = True + session['email'] = user['email'] + session['uid'] = user['localId'] + session['token'] = user['idToken'] + session['refreshToken'] = user['refreshToken'] + session['loginTime'] = datetime.now(tz) + session['showUpload'] = db.child("Users").child( + session['uid']).child("showUpload").get(session['token']).val() + return redirect('/manage') + raise Exception("not real person or admin") else: print("ReC Error:", email, flush=True) flash( @@ -79,7 +96,147 @@ def index(): '帳號或密碼錯誤,請重新輸入
Incorrect username or password') return redirect('/') else: - return redirect('/manage') + return redirect('/select') + + +@app.route('/select', methods=['GET', 'POST']) +def selSubUser(): + if check_login_status(): + print(session) + session.clear() + flash("Timeout. 遇時,請重新登入") + return redirect('/') + if 'subuser_type' in session and session['subuser_type'] == 'admin': + return redirect('/manage') + if request.method == 'GET': + usrData = db.child("Users").child(session['uid']).get( + session['token']).val() + session['subuser_type'] = '' + return render_template('selSubUser.html', data=usrData['accounts'], name=usrData['name']) + else: + data = request.form['subuser_sel'].split('^') + try: + if (verify_recaptcha("")): + if (data[0] == 'homeroom'): + session['homeroom'] = data[1] + '^' + data[2] + session['subuser_type'] = 'homeroom' + elif (data[0] == 'group'): + session['category'] = data[1] + session['class'] = data[2] + session['subuser_type'] = 'group' + return redirect('/manage') + else: + print("ReC Error:", data, flush=True) + flash( + 'reCAPTCHA 錯誤,請稍後再試一次
reCAPTCHA Failed. Please try again later.') + return redirect('/select') + except Exception as e: + print("Error:", data, str(e), flush=True) + flash(str(e)) + return redirect('/select') + + +@app.route('/chgPassword', methods=['POST', 'GET']) +def chgPassword(): + data = {} + if request.method == 'GET': + if not check_login_status(): + return render_template('chgPassword.html') + else: + return abort(404) + elif request.method == 'POST': + oldEmail = session['email'] + delUser = False + if not check_login_status(): + try: + if (verify_recaptcha("")): + oldUsr = auth.sign_in_with_email_and_password( + oldEmail, request.form['password']) + print("chgPwd oldUser:", oldEmail, flush=True) + old = {} + old['uid'] = oldUsr['localId'] + old['token'] = oldUsr['idToken'] + data = db.child("Users").child( + oldUsr['localId']).get(oldUsr['idToken']).val() + print("data:", data, flush=True) + + auth.delete_user_account(oldUsr['idToken']) + delUser = True + + newUsr = auth.create_user_with_email_and_password( + request.form['new_username'], request.form['new_password']) + db.child("Users").child(newUsr['localId']).set( + data, newUsr['idToken']) + session.clear() + flash( + '修改密碼成功,請重新登入
Password changed successfully. Please login again.') + return redirect('/') + else: + print("ReC Error:", oldEmail, flush=True) + flash( + 'reCAPTCHA 錯誤,請稍後再試一次
reCAPTCHA Failed. Please try again later.') + return redirect('/chgPassword') + except Exception as e: + if delUser: + try: + usr = auth.create_user_with_email_and_password( + oldEmail, request.form['password']) + db.child("Users").child(usr['localId']).set( + data, usr['idToken']) + except: + pass + print("Error:", oldEmail, str(e), flush=True) + flash(str(e)) + return redirect('/chgPassword') + + +@app.route('/iforgot', methods=['GET', 'POST']) +def iforgot(): + if request.method == 'GET': + return render_template('iforgot.html') + elif request.method == 'POST': + email = request.form['username'] + try: + if (verify_recaptcha("")): + auth.send_password_reset_email(email) + print("iforgot email sent:", email, flush=True) + flash( + '重置密碼信件已寄出,請至信箱收取
Password reset email has been sent to your email. Please check your email.') + return redirect('/') + else: + print("ReC Error:", email, flush=True) + flash( + 'reCAPTCHA 錯誤,請稍後再試一次
reCAPTCHA Failed. Please try again later.') + return redirect('/iforgot') + except Exception as e: + print("Error:", email, str(e), flush=True) + flash(str(e)) + return redirect('/iforgot') + + +@app.route('/resetPassword', methods=['GET', 'POST']) +def resetPassword(): + if request.method == 'GET': + session['oobCode'] = request.args.get('oobCode') + return render_template('verifiedChgPassword.html') + else: + try: + if (verify_recaptcha("")): + auth.verify_password_reset_code( + session['oobCode'], request.form['password']) + print("resetPassword success:", flush=True) + session.clear() + flash('重置密碼成功,請重新登入
Password reset success. Please login again.') + return redirect('/') + else: + print("ReC Error:", flush=True) + flash( + 'reCAPTCHA 錯誤,請稍後再試一次
reCAPTCHA Failed. Please try again later.') + return redirect('/resetPassword') + except Exception as e: + print("Error:", str(e), flush=True) + flash(str(e)) + return redirect('/resetPassword') @ app.route('/logout', methods=['GET']) diff --git a/manage.py b/manage.py index e567c5c..cef07b3 100644 --- a/manage.py +++ b/manage.py @@ -51,8 +51,7 @@ def manageProcess(fCommand, fData): db.child("Users").child( session['uid']).child("permission").get(session['token']).val() # end bug fix - pl = db.child("Users").child( - session['uid']).child("permission").get(session['token']).val() + pl = session['subuser_type'] if pl == 'admin': homerooms = db.child("Homerooms").get(session['token']).val() currRoom = [] @@ -80,21 +79,15 @@ def manageProcess(fCommand, fData): break return render_template('admin.html', homerooms=homerooms, absData=absData, homeroomCode=currRoom, homeroomData=homeroomData, currDate=currDate, periods=['m', '1', '2', '3', '4', - 'n', '5', '6', '7', '8', '9'], showUpload=db.child("Users").child( - session['uid']).child("showUpload").get(session['token']).val()) + 'n', '5', '6', '7', '8', '9'], showUpload=session['showUpload']) elif pl == 'group': - classes = db.child("Users").child( - session['uid']).child("class").get(session['token']).val() - cclass = {} - cateData = {} - for i in classes: - cateData = db.child("Classes").child( - "GP_Class").child(i).get(session['token']).val() - cclass = { - "name": cateData['Class'][classes[i]]['name'], - "category": i, - "class_id": classes[i] - } + cateData = db.child("Classes").child( + "GP_Class").child(session['category']).get(session['token']).val() + cclass = { + "name": cateData['Class'][session['class']]['name'], + "category": session['category'], + "class_id": session['class'] + } homerooms = cateData['Homerooms'] currDate = "" confirmed = [] @@ -175,8 +168,7 @@ def manageProcess(fCommand, fData): } return render_template('group_teach.html', cclass=cclass, absData=absData, dow=dow, currDate=currDate, tmpAbsData=tmpAbsData, confirmed=confirmed) elif pl == 'homeroom': - homeroom = db.child("Users").child( - session['uid']).child("homeroom").get(session['token']).val().split('^') + homeroom = session['homeroom'].split('^') homeroomData = db.child("Homerooms").child(homeroom[0]).child( homeroom[1]).get(session['token']).val() times = OrderedDict({ @@ -241,18 +233,14 @@ def manage_admin(): def group_teach_publish(): if (check_login_status()): return redirect('/logout') - classes = db.child("Users").child( - session['uid']).child("class").get(session['token']).val() - cclass = {} - for i in classes: - cclass = { - "name": db.child("Classes").child("GP_Class").child(i).child( - "Class").child(classes[i]).child("name").get(session['token']).val(), - "category": i, - "class_id": classes[i], - "homerooms": db.child("Classes").child( - "GP_Class").child(i).child("Homerooms").get(session['token']).val() - } + cclass = { + "name": db.child("Classes").child("GP_Class").child(session['category']).child( + "Class").child(session['class']).child("name").get(session['token']).val(), + "category": session['category'], + "class_id": session['class'], + "homerooms": db.child("Classes").child( + "GP_Class").child(session['category']).child("Homerooms").get(session['token']).val() + } date = request.form['date'] period = request.form['period'] signature = request.form['signatureData'] diff --git a/templates/chgPassword.html b/templates/chgPassword.html new file mode 100644 index 0000000..220349f --- /dev/null +++ b/templates/chgPassword.html @@ -0,0 +1,104 @@ + + + + + + + Attendance 點名系統 (β) + + + + + + + + + + + + +
+
+

Attendance 點名系統 (β) | Change Password 更改密碼

+
+
+
+
+
+
+
+
+
+ +
+
+
+
+
+
+
+
+
+
+ + +
+ + {% with messages = get_flashed_messages() %} + {% if messages %} + + {% endif %} + {% endwith %} +
+
+
+
+ + {% include 'footer.html' %} + + + + + + \ No newline at end of file diff --git a/templates/group_teach.html b/templates/group_teach.html index 48d3d85..1d5ba42 100644 --- a/templates/group_teach.html +++ b/templates/group_teach.html @@ -29,6 +29,7 @@

{{cclass['category']}}: {{cclass['class_id']}}: {{cclass['name']}}

[{{currDate}}]

+
{% for date in absData %} diff --git a/templates/iforgot.html b/templates/iforgot.html new file mode 100644 index 0000000..7a667fe --- /dev/null +++ b/templates/iforgot.html @@ -0,0 +1,73 @@ + + + + + + + Attendance 點名系統 (β) + + + + + + + + + + + +
+
+

Attendance 點名系統 (β) | Reset Password 忘記密碼

+
+
+
+ +
+
+
+
+
+ + +

This will send an email to the email address to verify your identity.
+ 這會傳送一個郵件到指定的信箱,以驗證您的身份 +

+ {% with messages = get_flashed_messages() %} + {% if messages %} + + {% endif %} + {% endwith %} +
+
+
+
+ + {% include 'footer.html' %} + + + + + + \ No newline at end of file diff --git a/templates/login.html b/templates/login.html index 324a9a3..b70c199 100644 --- a/templates/login.html +++ b/templates/login.html @@ -45,6 +45,7 @@ + Forgot Password 忘記密碼 {% with messages = get_flashed_messages() %} {% if messages %} -