| 1 |
/** |
|---|
| 2 |
* The contents of this file are subject to the OpenMRS Public License |
|---|
| 3 |
* Version 1.0 (the "License"); you may not use this file except in |
|---|
| 4 |
* compliance with the License. You may obtain a copy of the License at |
|---|
| 5 |
* http://license.openmrs.org |
|---|
| 6 |
* |
|---|
| 7 |
* Software distributed under the License is distributed on an "AS IS" |
|---|
| 8 |
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the |
|---|
| 9 |
* License for the specific language governing rights and limitations |
|---|
| 10 |
* under the License. |
|---|
| 11 |
* |
|---|
| 12 |
* Copyright (C) OpenMRS, LLC. All Rights Reserved. |
|---|
| 13 |
*/ |
|---|
| 14 |
package org.openmrs.web.taglib; |
|---|
| 15 |
|
|---|
| 16 |
import javax.servlet.jsp.tagext.TagSupport; |
|---|
| 17 |
|
|---|
| 18 |
import org.apache.commons.logging.Log; |
|---|
| 19 |
import org.apache.commons.logging.LogFactory; |
|---|
| 20 |
import org.openmrs.api.context.Context; |
|---|
| 21 |
import org.openmrs.api.context.UserContext; |
|---|
| 22 |
|
|---|
| 23 |
public class PrivilegeTag extends TagSupport { |
|---|
| 24 |
|
|---|
| 25 |
public static final long serialVersionUID = 11233L; |
|---|
| 26 |
|
|---|
| 27 |
private final Log log = LogFactory.getLog(getClass()); |
|---|
| 28 |
|
|---|
| 29 |
private String privilege; |
|---|
| 30 |
private String inverse; |
|---|
| 31 |
|
|---|
| 32 |
public int doStartTag() { |
|---|
| 33 |
|
|---|
| 34 |
UserContext userContext = Context.getUserContext(); |
|---|
| 35 |
|
|---|
| 36 |
log.debug("Checking user " + userContext.getAuthenticatedUser() + " for privs " + privilege); |
|---|
| 37 |
|
|---|
| 38 |
boolean hasPrivilege = false; |
|---|
| 39 |
if (privilege.contains(",")) { |
|---|
| 40 |
String[] privs = privilege.split(","); |
|---|
| 41 |
for (String p : privs) { |
|---|
| 42 |
if (userContext.hasPrivilege(p)) { |
|---|
| 43 |
hasPrivilege = true; |
|---|
| 44 |
break; |
|---|
| 45 |
} |
|---|
| 46 |
} |
|---|
| 47 |
} |
|---|
| 48 |
else { |
|---|
| 49 |
hasPrivilege = userContext.hasPrivilege(privilege); |
|---|
| 50 |
} |
|---|
| 51 |
|
|---|
| 52 |
// allow inversing |
|---|
| 53 |
boolean isInverted = false; |
|---|
| 54 |
if ( inverse != null ) isInverted = "true".equals(inverse.toLowerCase()); |
|---|
| 55 |
|
|---|
| 56 |
if ( (hasPrivilege && !isInverted) || (!hasPrivilege && isInverted)) { |
|---|
| 57 |
pageContext.setAttribute("authenticatedUser", userContext.getAuthenticatedUser()); |
|---|
| 58 |
return EVAL_BODY_INCLUDE; |
|---|
| 59 |
} |
|---|
| 60 |
else { |
|---|
| 61 |
return SKIP_BODY; |
|---|
| 62 |
} |
|---|
| 63 |
} |
|---|
| 64 |
|
|---|
| 65 |
/** |
|---|
| 66 |
* @return Returns the privilege. |
|---|
| 67 |
*/ |
|---|
| 68 |
public String getPrivilege() { |
|---|
| 69 |
return privilege; |
|---|
| 70 |
} |
|---|
| 71 |
|
|---|
| 72 |
/** |
|---|
| 73 |
* @param converse The privilege to set. |
|---|
| 74 |
*/ |
|---|
| 75 |
public void setPrivilege(String privilege) { |
|---|
| 76 |
this.privilege = privilege; |
|---|
| 77 |
} |
|---|
| 78 |
|
|---|
| 79 |
/** |
|---|
| 80 |
* @return Returns the inverse. |
|---|
| 81 |
*/ |
|---|
| 82 |
public String getInverse() { |
|---|
| 83 |
return inverse; |
|---|
| 84 |
} |
|---|
| 85 |
|
|---|
| 86 |
/** |
|---|
| 87 |
* @param inverse The inverse to set. |
|---|
| 88 |
*/ |
|---|
| 89 |
public void setInverse(String inverse) { |
|---|
| 90 |
this.inverse = inverse; |
|---|
| 91 |
} |
|---|
| 92 |
} |
|---|