From 586968a5f142234da9f5c55085c9ce4c8ef82453 Mon Sep 17 00:00:00 2001
From: "Endi S. Dewata" <edewata@redhat.com>
Date: Wed, 7 May 2014 16:01:25 -0400
Subject: [PATCH] Fixed problem adding enabled TPS profile.

The profile, profile mapping, connector, and authenticator services
in TPS have been modified to allow adding enabled entries directly
if the user has the proper rights.

The authenticator database has been moved into the config package
for consistency.

Ticket #948
---
 .../cmscore/src/com/netscape/cmscore/dbs/CSCfgDatabase.java  |  5 -----
 .../src/org/dogtagpki/server/tps/TPSSubsystem.java           |  2 +-
 .../server/tps/{dbs => config}/AuthenticatorDatabase.java    |  7 ++-----
 .../server/tps/{dbs => config}/AuthenticatorRecord.java      |  2 +-
 .../org/dogtagpki/server/tps/config/ConnectorDatabase.java   |  2 +-
 .../src/org/dogtagpki/server/tps/config/ProfileDatabase.java |  2 +-
 .../dogtagpki/server/tps/config/ProfileMappingDatabase.java  |  2 +-
 .../org/dogtagpki/server/tps/rest/AuthenticatorService.java  | 12 ++++++++++--
 .../src/org/dogtagpki/server/tps/rest/ConnectorService.java  |  8 ++++++++
 .../org/dogtagpki/server/tps/rest/ProfileMappingService.java |  8 ++++++++
 .../src/org/dogtagpki/server/tps/rest/ProfileService.java    |  8 ++++++++
 11 files changed, 41 insertions(+), 17 deletions(-)
 rename base/tps-tomcat/src/org/dogtagpki/server/tps/{dbs => config}/AuthenticatorDatabase.java (95%)
 rename base/tps-tomcat/src/org/dogtagpki/server/tps/{dbs => config}/AuthenticatorRecord.java (95%)

diff --git a/base/server/cmscore/src/com/netscape/cmscore/dbs/CSCfgDatabase.java b/base/server/cmscore/src/com/netscape/cmscore/dbs/CSCfgDatabase.java
index 66cbe5134e3618416a3c73b56a387771cd18c1bd..4dee392e28dfd6420f5eaddb88da2713e906fa97 100644
--- a/base/server/cmscore/src/com/netscape/cmscore/dbs/CSCfgDatabase.java
+++ b/base/server/cmscore/src/com/netscape/cmscore/dbs/CSCfgDatabase.java
@@ -62,11 +62,6 @@ public class CSCfgDatabase<E extends CSCfgRecord> extends Database<E> {
         return configStore.getString("config." + substoreName + "." + recordID + ".state", "Disabled");
     }
 
-    public void createRecordStatus(String recordID, String status) throws EBaseException {
-        if (status == null || requiresApproval()) status = "Disabled";
-        setRecordStatus(recordID, status);
-    }
-
     public void setRecordStatus(String recordID, String status) throws EBaseException {
         configStore.put("config." + substoreName + "." + recordID + ".state", status);
         configStore.put("config." + substoreName + "." + recordID + ".timestamp",
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSSubsystem.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSSubsystem.java
index 30696bbdb4b815b455b257dff13d2ca97fafb66b..81aabe94020737bf4a559341778d231e3921e975 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSSubsystem.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/TPSSubsystem.java
@@ -19,12 +19,12 @@ package org.dogtagpki.server.tps;
 
 import org.dogtagpki.server.tps.authentication.AuthenticationManager;
 import org.dogtagpki.server.tps.cms.ConnectionManager;
+import org.dogtagpki.server.tps.config.AuthenticatorDatabase;
 import org.dogtagpki.server.tps.config.ConfigDatabase;
 import org.dogtagpki.server.tps.config.ConnectorDatabase;
 import org.dogtagpki.server.tps.config.ProfileDatabase;
 import org.dogtagpki.server.tps.config.ProfileMappingDatabase;
 import org.dogtagpki.server.tps.dbs.ActivityDatabase;
-import org.dogtagpki.server.tps.dbs.AuthenticatorDatabase;
 import org.dogtagpki.server.tps.dbs.TPSCertDatabase;
 import org.dogtagpki.server.tps.dbs.TokenDatabase;
 import org.dogtagpki.server.tps.engine.TPSEngine;
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/dbs/AuthenticatorDatabase.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/config/AuthenticatorDatabase.java
similarity index 95%
rename from base/tps-tomcat/src/org/dogtagpki/server/tps/dbs/AuthenticatorDatabase.java
rename to base/tps-tomcat/src/org/dogtagpki/server/tps/config/AuthenticatorDatabase.java
index 912207501fa0132b61f03d00f7b02089ab580fb6..292b80afbfa7679466bdbd7d6ef6491c30c77ff0 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/dbs/AuthenticatorDatabase.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/config/AuthenticatorDatabase.java
@@ -16,15 +16,12 @@
 // All rights reserved.
 // --- END COPYRIGHT BLOCK ---
 
-package org.dogtagpki.server.tps.dbs;
+package org.dogtagpki.server.tps.config;
 
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Map;
 
-import org.dogtagpki.server.tps.config.ConfigDatabase;
-import org.dogtagpki.server.tps.config.ConfigRecord;
-
 import com.netscape.certsrv.apps.CMS;
 import com.netscape.certsrv.base.EBaseException;
 import com.netscape.cmscore.dbs.CSCfgDatabase;
@@ -96,7 +93,7 @@ public class AuthenticatorDatabase extends CSCfgDatabase<AuthenticatorRecord> {
         configDatabase.addProperties(configRecord, authenticatorID, properties);
 
         // create status
-        createRecordStatus(authenticatorID, authenticatorRecord.getStatus());
+        setRecordStatus(authenticatorID, authenticatorRecord.getStatus());
 
         configDatabase.commit();
     }
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/dbs/AuthenticatorRecord.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/config/AuthenticatorRecord.java
similarity index 95%
rename from base/tps-tomcat/src/org/dogtagpki/server/tps/dbs/AuthenticatorRecord.java
rename to base/tps-tomcat/src/org/dogtagpki/server/tps/config/AuthenticatorRecord.java
index 7586dbb0562bc7c63737e12d77b6e44e98026aa4..937a25e51e6404b7bdd1026ae38d1aad03145839 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/dbs/AuthenticatorRecord.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/config/AuthenticatorRecord.java
@@ -16,7 +16,7 @@
 // All rights reserved.
 // --- END COPYRIGHT BLOCK ---
 
-package org.dogtagpki.server.tps.dbs;
+package org.dogtagpki.server.tps.config;
 
 import com.netscape.cmscore.dbs.CSCfgRecord;
 
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/config/ConnectorDatabase.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/config/ConnectorDatabase.java
index 6abbafa1dab1d13f8199f8bee70d15a0026c686c..313d8c901b6200165d0d4561faaefde4d630807f 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/config/ConnectorDatabase.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/config/ConnectorDatabase.java
@@ -95,7 +95,7 @@ public class ConnectorDatabase extends CSCfgDatabase<ConnectorRecord> {
         configDatabase.addProperties(configRecord, connectorID, properties);
 
         // create status
-        createRecordStatus(connectorID, connectorRecord.getStatus());
+        setRecordStatus(connectorID, connectorRecord.getStatus());
 
         configDatabase.commit();
     }
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/config/ProfileDatabase.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/config/ProfileDatabase.java
index 195580ff9376d99df89f3bdb92863459e60bc4d8..01225d309a74c5d9b0d7645f0757dfcac1ad55e0 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/config/ProfileDatabase.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/config/ProfileDatabase.java
@@ -94,7 +94,7 @@ public class ProfileDatabase extends CSCfgDatabase<ProfileRecord> {
         configDatabase.addProperties(configRecord, profileID, properties);
 
         // create status
-        createRecordStatus(profileID, profileRecord.getStatus());
+        setRecordStatus(profileID, profileRecord.getStatus());
 
         configDatabase.commit();
     }
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/config/ProfileMappingDatabase.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/config/ProfileMappingDatabase.java
index 8310abe825535a59a2fdd547956b05b9053f587c..67a21d6ac349e39fc52e06feded1fdeedfd1e5d0 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/config/ProfileMappingDatabase.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/config/ProfileMappingDatabase.java
@@ -94,7 +94,7 @@ public class ProfileMappingDatabase extends CSCfgDatabase<ProfileMappingRecord>
         configDatabase.addProperties(configRecord, profileMappingID, properties);
 
         // create status
-        createRecordStatus(profileMappingID, profileMappingRecord.getStatus());
+        setRecordStatus(profileMappingID, profileMappingRecord.getStatus());
 
         configDatabase.commit();
     }
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/rest/AuthenticatorService.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/rest/AuthenticatorService.java
index 782570d8d99fed3bc96ecb3a5e34afc76421c8a8..d862e261dc9938d83133490ceccc2b9a28fbecc8 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/rest/AuthenticatorService.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/rest/AuthenticatorService.java
@@ -33,8 +33,8 @@ import javax.ws.rs.core.Response;
 import javax.ws.rs.core.UriInfo;
 
 import org.dogtagpki.server.tps.TPSSubsystem;
-import org.dogtagpki.server.tps.dbs.AuthenticatorDatabase;
-import org.dogtagpki.server.tps.dbs.AuthenticatorRecord;
+import org.dogtagpki.server.tps.config.AuthenticatorDatabase;
+import org.dogtagpki.server.tps.config.AuthenticatorRecord;
 import org.jboss.resteasy.plugins.providers.atom.Link;
 
 import com.netscape.certsrv.apps.CMS;
@@ -180,6 +180,14 @@ public class AuthenticatorService extends PKIService implements AuthenticatorRes
             TPSSubsystem subsystem = (TPSSubsystem)CMS.getSubsystem(TPSSubsystem.ID);
             AuthenticatorDatabase database = subsystem.getAuthenticatorDatabase();
 
+            String status = authenticatorData.getStatus();
+            Principal principal = servletRequest.getUserPrincipal();
+
+            if (status == null || database.requiresApproval() && !database.canApprove(principal)) {
+                // if status is unspecified or user doesn't have rights to approve, the entry is disabled
+                authenticatorData.setStatus("Disabled");
+            }
+
             database.addRecord(authenticatorData.getID(), createAuthenticatorRecord(authenticatorData));
             authenticatorData = createAuthenticatorData(database.getRecord(authenticatorData.getID()));
 
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/rest/ConnectorService.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/rest/ConnectorService.java
index d77c793494845c500c798453ddc5776bb6155a6d..c281265effa3602aa357a623fcd3891727f74eee 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/rest/ConnectorService.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/rest/ConnectorService.java
@@ -180,6 +180,14 @@ public class ConnectorService extends PKIService implements ConnectorResource {
             TPSSubsystem subsystem = (TPSSubsystem)CMS.getSubsystem(TPSSubsystem.ID);
             ConnectorDatabase database = subsystem.getConnectorDatabase();
 
+            String status = connectorData.getStatus();
+            Principal principal = servletRequest.getUserPrincipal();
+
+            if (status == null || database.requiresApproval() && !database.canApprove(principal)) {
+                // if status is unspecified or user doesn't have rights to approve, the entry is disabled
+                connectorData.setStatus("Disabled");
+            }
+
             database.addRecord(connectorData.getID(), createConnectorRecord(connectorData));
             connectorData = createConnectorData(database.getRecord(connectorData.getID()));
 
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/rest/ProfileMappingService.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/rest/ProfileMappingService.java
index fca225c9fcf1cff41e441bcf26abf8a6b5965e21..f3a6f2e38ce14f74001cdb9fecf2c1880c2f8c4d 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/rest/ProfileMappingService.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/rest/ProfileMappingService.java
@@ -176,6 +176,14 @@ public class ProfileMappingService extends PKIService implements ProfileMappingR
             TPSSubsystem subsystem = (TPSSubsystem)CMS.getSubsystem(TPSSubsystem.ID);
             ProfileMappingDatabase database = subsystem.getProfileMappingDatabase();
 
+            String status = profileMappingData.getStatus();
+            Principal principal = servletRequest.getUserPrincipal();
+
+            if (status == null || database.requiresApproval() && !database.canApprove(principal)) {
+                // if status is unspecified or user doesn't have rights to approve, the entry is disabled
+                profileMappingData.setStatus("Disabled");
+            }
+
             database.addRecord(profileMappingData.getID(), createProfileMappingRecord(profileMappingData));
             profileMappingData = createProfileMappingData(database.getRecord(profileMappingData.getID()));
 
diff --git a/base/tps-tomcat/src/org/dogtagpki/server/tps/rest/ProfileService.java b/base/tps-tomcat/src/org/dogtagpki/server/tps/rest/ProfileService.java
index 9724938d6d329933cdf4503df077c8492bc18552..e5bfd4663827e7bded285c5c11b3ab0426abe40b 100644
--- a/base/tps-tomcat/src/org/dogtagpki/server/tps/rest/ProfileService.java
+++ b/base/tps-tomcat/src/org/dogtagpki/server/tps/rest/ProfileService.java
@@ -180,6 +180,14 @@ public class ProfileService extends PKIService implements ProfileResource {
             TPSSubsystem subsystem = (TPSSubsystem)CMS.getSubsystem(TPSSubsystem.ID);
             ProfileDatabase database = subsystem.getProfileDatabase();
 
+            String status = profileData.getStatus();
+            Principal principal = servletRequest.getUserPrincipal();
+
+            if (status == null || database.requiresApproval() && !database.canApprove(principal)) {
+                // if status is unspecified or user doesn't have rights to approve, the entry is disabled
+                profileData.setStatus("Disabled");
+            }
+
             database.addRecord(profileData.getID(), createProfileRecord(profileData));
 
             profileData = createProfileData(database.getRecord(profileData.getID()));
-- 
1.8.4.2

