Begin work on user registration

This commit is contained in:
James Wang 2015-12-23 21:06:49 -05:00
parent 782eecb70c
commit dc262f778a
4 changed files with 56 additions and 20 deletions

View file

@ -13,9 +13,8 @@ class Users(db.Model):
password = db.Column(db.String(128)) password = db.Column(db.String(128))
admin = db.Column(db.Boolean) admin = db.Column(db.Boolean)
def __init__(self, name, tid, username, email, password): def __init__(self, name, username, email, password):
self.name = name self.name = name
self.tid = tid
self.username = username self.username = username
self.username_lower = username.lower() self.username_lower = username.lower()
self.email = email.lower() self.email = email.lower()
@ -25,7 +24,7 @@ class Teams(db.Model):
tid = db.Column(db.Integer, primary_key=True) tid = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64), unique=True) name = db.Column(db.String(64), unique=True)
join_code = db.Column(db.String(128), unique=True) join_code = db.Column(db.String(128), unique=True)
school = db.Column(db.String) school = db.Column(db.Text)
size = db.Column(db.Integer) size = db.Column(db.Integer)
score = db.Column(db.Integer) score = db.Column(db.Integer)
observer = db.Column(db.Boolean) observer = db.Column(db.Boolean)

View file

@ -1,4 +1,7 @@
from flask import Blueprint, session from flask import Blueprint, session, request
from flask import current_app as app
from models import db, Users
from utils import api_wrapper from utils import api_wrapper
blueprint = Blueprint("user", __name__) blueprint = Blueprint("user", __name__)
@ -6,7 +9,31 @@ blueprint = Blueprint("user", __name__)
@blueprint.route("/register", methods=["POST"]) @blueprint.route("/register", methods=["POST"])
@api_wrapper @api_wrapper
def user_register(): def user_register():
return { "success": 0, "message": "Registration is not open yet." } name = request.form["name"]
username = request.form["username"]
password = request.form["password"]
password_confirm = request.form["password_confirm"]
email = request.form["email"]
username_exists = Users.query.add_columns("name", "uid").filter_by(username=username).first()
email_exists = Users.query.add_columns("name", "uid").filter_by(email=email).first()
if password != password_confirm:
return { "success": 0, "message": "Passwords do not match." }
if len(password) > 128:
return { "success": 0, "message": "Password is too long." }
if len(password) == 0:
return { "success": 0, "message": "Password is too short." }
if len(username) > 64:
return { "success": 0, "message": "Username is too long." }
if username_exists:
return { "success": 0, "message": "Username is already taken." }
if email_exists:
return { "success": 0, "message": "Email has already been used." }
add_user(name, username, email, password)
return { "success": 1, "message": "Success!" }
@blueprint.route("/logout", methods=["POST"]) @blueprint.route("/logout", methods=["POST"])
@api_wrapper @api_wrapper
@ -18,3 +45,8 @@ def user_logout():
@api_wrapper @api_wrapper
def user_login(): def user_login():
pass pass
def add_user(name, username, email, password):
user = Users(name, username, email, password)
db.session.add(user)
db.session.commit()

View file

@ -9,28 +9,29 @@ from api.api import api as api_blueprint
from api.user import blueprint as user_blueprint from api.user import blueprint as user_blueprint
app = Flask(__name__) app = Flask(__name__)
with app.app_context():
from api.models import db
db.init_app(app)
db.create_all()
app.secret_key = config.SECRET app.secret_key = config.SECRET
app.config["SQLALCHEMY_DATABASE_URI"] = config.SQLALCHEMY_DATABASE_URI
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = config.SQLALCHEMY_TRACK_MODIFICATIONS
app.register_blueprint(api_blueprint) app.register_blueprint(api_blueprint)
app.register_blueprint(user_blueprint, url_prefix="/api/user") app.register_blueprint(user_blueprint, url_prefix="/api/user")
@app.route("/api") @app.route("/api")
def api_main(): def api_main():
return json.dumps({ "success": 1, "message": "The API is online." }) return json.dumps({ "success": 1, "message": "The API is online." })
if __name__ == "__main__": if __name__ == "__main__":
with app.app_context(): with app.app_context():
parser = ArgumentParser(description="EasyCTF Server Configuration") parser = ArgumentParser(description="EasyCTF Server Configuration")
parser.add_argument("-d", "--debug", action="store_true", help="Run the server in debug mode.", default=False) parser.add_argument("-d", "--debug", action="store_true", help="Run the server in debug mode.", default=False)
args = parser.parse_args() args = parser.parse_args()
keyword_args, _ = dict(args._get_kwargs()), args._get_args() keyword_args, _ = dict(args._get_kwargs()), args._get_args()
app.debug = keyword_args["debug"] app.debug = keyword_args["debug"]
app.config["SQLALCHEMY_DATABASE_URI"] = "mysql://root:i_hate_passwords@localhost/easyctf" app.run(host="0.0.0.0", port=8000)
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
from api.models import db
db.init_app(app)
db.create_all()
app.run(host="0.0.0.0", port=8000)

View file

@ -10,5 +10,9 @@ with open(".secret_key", "a+") as secret:
key = secret.read() key = secret.read()
SECRET = key SECRET = key
SQLALCHEMY_DATABASE_URI = "mysql://root:i_hate_passwords@localhost/easyctf"
SQLALCHEMY_TRACK_MODIFICATIONS = False
CTF_BEGIN = 0 # To be used later CTF_BEGIN = 0 # To be used later
CTF_END = 0 # To be used later CTF_END = 0 # To be used later