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 to access the different
030: * application functions of Cocoon Authentication.
031: *
032: * @version $Id: ApplicationUtil.java 433543 2006-08-22 06:22:54Z crossley $
033: */
034: public class ApplicationUtil implements Contextualizable {
035:
036: /** The Avalon context. */
037: protected Context context;
038:
039: /**
040: * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
041: */
042: public void contextualize(final Context aContext)
043: throws ContextException {
044: this .context = aContext;
045: }
046:
047: /**
048: * Return the current user.
049: * @param objectModel The object model of the current request.
050: * @return The current user or null.
051: */
052: public static User getUser(final Map objectModel) {
053: return (User) objectModel.get(ApplicationManager.USER);
054: }
055:
056: /**
057: * Return the current application.
058: * @param objectModel The object model of the current request.
059: * @return The current application or null.
060: */
061: public static Application getApplication(final Map objectModel) {
062: return (Application) objectModel
063: .get(ApplicationManager.APPLICATION);
064: }
065:
066: /**
067: * Return the current user data.
068: * @param objectModel The object model of the current request.
069: * @return The current user data or null.
070: */
071: public static Object getData(final Map objectModel) {
072: return objectModel.get(ApplicationManager.APPLICATION_DATA);
073: }
074:
075: /**
076: * Checks if the user has the given role.
077: * First {@link User#isUserInRole(String)} is invoked. If the result is false,
078: * the {@link Request#isUserInRole(java.lang.String)} is called.
079: *
080: * @param user The user to test.
081: * @param role The role.
082: * @param objectModel The Cocoon object model.
083: * @return This returns true, if the user has the role; otherwise false is returned.
084: */
085: public boolean isUserInRole(final User user, final String role,
086: final Map objectModel) {
087: boolean result = user.isUserInRole(role);
088: if (!result) {
089: final Request req = ObjectModelHelper
090: .getRequest(objectModel);
091: result = req.isUserInRole(role);
092: }
093: return result;
094: }
095:
096: /**
097: * Return the current user.
098: * @return The current user or null.
099: */
100: public User getUser() {
101: final Map objectModel = ContextHelper
102: .getObjectModel(this .context);
103: return (User) objectModel.get(ApplicationManager.USER);
104: }
105:
106: /**
107: * Return the current application.
108: * @return The current application or null.
109: */
110: public Application getApplication() {
111: final Map objectModel = ContextHelper
112: .getObjectModel(this .context);
113: return (Application) objectModel
114: .get(ApplicationManager.APPLICATION);
115: }
116:
117: /**
118: * Return the current user data.
119: * @return The current user data or null.
120: */
121: public Object getData() {
122: final Map objectModel = ContextHelper
123: .getObjectModel(this .context);
124: return objectModel.get(ApplicationManager.APPLICATION_DATA);
125: }
126:
127: /**
128: * Checks if the user has the given role.
129: * First {@link User#isUserInRole(String)} is invoked. If the result is false,
130: * the {@link Request#isUserInRole(java.lang.String)} is called.
131: *
132: * @param user The user to test.
133: * @param role The role.
134: * @return This returns true, if the user has the role; otherwise false is returned.
135: */
136: public boolean isUserInRole(final User user, final String role) {
137: final Map objectModel = ContextHelper
138: .getObjectModel(this.context);
139: return isUserInRole(user, role, objectModel);
140: }
141: }
|