Hi David,
The getNewXXX() methods provide you with an object with uninitialised
primary keys, since they haven't been written to the database. When
these objects are passed to TurbineSecurity.grant() it fails because of
the absent keys.
I've attached code that should do the trick.
J.
public void init( Object data )
throws InitializationException
{
super.init(data);
String password = new String ("blahin");
try
{
String ADMIN_PERMISSION="Admin";
String MSGUSER_PERMISSION="MsgUser";
String ADMIN_ROLE="Administrator";
String MSGUSER_ROLE="Message User";
String USER_NAME="Admin";
Permission adminPermission = null;
Permission msguserPermission = null;
Role adminRole = null;
Role msguserRole = null;
User adminUser = new TurbineUser();
Group rwtgroup=null;
try
{
rwtgroup=TurbineSecurity.getGlobalGroup();
}
catch(Exception e)
{
logError("Unable to get global group", e);
return;
}
try
{
adminPermission=TurbineSecurity.createPermission(ADMIN_PERMISSION);
}
catch (EntityExistsException eee1)
{
adminPermission=TurbineSecurity.getPermission(ADMIN_PERMISSION);
}
try
{
TurbineSecurity.addRole(adminRole);
adminRole=TurbineSecurity.getRole(ADMIN_ROLE);
}
catch (EntityExistsException e)
{
adminRole=TurbineSecurity.getRole(ADMIN_ROLE);
}
try
{
msguserPermission=TurbineSecurity.createPermission(MSGUSER_PERMISSION);
}
catch (EntityExistsException e)
{
msguserPermission=TurbineSecurity.getPermission(MSGUSER_PERMISSION);
}
try
{
TurbineSecurity.addRole(msguserRole);
msguserRole=TurbineSecurity.getRole(MSGUSER_ROLE);
}
catch (EntityExistsException e)
{
msguserRole=TurbineSecurity.getRole(MSGUSER_ROLE);
}
if (TurbineSecurity.accountExists(USER_NAME))
{
log("Administrator user already exists");
}
else
{
Date now = new Date();
adminUser.setUserName("Admin");
adminUser.setCreateDate(now);
adminUser.setLastLogin(new Date(0));
TurbineSecurity.addUser(adminUser, password);
}
adminUser = TurbineSecurity.getUser(USER_NAME);
try
{
TurbineSecurity.grant(adminUser, rwtgroup, adminRole);
log("New group and role granted to admin user");
}
catch(Exception e)
{
log("Unable to grant group and role to admin user.
This relationship
may already exist");
}
}
catch (Exception e)
{
logError("Unable to configure users", e);
}
}
>>>>>>>>>>>>>>>>>> Original Message <<<<<<<<<<<<<<<<<<
On 10/12/01, 3:35:26 AM, "David Wynter" <[EMAIL PROTECTED]> wrote
regarding Problem with Group object:
> Gents,
> 1. I am attempting to prime the database with one group and a couple of
> roles with one permission each. I do not need the full flux control over
> those entities. I have my own User page that uses a pull tool to get a
list
> of the roles that my users can play. The problem lies in setting up a
Group,
> it does not work and the subsequent TurbineSecurity.grant(...) fails. I
> assume because the Group I am trying to use is not there. The
DatabasePrimer
> runs as a TurbineBaseService. An excerpt from the code I use in
> DatabasePrimer is:
> ....
> log("Installing role, permission records into database");
> String password = new String ("blahin");
> try {
> Permission adminperm =
TurbineSecurity.getNewPermission("Admin");
> Permission msguserperm =
TurbineSecurity.getNewPermission("MsgUser");
> Role adminrole =
TurbineSecurity.getNewRole("Administrator");
> Role msguserrole =
TurbineSecurity.getNewRole("Message User");
> Group rwtgroup =
TurbineSecurity.getNewGroup("global"); <----here I tried
> 'Group.GLOBAL_GROUP_NAME' also
> User adminuser = new TurbineUser();
> try {
> TurbineSecurity.addPermission(adminperm);
> }
> catch (EntityExistsException eee1) {
> // It exists so that is good
> sLogger.debug("DatabasePrimer:
"+eee1.toString());
> }
> try {
> TurbineSecurity.addRole(adminrole);
> }
> catch (EntityExistsException eee2) {
> // It exists so that is good
> sLogger.debug("DatabasePrimer:
"+eee2.toString());
> }
> try {
> TurbineSecurity.addPermission(msguserperm);
> }
> catch (EntityExistsException eee3) {
> // It exists so that is good
> sLogger.debug("DatabasePrimer:
"+eee3.toString());
> }
> try {
> TurbineSecurity.addRole(msguserrole);
> }
> catch (EntityExistsException eee4) {
> // It exists so that is good
> sLogger.debug("DatabasePrimer:
"+eee4.toString());
> }
> if (TurbineSecurity.accountExists(username))
> {
> adminuser = TurbineSecurity.getUser(username);
> sLogger.debug("DatabasePrimer:
Administrator user already exists");
> }
> else
> {
> Date now = new Date();
> adminuser.setUserName("Admin");
> adminuser.setCreateDate(now);
> adminuser.setLastLogin(new Date(0));
> TurbineSecurity.addUser(adminuser, password);
> }
> // Assume that grant is benign if adminuser exists
with this role
> TurbineSecurity.grant(adminuser, rwtgroup,
adminrole);
> }
> catch (EntityExistsException eee) {
> etc.
> I get the error:
> DatabasePrimer: org.apache.turbine.util.security.DataBackendException:
> grant(User,Group,Role) failed: General error: Column 'GROUP_ID' cannot be
> null
> The method TurbineSecurity.getNewGroup("global") does not perform as
defined
> in the javadoc. According to the javadocs it should return the object
> associated with that name or if there is none then create one with that
> name. Any work around here.
> 2. I notice from the turbine.log that DatabasePrimer gets runs twice, one
> second apart, during initialisation. This is it's entry in
> turbineresources.properties.
>
services.DatabasePrimer.classname=com.roamware.rwtransform.services.Databa
se
> Primer
> Any reason for this?
> Thanks,
> David
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]