#1 2014-03-07 06:59:44

mingda
Member
Registered: 2013-01-04
Posts: 121

Two or more SQL Model how to use the same TAuthUser

I am learning, since TSQLHttpServer can have multiple TSQLRestServerDB, a TSQLRestServerDB can only has a TSQLMode, so if we have two or more TSQLRestServerDB, how can we use the Same TAuthUser and TAuthGroup?

function CreateModel: TSQLModel;
begin
  result := TSQLModel.Create([TSQLAuthGroup, TSQLAuthUser, TSQLTest], 'root');
end;

function CreateModel2: TSQLModel;
begin
  result := TSQLModel.Create([TSQLAuthGroup, TSQLAuthUser, TSQLTest2], 'root2');
end;

or make a sub class of TSQLAuthGroup and TSQLAuthUser like this:

function CreateModel2: TSQLModel;
begin
  result := TSQLModel.Create([TSQLAuthGroup2, TSQLAuthUser2, TSQLTest2], 'root2');
end;

this method a same user will need two entry, if more model, then more user entry, is there some method only has one entry for a user, the import thing is the AuthGroup's AccessRights, is it can set more than one Model's privilege? thanks!

Offline

#2 2014-03-07 09:52:14

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,231
Website

Re: Two or more SQL Model how to use the same TAuthUser

Accessing the same SQLite3 database from several TSQLRestServerDB is not a good idea, by design of the SQLite3 engine itself, which is followed by mORMot.

You can safely access external tables (e.g. in Oracle/PostgreSQL....) with several TSQLRestServerDB, but only if caching is disable, to avoid version mismatch.
It can be done for some particular tables, like TSQLAuthGroup, TSQLAuthUser.

Note also that the model shall be consistent when accessing the same databases, if you have TSQLRecordReference fields, or use security.
Those RecordReference properties uses the index of a class in the model to identify it in the DB.
And per-table CRUD security, as defined in TSQLAuthGroup rows, also uses the index of a class in the model to identify it.

Offline

#3 2014-03-07 12:38:58

mingda
Member
Registered: 2013-01-04
Posts: 121

Re: Two or more SQL Model how to use the same TAuthUser

Sorry, Ab, I not say clear, I mean external db, I know the order of TSQLRecord should not change, for this reason and organise, I think make more than one model to organise application, since these models all need authentication, then where put TAuthUser and TAuthGroup is a problem, demo code like below, is two model all need put TAuthGroup and TAutoUser or only one model need? thank you!

procedure TForm1.FormCreate(Sender: TObject);
begin
  fProps := TOleDBMSSQLConnectionProperties.Create('localhost', 'testdb', '', '');

  fModel := CreateModel;
  VirtualTableExternalRegisterAll(fModel, fProps);

  fDBServer := TSQLRestServerDB.Create(fModel, ChangeFileExt(paramstr(0),'.db3'), true);
  fDBServer.CreateMissingTables();

  fModel2 := CreateModel2;
  VirtualTableExternalRegisterAll(fModel2, fProps);

  fDBServer2 := TSQLRestServerDB.Create(fModel2, 'test2.db3', true);
  fDBServer2.CreateMissingTables();

  fHttpServer := TSQLHttpServer.Create('80', [fDBServer, fDBServer2], '+', useHttpApiRegisteringURI);
  fHttpServer.AccessControlAllowOrigin := '*';
end;

Offline

#4 2014-03-10 00:51:10

mingda
Member
Registered: 2013-01-04
Posts: 121

Re: Two or more SQL Model how to use the same TAuthUser

Later I think in some cases this mode is ideal for a site for different corporations, in one site, we can make root1(model1) for corporationA, root2(model2) for corporationB, in Model1 we can use sub-class TAuthGroup1 and TAuthUser1, in Model2 we can use sub-class TAuthGroup2 and TAuthUser2, then in one external DB, we can server two different corporation, great! If share and separate two mode all support, that would be great!

Offline

#5 2014-03-10 08:32:15

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,231
Website

Re: Two or more SQL Model how to use the same TAuthUser

You can use the same class in several models, without the need of inheriting it.

In fact, in revision 1.18, there are two classes in mORMot.pas to store the model information for each TSQLRecord:
- TSQLRecordProperties which is share by all models, which contain only common RTTI information;
- TSQLModelRecordProperties which has one instance per model, with the corresponding per-model information.
See the documentation of both classes.

You should better use it as external databases, but not SQLite3 instances, unless you share the same TSQLDBSQlite3Properties instance for all models.
Also ensure that caching is disable for those tables, or at least that there is a decent small timeout to allow replication of any modification in other models.

Offline

#6 2014-03-10 13:51:27

mingda
Member
Registered: 2013-01-04
Posts: 121

Re: Two or more SQL Model how to use the same TAuthUser

Thank you for your guidance, continue to read the documentation.

Offline

Board footer

Powered by FluxBB