Merge pull request #6 from nullmedium/initial-user-setup
Make first user the admin.
This commit is contained in:
18
app.py
18
app.py
@@ -262,16 +262,30 @@ def register():
|
||||
|
||||
if error is None:
|
||||
try:
|
||||
# Check if this is the first user account
|
||||
is_first_user = User.query.count() == 0
|
||||
|
||||
new_user = User(username=username, email=email, is_verified=False)
|
||||
new_user.set_password(password)
|
||||
|
||||
# Make first user an admin with full privileges
|
||||
if is_first_user:
|
||||
new_user.is_admin = True
|
||||
new_user.role = Role.ADMIN
|
||||
new_user.is_verified = True # Auto-verify first user
|
||||
|
||||
# Generate verification token
|
||||
token = new_user.generate_verification_token()
|
||||
|
||||
db.session.add(new_user)
|
||||
db.session.commit()
|
||||
|
||||
# Send verification email
|
||||
if is_first_user:
|
||||
# First user gets admin privileges and is auto-verified
|
||||
logger.info(f"First user account created: {username} with admin privileges")
|
||||
flash('Welcome! You are the first user and have been granted administrator privileges. You can now log in.', 'success')
|
||||
else:
|
||||
# Send verification email for regular users
|
||||
verification_url = url_for('verify_email', token=token, _external=True)
|
||||
msg = Message('Verify your TimeTrack account', recipients=[email])
|
||||
msg.body = f'''Hello {username},
|
||||
@@ -289,8 +303,8 @@ The TimeTrack Team
|
||||
'''
|
||||
mail.send(msg)
|
||||
logger.info(f"Verification email sent to {email}")
|
||||
|
||||
flash('Registration initiated! Please check your email to verify your account.', 'success')
|
||||
|
||||
return redirect(url_for('login'))
|
||||
except Exception as e:
|
||||
db.session.rollback()
|
||||
|
||||
Reference in New Issue
Block a user