1 29 30 package com.caucho.server.security; 31 32 import com.caucho.util.CharBuffer; 33 34 import javax.servlet.ServletContext ; 35 import javax.servlet.ServletException ; 36 import javax.servlet.http.HttpServletRequest ; 37 import javax.servlet.http.HttpServletResponse ; 38 import java.io.IOException ; 39 40 public class RoleConstraint extends AbstractConstraint { 41 private String []_roles; 42 43 public void addRoleName(String role) 44 { 45 if (_roles == null) 46 _roles = new String [] { role }; 47 else { 48 String []newRoles = new String [_roles.length + 1]; 49 System.arraycopy(_roles, 0, newRoles, 0, _roles.length); 50 newRoles[_roles.length] = role; 51 _roles = newRoles; 52 } 53 } 54 55 58 public boolean needsAuthentication() 59 { 60 return _roles != null && _roles.length > 0; 61 } 62 63 66 public boolean isAuthorized(HttpServletRequest request, 67 HttpServletResponse response, 68 ServletContext application) 69 throws ServletException , IOException 70 { 71 for (int i = 0; _roles != null && i < _roles.length; i++) { 72 if (_roles[i].equals("*")) 73 return true; 74 if (request.isUserInRole(_roles[i])) { 75 return true; 76 } 77 } 78 79 response.sendError(HttpServletResponse.SC_FORBIDDEN, null); 80 81 return false; 82 } 83 84 public String toString() 85 { 86 CharBuffer cb = new CharBuffer(); 87 88 cb.append("RoleConstraint["); 89 for (int i = 0; i < _roles.length; i++) { 90 if (i != 0) 91 cb.append(','); 92 cb.append(_roles[i]); 93 } 94 cb.append("]"); 95 96 return cb.close(); 97 } 98 } 99 | Popular Tags |