@davoodmehraban89 / Factor-easy-2 Mirror · read-only / commits / 2ffe5b5
Fork 0

سامانه صدور فاکتور و حسابداری

Mirrored from https://github.com/davoodmehraban89/Factor-easy.git

Synced Dec 3, 14:30 UTC

Fix stale admin password reset dialog results and duplicate submissions

JVK committed Oct 30, 2026 at 09:37 UTC 2ffe5b527fdeea28a8608a1d176c14f66da1064b
3 files changed +68 -5
PROJECT_STATE.md
+9
@@ -170,3 +170,12 @@
170 - Owner requests direct assignment of a forgotten password from the central users/licenses panel. This isolated branch `fix/admin-central-password-reset-20261028` is based on protected main `c19a3590674a65a0857576c1093ef17ab3119df8`. The existing global platform role `profiles.role='admin'` is the sole direct-reset authority; company managers remain recovery-link-only because a user's password applies across organizations.
171 - **Implemented and verified locally:** handler tests **49/49 PASS**, central reset UI and existing office/Pages browser suite **28/28 PASS**, admin provisioning contract, JavaScript syntax checks, and `git diff --check` PASS. Synthetic tests click the central row button, validate strong matching values, call the server once, clear both fields, reject organization owners, and verify provider errors never become false success or leak details. No real password/account was changed. Upstream Supabase documents the server-side `auth.admin.updateUserById(..., { password })` operation at https://supabase.com/docs/reference/javascript/auth-admin-updateuserbyid; the changelog scan found no applicable Auth breaking change.
172 - **NOT VERIFIED / release gate:** production still runs the prior `admin-user-management` function. Do not deploy the function or dependent UI until the owner separately approves production Auth activation. Real account reset and post-deployment production UI are NOT VERIFIED.
173 +
174 +
175 +### 2026-10-30 — Owner continuation: company Edit and central password assignment
176 +- Owner asks to verify Settings/Companies Edit, continue central software-admin password assignment, identify related unfinished work and list required integrations. Remote main c19a359 and PR188 head0615dea independently checked; company adapter is present in the live factoreasy.ir foundation bundle and the unsupported expression is absent. PR188 exact-head required checks pass; production backend activation remains unverified and unavailable in this session.
177 +- GitHub CLI authenticated access works. Plugin discovery reports GitHub and TinyFish installed, but neither exposes connector tools in this session; Supabase is not installed/connected and was suggested. Resend was used historically through a browser; plugin search returned no result. Cloud environment is available.
178 +- Fresh local handler49/49 and admin provisioning contract PASS. Focused synthetic source/generated Pages tests running. Review found a pending password-request dialog race: close user A, reopen for B, then A response falsely reports success in B dialog and clears B inputs. A scoped fix/regression is being implemented before release. No production account/password/financial operation performed.
179 +- Verified baseline synthetic browser15/15 PASS including real company Edit/Save clicks on source and generated Pages, logout/reload and settings preservation. Live bundle independently confirms shipped company repair; actual owner-account click remains unverified. Static quality, finance integrity and release/QA safety PASS.
180 +- Added dialog-generation and single-pending-operation guards to central reset after3 new regressions reproduced stale response attribution and duplicate submission on unchanged PR188. Server target authorization is unchanged. PR137 company isolation and PR134 expense/income corrections are both currently CONFLICTING; navigation174 and real-account settings178 remain separate open work.
181 +- Final scoped verification: all16 password-admin browser tests PASS; all3 new regressions also PASS against freshly generated strict-CSP Pages. Handler49/49 and provisioning/quality checks PASS. No production backend deployment, real password reset, mail or financial operation. Next: verify new PR188 CI, reconnect Supabase, inspect backend version, then complete backend-first approved activation/release; real-account acceptance stays distinct.
js/admin.js
+20 -5
@@ -75,12 +75,16 @@ function refreshAdminPanel(){renderAdminPanel();}
75 };
76 })();
77 let adminPasswordResetTarget='';
78 +let adminPasswordResetEpoch=0;
79 +let adminPasswordResetPending=false;
80 function adminOpenPasswordReset(userId){
81 if(!currentUser||currentUser.role!=='admin')return;
82 const uuid=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
83 const target=adminUsersCache.find(user=>user.id===userId);
84 if(!target||!uuid.test(userId))return;
85 + adminPasswordResetEpoch++;
86 adminPasswordResetTarget=userId;
87 + document.getElementById('admin-reset-password-submit').disabled=adminPasswordResetPending;
88 document.getElementById('admin-password-reset-user').textContent=target.email||target.full_name||'کاربر انتخاب‌شده';
89 document.getElementById('admin-reset-password').value='';
90 document.getElementById('admin-reset-password-confirm').value='';
@@ -89,6 +93,7 @@ function adminOpenPasswordReset(userId){
93 document.getElementById('admin-reset-password').focus();
94 }
95 function adminClosePasswordReset(){
96 + adminPasswordResetEpoch++;
97 document.getElementById('admin-reset-password').value='';
98 document.getElementById('admin-reset-password-confirm').value='';
99 document.getElementById('admin-password-reset-status').textContent='';
@@ -96,7 +101,9 @@ function adminClosePasswordReset(){
101 adminPasswordResetTarget='';
102 }
103 async function adminSetPassword(){
99 - if(!currentUser||currentUser.role!=='admin'||!adminPasswordResetTarget)return;
104 + if(adminPasswordResetPending||!currentUser||currentUser.role!=='admin'||!adminPasswordResetTarget)return;
105 + const epoch=adminPasswordResetEpoch;
106 + const isCurrentDialog=()=>epoch===adminPasswordResetEpoch;
107 const targetId=adminPasswordResetTarget;
108 const passwordInput=document.getElementById('admin-reset-password');
109 const confirmInput=document.getElementById('admin-reset-password-confirm');
@@ -110,14 +117,22 @@ async function adminSetPassword(){
117 const target=adminUsersCache.find(user=>user.id===adminPasswordResetTarget);
118 if(!target)return;
119 if(!confirm('رمز ورود '+(target.email||target.full_name||'این کاربر')+' با رمز جدید جایگزین شود؟'))return;
120 + adminPasswordResetPending=true;
121 button.disabled=true;status.textContent='در حال ثبت امن رمز جدید...';
122 passwordInput.value='';confirmInput.value='';
123 try{
124 const {data,error}=await sb.functions.invoke('admin-user-management',{body:{action:'set_password',user_id:targetId,password}});
117 - if(error||!data?.ok){status.textContent=await adminPasswordResetErrorMessage(data,error);return;}
118 - status.textContent='رمز جدید برای این کاربر ثبت شد؛ آن را فقط از مسیر امن در اختیارش بگذارید.';
119 - }catch(_error){status.textContent='نتیجهٔ تغییر رمز مشخص نیست؛ پیش از تلاش دوباره وضعیت حساب را بررسی کنید.';}
120 - finally{button.disabled=false;passwordInput.value='';confirmInput.value='';}
125 + if(error||!data?.ok){
126 + const message=await adminPasswordResetErrorMessage(data,error);
127 + if(isCurrentDialog())status.textContent=message;
128 + return;
129 + }
130 + if(isCurrentDialog())status.textContent='رمز جدید برای این کاربر ثبت شد؛ آن را فقط از مسیر امن در اختیارش بگذارید.';
131 + }catch(_error){if(isCurrentDialog())status.textContent='نتیجهٔ تغییر رمز مشخص نیست؛ پیش از تلاش دوباره وضعیت حساب را بررسی کنید.';}
132 + finally{
133 + adminPasswordResetPending=false;button.disabled=false;
134 + if(isCurrentDialog()){passwordInput.value='';confirmInput.value='';}
135 + }
136 }
137 async function adminPasswordResetErrorMessage(data,error){
138 let code=String(data?.error||'');
scripts/password-admin-ui.spec.js
+39
@@ -188,3 +188,42 @@ test('a delegated core manager can assist an active member independently of paid
188 await button.click();
189 await expect.poll(()=>page.evaluate(()=>window.__recoveryCalls)).toEqual([{name:'admin-user-management',body:{action:'send_recovery',user_id:TARGET,organization_id:ORG}}]);
190 });
191 +
192 +for(const outcome of ['success','failure'])test('central pending '+outcome+' cannot alter a reopened password dialog',async({page})=>{
193 + await managerFixture(page);
194 + await page.evaluate(async({OTHER})=>{
195 + navigateShell('settings','view-admin');await renderAdminPanel();
196 + adminUsersCache.push({id:OTHER,email:'other@example.invalid',role:'user'});
197 + sb.functions.invoke=(name,{body})=>{window.__recoveryCalls.push({name,body});return new Promise(resolve=>{window.__finishPasswordReset=resolve})};
198 + },{OTHER});
199 + await page.locator('[data-reset-user="'+TARGET+'"]').click();
200 + await page.locator('#admin-reset-password').fill('First!StrongPassword123');
201 + await page.locator('#admin-reset-password-confirm').fill('First!StrongPassword123');
202 + await page.locator('#admin-reset-password-submit').click();
203 + await page.locator('#modal-admin-reset-password button').filter({hasText:'بستن'}).click();
204 + await page.evaluate(({OTHER})=>adminOpenPasswordReset(OTHER),{OTHER});
205 + await page.locator('#admin-reset-password').fill('Second!StrongPassword123');
206 + await page.locator('#admin-reset-password-confirm').fill('Second!StrongPassword123');
207 + await page.evaluate(outcome=>window.__finishPasswordReset(outcome==='success'?{data:{ok:true},error:null}:{data:null,error:{message:'failed'}}),outcome);
208 + await expect(page.locator('#admin-reset-password-submit')).toBeEnabled();
209 + await expect(page.locator('#admin-password-reset-user')).toHaveText('other@example.invalid');
210 + await expect(page.locator('#admin-password-reset-status')).toBeEmpty();
211 + await expect(page.locator('#admin-reset-password')).toHaveValue('Second!StrongPassword123');
212 + await expect(page.locator('#admin-reset-password-confirm')).toHaveValue('Second!StrongPassword123');
213 + expect(await page.evaluate(()=>window.__recoveryCalls)).toHaveLength(1);
214 +});
215 +
216 +test('central password action rejects another submission while its request is pending',async({page})=>{
217 + await managerFixture(page);
218 + await page.evaluate(async({TARGET})=>{
219 + await renderAdminPanel();adminOpenPasswordReset(TARGET);
220 + sb.functions.invoke=(name,{body})=>{window.__recoveryCalls.push({name,body});return new Promise(resolve=>{window.__finishPasswordReset=resolve})};
221 + for(let i=0;i<2;i++){
222 + document.getElementById('admin-reset-password').value='Strong!Password123';
223 + document.getElementById('admin-reset-password-confirm').value='Strong!Password123';
224 + adminSetPassword();
225 + }
226 + },{TARGET});
227 + expect(await page.evaluate(()=>window.__recoveryCalls)).toHaveLength(1);
228 + await page.evaluate(()=>window.__finishPasswordReset({data:{ok:true},error:null}));
229 +});