Bill F
Bill F

Reputation: 2087

Setting the ACL when creating a new database via JS in XPages

I am creating a new database using JS from an action button. The following sequence happens:

If I open the Db the ACL looks and acts correctly except the -default- has an access level of Manager.

So I tried change my code to set the access level for -default- to Author in various ways, all resulting in an exception:

It seems that it does not recognize me as a manger in the ACL.

Q: How do I change the -default- access away from manager?

Upvotes: 1

Views: 314

Answers (1)

stwissel
stwissel

Reputation: 20384

Probably the easiest way to get a good default ACL is using a template database. All entries in your template that come in square brackets are copied into the new database as ACL entries.

So in your template you would have

  • [-Default-]=Author and [Anonymous]=No Access which results in
  • -Default-=Author and Anonymous=No Access in the new DB

Update Easier that it looks. You need to make sure to get the entries right...
Use this function:

function makeDB(dbName) {
   var server = database.getServer();
   var you = @UserName();
   var dbDir = session.getDbDirectory(server);
   var db = dbDir.createDatabase(dbName);
   var acl = db.getACL();
   acl.createACLEntry(you,6);
   if (server != "" && server != you) {
      acl.createACLEntry(server,6);
   }   
   var def = acl.getEntry("-Default-");
   def.setLevel(3);
   acl.save();
}

Then you call the function using:

makeDB("someFancyDBName");

In the function we make sure that you, who runs the script and the server where it runs are both in the ACL as managers (including the case of a local database, where in the Notes client the server is empty and in the web preview would be your user name).

Works like a charm. If it doesn't work for you, there are a few things to check:

  • Does it work on local (no server involved)?
  • Do you have errors on the console?
  • What access level do you have in the server ECL
  • Check the "max Internet access" of the database

The previous answer (obsolete):

Other than that it is a little trickier...

You create the new database:

   var db:Database = [... whatever you have todo here ...];
   var acl:Acl = db.getAcl();
   // Do whatever you do, don't touch -Default-
   acl.save();
   acl.recycle();
   var dbURL = db.getUrl(); // <-- off my head might be slightly different name
   db.recycle();

   // Now the real work
   var db2 = session.evaluate(dbUrl);
   var acl2 = db2.getAcl();

   // Change default here;

Typed off my head, contains typos.

This should work. Let us know how it goes

Upvotes: 2

Related Questions