noranisa commited on
Commit
c871548
·
verified ·
1 Parent(s): 813ada0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -22
app.py CHANGED
@@ -93,34 +93,22 @@ def authenticate():
93
  'You have to login with proper credentials', 401,
94
  {'WWW-Authenticate': 'Basic realm="Login Required"'})
95
 
96
- def protected(f):
97
- @wraps(f)
98
- def decorated(*args, **kwargs):
99
- auth = request.authorization
100
- if not auth or not check_auth(auth.username, auth.password):
101
- return authenticate()
102
- return f(*args, **kwargs)
103
- return decorated
104
-
105
 
106
- # --- PENGATURAN ADMIN PANEL (VERSI DIPERBAIKI) ---
107
 
108
- # Buat kelas dasar yang aman untuk semua view admin
109
- class SecureView:
110
- @protected
111
- def dispatch_request(self, *args, **kwargs):
112
- # Memanggil implementasi asli dari dispatch_request
113
- return super().dispatch_request(*args, **kwargs)
114
 
115
- # Gabungkan keamanan dengan kelas bawaan Flask-Admin
116
- class SecureAdminIndexView(SecureView, AdminIndexView):
117
- pass
118
 
119
- class SecureModelView(SecureView, ModelView):
120
  pass
121
 
122
- # Dashboard View sekarang hanya fokus pada logika tampilan
123
- class DashboardView(SecureAdminIndexView):
124
  def get_sales_data(self):
125
  """Query data penjualan untuk 7 hari terakhir."""
126
  today = date.today()
 
93
  'You have to login with proper credentials', 401,
94
  {'WWW-Authenticate': 'Basic realm="Login Required"'})
95
 
 
 
 
 
 
 
 
 
 
96
 
97
+ # --- PENGATURAN ADMIN PANEL (VERSI BENAR & FINAL) ---
98
 
99
+ class AuthMixin:
100
+ """Mixin untuk menambahkan autentikasi ke view admin."""
101
+ def is_accessible(self):
102
+ auth = request.authorization
103
+ return auth and check_auth(auth.username, auth.password)
 
104
 
105
+ def inaccessible_callback(self, name, **kwargs):
106
+ return authenticate()
 
107
 
108
+ class SecureModelView(AuthMixin, ModelView):
109
  pass
110
 
111
+ class DashboardView(AuthMixin, AdminIndexView):
 
112
  def get_sales_data(self):
113
  """Query data penjualan untuk 7 hari terakhir."""
114
  today = date.today()