Read who has opted into a contract / limit new optin and noop

Hi Algorand community,

I want Reject new optins if there are 2 accounts that have opted into a contract. And also limit noop to be performed only by who has opted in.

Can this be done?

I was thinking to read the accounts that have opted in, but I cannot find a way to do so, as instance the AlgoExplorer API doesnt return the accounts that have opted into a contract.

Any idea for an elegant solution here?

Thanks to @nullun for the reply on Discord:

if an application is on chain, you cannot prevent people form calling it, however you can validate an account has opted in before approving the noop or any other call

in Python:

Assert(App.optedIn(Txn.sender(), App.id()))