incubator-graffito-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From clomb...@apache.org
Subject svn commit: r164133 - /incubator/graffito/trunk/components/src/java/org/apache/portals/graffito/impl/ContentPermissionServiceImpl.java
Date Thu, 21 Apr 2005 22:07:21 GMT
Author: clombart
Date: Thu Apr 21 15:07:20 2005
New Revision: 164133

URL: http://svn.apache.org/viewcvs?rev=164133&view=rev
Log:
work on the permission management

Added:
    incubator/graffito/trunk/components/src/java/org/apache/portals/graffito/impl/ContentPermissionServiceImpl.java

Added: incubator/graffito/trunk/components/src/java/org/apache/portals/graffito/impl/ContentPermissionServiceImpl.java
URL: http://svn.apache.org/viewcvs/incubator/graffito/trunk/components/src/java/org/apache/portals/graffito/impl/ContentPermissionServiceImpl.java?rev=164133&view=auto
==============================================================================
--- incubator/graffito/trunk/components/src/java/org/apache/portals/graffito/impl/ContentPermissionServiceImpl.java
(added)
+++ incubator/graffito/trunk/components/src/java/org/apache/portals/graffito/impl/ContentPermissionServiceImpl.java
Thu Apr 21 15:07:20 2005
@@ -0,0 +1,221 @@
+/* Copyright 2004 Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.portals.graffito.impl;
+
+import java.lang.reflect.Constructor;
+import java.security.Permission;
+import java.security.Permissions;
+import java.sql.Timestamp;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Iterator;
+
+import org.apache.jetspeed.security.impl.PermissionManagerImpl;
+import org.apache.jetspeed.security.om.InternalPermission;
+import org.apache.jetspeed.security.om.InternalPrincipal;
+import org.apache.jetspeed.security.om.impl.InternalPermissionImpl;
+import org.apache.jetspeed.security.om.impl.InternalPrincipalImpl;
+import org.apache.ojb.broker.query.Criteria;
+import org.apache.ojb.broker.query.Query;
+import org.apache.ojb.broker.query.QueryFactory;
+
+import org.apache.portals.graffito.ContentPermissionService;
+import org.apache.portals.graffito.exception.ContentManagementException;
+import org.apache.portals.graffito.model.CmsObject;
+import org.apache.portals.graffito.model.CmsPermission;
+import org.apache.portals.graffito.security.impl.CmsPermissionImpl;
+
+
+/**
+ * Default implementation for {@link org.apache.portals.graffito.ContentPermissionManager}
+ *
+ * @author <a href="mailto:christophe.lombart@sword-technologies.com">Lombart Christophe
</a>
+ * @version $Id: Exp $
+ */
+public class ContentPermissionServiceImpl extends PermissionManagerImpl implements ContentPermissionService
+{
+    
+
+    /**
+     * 
+     * @see org.apache.portals.graffito.ContentPermissionService#createPermission(java.lang.String,
java.lang.String)
+     */
+    public CmsPermission createPermission(String name, String actions) throws ContentManagementException
+    {
+        return new CmsPermissionImpl(name, actions);
+    }
+
+    /**
+     * 
+     * @see org.apache.portals.graffito.ContentPermissionService#getPermissions(org.apache.portals.graffito.model.CmsObject)
+     */
+    public Permissions getPermissions(CmsObject cmsObject)
+    {       
+        return this.getPermissions(cmsObject.getUri());
+    }
+    
+    /**
+     * 
+     * @see org.apache.portals.graffito.ContentPermissionService#getPermissions(java.lang.String)
+     */
+    public Permissions getPermissions(String uri)
+    {
+        Criteria criteria = new Criteria();
+        
+        // Some permission can contains special char like "*", "-" (see CmsPermissionImpl)
+        ArrayList values = new ArrayList();
+        values.add(uri);
+        values.add(uri + "/*");
+        values.add(uri + "/-");
+        
+        criteria.addIn("name", values);
+        Query query = QueryFactory.newQuery(InternalPermissionImpl.class, criteria);
+        Collection internalPermissions = getPersistenceBrokerTemplate().getCollectionByQuery(query);
+        
+        Permissions permissions = appendSecurityPermissions(internalPermissions);
+        return permissions;
+    }
+
+    
+    /**
+     * 
+     * @see org.apache.portals.graffito.ContentPermissionService#grantPermission(java.lang.String,
org.apache.portals.graffito.model.CmsPermission)
+     */
+    public void grantPermission(String fullPath, CmsPermission permission) throws ContentManagementException
+    {
+
+        boolean createPermission = true;
+        Collection internalPermissions = new ArrayList();
+
+        InternalPrincipal internalPrincipal = getInternalPrincipal(fullPath);
+        if (null == internalPrincipal)
+        {
+            throw new ContentManagementException("Impossible to find the principal for "
+ fullPath);
+        }
+        InternalPermission internalPermission = getInternalPermission(permission);
+        if (null == internalPermission)
+        {
+            throw new ContentManagementException("Impossible to find the internal permission");
+        }
+
+        if (null != internalPrincipal.getPermissions())
+        {
+            internalPermissions.addAll(internalPrincipal.getPermissions());
+        }
+        if (!internalPermissions.contains(internalPermission))
+        {
+            internalPermissions.add(internalPermission);
+        }
+        try
+        {
+            internalPrincipal.setModifiedDate(new Timestamp(System.currentTimeMillis()));
+            internalPrincipal.setPermissions(internalPermissions);
+            
+            getPersistenceBrokerTemplate().store(internalPrincipal);
+        }
+        catch (Exception e)
+        {
+            throw new ContentManagementException("Impossible to grant the permission " ,
e);
+        }
+
+
+    }
+    /**
+     * 
+     * Iterate through a collection of {@link InternalPermission}and build a
+     *  map with key = the principal and value =  a collection of {@link Permission}
+     * </p>
+     * 
+     * @param omPermissions The collection of {@link InternalPermission}.
+     *  
+     * @return The collection of {@link java.security.Permission}.
+     */
+    private Permissions appendSecurityPermissions(Collection omPermissions)
+    {     
+        Permissions permissions = new Permissions();
+        Iterator internalPermissionsIter = omPermissions.iterator();
+        while (internalPermissionsIter.hasNext())
+        {
+            InternalPermission internalPermission = (InternalPermission) internalPermissionsIter.next();
+            
+            Permission permission = null;
+            try
+            {
+                                
+                ArrayList fullPaths = new ArrayList();
+                Iterator internalPrincipalsIter = internalPermission.getPrincipals().iterator();
+                while (internalPrincipalsIter.hasNext())
+                {
+                    InternalPrincipal internalPrincipal = (InternalPrincipal) internalPrincipalsIter.next();
+                    fullPaths.add(internalPrincipal.getFullPath());
+                }
+
+                Class permissionClass = Class.forName(internalPermission.getClassname());
+                Class[] parameterTypes = { String.class, String.class, Collection.class };
+                Constructor permissionConstructor = permissionClass.getConstructor(parameterTypes);
+                Object[] initArgs = { internalPermission.getName(), internalPermission.getActions(),
fullPaths };
+                permission = (Permission) permissionConstructor.newInstance(initArgs);
+                if(!Collections.list(permissions.elements()).contains(permission))
+                {                    
+                    permissions.add(permission);
+                }
+
+                
+            }
+            catch (Exception e)
+            {
+                e.printStackTrace();
+            }
+        }
+        return permissions;
+    }
+
+    /**
+     *
+     * Returns the {@link InternalPrincipal} from the full path.
+     *      
+     * @param fullPath The full path.
+     * @return The {@link InternalPrincipal}.
+     */
+    InternalPrincipal getInternalPrincipal(String fullPath)
+    {
+        Criteria filter = new Criteria();
+        filter.addEqualTo("fullPath", fullPath);
+        Query query = QueryFactory.newQuery(InternalPrincipalImpl.class, filter);
+        InternalPrincipal internalPrincipal = (InternalPrincipal) getPersistenceBrokerTemplate().getObjectByQuery(query);
+        return internalPrincipal;
+    }
+    
+    /**
+     * <p>
+     * Returns the {@link InternalPermission} from a Permission object
+     * </p>
+     * 
+     * @param permission The permision 
+     * @return The {@link InternalPermission}.
+     */
+    InternalPermission getInternalPermission(CmsPermission permission)
+    {
+        Criteria filter = new Criteria();
+        filter.addEqualTo("classname", permission.getClass().getName());
+        filter.addEqualTo("name", permission.getName());
+        filter.addEqualTo("actions", permission.getActions());
+        Query query = QueryFactory.newQuery(InternalPermissionImpl.class, filter);
+        InternalPermission internalPermission = (InternalPermission) getPersistenceBrokerTemplate().getObjectByQuery(query);
+        return internalPermission;
+    }
+    
+}
\ No newline at end of file



Mime
View raw message