1 40 package edu.mit.dspace; 41 42 import java.io.IOException ; 43 import java.sql.SQLException ; 44 import javax.servlet.ServletException ; 45 import javax.servlet.http.HttpServletRequest ; 46 import javax.servlet.http.HttpServletResponse ; 47 import javax.servlet.jsp.PageContext ; 48 import javax.servlet.jsp.jstl.fmt.LocaleSupport; 49 import java.util.ArrayList ; 50 51 import org.apache.log4j.Logger; 52 import org.dspace.core.Context; 53 import org.dspace.core.LogManager; 54 import org.dspace.eperson.EPerson; 55 import org.dspace.eperson.Group; 56 import org.dspace.eperson.AuthenticationMethod; 57 import org.dspace.authorize.AuthorizeException; 58 59 79 public class MITSpecialGroup 80 implements AuthenticationMethod { 81 82 83 private static Logger log = Logger.getLogger(MITSpecialGroup.class); 84 85 89 public static final String MIT_GROUPNAME = "MIT Users"; 90 91 95 public boolean canSelfRegister(Context context, 96 HttpServletRequest request, 97 String username) 98 throws SQLException 99 { 100 return false; 101 } 102 103 107 public void initEPerson(Context context, HttpServletRequest request, 108 EPerson eperson) 109 throws SQLException 110 { 111 if (isMITEmail(eperson.getEmail())) 113 { 114 eperson.setRequireCertificate(true); 115 } 116 } 117 118 123 public boolean allowSetPassword(Context context, 124 HttpServletRequest request, 125 String username) 126 throws SQLException 127 { 128 return !isMITEmail(username); 129 } 130 131 135 public boolean isImplicit() 136 { 137 return true; 138 } 139 140 143 public int[] getSpecialGroups(Context context, HttpServletRequest request) 144 { 145 EPerson user = context.getCurrentUser(); 146 boolean hasMITEmail = ((user != null) && isMITEmail(user.getEmail())); 147 148 try { 149 if (hasMITEmail || (request != null && isFromMITCommunity(request))) 150 { 151 log.debug(LogManager.getHeader(context, "getSpecialGroups", 152 "Got an MIT user, looking for group")); 153 154 Group mitGroup = Group.findByName(context, MIT_GROUPNAME); 155 if (mitGroup == null) 156 { 157 log.warn(LogManager.getHeader(context, 159 "No MIT Group found!! Admin needs to create group named \""+ 160 MIT_GROUPNAME+"\"", "")); 161 162 return new int[0]; 163 } 164 165 return new int[] { mitGroup.getID() }; 166 } 167 else 168 log.debug(LogManager.getHeader(context, "getSpecialGroups", 169 "Not an MIT user, no groups for you.")); 170 171 } 172 catch (java.sql.SQLException e) 173 { 174 } 175 return new int[0]; 176 } 177 178 185 public int authenticate(Context context, 186 String username, 187 String password, 188 String realm, 189 HttpServletRequest request) 190 throws SQLException 191 { 192 return BAD_ARGS; 193 } 194 195 210 public String loginPageURL(Context context, 211 HttpServletRequest request, 212 HttpServletResponse response) 213 { 214 return null; 215 } 216 217 public String loginPageTitle(Context context) 218 { 219 return null; 220 } 221 222 228 private static boolean isMITEmail(String email) 229 { 230 return email.toLowerCase().trim().endsWith("@mit.edu"); 231 } 232 233 247 private static boolean isFromMITCommunity(HttpServletRequest request) 248 { 249 String addr = request.getRemoteAddr(); 250 251 log.debug("checking MIT membership of IP addr="+addr); 252 253 final String [] mitIPs = { 254 "18.", "128.52.", "129.55.", "192.52.65.", "192.52.61.", "198.125.160.", "198.125.161.", "198.125.162.", "198.125.163.", "198.125.176.", "198.125.177.", "198.125.178.", "198.125.179." }; 268 269 for (int i = 0; i < mitIPs.length; i++) 270 { 271 if (addr.startsWith(mitIPs[i])) 272 { 273 return true; 274 } 275 } 276 277 return false; 278 } 279 280 } 281 | Popular Tags |