22from flask_login import LoginManager , UserMixin , login_user , login_required , logout_user , current_user
33from flask_sqlalchemy import SQLAlchemy
44from werkzeug .security import generate_password_hash , check_password_hash
5+
6+
7+ from flask import Flask , render_template , request , jsonify
8+ import subprocess
9+
510from src .scripts .email_me import send_email
611
712from src .config import app , db
8- from src .models import User , EmailPassword , DashboardSettings
13+ from src .models import User , SmptEamilPasswordConfig , DashboardSettings
14+ from src .utils import read_html_file
915
1016auth_bp = blueprints .Blueprint ('auth' , __name__ )
1117
@@ -64,7 +70,8 @@ def login():
6470 # Get Admin Emails with Alerts Enabled:
6571 admin_email_address = get_email_addresses (user_level = 'admin' , receive_email_alerts = True )
6672 if admin_email_address :
67- send_email (admin_email_address , 'Login Alert' , f'{ user .username } logged in to the system.' )
73+ login_body = read_html_file ("src/templates/email_templates/login.html" )
74+ send_email (admin_email_address , 'Login Alert' , login_body , is_html = True )
6875
6976 return redirect (url_for ('dashboard' ))
7077 flash ('Invalid username or password' , 'danger' )
@@ -114,30 +121,13 @@ def protected():
114121
115122@app .route ('/logout' )
116123def logout ():
124+ receiver_email = get_email_addresses (user_level = 'admin' , receive_email_alerts = True )
125+ if receiver_email :
126+ logout_body = read_html_file ("src/templates/email_templates/logout.html" )
127+ send_email (receiver_email , 'Logout Alert' , logout_body , is_html = True )
117128 logout_user ()
118129 return redirect (url_for ('login' ))
119130
120-
121- # def admin_required(f):
122- # """Decorator to ensure the current user is an admin."""
123- # @wraps(f)
124- # def decorated_function(*args, **kwargs):
125- # if not current_user.is_authenticated or current_user.user_level != 'admin':
126- # flash('Access denied. Admins only.')
127- # return redirect(url_for('login'))
128- # return f(*args, **kwargs)
129- # return decorated_function
130-
131- # def user_required(f):
132- # """Decorator to ensure the current user is a regular user."""
133- # @wraps(f)
134- # def decorated_function(*args, **kwargs):
135- # if not current_user.is_authenticated or current_user.user_level != 'user':
136- # flash('Access denied. Users only.')
137- # return redirect(url_for('login'))
138- # return f(*args, **kwargs)
139- # return decorated_function
140-
141131@app .route ('/users' )
142132@login_required
143133def view_users ():
@@ -190,28 +180,36 @@ def delete_user(username):
190180
191181@app .route ("/update-email-password" , methods = ["GET" , "POST" ])
192182@login_required
193- def update_email_password ():
194- email_password = EmailPassword .query .first ()
183+ def update_smpt_email_password ():
184+ smtp_config = SmptEamilPasswordConfig .query .first ()
195185
196186 if request .method == "POST" :
197187 new_email = request .form .get ("email" )
198188 new_password = request .form .get ("password" )
199189
200- if new_email :
201- email_password .email = new_email
202- if new_password :
203- email_password .password = new_password
204-
190+ if not new_email or not new_password :
191+ flash ("Please provide email and password." , "danger" )
192+ return redirect (url_for ("update_smpt_email_password" ))
193+
194+
195+ if not smtp_config :
196+ smtp_config = SmptEamilPasswordConfig (email = new_email , password = new_password )
197+ db .session .add (smtp_config )
198+ else :
199+ smtp_config .email = new_email
200+ smtp_config .password = new_password
201+
205202 db .session .commit ()
206203 flash ("Email and password updated successfully!" , "success" )
207- return redirect (url_for ("update_email_password " ))
204+ return redirect (url_for ("update_smpt_email_password " ))
208205
209- return render_template ("update_email_password .html" , email_password = email_password )
206+ return render_template ("update_smpt_email_password .html" , smtp_config = smtp_config )
210207
211208@app .route ("/send_email" , methods = ["GET" , "POST" ])
212209@login_required
213210def send_email_page ():
214211 dasboard_settings = DashboardSettings .query .first ()
212+ receiver_email = get_email_addresses (user_level = 'admin' , receive_email_alerts = True )
215213 if dasboard_settings :
216214 enable_alerts = dasboard_settings .enable_alerts
217215 if request .method == "POST" :
@@ -225,18 +223,24 @@ def send_email_page():
225223 flash ("Please provide recipient, subject, and body." , "danger" )
226224 return redirect (url_for ('send_email_page' ))
227225
226+ print ("Priority:" , priority )
227+ print ("receiver_email:" , receiver_email )
228+
228229 # on high priority, send to all users or admin users even the receive_email_alerts is False
229230 if priority == "high" and receiver_email == "all_users" :
231+ print ("Sending to all users" )
230232 receiver_email = get_email_addresses (fetch_all_users = True )
231233 elif priority == "high" and receiver_email == "admin_users" :
234+ print ("Sending to admin users" )
232235 receiver_email = get_email_addresses (user_level = 'admin' , fetch_all_users = True )
233236
234237 # priority is low, send to users with receive_email_alerts is True
235238 if priority == "low" and receiver_email == "all_users" :
239+ print ("Sending to all users with receive_email_alerts=True" )
236240 receiver_email = get_email_addresses (receive_email_alerts = True )
237241 elif priority == "low" and receiver_email == "admin_users" :
238- receiver_email = get_email_addresses ( user_level = ' admin' , receive_email_alerts = True )
239-
242+ print ( "Sending to admin users with receive_email_alerts=True" )
243+ receiver_email = get_email_addresses ( user_level = 'admin' , receive_email_alerts = True )
240244
241245 if not receiver_email :
242246 flash ("No users found to send email to." , "danger" )
@@ -248,15 +252,34 @@ def send_email_page():
248252 attachment_path = f"/tmp/{ attachment .filename } "
249253 attachment .save (attachment_path )
250254 try :
251- respone = send_email (receiver_email , subject , body , attachment_path )
252- if respone and respone ["status" ] == "success" :
253- flash (respone ['message' ], "success" )
254- elif respone and respone ["status" ] == "failed" :
255- flash (respone ['message' ], "danger" )
256- return redirect (url_for (respone ["type" ]))
255+ respose = send_email (receiver_email , subject , body , attachment_path )
256+ print (respose )
257+ if respose and respose .get ("status" ) == "success" :
258+ flash (respose .get ("message" ), "success" )
257259 except Exception as e :
258260 flash (f"Failed to send email: { str (e )} " , "danger" )
259261
260262 return redirect (url_for ('send_email_page' ))
261263
262- return render_template ("send_email.html" , enable_alerts = enable_alerts )
264+ return render_template ("send_email.html" , enable_alerts = enable_alerts )
265+
266+ @app .route ('/terminal' , methods = ['GET' , 'POST' ])
267+ @login_required
268+ def terminal ():
269+ if current_user .user_level != 'admin' :
270+ flash ("Your account does not have permission to view this page." , "danger" )
271+ return render_template ("error/permission_denied.html" )
272+ if request .method == 'POST' :
273+ command = request .form .get ('command' )
274+ if command :
275+ try :
276+ # Run the command and capture the output
277+ output = subprocess .check_output (command , shell = True , stderr = subprocess .STDOUT , universal_newlines = True )
278+ except subprocess .CalledProcessError as e :
279+ # If the command fails, capture the error output
280+ output = e .output
281+ return jsonify (output = output )
282+ return render_template ('terminal.html' )
283+
284+ if __name__ == '__main__' :
285+ app .run (debug = True )
0 commit comments