[Registry-dev] svn commit r9380 - in trunk/registry/modules/core/src: main/java/org/wso2/registry/secure main/java/org/wso2/registry/servlet test/java/org/wso2/registry/jdbc test/java/org/wso2/registry/secure

svn at wso2.org svn at wso2.org
Thu Nov 1 04:25:28 PDT 2007


Author: dimuthul
Date: Thu Nov  1 04:25:17 2007
New Revision: 9380

Modified:
   trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/HSQLDBInitializer.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/RegistryRealmFactory.java
   trunk/registry/modules/core/src/main/java/org/wso2/registry/servlet/RegistryServlet.java
   trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/JDBCRegistryTest.java
   trunk/registry/modules/core/src/test/java/org/wso2/registry/secure/SecureRegistryTest.java
Log:
Fixing the Registry to use the UserManager correctly


Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/HSQLDBInitializer.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/HSQLDBInitializer.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/HSQLDBInitializer.java	Thu Nov  1 04:25:17 2007
@@ -20,6 +20,8 @@
 import org.apache.commons.logging.LogFactory;
 import org.wso2.registry.RegistryException;
 import org.wso2.registry.jdbc.DatabaseConstants;
+import org.wso2.usermanager.UserManagerException;
+import org.wso2.usermanager.readwrite.DefaultDatabaseUtil;
 
 import java.sql.*;
 
@@ -27,24 +29,6 @@
 
     private static final Log log = LogFactory.getLog(RegistryRealmFactory.class);
 
-    public static final String UM_USERS_TABLE = "create table um_users (id varchar(255) not null, user_name varchar(255) not null, password varchar(255) not null, primary key (id), unique(user_name));";
-    public static final String UM_USER_ATTRIBUTES = "create table um_user_attributes (id varchar(255) not null, attr_name varchar(255) not null, attr_value varchar(255), user_id varchar(255) not null, primary key (id));";
-    public static final String UM_ROLES_TABLE = "create table um_roles (id varchar(255) not null, role_name varchar(255) not null, primary key (id), unique(role_name));";
-    public static final String UM_ROLE_ATTRIBUTES_TABLE = "create table um_role_attributes (id varchar(255) not null, attr_name varchar(255) not null, attr_value varchar(255), role_id varchar(255) not null, primary key (id));";
-    public static final String UM_PERMISSIONS_TABLE = "create table um_permissions (id varchar(255) not null, resource_id varchar(255) not null, action varchar(255) not null, primary key (id));";
-    public static final String UM_ROLE_PERMISSIONS_TABLE = "create table um_role_permissions (id varchar(255) not null, permission_id varchar(255) not null, is_allowed smallint not null, role_id varchar(255) not null, primary key (id));";
-    public static final String UM_USER_PERMISSIONS_TABLE = "create table um_user_permissions (id varchar(255) not null, is_allowed smallint not null, permission_id varchar(255) not null, user_id varchar(255) not null, primary key (id));";
-    public static final String UM_USER_ROLES_TABLE = "create table um_user_roles (role_id varchar(255) not null, user_id varchar(255) not null, primary key (user_id, role_id));";
-
-    public static final String ALTER_ROLE_ATTRIBUTES = "alter table um_role_attributes add constraint FKD869ED19F08E7776 foreign key (role_id) references um_roles ON DELETE CASCADE;";
-    public static final String ALTER_ROLE_PERMISSIONS_1 = "alter table um_role_permissions add constraint FK8ADF2902918A81D6 foreign key (permission_id) references um_permissions ON DELETE CASCADE;";
-    public static final String ALTER_ROLE_PERMISSIONS_2 = "alter table um_role_permissions add constraint FK8ADF2902F08E7776 foreign key (role_id) references um_roles ON DELETE CASCADE;";
-    public static final String ALTER_USER_ATTRIBUTES = "alter table um_user_attributes add constraint FK4BE38AA49B0BBA16 foreign key (user_id) references um_users ON DELETE CASCADE;";
-    public static final String ALTER_USER_PERMISSIONS_1 = "alter table um_user_permissions add constraint FK86993CD7918A81D6 foreign key (permission_id) references um_permissions ON DELETE CASCADE;";
-    public static final String ALTER_USER_PERMISSIONS_2 = "alter table um_user_permissions add constraint FK86993CD79B0BBA16 foreign key (user_id) references um_users ON DELETE CASCADE;";
-    public static final String ALTER_USER_ROLES_1 = "alter table um_user_roles add constraint FKFCA65150A3D254B4 foreign key (user_id) references um_users ON DELETE CASCADE;";
-    public static final String ALTER_USER_ROLES_2 = "alter table um_user_roles add constraint FKFCA65150F08E7776 foreign key (role_id) references um_roles ON DELETE CASCADE;";
-
     public void createHSQLTables() throws RegistryException {
 
         try {
@@ -80,29 +64,14 @@
         }
 
         try {
-            Statement s = conn.createStatement();
-
-            s.executeUpdate(UM_USERS_TABLE);
-            s.executeUpdate(UM_USER_ATTRIBUTES);
-            s.executeUpdate(UM_ROLES_TABLE);
-            s.executeUpdate(UM_ROLE_ATTRIBUTES_TABLE);
-            s.executeUpdate(UM_PERMISSIONS_TABLE);
-            s.executeUpdate(UM_ROLE_PERMISSIONS_TABLE);
-            s.executeUpdate(UM_USER_PERMISSIONS_TABLE);
-            s.executeUpdate(UM_USER_ROLES_TABLE);
-
-            s.executeUpdate(ALTER_ROLE_ATTRIBUTES);
-            s.executeUpdate(ALTER_ROLE_PERMISSIONS_1);
-            s.executeUpdate(ALTER_ROLE_PERMISSIONS_2);
-            s.executeUpdate(ALTER_USER_ATTRIBUTES);
-            s.executeUpdate(ALTER_USER_PERMISSIONS_1);
-            s.executeUpdate(ALTER_USER_PERMISSIONS_2);
-            s.executeUpdate(ALTER_USER_ROLES_1);
-            s.executeUpdate(ALTER_USER_ROLES_2);
             
-            s.close();
-
-        } catch (SQLException e) {
+           DefaultDatabaseUtil.createDatabase(conn);
+       
+        }catch (UserManagerException e) {
+            String msg = "Could not create tables for user manager";
+            log.fatal(msg, e);
+            throw new RegistryException(msg);
+        }catch (SQLException e) {
             String msg = "Could not create tables for the User Manager.";
             log.fatal(msg, e);
             throw new RegistryException(msg);

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/RegistryRealmFactory.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/RegistryRealmFactory.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/secure/RegistryRealmFactory.java	Thu Nov  1 04:25:17 2007
@@ -33,14 +33,13 @@
 
     private static final Log log = LogFactory.getLog(RegistryRealmFactory.class);
 
-    public Realm newInMemoryRegistryRealm(String sqlFilePath) throws RegistryException {
+    public Realm newInMemoryRegistryRealm() throws RegistryException {
 
         new HSQLDBInitializer().createHSQLTables();
-        return newRegistryRealm(sqlFilePath,
-                UserManagerConstants.HSQL_DB_URL, DatabaseConstants.HSQL_DRIVER_NAME);
+        return newRegistryRealm(UserManagerConstants.HSQL_DB_URL, DatabaseConstants.HSQL_DRIVER_NAME);
     }
 
-    public Realm newRegistryRealm(String sqlFilePath, String dbURL, String driver)
+    public Realm newRegistryRealm(String dbURL, String driver)
             throws RegistryException {
 
         try {            

Modified: trunk/registry/modules/core/src/main/java/org/wso2/registry/servlet/RegistryServlet.java
==============================================================================
--- trunk/registry/modules/core/src/main/java/org/wso2/registry/servlet/RegistryServlet.java	(original)
+++ trunk/registry/modules/core/src/main/java/org/wso2/registry/servlet/RegistryServlet.java	Thu Nov  1 04:25:17 2007
@@ -69,11 +69,11 @@
 
             // get the database config of the user manager from servlet init parameters. We can omit
             // this once user manager supports DataSources
-            String sqlFilePath = config.getInitParameter(UserManagerConstants.SQL_FILE_PATH_PARAM);
+           // String sqlFilePath = config.getInitParameter(UserManagerConstants.SQL_FILE_PATH_PARAM);
             String dbURL = config.getInitParameter(UserManagerConstants.DB_URL_PARAM);
             String driver = config.getInitParameter(UserManagerConstants.DB_DRIVER_PARAM);
 
-            if (sqlFilePath == null || sqlFilePath.length() == 0) {
+        /*    if (sqlFilePath == null || sqlFilePath.length() == 0) {
 
                 ServletContext servletContext = config.getServletContext();
                 String webINFPath = servletContext.getRealPath("WEB-INF");
@@ -87,13 +87,13 @@
                     log.fatal(msg);
                     throw new ServletException(msg);
                 }
-            }
+            }*/
 
             if (dbURL == null || driver == null || dbURL.length() == 0 || driver.length() == 0) {
-                registryRealm = new RegistryRealmFactory().newInMemoryRegistryRealm(sqlFilePath);
+                registryRealm = new RegistryRealmFactory().newInMemoryRegistryRealm();
             } else {
                 registryRealm =
-                        new RegistryRealmFactory().newRegistryRealm(sqlFilePath, dbURL, driver);
+                        new RegistryRealmFactory().newRegistryRealm(dbURL, driver);
             }
 
             this.coreRegistry = new JDBCRegistry(registryRealm);

Modified: trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/JDBCRegistryTest.java
==============================================================================
--- trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/JDBCRegistryTest.java	(original)
+++ trunk/registry/modules/core/src/test/java/org/wso2/registry/jdbc/JDBCRegistryTest.java	Thu Nov  1 04:25:17 2007
@@ -39,10 +39,8 @@
     public void setUp() {
         try {
             if (registry == null) {
-                //String sqlFilePath = "modules/core/src/test/resources/um.sql";
-                String sqlFilePath = "src/test/resources/um.sql";
                 Realm registryRealm =
-                        new RegistryRealmFactory().newInMemoryRegistryRealm(sqlFilePath);
+                        new RegistryRealmFactory().newInMemoryRegistryRealm();
                 registry = new JDBCRegistry(registryRealm);
             }
         } catch (RegistryException e) {

Modified: trunk/registry/modules/core/src/test/java/org/wso2/registry/secure/SecureRegistryTest.java
==============================================================================
--- trunk/registry/modules/core/src/test/java/org/wso2/registry/secure/SecureRegistryTest.java	(original)
+++ trunk/registry/modules/core/src/test/java/org/wso2/registry/secure/SecureRegistryTest.java	Thu Nov  1 04:25:17 2007
@@ -32,9 +32,7 @@
     public void setUp() {
         try {
             if (registry == null) {
-                //String sqlFilePath = "modules/core/src/test/resources/um.sql";
-                String sqlFilePath = "src/test/resources/um.sql";
-                realm = new RegistryRealmFactory().newInMemoryRegistryRealm(sqlFilePath);
+                realm = new RegistryRealmFactory().newInMemoryRegistryRealm();
                 registry = new JDBCRegistry(realm);
             }
         } catch (RegistryException e) {
@@ -86,52 +84,52 @@
         assertTrue("Allowed to get unauthorized resource.", failed);
     }
 
-    //public void testRoleBasedAuthorization() throws RegistryException {
+    public void testRoleBasedAuthorization() throws RegistryException {
 
-        //SecureRegistry adminRegistry =
-        //        new SecureRegistry(RegistryConstants.ADMIN_USER, "admin", registry, realm);
-        //RegistryUserManager adminUserManager =
-        //        new RegistryUserManager(realm, RegistryConstants.ADMIN_USER);
-        //
-        //String r1Content = "R1";
-        //Artifact r1 = new Artifact();
-        //r1.setContent(r1Content.getBytes());
-        //
-        //try {
-        //    adminRegistry.put("/r1", r1);
-        //} catch (RegistryException e) {
-        //    fail("Valid put scenario failed.");
-        //}
-        //
-        //adminUserManager.addRole("registryTeam");
-        //adminUserManager.authorizeRole("registryTeam", "/r1", ActionConstants.GET);
-        //
-        //adminUserManager.addUser("chathura", "cce");
-        //adminUserManager.addUserToRole("chathura", "registryTeam");
-        //
-        //adminUserManager.addUser("upul", "u");
-        //
-        //SecureRegistry chathuraRegistry =
-        //        new SecureRegistry("chathura", "cce", registry, realm);
-        //
-        //try {
-        //    chathuraRegistry.get("/r1");
-        //} catch (AuthorizationFailedException e) {
-        //    fail("Could not get authorized resource.");
-        //}
-        //
-        //SecureRegistry upulRegistry =
-        //        new SecureRegistry("upul", "u", registry, realm);
-        //
-        //boolean failed = false;
-        //try {
-        //    upulRegistry.get("/r1");
-        //} catch (AuthorizationFailedException e) {
-        //    failed = true;
-        //}
-        //
-        //assertTrue("Allowed to get unauthorized resource.", failed);
-    //}
+        SecureRegistry adminRegistry =
+                new SecureRegistry(RegistryConstants.ADMIN_USER, "admin", registry, realm);
+        RegistryUserManager adminUserManager =
+                new RegistryUserManager(realm, RegistryConstants.ADMIN_USER);
+        
+        String r1Content = "R1";
+        Artifact r1 = new Artifact();
+        r1.setContent(r1Content.getBytes());
+        
+        try {
+            adminRegistry.put("/r1", r1);
+        } catch (RegistryException e) {
+            fail("Valid put scenario failed.");
+        }
+        
+        adminUserManager.addRole("registryTeam");
+        adminUserManager.authorizeRole("registryTeam", "/r1", ActionConstants.GET);
+        
+        adminUserManager.addUser("chathurax", "cce");
+        adminUserManager.addUserToRole("chathura", "registryTeam");
+        
+        adminUserManager.addUser("upulx", "u");
+        
+        SecureRegistry chathuraRegistry =
+                new SecureRegistry("chathura", "cce", registry, realm);
+        
+        try {
+            chathuraRegistry.get("/r1");
+        } catch (AuthorizationFailedException e) {
+            fail("Could not get authorized resource.");
+        }
+        
+        SecureRegistry upulRegistry =
+                new SecureRegistry("upul", "u", registry, realm);
+        
+        boolean failed = false;
+        try {
+            upulRegistry.get("/r1");
+        } catch (AuthorizationFailedException e) {
+            failed = true;
+        }
+        
+        assertTrue("Allowed to get unauthorized resource.", failed);
+    }
 
     //public void putResourceTest() {
     //



More information about the Registry-dev mailing list