You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
6.1 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  1. import ldap as l
  2. from ldap3 import Server, Connection, ALL, MODIFY_REPLACE
  3. from flask import Flask, g, request, session, redirect, url_for, render_template
  4. from flask_bs4 import Bootstrap
  5. from flask_simpleldap import LDAP
  6. import yaml
  7. import datetime as dt
  8. import pytz
  9. import os
  10. import sqlite3
  11. app = Flask(__name__)
  12. Bootstrap(app)
  13. app.secret_key = 'asdf'
  14. app.debug = True
  15. # Base
  16. app.config['LDAP_REALM_NAME'] = 'OpenLDAP Authentication'
  17. app.config['LDAP_HOST'] = os.environ.get('LDAP_HOST')
  18. app.config['LDAP_BASE_DN'] = os.environ.get('LDAP_BASE_DN')
  19. app.config['LDAP_USERNAME'] = os.environ.get('LDAP_USERNAME')
  20. app.config['LDAP_PASSWORD'] = os.environ.get('LDAP_PASSWORD')
  21. # OpenLDAP
  22. app.config['LDAP_OBJECTS_DN'] = 'dn'
  23. app.config['LDAP_OPENLDAP'] = True
  24. app.config['LDAP_USER_OBJECT_FILTER'] = '(&(objectclass=posixAccount)(uid=%s))'
  25. ldap = LDAP(app)
  26. eastern = pytz.timezone('US/Eastern')
  27. with open('config/config.yaml') as f:
  28. yaml_data = yaml.load(f, Loader=yaml.SafeLoader)
  29. search = yaml_data['search']
  30. account_url = yaml_data['accounts']['account_url']
  31. description = yaml_data['description']
  32. game_description = yaml_data['game_description']
  33. countdown_data = None
  34. if yaml_data['countdown']['active'] == True:
  35. countdown_data = yaml_data['countdown']
  36. final_countdown_data = None
  37. final_time = None
  38. if yaml_data['final_countdown']['active'] == True:
  39. final_countdown_data = yaml_data['final_countdown']
  40. final_time = eastern.localize(dt.datetime.strptime(final_countdown_data['timestamp'], '%B %d %Y %H:%M:%S%z').replace(tzinfo=None))
  41. apps = []
  42. for itm in yaml_data['apps'].items():
  43. apps.append(itm[1])
  44. games = []
  45. for itm in yaml_data['games'].items():
  46. games.append(itm[1])
  47. server = Server(app.config['LDAP_HOST'])
  48. conn = Connection(server, app.config['LDAP_USERNAME'], app.config['LDAP_PASSWORD'], auto_bind=True)
  49. @app.before_request
  50. def before_request():
  51. g.user = None
  52. if 'user_id' in session:
  53. # This is where you'd query your database to get the user info.
  54. g.user = {}
  55. @app.route('/')
  56. def index():
  57. current_time = eastern.localize(dt.datetime.now())
  58. if final_countdown_data != None:
  59. if (final_time - current_time).days > -1:
  60. return render_template('final_countdown.j2', final_countdown = final_countdown_data)
  61. if countdown_data != None:
  62. return render_template('index.j2', apps = apps, search = search, account_url = account_url, description = description, countdown = countdown_data)
  63. return render_template('index.j2', apps = apps, search = search, account_url = account_url, description = description)
  64. @app.route('/games')
  65. def game():
  66. if 'user_id' in session:
  67. user_dict = ldap.get_object_details(session['user_id'])
  68. user = {'dn': 'cn={},cn=usergroup,ou=users,dc=technicalincompetence,dc=club'.format(user_dict['cn'][0].decode('ascii')),
  69. 'firstName': user_dict['givenName'][0].decode('ascii'),
  70. 'lastName': user_dict['sn'][0].decode('ascii'),
  71. 'email': user_dict['mail'][0].decode('ascii'),
  72. 'userName': user_dict['uid'][0].decode('ascii'),
  73. }
  74. current_time = eastern.localize(dt.datetime.now())
  75. if final_countdown_data != None:
  76. if (final_time - current_time).days > -1:
  77. return render_template('final_countdown.j2', final_countdown = final_countdown_data)
  78. if countdown_data != None:
  79. return render_template('games.j2', apps = games, search = search, account_url = account_url, description = game_description, countdown = countdown_data)
  80. if 'user_id' in session:
  81. return render_template('games.j2', apps = games, search = search, account_url = account_url, description = game_description, user = user, game_list = generate_game_list())
  82. return render_template('games.j2', apps = games, search = search, account_url = account_url, description = game_description)
  83. @app.route('/login', methods=['GET', 'POST'])
  84. def login():
  85. if g.user:
  86. return redirect(url_for('index'))
  87. if request.method == 'POST':
  88. user = request.form['user']
  89. passwd = request.form['passwd']
  90. test = ldap.bind_user(user, passwd)
  91. if test is None or passwd == '':
  92. return render_template('login.j2', error='Invalid credentials')
  93. else:
  94. session['user_id'] = request.form['user']
  95. session['passwd'] = request.form['passwd']
  96. return redirect('/games')
  97. return render_template('login.j2')
  98. @ldap.login_required
  99. @app.route('/add', methods=['POST'])
  100. def add_game():
  101. if request.method == 'POST':
  102. game_title = request.form['game_title']
  103. game_link = request.form['game_link']
  104. conn = sqlite3.connect('config/games_in_progress.db')
  105. c = conn.cursor()
  106. if game_title is not None and len(game_title) > 0 and game_link is not None and len(game_link) > 0:
  107. c.execute("INSERT INTO games (user_id, game_title, game_link) VALUES (?, ?, ?)", (session['user_id'], game_title, game_link,))
  108. conn.commit()
  109. conn.close()
  110. return 'Success'
  111. conn.commit()
  112. conn.close()
  113. return 'Error'
  114. @ldap.login_required
  115. @app.route('/delete', methods=['POST'])
  116. def delete_game():
  117. if request.method == 'POST':
  118. game_id = request.form['game_id']
  119. conn = sqlite3.connect('config/games_in_progress.db')
  120. c = conn.cursor()
  121. if game_id is not None and len(game_id) > 0:
  122. c.execute("DELETE FROM games WHERE id=? AND user_id=?", (game_id, session['user_id'],))
  123. conn.commit()
  124. conn.close()
  125. return 'Success'
  126. conn.commit()
  127. conn.close()
  128. return 'Error'
  129. def generate_game_list():
  130. conn = sqlite3.connect('config/games_in_progress.db')
  131. c = conn.cursor()
  132. if 'user_id' in session:
  133. c.execute('SELECT * FROM games WHERE user_id=?', (session['user_id'], ))
  134. rows = c.fetchall()
  135. conn.close()
  136. return rows
  137. conn.close()
  138. return []
  139. @app.route('/logout')
  140. def logout():
  141. session.pop('user_id', None)
  142. return redirect(url_for('game'))
  143. if __name__ == '__main__':
  144. app.run(extra_files="config/config.yaml")