Deploy a stateful contract through a stateful contract

Is it possible to save a stateful contract template inside a stateful contract so that a new stateful contract is created when a certain event occurs?
Assuming we have two types of smart contracts:
Stateful A: it manages a list of whitelisted accounts and allows them to create type B smart contracts
Stateful B: a generic smart contract representing (for example) a survey.
Is there a way to only allow whitelisted accounts to create a survey based on stateful B and thus ensure that the newly created stateful B was allowed by stateful A and not created by any account?
Maybe there is some better solution that doesn’t need to deploy stateful B through stateful A, that’s what I came up with.

Why not do something like what is shown in this example: beaker/examples/c2c at master · algorand-devrel/beaker · GitHub

It depends how the template looks like.
If you’re talking about a survey, it looks like it can be defined by a list of questions/answers.
Then, in that case, I don’t think you need multiple smart contracts.
Smart contract A can just also handle the survey.
There should be a special method creating the survey, and store the parameters of the survey in a “box” (used to create as many surveys as you want).

You can also still have two smart contracts: A is managing the whitelisted list of account and a single B is managing all the surveys.

Where this would not work is if you need to allow adding complex logic to B depending on the survey.
However, in that case, I don’t think you will manage to design a template that would be reasonable (in the sense that the template really just allows for surveys and does not allow for more — it may not be a security concern though, in which case you don’t need to enforce any template).

The example of the surveys is because I would like to manage this type of activity.
In my scenario there are two types of users: users who are allowed to create surveys and users who are allowed to vote on the specific survey.
Currently I’ve designed a stateful contract that manages a single survey. This smart contract stores the options to be voted in global variables and some data such as the initial and the final round. It also contains the logic to add the choices to vote and allow users to vote. Voting permission is managed with tokens.
Now I would like manage a whitelisted list of users who can create a new survey (keeping the stateful above as template) in order to distinguish legit surveys from those created by any other user.
I decided to deploy a stateful contract for each survey to manage multiple surveys at the same time and to make all operations by users (both creators and voters) gas free. So transactions are created and signed by a central account while the survey checks messages signed by users to approve these transactions.

How do you make operations “gas free” (which I guess mean that they don’t cost anything)? Do you have an associated stateless smart signature account?

If this is how you do it, using a single big smart contract B for all surveys (and storing the survey parameters in boxes instead of global parameters) should work.

No I don’t use a stateless smart contract. When the user wants to create a transaction (survey or vote) he concatenate all the parameters creating a message and signs it. Then the transaction is composed and signed by the central account using the user parameters in the appropriate fields (application args, accounts and assets) and in addition the message and the user signature. Finally, the smart contract uses the parameters passed only if concatenated they make up the message and if the message has been signed by the user.

What do you mean by storing parameters in boxes instead of using global parameters?

That would also work.

Boxes are a new way to store unlimited global storage:

They are not yet on MainNet/TestNet but should soon be (the upgrade has been released, we now need to wait for consensus upgrade).