Skip to content

Commit 5024c8c

Browse files
committed
Revert "Added new user management functions"
This reverts commit 815c93c.
1 parent 815c93c commit 5024c8c

File tree

4 files changed

+10
-488
lines changed

4 files changed

+10
-488
lines changed

routes/auth_routes.py

Lines changed: 7 additions & 166 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from flask import Blueprint, redirect, url_for, flash, render_template, request, make_response, session, current_app as app
1+
from flask import Blueprint, redirect, url_for, flash, render_template, request, make_response, session
22
from flask_login import LoginManager, UserMixin, login_user, login_required, logout_user, current_user
33
from flask_sqlalchemy import SQLAlchemy
44
from werkzeug.security import generate_password_hash, check_password_hash
@@ -11,7 +11,6 @@
1111
from .utils import is_user_system_enabled
1212
import random
1313
import string
14-
from sqlalchemy import inspect
1514

1615
auth_bp = Blueprint('auth', __name__, url_prefix='/auth')
1716

@@ -57,86 +56,11 @@ class User(UserMixin, db.Model):
5756
role = db.Column(db.String(20), nullable=False)
5857
is_default = db.Column(db.Boolean, default=False)
5958
onboarding_complete = db.Column(db.Boolean, default=False)
60-
registration_key = db.Column(db.String(120), nullable=True)
61-
registration_key_limit = db.Column(db.Integer, nullable=True) # New column for key usage limit
62-
registration_key_used = db.Column(db.Integer, default=0) # New column for current usage count
63-
64-
@staticmethod
65-
def get_registration_key():
66-
admin_user = User.query.filter_by(role='admin').first()
67-
if admin_user:
68-
return {
69-
'key': admin_user.registration_key,
70-
'limit': admin_user.registration_key_limit,
71-
'used': admin_user.registration_key_used
72-
}
73-
return None
74-
75-
@staticmethod
76-
def set_registration_key(key, limit=None):
77-
admin_user = User.query.filter_by(role='admin').first()
78-
if admin_user:
79-
admin_user.registration_key = key
80-
if limit is not None:
81-
admin_user.registration_key_limit = limit
82-
admin_user.registration_key_used = 0 # Reset usage count when key/limit changes
83-
db.session.commit()
84-
return True
85-
return False
86-
87-
@staticmethod
88-
def increment_key_usage():
89-
admin_user = User.query.filter_by(role='admin').first()
90-
if admin_user:
91-
admin_user.registration_key_used += 1
92-
db.session.commit()
93-
return True
94-
return False
95-
96-
def recreate_database():
97-
"""Recreate the database with the new schema."""
98-
# Drop all tables
99-
db.drop_all()
100-
# Create all tables with new schema
101-
db.create_all()
102-
# Create default admin if no users exist
103-
create_default_admin()
10459

10560
def init_db(app):
10661
db.init_app(app)
10762
with app.app_context():
108-
# Create tables if they don't exist
10963
db.create_all()
110-
111-
# Check if the columns exist
112-
inspector = inspect(db.engine)
113-
if 'user' in inspector.get_table_names():
114-
columns = [col['name'] for col in inspector.get_columns('user')]
115-
116-
# Add registration_key column if it doesn't exist
117-
if 'registration_key' not in columns:
118-
with db.engine.connect() as conn:
119-
conn.execute(db.text('ALTER TABLE user ADD COLUMN registration_key VARCHAR(120)'))
120-
db.session.commit()
121-
logging.info("Added registration_key column to user table")
122-
123-
# Add registration_key_limit column if it doesn't exist
124-
if 'registration_key_limit' not in columns:
125-
with db.engine.connect() as conn:
126-
conn.execute(db.text('ALTER TABLE user ADD COLUMN registration_key_limit INTEGER'))
127-
db.session.commit()
128-
logging.info("Added registration_key_limit column to user table")
129-
130-
# Add registration_key_used column if it doesn't exist
131-
if 'registration_key_used' not in columns:
132-
with db.engine.connect() as conn:
133-
conn.execute(db.text('ALTER TABLE user ADD COLUMN registration_key_used INTEGER DEFAULT 0'))
134-
db.session.commit()
135-
logging.info("Added registration_key_used column to user table")
136-
137-
# Create default admin if no users exist
138-
if User.query.count() == 0:
139-
create_default_admin()
14064

14165
@login_manager.user_loader
14266
def load_user(user_id):
@@ -166,16 +90,15 @@ def login():
16690
password = request.form.get('password')
16791
remember = bool(request.form.get('remember_me'))
16892

93+
16994
user = User.query.filter_by(username=username).first()
17095
if user:
96+
17197
if check_password_hash(user.password, password):
172-
# Set session as permanent if remember me is checked
173-
if remember:
174-
session.permanent = True # This will use the PERMANENT_SESSION_LIFETIME value
175-
else:
176-
session.permanent = False # Session will expire when browser closes
17798

178-
# Login the user with the remember flag
99+
# Always set session as permanent
100+
session.permanent = True
101+
179102
login_user(user, remember=remember)
180103

181104
# Force session save
@@ -219,86 +142,4 @@ def logout():
219142
@auth_bp.route('/unauthorized')
220143
def unauthorized():
221144
flash('You are not authorized to access this page.', 'error')
222-
return redirect(url_for('auth.login'))
223-
224-
@auth_bp.route('/register', methods=['GET', 'POST'])
225-
def register():
226-
if not is_user_system_enabled():
227-
return redirect(url_for('root.root'))
228-
229-
if current_user.is_authenticated:
230-
return redirect(url_for('root.root'))
231-
232-
if request.method == 'POST':
233-
username = request.form['username']
234-
password = request.form['password']
235-
registration_key = request.form['registration_key']
236-
237-
# Validate registration key
238-
key_info = User.get_registration_key()
239-
if not key_info or registration_key != key_info['key']:
240-
flash('Invalid registration key.', 'error')
241-
return redirect(url_for('auth.login'))
242-
243-
# Check key usage limit
244-
if key_info['limit'] is not None and key_info['used'] >= key_info['limit']:
245-
flash('Registration key has reached its usage limit.', 'error')
246-
return redirect(url_for('auth.login'))
247-
248-
existing_user = User.query.filter_by(username=username).first()
249-
if existing_user:
250-
flash('Username already exists.', 'error')
251-
return redirect(url_for('auth.login'))
252-
253-
hashed_password = generate_password_hash(password)
254-
new_user = User(
255-
username=username,
256-
password=hashed_password,
257-
role='user', # New users are always regular users
258-
onboarding_complete=True
259-
)
260-
db.session.add(new_user)
261-
262-
# Increment key usage count
263-
User.increment_key_usage()
264-
265-
db.session.commit()
266-
login_user(new_user)
267-
flash('Registered successfully.', 'success')
268-
return redirect(url_for('root.root'))
269-
return redirect(url_for('auth.login'))
270-
271-
@auth_bp.route('/account')
272-
@login_required
273-
def account():
274-
if current_user.role == 'admin':
275-
return redirect(url_for('root.root'))
276-
return render_template('account.html')
277-
278-
@auth_bp.route('/change_password', methods=['POST'])
279-
@login_required
280-
def change_password():
281-
if current_user.role == 'admin':
282-
return redirect(url_for('root.root'))
283-
284-
current_password = request.form.get('current_password')
285-
new_password = request.form.get('new_password')
286-
confirm_password = request.form.get('confirm_password')
287-
288-
# Verify current password
289-
if not check_password_hash(current_user.password, current_password):
290-
flash('Current password is incorrect.', 'error')
291-
return redirect(url_for('auth.account'))
292-
293-
# Verify new password match
294-
if new_password != confirm_password:
295-
flash('New passwords do not match.', 'error')
296-
return redirect(url_for('auth.account'))
297-
298-
# Update password
299-
current_user.password = generate_password_hash(new_password)
300-
current_user.is_default = False
301-
db.session.commit()
302-
303-
flash('Password changed successfully.', 'success')
304-
return redirect(url_for('auth.account'))
145+
return redirect(url_for('auth.login'))

routes/user_management_routes.py

Lines changed: 1 addition & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -32,28 +32,7 @@ def manage_users():
3232
if not is_user_system_enabled():
3333
return redirect(url_for('root.root'))
3434
users = User.query.all()
35-
key_info = User.get_registration_key()
36-
return render_template('manage_users.html', users=users, registration_key=key_info)
37-
38-
@user_management_bp.route('/update_registration_key', methods=['POST'])
39-
@admin_required
40-
def update_registration_key():
41-
key = request.form.get('registration_key')
42-
limit = request.form.get('registration_key_limit')
43-
44-
if key:
45-
try:
46-
# Convert limit to integer if provided, otherwise set to None
47-
limit = int(limit) if limit and limit.strip() else None
48-
if User.set_registration_key(key, limit):
49-
flash('Registration key updated successfully.', 'success')
50-
else:
51-
flash('Failed to update registration key.', 'error')
52-
except ValueError:
53-
flash('Key usage limit must be a valid number.', 'error')
54-
else:
55-
flash('Registration key cannot be empty.', 'error')
56-
return redirect(url_for('user_management.manage_users'))
35+
return render_template('manage_users.html', users=users)
5736

5837
@user_management_bp.route('/add_user', methods=['POST'])
5938
@admin_required
@@ -97,29 +76,6 @@ def delete_user(user_id):
9776
db.session.rollback()
9877
return jsonify({'success': False, 'error': 'Database error'}), 500
9978

100-
@user_management_bp.route('/change_user_password/<int:user_id>', methods=['POST'])
101-
@admin_required
102-
def change_user_password(user_id):
103-
if current_user.role != 'admin':
104-
return jsonify({'success': False, 'error': 'Unauthorized'}), 403
105-
106-
user = User.query.get(user_id)
107-
if not user:
108-
return jsonify({'success': False, 'error': 'User not found'}), 404
109-
110-
new_password = request.form.get('new_password')
111-
if not new_password:
112-
return jsonify({'success': False, 'error': 'Password is required'}), 400
113-
114-
try:
115-
user.password = generate_password_hash(new_password)
116-
user.is_default = False
117-
db.session.commit()
118-
return jsonify({'success': True})
119-
except Exception as e:
120-
db.session.rollback()
121-
return jsonify({'success': False, 'error': 'Database error'}), 500
122-
12379
# Modify the register route
12480
@user_management_bp.route('/register', methods=['GET', 'POST'])
12581
def register():

templates/account.html

Lines changed: 0 additions & 90 deletions
This file was deleted.

0 commit comments

Comments
 (0)