diff --git a/db/sqlite.py b/db/sqlite.py index 1b1cb11..802bc62 100644 --- a/db/sqlite.py +++ b/db/sqlite.py @@ -360,7 +360,10 @@ class DB: return True return False - def add_user(self, username, password, time): + def register_user(self, username, password, time): + ''' + Add a user if registrations are enabled. + ''' try: db = self._db() c = db.cursor() @@ -380,6 +383,27 @@ class DB: # User already exists, probably return False + def add_user(self, username, password, time): + ''' + Add a user without checking if registrations are enabled. + ''' + try: + db = self._db() + c = db.cursor() + c.execute(''' + insert into users(name, password, join_time) + values (lower(?), ?, ?) + ''', + (username, password, time) + ) + if c.rowcount > 0: + db.commit() + return True + return False + except sqlite3.IntegrityError: + # User already exists, probably + return False + def get_users(self): return self._db().execute(''' select user_id, name, join_time, role, banned_until diff --git a/main.py b/main.py index 3f0ff76..5e27f61 100644 --- a/main.py +++ b/main.py @@ -335,7 +335,7 @@ def register(): request.form['answer'], ): flash('CAPTCHA answer is incorrect', 'error') - elif not db.add_user(username, hash_password(password), time.time_ns()): + elif not db.register_user(username, hash_password(password), time.time_ns()): flash('Failed to create account (username may already be taken)', 'error') else: flash('Account has been created. You can login now.', 'success') @@ -491,6 +491,18 @@ def admin_unban_user(user_id): flash(str(e), 'error') return redirect(url_for('admin')) +@app.route('/admin/user/new/', methods = ['POST']) +def admin_new_user(): + try: + name, password = request.form['name'], request.form['password'] + if db.add_user(name, hash_password(password), time.time_ns()): + flash('Added user', 'success') + else: + flash('Failed to add user', 'error') + except Exception as e: + flash(str(e), 'error') + return redirect(url_for('admin')) + @app.route('/admin/restart/', methods = ['POST']) def admin_restart(): chk, user = _admin_check() diff --git a/templates/admin/index.html b/templates/admin/index.html index 287a7f7..576ad6e 100644 --- a/templates/admin/index.html +++ b/templates/admin/index.html @@ -114,4 +114,12 @@ {%- endfor -%} +