View Javadoc

1   /*
2    *  Apache License
3    *  Version 2.0, January 2004
4    *  http://www.apache.org/licenses/
5    *
6    *  Copyright 2008 by chenillekit.org
7    *
8    *  Licensed under the Apache License, Version 2.0 (the "License");
9    *  you may not use this file except in compliance with the License.
10   *  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   */
14  
15  /**
16   *
17   */
18  
19  package org.chenillekit.access.services.impl;
20  
21  import org.apache.tapestry5.annotations.OnEvent;
22  import org.apache.tapestry5.model.MutableComponentModel;
23  import org.apache.tapestry5.services.ClassTransformation;
24  import org.apache.tapestry5.services.ComponentClassTransformWorker;
25  import org.apache.tapestry5.services.TransformMethodSignature;
26  import org.chenillekit.access.ChenilleKitAccessConstants;
27  import org.chenillekit.access.ChenilleKitAccessException;
28  import org.chenillekit.access.annotations.Restricted;
29  import org.chenillekit.access.internal.ChenillekitAccessInternalUtils;
30  
31  /**
32   *
33   * @version $Id: AccessControlWorker.java 88 2008-06-16 16:43:40Z homburgs $
34   */
35  public class RestrictedWorker implements ComponentClassTransformWorker
36  {
37  	// FIXME How do we get the Logger from the IoC?
38  	// private final Logger logger;
39  
40  
41  	/* (non-Javadoc)
42  	 * @see org.apache.tapestry5.services.ComponentClassTransformWorker#transform(org.apache.tapestry5.services.ClassTransformation, org.apache.tapestry5.model.MutableComponentModel)
43  	 */
44  	public void transform(ClassTransformation transformation, MutableComponentModel model)
45  	{
46  		processPageRestriction(transformation, model);
47  
48  		processEventHandlerRestrictions(transformation, model);
49  
50  		// processComponentsRestrictions(transformation, model);
51  	}
52  
53  	/**
54  	 * Read and process restriction on page classes annotated with {@link Restricted} annotation
55  	 *
56  	 * @param transformation Contains class-specific information used when transforming a raw component class
57  	 *                       into an executable component class.
58  	 * @param model          Mutable version of {@link org.apache.tapestry5.model.ComponentModel} used during
59  	 *                       the transformation phase.
60  	 */
61  	private void processPageRestriction(ClassTransformation transformation, MutableComponentModel model)
62  	{
63  		Restricted pageRestricted = transformation.getAnnotation(Restricted.class);
64  		if (pageRestricted != null)
65  		{
66  			String roleWeigh = Integer.toString(pageRestricted.role());
67  			String[] groups = pageRestricted.groups();
68  
69  			model.setMeta(ChenilleKitAccessConstants.RESTRICTED_PAGE_ROLE, roleWeigh);
70  
71  			if (groups.length > 0)
72  				model.setMeta(ChenilleKitAccessConstants.RESTRICTED_PAGE_GROUP,
73  						ChenillekitAccessInternalUtils.getStringArrayAsString(groups));
74  		}
75  	}
76  
77  	/**
78  	 * inject meta datas about annotated methods
79  	 *
80  	 * @param transformation Contains class-specific information used when transforming a raw component class
81  	 *                       into an executable component class.
82  	 * @param model          Mutable version of {@link org.apache.tapestry5.model.ComponentModel} used during
83  	 *                       the transformation phase.
84  	 */
85  	private void processEventHandlerRestrictions(ClassTransformation transformation, MutableComponentModel model)
86  	{
87  		for (TransformMethodSignature method : transformation.findMethodsWithAnnotation(Restricted.class))
88  		{
89  			String methodName = method.getMethodName();
90  			Restricted restricted = transformation.getMethodAnnotation(method, Restricted.class);
91  			OnEvent event = transformation.getMethodAnnotation(method, OnEvent.class);
92  			if (methodName.startsWith("on") || event != null)
93  			{
94  				String componentId = ChenillekitAccessInternalUtils.extractComponentId(method, event);
95  				String eventType = ChenillekitAccessInternalUtils.extractEventType(method, event);
96  
97  				String groupMeta = ChenillekitAccessInternalUtils.buildMetaForHandlerMethod(componentId,
98  						eventType,
99  						ChenilleKitAccessConstants.RESTRICTED_EVENT_HANDLER_GROUPS_SUFFIX);
100 
101 				String roleMeta = ChenillekitAccessInternalUtils.buildMetaForHandlerMethod(componentId,
102 						eventType,
103 						ChenilleKitAccessConstants.RESTRICTED_EVENT_HANDLER_ROLE_SUFFIX);
104 
105 				model.setMeta(groupMeta, ChenillekitAccessInternalUtils.getStringArrayAsString(restricted.groups()));
106 				model.setMeta(roleMeta, Integer.toString(restricted.role()));
107 			}
108 			else
109 			{
110 				throw new ChenilleKitAccessException("Cannot put Restricted annotation on a non event handler method");
111 			}
112 		}
113 	}
114 
115 	/**
116 	 * inject meta datas about annotated methods
117 	 *
118 	 * @param transformation Contains class-specific information used when transforming a raw component class
119 	 *                       into an executable component class.
120 	 * @param model          Mutable version of {@link org.apache.tapestry5.model.ComponentModel} used during
121 	 *                       the transformation phase.
122 	 */
123 	private void processComponentsRestrictions(ClassTransformation transformation, MutableComponentModel model)
124 	{
125 		throw new RuntimeException("NOT YET IMPLEMENTED");
126 	}
127 }