The Security dialog in Stonefield Query is used to define users and roles and what permissions roles have. However, you can also set up security programmatically if you wish.

First, see the information in the Using Stonefield Query from Other Applications topic for details on how to instantiate and set up the Application object. Then you can use code as shown below.

Adding roles

To add a role, add an IRole object to the Security's Roles collection. To save the new role to the Roles security table, call SaveItem. For example:

IRole role = _app.Security.Roles["My Role"];
if (role == null)
{
  IRole role = _app.Security.Roles.New["My Role"];
  _app.Security.Roles.SaveItem(role);
}

Adding users

To add a user, add an IUser object to the Security's Users collection. To save the new user to the Users security table, call SaveItem. For example:

IUser user = _app.Security.Users["My User Name"];
if (user == null)
{
  IUser user = _app.Security.Users.New["My User Name"];
  user.Password = "my password";
  user.LicenseType = LicenseType.ReportDesigner;
  IRole role = _app.Security.Roles["My Role"];
  user.Roles.Add(role);
  _app.Security.Users.SaveItem(user);
}

Reports

To define the security for a report, add IPermission objects to the report's Permissions collection. For example:

IRole role = _app.Security.Roles["My Role"];
IReport report = _app.ReportEngine.Reports["My Report"];
report.Permissions.Add(new Permission(role, AccessRights.Full));

Data group

To define the security for a data group, call the data group's AddPermssion method. For example:

IRole role = _app.Security.Roles["My Role"];
IDataGroup dg = _app.DataDictionary.DataGroups["My DataGroup"];
dg.AddPermission(new Permission(role, AccessRights.Full));

Data source

To define the security for a data source, add IRole objects to the data source's Roles collection. For example:

IRole role = _app.Security.Roles["My Role"];
IDataSource ds = _app.ConnectionManager.DataSources["My Datasource"];
ds.Roles.Add(role);

Resources

To define the security for a resource, call the SetResourcePermission method of the Security object. For example:

IRole role = _app.Security.Roles["My Role"];
_app.Security.SetResourcePermission("TagEditorAccess", role,
    AccessRights.Full);
_app.Security.SetResourcePermission("SchedulerAccess", role,
    AccessRights.Full);