Toad World® Forums

How to change, using scripts, entity's permissions?

Using scripts could i change entity permissions, adding new permissions to each table?

I try:

function main()

{
var app = System.GetInterface(‘Application’);
var Model = app.Models.GetObject(0);
var e, a, b, c, Entity, Perm;

for (e=0; e
{
Entity = Model.Entities.GetObject(e);
for (a=0; a
{
Log.Information("Entity "+Entity.Name);
Perm = Entity.InstanceUserGroupRelations.GetObject(a);
Log.Information("Perm "+Perm.FullName);
for (b=0; b
{
Right = Perm.Rights.GetObject(b);
if (!Right.GrantOption)
{
Log.Information("Right “+Right.FullName + " granted”);
}
}
}
}
}

So I can read all entity’s permission, but How I can add one?
I try:

Perm = Entity.CreateNewObject( 1020 );

but the I receive the error:
TPEREntity can’t create object of type 2010.

Please help.

Hi,

you can use method Model.AddUserRight(Entity, User, ‘SELECT’, ‘Grant’)
Our scripting specialist Mario prepared example for you:

function main()
{
var app = System.GetInterface(‘Application’);
var Model = app.Models.GetObject(0);
var i, Entity, User;

User = GetUser(Model, “User1”);
for(i=0; i<Model.Entities.Count;i++)
{
Entity = Model.Entities.GetObject(i);
Model.AddUserRight(Entity, User, ‘SELECT’, ‘Grant’);
}
}

function GetUser(Model, UserName)
{
var User = Model.Users.GetObjectByName(UserName);
if (User != null)
return Model.Users.GetObjectByName(UserName);
else
{
var User = Model.CreateNewObject(1015); //new User
User.Caption = UserName;
User.Name = UserName;
return User;
}
}

Regards,

Vaclav & Mario

Hello.

Many thanks for your reply.

The script generates an error
Object doesn’t support this property or method
at line
Model.AddUserRight(Entity, User, ‘SELECT’, ‘Grant’);

I think it can depends on the specific database model. My model is for
PostGreSQL 8.3 (and not Oracle).
I wish to add permission to a user group and not a user.

Thanks in advance for your help.

P.S.: For what I understand, Toad Data Modeler is a wonderful software with
incredible scripting enhancements. But is there a comprehensive
documentation to make TDM script programming something less esoteric than
Brainfuck (http://en.wikipedia.org/wiki/Brainfuck) ?

Thanks, Fabrizio Verdesca

Hi Fabrizio,

you have older version of TDM. The current version is 3.6, please update to the latest version and the script will work.

In PostgreSQL you need to modify the GetUser function this way:

function GetUser(Model, UserName)
{
var User = Model.UserGroups.GetObjectByName(UserName);
if (User != null)
return Model.UserGroups.GetObjectByName(UserName);
else
{
var User = Model.CreateNewObject(1017); //new UserGroup
User.Caption = UserName;
User.Name = UserName;
return User;
}
}

Re documentation: the scripting area is very large and writing detailed documentation would take too long, that’s why we prefer a communication via this community. Write us what you want and we will explain you how to achieve your goal :slight_smile: BTW esoteric languages are really nice, but I hope our scripting (which uses JScript significantly) is not that complicated :wink:

Following on from this… how do you then set the grantor? I have looked at the reference guide and as far as I can tell the following should work but the grantor is appearing as “-- None --”

function main() {
var app = System.GetInterface(‘Application’);
var Model = app.Models.GetObject(0);
var LoadWarehouseRole = Model.UserGroups.GetObjectByName(‘LOAD_WAREHOUSE’);
var FactOwner = Model.Users.GetObjectByName(‘MCN_DATA’);

Model.Lock();
for (e=0; e
    Entity = Model.Entities.GetObject(e);
    Model.AddUserRight(Entity, LoadWarehouseRole, 'INSERT','Grant',FactOwner);
    Model.AddUserRight(Entity, LoadWarehouseRole, 'UPDATE','Grant',FactOwner);
}
Model.UnLock();

}