001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.auth;
018:
019: import java.util.Map;
020:
021: import org.apache.avalon.framework.context.Context;
022: import org.apache.avalon.framework.context.ContextException;
023: import org.apache.avalon.framework.context.Contextualizable;
024: import org.apache.cocoon.components.ContextHelper;
025: import org.apache.cocoon.environment.ObjectModelHelper;
026: import org.apache.cocoon.environment.Request;
027:
028: /**
029: * Utility class that can be used from flow script to access the different
030: * application functions of Cocoon Authentication.
031: * The easiest way to use this class in flow script is to create an instance
032: * using cocoon.createObject():
033: * var util = cocoon.createObject("org.apache.cocoon.auth.ApplicationUtil");
034: * and then you can invoke one of the instance methods like
035: * var user = util.getUser();
036: *
037: * @version $Id: ApplicationUtil.java 502601 2007-02-02 13:15:47Z cziegeler $
038: */
039: public class ApplicationUtil implements Contextualizable {
040:
041: /** The Avalon context. */
042: protected Context context;
043:
044: /**
045: * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
046: */
047: public void contextualize(final Context aContext)
048: throws ContextException {
049: this .context = aContext;
050: }
051:
052: /**
053: * Return the current user.
054: * @param objectModel The object model of the current request.
055: * @return The current user or null.
056: */
057: public static User getUser(final Map objectModel) {
058: return (User) objectModel.get(ApplicationManager.USER);
059: }
060:
061: /**
062: * Return the current application.
063: * @param objectModel The object model of the current request.
064: * @return The current application or null.
065: */
066: public static Application getApplication(final Map objectModel) {
067: return (Application) objectModel
068: .get(ApplicationManager.APPLICATION);
069: }
070:
071: /**
072: * Return the current user data.
073: * @param objectModel The object model of the current request.
074: * @return The current user data or null.
075: */
076: public static Object getData(final Map objectModel) {
077: return objectModel.get(ApplicationManager.APPLICATION_DATA);
078: }
079:
080: /**
081: * Checks if the user has the given role.
082: * First {@link User#isUserInRole(String)} is invoked. If the result is false,
083: * the {@link Request#isUserInRole(java.lang.String)} is called.
084: *
085: * @param user The user to test.
086: * @param role The role.
087: * @param objectModel The Cocoon object model.
088: * @return This returns true, if the user has the role; otherwise false is returned.
089: */
090: public static boolean isUserInRole(final User user,
091: final String role, final Map objectModel) {
092: boolean result = user.isUserInRole(role);
093: if (!result) {
094: final Request req = ObjectModelHelper
095: .getRequest(objectModel);
096: result = req.isUserInRole(role);
097: }
098: return result;
099: }
100:
101: /**
102: * Return the current user.
103: * @return The current user or null.
104: */
105: public User getUser() {
106: final Map objectModel = ContextHelper
107: .getObjectModel(this .context);
108: return (User) objectModel.get(ApplicationManager.USER);
109: }
110:
111: /**
112: * Return the current application.
113: * @return The current application or null.
114: */
115: public Application getApplication() {
116: final Map objectModel = ContextHelper
117: .getObjectModel(this .context);
118: return (Application) objectModel
119: .get(ApplicationManager.APPLICATION);
120: }
121:
122: /**
123: * Return the current user data.
124: * @return The current user data or null.
125: */
126: public Object getData() {
127: final Map objectModel = ContextHelper
128: .getObjectModel(this .context);
129: return objectModel.get(ApplicationManager.APPLICATION_DATA);
130: }
131:
132: /**
133: * Checks if the user has the given role.
134: * First {@link User#isUserInRole(String)} is invoked. If the result is false,
135: * the {@link Request#isUserInRole(java.lang.String)} is called.
136: *
137: * @param user The user to test.
138: * @param role The role.
139: * @return This returns true, if the user has the role; otherwise false is returned.
140: */
141: public boolean isUserInRole(final User user, final String role) {
142: final Map objectModel = ContextHelper
143: .getObjectModel(this.context);
144: return isUserInRole(user, role, objectModel);
145: }
146: }
|