Sahil Kumar commited on
Commit
eafd264
·
1 Parent(s): 33a9036

tagger: make Full Approve admin-only (9939921144), others locked to Under Review

Browse files
README.md CHANGED
@@ -122,6 +122,12 @@ JEE Main, …) and approval mode → Run. Live log + progress; on finish you get
122
  failed-questions report and the updated Excel. Each user gets their own run; a
123
  second run for the same user is refused while one is in progress.
124
 
 
 
 
 
 
 
125
  `python backend/make_template.py` regenerates `database/tagging_template.xlsx` by scraping the live
126
  CMS form with Playwright, so every dropdown column carries valid in-Excel options.
127
  `/tagger/refresh_template` does the same from the page.
 
122
  failed-questions report and the updated Excel. Each user gets their own run; a
123
  second run for the same user is refused while one is in progress.
124
 
125
+ **Approval mode is admin-only:** Full Approve (the workflow all the way to
126
+ *approved*) is restricted to the admin CMS phone `9939921144`; everyone else is
127
+ locked to *Under Review* and the radio is hidden. The check is enforced in
128
+ `/tagger/start` server-side, not just in the UI. `can_approve` gates the radio
129
+ in `tagger.html` / `tag.html`.
130
+
131
  `python backend/make_template.py` regenerates `database/tagging_template.xlsx` by scraping the live
132
  CMS form with Playwright, so every dropdown column carries valid in-Excel options.
133
  `/tagger/refresh_template` does the same from the page.
backend/app.py CHANGED
@@ -500,6 +500,16 @@ def _all_state_paths():
500
  # =============================================================================
501
  # AUTH HELPERS
502
  # =============================================================================
 
 
 
 
 
 
 
 
 
 
503
  def login_required(f):
504
  @wraps(f)
505
  def decorated(*args, **kwargs):
@@ -1490,7 +1500,8 @@ def tagger_page():
1490
  tagger_ok=TAGGER_OK, tagger_missing=TAGGER_MISSING,
1491
  state_exists=connected, state_age=sess_age,
1492
  cms_user=session.get('tagger_user', ''),
1493
- is_local=_is_local_request())
 
1494
 
1495
  @app.route('/tagger/upload_excel', methods=['POST'])
1496
  @login_required
@@ -1573,6 +1584,11 @@ def tagger_start():
1573
  tocs = [job['_tag_exam']]
1574
  workers = min(max(int(data.get('workers', 3) or 3), 1), 5)
1575
  full_approve = data.get('approval_mode', 'full') == 'full'
 
 
 
 
 
1576
  stop_event = threading.Event()
1577
 
1578
  # Reset run state on this user's job in place (keep the pending-excel fields).
@@ -3724,7 +3740,8 @@ def tag_page():
3724
  return render_template('tag.html',
3725
  exams=tag_assist.exams(),
3726
  tagger_ok=TAGGER_OK,
3727
- cms_connected=bool(cms_connected))
 
3728
 
3729
 
3730
  @app.route('/tag/subjects', methods=['POST'])
 
500
  # =============================================================================
501
  # AUTH HELPERS
502
  # =============================================================================
503
+ # Only this CMS phone may run the Tagger's Full Approve workflow; everyone else
504
+ # is restricted to "Under Review". Identified by the CMS phone stored server-side
505
+ # on the Dashboard login (the tagger state file itself only holds token+userId).
506
+ ADMIN_CMS_PHONES = {"9939921144"}
507
+
508
+ def _can_approve():
509
+ """True only when the logged-in CMS user holds approval rights."""
510
+ return _il('phone') in ADMIN_CMS_PHONES
511
+
512
+
513
  def login_required(f):
514
  @wraps(f)
515
  def decorated(*args, **kwargs):
 
1500
  tagger_ok=TAGGER_OK, tagger_missing=TAGGER_MISSING,
1501
  state_exists=connected, state_age=sess_age,
1502
  cms_user=session.get('tagger_user', ''),
1503
+ is_local=_is_local_request(),
1504
+ can_approve=_can_approve())
1505
 
1506
  @app.route('/tagger/upload_excel', methods=['POST'])
1507
  @login_required
 
1584
  tocs = [job['_tag_exam']]
1585
  workers = min(max(int(data.get('workers', 3) or 3), 1), 5)
1586
  full_approve = data.get('approval_mode', 'full') == 'full'
1587
+ # Full Approve is an admin-only workflow. Enforced here server-side (the UI
1588
+ # hides the option for non-admins, but a crafted request could still ask).
1589
+ if full_approve and not _can_approve():
1590
+ return jsonify({'error': 'Full Approve is restricted to the admin account '
1591
+ '(9939921144). Use Under Review mode.'}), 403
1592
  stop_event = threading.Event()
1593
 
1594
  # Reset run state on this user's job in place (keep the pending-excel fields).
 
3740
  return render_template('tag.html',
3741
  exams=tag_assist.exams(),
3742
  tagger_ok=TAGGER_OK,
3743
+ cms_connected=bool(cms_connected),
3744
+ can_approve=_can_approve())
3745
 
3746
 
3747
  @app.route('/tag/subjects', methods=['POST'])
frontend/templates/tag.html CHANGED
@@ -190,11 +190,16 @@
190
 
191
  <label class="fld">Approval mode</label>
192
  <div class="radio-row">
 
193
  <label><input type="radio" name="approval" value="full" checked>
194
  <b style="color:#2e7d32;">Full Approve</b> — tag &amp; approve in CMS</label>
195
- <label><input type="radio" name="approval" value="under_review">
 
196
  <b style="color:#1890ff;">Under Review</b> — tag &amp; leave at "under review"</label>
197
  </div>
 
 
 
198
 
199
  <div class="btn-row" style="margin-top:14px;">
200
  <button class="btn btn-success" id="btnRun" disabled>▶ Run Tagger</button>
 
190
 
191
  <label class="fld">Approval mode</label>
192
  <div class="radio-row">
193
+ {% if can_approve %}
194
  <label><input type="radio" name="approval" value="full" checked>
195
  <b style="color:#2e7d32;">Full Approve</b> — tag &amp; approve in CMS</label>
196
+ {% endif %}
197
+ <label><input type="radio" name="approval" value="under_review" {% if not can_approve %}checked{% endif %}>
198
  <b style="color:#1890ff;">Under Review</b> — tag &amp; leave at "under review"</label>
199
  </div>
200
+ {% if not can_approve %}
201
+ <div style="font-size:12px; color:#aaa; margin-top:6px;">Full Approve is restricted to the admin account — runs stop at "under review".</div>
202
+ {% endif %}
203
 
204
  <div class="btn-row" style="margin-top:14px;">
205
  <button class="btn btn-success" id="btnRun" disabled>▶ Run Tagger</button>
frontend/templates/tagger.html CHANGED
@@ -261,12 +261,18 @@
261
  <div style="margin-top:14px;">
262
  <div class="opt-label">Approval Mode</div>
263
  <div class="radio-row">
 
264
  <label><input type="radio" name="approval" value="full" checked>
265
- <span style="color:#2e7d32; font-weight:600;">Full Approve</span></label>
266
- <label><input type="radio" name="approval" value="under_review">
 
 
267
  <span style="color:#1890ff; font-weight:600;">Under Review</span>
268
  <span style="color:#999; font-size:11.5px;">(tag &amp; leave at "under review", no Approve)</span></label>
269
  </div>
 
 
 
270
  </div>
271
 
272
  <div class="opt-label">Question IDs (optional)</div>
 
261
  <div style="margin-top:14px;">
262
  <div class="opt-label">Approval Mode</div>
263
  <div class="radio-row">
264
+ {% if can_approve %}
265
  <label><input type="radio" name="approval" value="full" checked>
266
+ <span style="color:#2e7d32; font-weight:600;">Full Approve</span>
267
+ <span style="color:#999; font-size:11.5px;">(admin only)</span></label>
268
+ {% endif %}
269
+ <label><input type="radio" name="approval" value="under_review" {% if not can_approve %}checked{% endif %}>
270
  <span style="color:#1890ff; font-weight:600;">Under Review</span>
271
  <span style="color:#999; font-size:11.5px;">(tag &amp; leave at "under review", no Approve)</span></label>
272
  </div>
273
+ {% if not can_approve %}
274
+ <div style="font-size:12px; color:#aaa; margin-top:6px;">Full Approve is restricted to the admin account — runs stop at "under review".</div>
275
+ {% endif %}
276
  </div>
277
 
278
  <div class="opt-label">Question IDs (optional)</div>
tests/test_admin_approve_gate.py ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """The Tagger's Full Approve workflow is admin-only: the UI hides the radio for
2
+ non-admins and /tagger/start refuses (403) a non-admin asking for 'full'."""
3
+ import types
4
+
5
+ import pytest
6
+
7
+
8
+ ADMIN = '9939921144'
9
+
10
+
11
+ def test_can_approve_requires_admin_phone(app_mod):
12
+ with app_mod.app.test_request_context('/'):
13
+ app_mod.session['tagger_cid'] = 't1'
14
+ app_mod._il_store(phone=ADMIN)
15
+ assert app_mod._can_approve() is True
16
+ app_mod._il_store(phone='9000000000')
17
+ assert app_mod._can_approve() is False
18
+ app_mod._il_store(phone='')
19
+ assert app_mod._can_approve() is False
20
+
21
+
22
+ @pytest.mark.parametrize('page', ['tagger.html', 'tag.html'])
23
+ @pytest.mark.parametrize('can_approve', [True, False])
24
+ def test_full_approve_radio_gated(app_mod, page, can_approve):
25
+ ctx = {'can_approve': can_approve, 'tagger_ok': True, 'tagger_missing': '',
26
+ 'state_exists': True, 'state_age': 1, 'cms_user': 'u',
27
+ 'is_local': True, 'exams': ['NEET'], 'cms_connected': True}
28
+ with app_mod.app.test_request_context('/'):
29
+ html = app_mod.render_template(page, **ctx)
30
+ has_full = 'name="approval" value="full"' in html
31
+ has_under = 'name="approval" value="under_review"' in html
32
+ assert has_under is True
33
+ assert has_full is can_approve
34
+
35
+
36
+ def test_tagger_start_403_for_non_admin_full_approve(app_mod, monkeypatch, tmp_path):
37
+ excel = tmp_path / 'tag.xlsx'
38
+ excel.write_bytes(b'')
39
+ state = tmp_path / 'state.json'
40
+ state.write_text('{}')
41
+
42
+ stub = types.SimpleNamespace()
43
+ stub.load_excel = lambda _p: None
44
+ stub.get_records = lambda qids, df: ([{'question_id': 'ILQ-1', 'source_row': 1}], [])
45
+ monkeypatch.setattr(app_mod, 'cms_tagger', stub)
46
+ monkeypatch.setattr(app_mod, '_tagger_unavailable', lambda: None)
47
+ monkeypatch.setattr(app_mod, '_user_state_path', lambda: str(state))
48
+
49
+ with app_mod.app.test_request_context('/', json={'approval_mode': 'full'}):
50
+ app_mod.session['app_authed'] = True
51
+ app_mod.session['tagger_cid'] = 't-admin-gate'
52
+ job = app_mod._job()
53
+ job['_pending_excel'] = str(excel)
54
+ job['_pending_name'] = 'tag.xlsx'
55
+ resp, code = app_mod.tagger_start()
56
+ assert code == 403
57
+ assert 'restricted to the admin account' in resp.get_json()['error']
58
+
59
+ # An explicit non-admin phone gets the same 403.
60
+ with app_mod.app.test_request_context('/'):
61
+ app_mod._il_store(phone='9000000000')
62
+ with app_mod.app.test_request_context('/', json={'approval_mode': 'full'}):
63
+ app_mod.session['app_authed'] = True
64
+ app_mod.session['tagger_cid'] = 't-admin-gate-2'
65
+ job = app_mod._job()
66
+ job['_pending_excel'] = str(excel)
67
+ job['_pending_name'] = 'tag.xlsx'
68
+ resp, code = app_mod.tagger_start()
69
+ assert code == 403