View Javadoc

1   /*
2    * Copyright 2000-2004 The Apache Software Foundation.
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package org.apache.jetspeed.util;
18  
19  import org.apache.jetspeed.om.profile.Portlets;
20  import org.apache.jetspeed.om.profile.Entry;
21  import org.apache.jetspeed.om.profile.Reference;
22  import org.apache.jetspeed.services.idgenerator.JetspeedIdGenerator;
23  
24  // Jetspeed
25  import org.apache.jetspeed.om.profile.Profile;
26  import org.apache.jetspeed.services.security.PortalResource;
27  import org.apache.jetspeed.om.security.JetspeedUser;
28  import org.apache.jetspeed.services.JetspeedSecurity;
29  
30  // Turbine stuff
31  import org.apache.turbine.util.RunData;
32  
33  /***
34   * This class provides static util methods for portlet manipulation that 
35   * aren't part of the default services.
36   *
37   * @author <a href="mailto:morciuch@apache.org">Mark Orciuch</a> 
38   * @version $Id: PortletUtils.java,v 1.4 2004/02/23 03:23:42 jford Exp $
39   */
40  public class PortletUtils 
41  {
42      /***
43       * Regenerates jspeid for all portlets, entries and references
44       *
45       * @param profile Profile to process
46       * @return Profile with portlet ids regenerated
47       */
48      public static void regenerateIds(Portlets topPortlets)
49      throws Exception
50      {
51          // Display some portlets
52          Portlets[] portlets = topPortlets.getPortletsArray();
53          for (int i = 0; i < portlets.length; i++)
54          {
55              portlets[i].setId(JetspeedIdGenerator.getNextPeid());
56              
57              Entry[] entries = portlets[i].getEntriesArray();
58              for (int j = 0; j < entries.length; j++)
59              {
60                  entries[j].setId(JetspeedIdGenerator.getNextPeid());
61              }
62  
63              Reference[] refs = portlets[i].getReferenceArray();
64              for (int k = 0; k < refs.length; k++)
65              {
66                  refs[k].setId(JetspeedIdGenerator.getNextPeid());
67              }
68  
69              regenerateIds(portlets[i]);
70          }
71      }
72  
73      /***
74       * Returns true if specific profile is accessible by the current user
75       * 
76       * @param data
77       * @param profile
78       * @return 
79       */
80      public static boolean canAccessProfile(RunData rundata, Profile profile)
81      {
82          boolean result = true;
83  
84          if (profile != null && profile.getRootSet() != null)
85          {
86              PortalResource portalResource = new PortalResource(profile.getRootSet());
87              String owner = null;
88              if (profile.getUserName() != null)
89              {
90                  owner = profile.getUserName();
91              }
92              portalResource.setOwner(owner);
93  
94              result = JetspeedSecurity.checkPermission((JetspeedUser) rundata.getUser(), 
95                                                        portalResource, 
96                                                        JetspeedSecurity.PERMISSION_CUSTOMIZE);
97          }
98  
99          return result;
100 
101     }
102 
103 }