| 1155 | | /** This is the routine which does the work. |
|---|
| 1156 | | * |
|---|
| 1157 | | * @param request Needed to locate session |
|---|
| 1158 | | * @param response |
|---|
| 1159 | | * @param sess UWCalSession calendar session object |
|---|
| 1160 | | * @param frm Action form |
|---|
| 1161 | | * @return String forward name |
|---|
| 1162 | | * @throws Throwable |
|---|
| 1163 | | */ |
|---|
| 1164 | | public abstract String doAction(HttpServletRequest request, |
|---|
| 1165 | | HttpServletResponse response, |
|---|
| 1166 | | BwSession sess, |
|---|
| 1167 | | BwActionFormBase frm) throws Throwable; |
|---|
| 1168 | | |
|---|
| 1169 | | /** Get the session state object for a web session. If we've already been |
|---|
| 1170 | | * here it's embedded in the current session. Otherwise create a new one. |
|---|
| 1171 | | * |
|---|
| 1172 | | * <p>We also carry out a number of web related operations. |
|---|
| 1173 | | * |
|---|
| 1174 | | * @param request HttpServletRequest Needed to locate session |
|---|
| 1175 | | * @param form Action form |
|---|
| 1176 | | * @param messages MessageResources needed for the resources |
|---|
| 1177 | | * @param adminUserId id we want to administer |
|---|
| 1178 | | * @param admin Get this for the admin client |
|---|
| 1179 | | * @return UWCalSession null on failure |
|---|
| 1180 | | * @throws Throwable |
|---|
| 1181 | | */ |
|---|
| 1182 | | private synchronized BwSession getState(HttpServletRequest request, |
|---|
| 1183 | | BwActionFormBase form, |
|---|
| 1184 | | MessageResources messages, |
|---|
| 1185 | | String adminUserId, |
|---|
| 1186 | | boolean admin) throws Throwable { |
|---|
| 1187 | | BwSession s = BwWebUtil.getState(request); |
|---|
| 1188 | | HttpSession sess = request.getSession(false); |
|---|
| 1189 | | String appName = getAppName(sess); |
|---|
| 1190 | | |
|---|
| 1191 | | if (s != null) { |
|---|
| 1192 | | if (debug) { |
|---|
| 1193 | | debugMsg("getState-- obtainedfrom session"); |
|---|
| 1194 | | debugMsg("getState-- timeout interval = " + |
|---|
| 1195 | | sess.getMaxInactiveInterval()); |
|---|
| 1196 | | } |
|---|
| 1197 | | |
|---|
| 1198 | | form.assignNewSession(false); |
|---|
| 1199 | | } else { |
|---|
| 1200 | | if (debug) { |
|---|
| 1201 | | debugMsg("getState-- get new object"); |
|---|
| 1202 | | } |
|---|
| 1203 | | |
|---|
| 1204 | | form.assignNewSession(true); |
|---|
| 1205 | | |
|---|
| 1206 | | CalEnv env = getEnv(request, form); |
|---|
| 1207 | | String appRoot = env.getAppProperty("root"); |
|---|
| 1208 | | |
|---|
| 1209 | | /** The actual session class used is possibly site dependent |
|---|
| 1210 | | */ |
|---|
| 1211 | | s = new BwSessionImpl(form.getCurrentUser(), appRoot, appName, |
|---|
| 1212 | | form.getPresentationState(), messages, |
|---|
| 1213 | | form.getSchemeHostPort(), debug); |
|---|
| 1214 | | |
|---|
| 1215 | | BwWebUtil.setState(request, s); |
|---|
| 1216 | | |
|---|
| 1217 | | setSessionAttr(request, "cal.pubevents.client.uri", |
|---|
| 1218 | | messages.getMessage("org.bedework.public.calendar.uri")); |
|---|
| 1219 | | |
|---|
| 1220 | | setSessionAttr(request, "cal.personal.client.uri", |
|---|
| 1221 | | messages.getMessage("org.bedework.personal.calendar.uri")); |
|---|
| 1222 | | |
|---|
| 1223 | | setSessionAttr(request, "cal.admin.client.uri", |
|---|
| 1224 | | messages.getMessage("org.bedework.public.admin.uri")); |
|---|
| 1225 | | |
|---|
| 1226 | | String temp = messages.getMessage("org.bedework.host"); |
|---|
| 1227 | | if (temp == null) { |
|---|
| 1228 | | temp = form.getSchemeHostPort(); |
|---|
| 1229 | | } |
|---|
| 1230 | | |
|---|
| 1231 | | setSessionAttr(request, "cal.server.host", temp); |
|---|
| 1232 | | |
|---|
| 1233 | | String raddr = request.getRemoteAddr(); |
|---|
| 1234 | | String rhost = request.getRemoteHost(); |
|---|
| 1235 | | info("===============" + appName + ": New session (" + |
|---|
| 1236 | | s.getSessionNum() + ") from " + |
|---|
| 1237 | | rhost + "(" + raddr + ")"); |
|---|
| 1238 | | |
|---|
| 1239 | | if (!admin) { |
|---|
| 1240 | | /** Ensure the session timeout interval is longer than our refresh period |
|---|
| 1241 | | */ |
|---|
| 1242 | | // Should come from db -- int refInt = s.getRefreshInterval(); |
|---|
| 1243 | | int refInt = 60; // 1 min refresh? |
|---|
| 1244 | | |
|---|
| 1245 | | if (refInt > 0) { |
|---|
| 1246 | | int timeout = sess.getMaxInactiveInterval(); |
|---|
| 1247 | | |
|---|
| 1248 | | if (timeout <= refInt) { |
|---|
| 1249 | | // An extra minute should do it. |
|---|
| 1250 | | debugMsg("@+@+@+@+@+ set timeout to " + (refInt + 60)); |
|---|
| 1251 | | sess.setMaxInactiveInterval(refInt + 60); |
|---|
| 1252 | | } |
|---|
| 1253 | | } |
|---|
| 1254 | | } |
|---|
| 1255 | | } |
|---|
| 1256 | | |
|---|
| 1257 | | int access = getAccess(request, messages); |
|---|
| 1258 | | if (debug) { |
|---|
| 1259 | | debugMsg("Container says that current user has the type: " + access); |
|---|
| 1260 | | } |
|---|
| 1261 | | |
|---|
| 1262 | | /** Ensure we have a CalAdminSvcI object |
|---|
| 1263 | | */ |
|---|
| 1264 | | checkSvci(request, form, s, access, adminUserId, |
|---|
| 1265 | | getPublicAdmin(form), false, debug); |
|---|
| 1266 | | |
|---|
| 1267 | | /* |
|---|
| 1268 | | UserAuth ua = null; |
|---|
| 1269 | | UserAuthPar par = new UserAuthPar(); |
|---|
| 1270 | | par.svlt = servlet; |
|---|
| 1271 | | par.req = request; |
|---|
| 1272 | | |
|---|
| 1273 | | try { |
|---|
| 1274 | | ua = form.fetchSvci().getUserAuth(s.getUser(), par); |
|---|
| 1275 | | |
|---|
| 1276 | | form.assignAuthorisedUser(ua.getUsertype() != UserAuth.noPrivileges); |
|---|
| 1277 | | |
|---|
| 1278 | | if (debug) { |
|---|
| 1279 | | debugMsg("UserAuth says that current user has the type: " + |
|---|
| 1280 | | ua.getUsertype()); |
|---|
| 1281 | | } |
|---|
| 1282 | | } catch (Throwable t) { |
|---|
| 1283 | | form.getErr().emit("org.bedework.client.error.exc", t.getMessage()); |
|---|
| 1284 | | form.getErr().emit(t); |
|---|
| 1285 | | return null; |
|---|
| 1286 | | } |
|---|
| 1287 | | */ |
|---|
| 1288 | | |
|---|
| 1289 | | return s; |
|---|
| 1290 | | } |
|---|
| 1291 | | |
|---|
| 1292 | | private String getAppName(HttpSession sess) { |
|---|
| 1293 | | ServletContext sc = sess.getServletContext(); |
|---|
| 1294 | | |
|---|
| 1295 | | String appname = sc.getInitParameter(appNameInitParameter); |
|---|
| 1296 | | if (appname == null) { |
|---|
| 1297 | | appname = "?"; |
|---|
| 1298 | | } |
|---|
| 1299 | | |
|---|
| 1300 | | return appname; |
|---|
| 1301 | | } |
|---|
| 1302 | | |
|---|
| 1330 | | } |
|---|
| 1331 | | |
|---|
| 1332 | | /** Ensure we have a CalAdminSvcI object for the given user. |
|---|
| 1333 | | * |
|---|
| 1334 | | * <p>For an admin client with a super user we may switch to a different |
|---|
| 1335 | | * user to administer their events. |
|---|
| 1336 | | * |
|---|
| 1337 | | * @param request Needed to locate session |
|---|
| 1338 | | * @param form Action form |
|---|
| 1339 | | * @param sess Session object for global parameters |
|---|
| 1340 | | * @param access int unadjusted access |
|---|
| 1341 | | * @param user String user we want to be |
|---|
| 1342 | | * @param publicAdmin true if this is an administrative client |
|---|
| 1343 | | * @param canSwitch true if we should definitely allow user to switch |
|---|
| 1344 | | * this allows a user to switch between and into |
|---|
| 1345 | | * groups of which they are a member |
|---|
| 1346 | | * @param debug true for all that debugging stuff |
|---|
| 1347 | | * @return boolean false for problems. |
|---|
| 1348 | | * @throws CalFacadeException |
|---|
| 1349 | | */ |
|---|
| 1350 | | private boolean checkSvci(HttpServletRequest request, |
|---|
| 1351 | | BwActionFormBase form, |
|---|
| 1352 | | BwSession sess, |
|---|
| 1353 | | int access, |
|---|
| 1354 | | String user, |
|---|
| 1355 | | boolean publicAdmin, |
|---|
| 1356 | | boolean canSwitch, |
|---|
| 1357 | | boolean debug) throws CalFacadeException { |
|---|
| 1358 | | /** Do some checks first |
|---|
| 1359 | | */ |
|---|
| 1360 | | String authUser = String.valueOf(form.getCurrentUser()); |
|---|
| 1361 | | |
|---|
| 1362 | | if (!publicAdmin) { |
|---|
| 1363 | | /* We're never allowed to switch identity as a user client. |
|---|
| 1364 | | */ |
|---|
| 1365 | | if (!authUser.equals(String.valueOf(user))) { |
|---|
| 1366 | | return false; |
|---|
| 1367 | | } |
|---|
| 1368 | | } else if (user == null) { |
|---|
| 1369 | | throw new CalFacadeException("Null user parameter for public admin."); |
|---|
| 1370 | | } |
|---|
| 1371 | | |
|---|
| 1372 | | CalSvcI svci = BwWebUtil.getCalSvcI(request); |
|---|
| 1373 | | |
|---|
| 1374 | | /** Make some checks to see if this is an old - restarted session. |
|---|
| 1375 | | If so discard the svc interface |
|---|
| 1376 | | */ |
|---|
| 1377 | | if (svci != null) { |
|---|
| 1378 | | if (!svci.isOpen()) { |
|---|
| 1379 | | svci = null; |
|---|
| 1380 | | info(".Svci interface discarded from old session"); |
|---|
| 1381 | | } |
|---|
| 1382 | | } |
|---|
| 1383 | | |
|---|
| 1384 | | if (svci != null) { |
|---|
| 1385 | | /* Already there and already opened */ |
|---|
| 1386 | | if (debug) { |
|---|
| 1387 | | debugMsg("CalSvcI-- Obtained from session for user " + |
|---|
| 1388 | | svci.getUser()); |
|---|
| 1389 | | } |
|---|
| 1390 | | |
|---|
| 1391 | | // XXX access - disable use of roles |
|---|
| 1392 | | access = svci.getUserAuth().getUsertype(); |
|---|
| 1393 | | } else { |
|---|
| 1394 | | if (debug) { |
|---|
| 1395 | | debugMsg(".CalSvcI-- get new object for user " + user); |
|---|
| 1396 | | } |
|---|
| 1397 | | |
|---|
| 1398 | | /* create a call back object so the filter can open the service |
|---|
| 1399 | | interface */ |
|---|
| 1400 | | BwCallback cb = new Callback(form); |
|---|
| 1401 | | HttpSession hsess = request.getSession(); |
|---|
| 1402 | | hsess.setAttribute(BwCallback.cbAttrName, cb); |
|---|
| 1403 | | |
|---|
| 1404 | | String runAsUser = user; |
|---|
| 1405 | | String calSuite = form.retrieveConfig().getCalSuite(); |
|---|
| 1406 | | |
|---|
| 1407 | | try { |
|---|
| 1408 | | svci = new CalSvc(); |
|---|
| 1409 | | if (publicAdmin || (user == null)) { |
|---|
| 1410 | | if (calSuite == null) { |
|---|
| 1411 | | runAsUser = form.getEnv().getAppProperty("run.as.user"); |
|---|
| 1412 | | } |
|---|
| 1413 | | } |
|---|
| 1414 | | |
|---|
| 1415 | | CalSvcIPars pars = new CalSvcIPars(user, //access, |
|---|
| 1416 | | runAsUser, |
|---|
| 1417 | | calSuite, |
|---|
| 1418 | | form.getEnv().getAppPrefix(), |
|---|
| 1419 | | publicAdmin, |
|---|
| 1420 | | false, // caldav |
|---|
| 1421 | | null, // synchId |
|---|
| 1422 | | debug); |
|---|
| 1423 | | svci.init(pars); |
|---|
| 1424 | | |
|---|
| 1425 | | BwWebUtil.setCalSvcI(request, svci); |
|---|
| 1426 | | |
|---|
| 1427 | | form.setCalSvcI(svci); |
|---|
| 1428 | | |
|---|
| 1429 | | cb.in(true); |
|---|
| 1430 | | |
|---|
| 1431 | | UserAuth ua = null; |
|---|
| 1432 | | UserAuthPar par = new UserAuthPar(); |
|---|
| 1433 | | par.svlt = servlet; |
|---|
| 1434 | | par.req = request; |
|---|
| 1435 | | |
|---|
| 1436 | | if (publicAdmin) { |
|---|
| 1437 | | try { |
|---|
| 1438 | | ua = svci.getUserAuth(user, par); |
|---|
| 1439 | | |
|---|
| 1440 | | form.assignAuthorisedUser(ua.getUsertype() != UserAuth.noPrivileges); |
|---|
| 1441 | | svci.setSuperUser((ua.getUsertype() & UserAuth.superUser) != 0); |
|---|
| 1442 | | |
|---|
| 1443 | | // XXX access - disable use of roles |
|---|
| 1444 | | access = ua.getUsertype(); |
|---|
| 1445 | | |
|---|
| 1446 | | if (debug) { |
|---|
| 1447 | | debugMsg("UserAuth says that current user has the type: " + |
|---|
| 1448 | | ua.getUsertype()); |
|---|
| 1449 | | } |
|---|
| 1450 | | } catch (Throwable t) { |
|---|
| 1451 | | form.getErr().emit("org.bedework.client.error.exc", t.getMessage()); |
|---|
| 1452 | | form.getErr().emit(t); |
|---|
| 1453 | | return false; |
|---|
| 1454 | | } |
|---|
| 1455 | | } |
|---|
| 1456 | | } catch (CalFacadeException cfe) { |
|---|
| 1457 | | throw cfe; |
|---|
| 1458 | | } catch (Throwable t) { |
|---|
| 1459 | | throw new CalFacadeException(t); |
|---|
| 1460 | | } |
|---|
| 1461 | | } |
|---|
| 1462 | | |
|---|
| 1463 | | form.assignUserVO((BwUser)svci.getUser().clone()); |
|---|
| 1464 | | |
|---|
| 1465 | | if (publicAdmin) { |
|---|
| 1466 | | canSwitch = canSwitch || ((access & UserAuth.contentAdminUser) != 0) || |
|---|
| 1467 | | ((access & UserAuth.superUser) != 0); |
|---|
| 1468 | | |
|---|
| 1469 | | BwUser u = svci.getUser(); |
|---|
| 1470 | | if (u == null) { |
|---|
| 1471 | | throw new CalFacadeException("Null user for public admin."); |
|---|
| 1472 | | } |
|---|
| 1473 | | |
|---|
| 1474 | | String curUser = u.getAccount(); |
|---|
| 1475 | | |
|---|
| 1476 | | if (!canSwitch && !user.equals(curUser)) { |
|---|
| 1477 | | /** Trying to switch but not allowed */ |
|---|
| 1478 | | return false; |
|---|
| 1479 | | } |
|---|
| 1480 | | |
|---|
| 1481 | | if (!user.equals(curUser)) { |
|---|
| 1482 | | /** Switching user */ |
|---|
| 1483 | | svci.setUser(user); |
|---|
| 1484 | | curUser = user; |
|---|
| 1485 | | } |
|---|
| 1486 | | |
|---|
| 1487 | | form.assignCurrentAdminUser(curUser); |
|---|
| 1488 | | } |
|---|
| 1489 | | |
|---|
| 1490 | | return true; |
|---|
| 1491 | | } |
|---|
| 1492 | | |
|---|
| 1493 | | /** This method determines the access rights of the current user based on |
|---|
| 1494 | | * their assigned roles. There are two sections to this which appear to do |
|---|
| 1495 | | * the same thing. |
|---|
| 1496 | | * |
|---|
| 1497 | | * <p>They are there because some servlet containers (jetty for one) |
|---|
| 1498 | | * appeared to be broken. Role mapping does not appear to work reliably. |
|---|
| 1499 | | * This seems to have something to do with jetty doing internal redirects |
|---|
| 1500 | | * to handle login. In the process it seems to lose the appropriate servlet |
|---|
| 1501 | | * context and with it the mapping of roles. |
|---|
| 1502 | | * |
|---|
| 1503 | | * @param req HttpServletRequest |
|---|
| 1504 | | * @param messages MessageResources |
|---|
| 1505 | | * @return int access |
|---|
| 1506 | | * @throws CalFacadeException |
|---|
| 1507 | | */ |
|---|
| 1508 | | private int getAccess(HttpServletRequest req, |
|---|
| 1509 | | MessageResources messages) throws CalFacadeException { |
|---|
| 1510 | | int access = 0; |
|---|
| 1511 | | |
|---|
| 1512 | | /** This form works with broken containers. |
|---|
| 1513 | | */ |
|---|
| 1514 | | if (req.isUserInRole( |
|---|
| 1515 | | getMessages().getMessage("org.bedework.role.admin"))) { |
|---|
| 1516 | | access += UserAuth.superUser; |
|---|
| 1517 | | } |
|---|
| 1518 | | |
|---|
| 1519 | | if (req.isUserInRole( |
|---|
| 1520 | | getMessages().getMessage("org.bedework.role.contentadmin"))) { |
|---|
| 1521 | | access += UserAuth.contentAdminUser; |
|---|
| 1522 | | } |
|---|
| 1523 | | |
|---|
| 1524 | | if (req.isUserInRole( |
|---|
| 1525 | | getMessages().getMessage("org.bedework.role.alert"))) { |
|---|
| 1526 | | access += UserAuth.alertUser; |
|---|
| 1527 | | } |
|---|
| 1528 | | |
|---|
| 1529 | | if (req.isUserInRole( |
|---|
| 1530 | | getMessages().getMessage("org.bedework.role.owner"))) { |
|---|
| 1531 | | access += UserAuth.publicEventUser; |
|---|
| 1532 | | } |
|---|
| 1533 | | |
|---|
| 1534 | | /** This is how it ought to look |
|---|
| 1535 | | if (req.isUserInRole("admin")) { |
|---|
| 1536 | | access += UserAuth.superUser; |
|---|
| 1537 | | } |
|---|
| 1538 | | |
|---|
| 1539 | | if (req.isUserInRole("contentadmin")) { |
|---|
| 1540 | | access += UserAuth.contentAdminUser; |
|---|
| 1541 | | } |
|---|
| 1542 | | |
|---|
| 1543 | | if (req.isUserInRole("alert")) { |
|---|
| 1544 | | access += UserAuth.alertUser; |
|---|
| 1545 | | } |
|---|
| 1546 | | |
|---|
| 1547 | | if (req.isUserInRole("owner")) { |
|---|
| 1548 | | access += UserAuth.publicEventUser; |
|---|
| 1549 | | } */ |
|---|
| 1550 | | |
|---|
| 1551 | | return access; |
|---|
| | 1250 | /** Get the session state object for a web session. If we've already been |
|---|
| | 1251 | * here it's embedded in the current session. Otherwise create a new one. |
|---|
| | 1252 | * |
|---|
| | 1253 | * <p>We also carry out a number of web related operations. |
|---|
| | 1254 | * |
|---|
| | 1255 | * @param request HttpServletRequest Needed to locate session |
|---|
| | 1256 | * @param form Action form |
|---|
| | 1257 | * @param messages MessageResources needed for the resources |
|---|
| | 1258 | * @param adminUserId id we want to administer |
|---|
| | 1259 | * @param admin Get this for the admin client |
|---|
| | 1260 | * @return UWCalSession null on failure |
|---|
| | 1261 | * @throws Throwable |
|---|
| | 1262 | */ |
|---|
| | 1263 | private synchronized BwSession getState(HttpServletRequest request, |
|---|
| | 1264 | BwActionFormBase form, |
|---|
| | 1265 | MessageResources messages, |
|---|
| | 1266 | String adminUserId, |
|---|
| | 1267 | boolean admin) throws Throwable { |
|---|
| | 1268 | BwSession s = BwWebUtil.getState(request); |
|---|
| | 1269 | HttpSession sess = request.getSession(false); |
|---|
| | 1270 | String appName = getAppName(sess); |
|---|
| | 1271 | |
|---|
| | 1272 | if (s != null) { |
|---|
| | 1273 | if (debug) { |
|---|
| | 1274 | debugMsg("getState-- obtainedfrom session"); |
|---|
| | 1275 | debugMsg("getState-- timeout interval = " + |
|---|
| | 1276 | sess.getMaxInactiveInterval()); |
|---|
| | 1277 | } |
|---|
| | 1278 | |
|---|
| | 1279 | form.assignNewSession(false); |
|---|
| | 1280 | } else { |
|---|
| | 1281 | if (debug) { |
|---|
| | 1282 | debugMsg("getState-- get new object"); |
|---|
| | 1283 | } |
|---|
| | 1284 | |
|---|
| | 1285 | form.assignNewSession(true); |
|---|
| | 1286 | |
|---|
| | 1287 | CalEnv env = getEnv(request, form); |
|---|
| | 1288 | String appRoot = env.getAppProperty("root"); |
|---|
| | 1289 | |
|---|
| | 1290 | /** The actual session class used is possibly site dependent |
|---|
| | 1291 | */ |
|---|
| | 1292 | s = new BwSessionImpl(form.getCurrentUser(), appRoot, appName, |
|---|
| | 1293 | form.getPresentationState(), messages, |
|---|
| | 1294 | form.getSchemeHostPort(), debug); |
|---|
| | 1295 | |
|---|
| | 1296 | BwWebUtil.setState(request, s); |
|---|
| | 1297 | |
|---|
| | 1298 | setSessionAttr(request, "cal.pubevents.client.uri", |
|---|
| | 1299 | messages.getMessage("org.bedework.public.calendar.uri")); |
|---|
| | 1300 | |
|---|
| | 1301 | setSessionAttr(request, "cal.personal.client.uri", |
|---|
| | 1302 | messages.getMessage("org.bedework.personal.calendar.uri")); |
|---|
| | 1303 | |
|---|
| | 1304 | setSessionAttr(request, "cal.admin.client.uri", |
|---|
| | 1305 | messages.getMessage("org.bedework.public.admin.uri")); |
|---|
| | 1306 | |
|---|
| | 1307 | String temp = messages.getMessage("org.bedework.host"); |
|---|
| | 1308 | if (temp == null) { |
|---|
| | 1309 | temp = form.getSchemeHostPort(); |
|---|
| | 1310 | } |
|---|
| | 1311 | |
|---|
| | 1312 | setSessionAttr(request, "cal.server.host", temp); |
|---|
| | 1313 | |
|---|
| | 1314 | String raddr = request.getRemoteAddr(); |
|---|
| | 1315 | String rhost = request.getRemoteHost(); |
|---|
| | 1316 | info("===============" + appName + ": New session (" + |
|---|
| | 1317 | s.getSessionNum() + ") from " + |
|---|
| | 1318 | rhost + "(" + raddr + ")"); |
|---|
| | 1319 | |
|---|
| | 1320 | if (!admin) { |
|---|
| | 1321 | /** Ensure the session timeout interval is longer than our refresh period |
|---|
| | 1322 | */ |
|---|
| | 1323 | // Should come from db -- int refInt = s.getRefreshInterval(); |
|---|
| | 1324 | int refInt = 60; // 1 min refresh? |
|---|
| | 1325 | |
|---|
| | 1326 | if (refInt > 0) { |
|---|
| | 1327 | int timeout = sess.getMaxInactiveInterval(); |
|---|
| | 1328 | |
|---|
| | 1329 | if (timeout <= refInt) { |
|---|
| | 1330 | // An extra minute should do it. |
|---|
| | 1331 | debugMsg("@+@+@+@+@+ set timeout to " + (refInt + 60)); |
|---|
| | 1332 | sess.setMaxInactiveInterval(refInt + 60); |
|---|
| | 1333 | } |
|---|
| | 1334 | } |
|---|
| | 1335 | } |
|---|
| | 1336 | } |
|---|
| | 1337 | |
|---|
| | 1338 | int access = getAccess(request, messages); |
|---|
| | 1339 | if (debug) { |
|---|
| | 1340 | debugMsg("Container says that current user has the type: " + access); |
|---|
| | 1341 | } |
|---|
| | 1342 | |
|---|
| | 1343 | /** Ensure we have a CalAdminSvcI object |
|---|
| | 1344 | */ |
|---|
| | 1345 | String calSuite = form.retrieveConfig().getCalSuite(); |
|---|
| | 1346 | checkSvci(request, form, s, access, adminUserId, calSuite, |
|---|
| | 1347 | getPublicAdmin(form), false, debug); |
|---|
| | 1348 | |
|---|
| | 1349 | /* |
|---|
| | 1350 | UserAuth ua = null; |
|---|
| | 1351 | UserAuthPar par = new UserAuthPar(); |
|---|
| | 1352 | par.svlt = servlet; |
|---|
| | 1353 | par.req = request; |
|---|
| | 1354 | |
|---|
| | 1355 | try { |
|---|
| | 1356 | ua = form.fetchSvci().getUserAuth(s.getUser(), par); |
|---|
| | 1357 | |
|---|
| | 1358 | form.assignAuthorisedUser(ua.getUsertype() != UserAuth.noPrivileges); |
|---|
| | 1359 | |
|---|
| | 1360 | if (debug) { |
|---|
| | 1361 | debugMsg("UserAuth says that current user has the type: " + |
|---|
| | 1362 | ua.getUsertype()); |
|---|
| | 1363 | } |
|---|
| | 1364 | } catch (Throwable t) { |
|---|
| | 1365 | form.getErr().emit("org.bedework.client.error.exc", t.getMessage()); |
|---|
| | 1366 | form.getErr().emit(t); |
|---|
| | 1367 | return null; |
|---|
| | 1368 | } |
|---|
| | 1369 | */ |
|---|
| | 1370 | |
|---|
| | 1371 | return s; |
|---|
| | 1372 | } |
|---|
| | 1373 | |
|---|
| | 1374 | private String getAppName(HttpSession sess) { |
|---|
| | 1375 | ServletContext sc = sess.getServletContext(); |
|---|
| | 1376 | |
|---|
| | 1377 | String appname = sc.getInitParameter(appNameInitParameter); |
|---|
| | 1378 | if (appname == null) { |
|---|
| | 1379 | appname = "?"; |
|---|
| | 1380 | } |
|---|
| | 1381 | |
|---|
| | 1382 | return appname; |
|---|
| | 1383 | } |
|---|
| | 1384 | |
|---|
| | 1385 | private int setGroup(HttpServletRequest request, |
|---|
| | 1386 | BwActionFormBase form, |
|---|
| | 1387 | Groups adgrps, |
|---|
| | 1388 | BwAdminGroup adg) throws Throwable { |
|---|
| | 1389 | CalSvcI svci = form.fetchSvci(); |
|---|
| | 1390 | |
|---|
| | 1391 | adgrps.getMembers(adg); |
|---|
| | 1392 | |
|---|
| | 1393 | if (debug) { |
|---|
| | 1394 | logIt("Set admin group to " + adg); |
|---|
| | 1395 | } |
|---|
| | 1396 | |
|---|
| | 1397 | /* Determine which calsuites they are administering */ |
|---|
| | 1398 | |
|---|
| | 1399 | Collection css = findAllCalSuites(svci, adg, adgrps); |
|---|
| | 1400 | |
|---|
| | 1401 | if (css.size() > 1) { |
|---|
| | 1402 | form.getErr().emit("org.bedework.error.cannot.handle.this.yet"); |
|---|
| | 1403 | return forwardError; |
|---|
| | 1404 | } |
|---|
| | 1405 | |
|---|
| | 1406 | BwCalSuiteWrapper cs = null; |
|---|
| | 1407 | String calSuiteName = null; |
|---|
| | 1408 | |
|---|
| | 1409 | if (css.size() == 1) { |
|---|
| | 1410 | cs = (BwCalSuiteWrapper)css.iterator().next(); |
|---|
| | 1411 | calSuiteName = cs.getName(); |
|---|
| | 1412 | } |
|---|
| | 1413 | |
|---|
| | 1414 | if (debug) { |
|---|
| | 1415 | debugMsg("Found calSuite " + cs); |
|---|
| | 1416 | } else { |
|---|
| | 1417 | debugMsg("No calsuite found"); |
|---|
| | 1418 | } |
|---|
| | 1419 | |
|---|
| | 1420 | form.setCurrentCalSuite(cs); |
|---|
| | 1421 | form.assignAdminGroup(adg); |
|---|
| | 1422 | |
|---|
| | 1423 | int access = getAccess(request, getMessages()); |
|---|
| | 1424 | |
|---|
| | 1425 | if (!checkSvci(request, form, form.getSession(), access, |
|---|
| | 1426 | adg.getOwner().getAccount(), |
|---|
| | 1427 | calSuiteName, true, isMember(adg, form), debug)) { |
|---|
| | 1428 | return forwardNoAccess; |
|---|
| | 1429 | } |
|---|
| | 1430 | |
|---|
| | 1431 | form.setAdminUserId(form.fetchSvci().getUser().getAccount()); |
|---|
| | 1432 | |
|---|
| | 1433 | return forwardNoAction; |
|---|
| | 1434 | } |
|---|
| | 1435 | |
|---|
| | 1436 | private boolean isMember(BwAdminGroup ag, |
|---|
| | 1437 | BwActionFormBase form) throws Throwable { |
|---|
| | 1438 | return ag.isMember(String.valueOf(form.getCurrentUser()), false); |
|---|
| | 1439 | } |
|---|
| | 1440 | |
|---|
| | 1441 | /** Ensure we have a CalAdminSvcI object for the given user. |
|---|
| | 1442 | * |
|---|
| | 1443 | * <p>For an admin client with a super user we may switch to a different |
|---|
| | 1444 | * user to administer their events. |
|---|
| | 1445 | * |
|---|
| | 1446 | * @param request Needed to locate session |
|---|
| | 1447 | * @param form Action form |
|---|
| | 1448 | * @param sess Session object for global parameters |
|---|
| | 1449 | * @param access int unadjusted access |
|---|
| | 1450 | * @param user String user we want to be |
|---|
| | 1451 | * @param calSuite Name of calendar suite we are administering |
|---|
| | 1452 | * @param publicAdmin true if this is an administrative client |
|---|
| | 1453 | * @param canSwitch true if we should definitely allow user to switch |
|---|
| | 1454 | * this allows a user to switch between and into |
|---|
| | 1455 | * groups of which they are a member |
|---|
| | 1456 | * @param debug true for all that debugging stuff |
|---|
| | 1457 | * @return boolean false for problems. |
|---|
| | 1458 | * @throws CalFacadeException |
|---|
| | 1459 | */ |
|---|
| | 1460 | private boolean checkSvci(HttpServletRequest request, |
|---|
| | 1461 | BwActionFormBase form, |
|---|
| | 1462 | BwSession sess, |
|---|
| | 1463 | int access, |
|---|
| | 1464 | String user, |
|---|
| | 1465 | String calSuite, |
|---|
| | 1466 | boolean publicAdmin, |
|---|
| | 1467 | boolean canSwitch, |
|---|
| | 1468 | boolean debug) throws CalFacadeException { |
|---|
| | 1469 | /** Do some checks first |
|---|
| | 1470 | */ |
|---|
| | 1471 | String authUser = String.valueOf(form.getCurrentUser()); |
|---|
| | 1472 | |
|---|
| | 1473 | if (!publicAdmin) { |
|---|
| | 1474 | /* We're never allowed to switch identity as a user client. |
|---|
| | 1475 | */ |
|---|
| | 1476 | if (!authUser.equals(String.valueOf(user))) { |
|---|
| | 1477 | return false; |
|---|
| | 1478 | } |
|---|
| | 1479 | } else if (user == null) { |
|---|
| | 1480 | throw new CalFacadeException("Null user parameter for public admin."); |
|---|
| | 1481 | } |
|---|
| | 1482 | |
|---|
| | 1483 | CalSvcI svci = BwWebUtil.getCalSvcI(request); |
|---|
| | 1484 | |
|---|
| | 1485 | /** Make some checks to see if this is an old - restarted session. |
|---|
| | 1486 | If so discard the svc interface |
|---|
| | 1487 | */ |
|---|
| | 1488 | if (svci != null) { |
|---|
| | 1489 | if (!svci.isOpen()) { |
|---|
| | 1490 | svci = null; |
|---|
| | 1491 | info(".Svci interface discarded from old session"); |
|---|
| | 1492 | } |
|---|
| | 1493 | } |
|---|
| | 1494 | |
|---|
| | 1495 | if (svci != null) { |
|---|
| | 1496 | /* Already there and already opened */ |
|---|
| | 1497 | if (debug) { |
|---|
| | 1498 | debugMsg("CalSvcI-- Obtained from session for user " + |
|---|
| | 1499 | svci.getUser()); |
|---|
| | 1500 | } |
|---|
| | 1501 | |
|---|
| | 1502 | // XXX access - disable use of roles |
|---|
| | 1503 | access = svci.getUserAuth().getUsertype(); |
|---|
| | 1504 | } else { |
|---|
| | 1505 | if (debug) { |
|---|
| | 1506 | debugMsg(".CalSvcI-- get new object for user " + user); |
|---|
| | 1507 | } |
|---|
| | 1508 | |
|---|
| | 1509 | /* create a call back object so the filter can open the service |
|---|
| | 1510 | interface */ |
|---|
| | 1511 | BwCallback cb = new Callback(form); |
|---|
| | 1512 | HttpSession hsess = request.getSession(); |
|---|
| | 1513 | hsess.setAttribute(BwCallback.cbAttrName, cb); |
|---|
| | 1514 | |
|---|
| | 1515 | String runAsUser = user; |
|---|
| | 1516 | |
|---|
| | 1517 | try { |
|---|
| | 1518 | svci = new CalSvc(); |
|---|
| | 1519 | if (publicAdmin || (user == null)) { |
|---|
| | 1520 | if (calSuite == null) { |
|---|
| | 1521 | runAsUser = form.getEnv().getAppProperty("run.as.user"); |
|---|
| | 1522 | } |
|---|
| | 1523 | } |
|---|
| | 1524 | |
|---|
| | 1525 | CalSvcIPars pars = new CalSvcIPars(user, //access, |
|---|
| | 1526 | runAsUser, |
|---|
| | 1527 | calSuite, |
|---|
| | 1528 | form.getEnv().getAppPrefix(), |
|---|
| | 1529 | publicAdmin, |
|---|
| | 1530 | false, // caldav |
|---|
| | 1531 | null, // synchId |
|---|
| | 1532 | debug); |
|---|
| | 1533 | svci.init(pars); |
|---|
| | 1534 | |
|---|
| | 1535 | BwWebUtil.setCalSvcI(request, svci); |
|---|
| | 1536 | |
|---|
| | 1537 | form.setCalSvcI(svci); |
|---|
| | 1538 | |
|---|
| | 1539 | cb.in(true); |
|---|
| | 1540 | |
|---|
| | 1541 | UserAuth ua = null; |
|---|
| | 1542 | UserAuthPar par = new UserAuthPar(); |
|---|
| | 1543 | par.svlt = servlet; |
|---|
| | 1544 | par.req = request; |
|---|
| | 1545 | |
|---|
| | 1546 | if (publicAdmin) { |
|---|
| | 1547 | try { |
|---|
| | 1548 | ua = svci.getUserAuth(user, par); |
|---|
| | 1549 | |
|---|
| | 1550 | form.assignAuthorisedUser(ua.getUsertype() != UserAuth.noPrivileges); |
|---|
| | 1551 | svci.setSuperUser((ua.getUsertype() & UserAuth.superUser) != 0); |
|---|
| | 1552 | |
|---|
| | 1553 | // XXX access - disable use of roles |
|---|
| | 1554 | access = ua.getUsertype(); |
|---|
| | 1555 | |
|---|
| | 1556 | if (debug) { |
|---|
| | 1557 | debugMsg("UserAuth says that current user has the type: " + |
|---|
| | 1558 | ua.getUsertype()); |
|---|
| | 1559 | } |
|---|
| | 1560 | } catch (Throwable t) { |
|---|
| | 1561 | form.getErr().emit("org.bedework.client.error.exc", t.getMessage()); |
|---|
| | 1562 | form.getErr().emit(t); |
|---|
| | 1563 | return false; |
|---|
| | 1564 | } |
|---|
| | 1565 | } |
|---|
| | 1566 | } catch (CalFacadeException cfe) { |
|---|
| | 1567 | throw cfe; |
|---|
| | 1568 | } catch (Throwable t) { |
|---|
| | 1569 | throw new CalFacadeException(t); |
|---|
| | 1570 | } |
|---|
| | 1571 | } |
|---|
| | 1572 | |
|---|
| | 1573 | form.assignUserVO((BwUser)svci.getUser().clone()); |
|---|
| | 1574 | |
|---|
| | 1575 | if (publicAdmin) { |
|---|
| | 1576 | canSwitch = canSwitch || ((access & UserAuth.contentAdminUser) != 0) || |
|---|
| | 1577 | ((access & UserAuth.superUser) != 0); |
|---|
| | 1578 | |
|---|
| | 1579 | BwUser u = svci.getUser(); |
|---|
| | 1580 | if (u == null) { |
|---|
| | 1581 | throw new CalFacadeException("Null user for public admin."); |
|---|
| | 1582 | } |
|---|
| | 1583 | |
|---|
| | 1584 | String curUser = u.getAccount(); |
|---|
| | 1585 | |
|---|
| | 1586 | if (!canSwitch && !user.equals(curUser)) { |
|---|
| | 1587 | /** Trying to switch but not allowed */ |
|---|
| | 1588 | return false; |
|---|
| | 1589 | } |
|---|
| | 1590 | |
|---|
| | 1591 | if (!user.equals(curUser)) { |
|---|
| | 1592 | /** Switching user */ |
|---|
| | 1593 | svci.setUser(user); |
|---|
| | 1594 | curUser = user; |
|---|
| | 1595 | } |
|---|
| | 1596 | |
|---|
| | 1597 | form.assignCurrentAdminUser(curUser); |
|---|
| | 1598 | } |
|---|
| | 1599 | |
|---|
| | 1600 | return true; |
|---|
| | 1601 | } |
|---|
| | 1602 | |
|---|
| | 1603 | /** This method determines the access rights of the current user based on |
|---|
| | 1604 | * their assigned roles. There are two sections to this which appear to do |
|---|
| | 1605 | * the same thing. |
|---|
| | 1606 | * |
|---|
| | 1607 | * <p>They are there because some servlet containers (jetty for one) |
|---|
| | 1608 | * appeared to be broken. Role mapping does not appear to work reliably. |
|---|
| | 1609 | * This seems to have something to do with jetty doing internal redirects |
|---|
| | 1610 | * to handle login. In the process it seems to lose the appropriate servlet |
|---|
| | 1611 | * context and with it the mapping of roles. |
|---|
| | 1612 | * |
|---|
| | 1613 | * @param req HttpServletRequest |
|---|
| | 1614 | * @param messages MessageResources |
|---|
| | 1615 | * @return int access |
|---|
| | 1616 | * @throws CalFacadeException |
|---|
| | 1617 | */ |
|---|
| | 1618 | private int getAccess(HttpServletRequest req, |
|---|
| | 1619 | MessageResources messages) throws CalFacadeException { |
|---|
| | 1620 | int access = 0; |
|---|
| | 1621 | |
|---|
| | 1622 | /** This form works with broken containers. |
|---|
| | 1623 | */ |
|---|
| | 1624 | if (req.isUserInRole( |
|---|
| | 1625 | getMessages().getMessage("org.bedework.role.admin"))) { |
|---|
| | 1626 | access += UserAuth.superUser; |
|---|
| | 1627 | } |
|---|
| | 1628 | |
|---|
| | 1629 | if (req.isUserInRole( |
|---|
| | 1630 | getMessages().getMessage("org.bedework.role.contentadmin"))) { |
|---|
| | 1631 | access += UserAuth.contentAdminUser; |
|---|
| | 1632 | } |
|---|
| | 1633 | |
|---|
| | 1634 | if (req.isUserInRole( |
|---|
| | 1635 | getMessages().getMessage("org.bedework.role.alert"))) { |
|---|
| | 1636 | access += UserAuth.alertUser; |
|---|
| | 1637 | } |
|---|
| | 1638 | |
|---|
| | 1639 | if (req.isUserInRole( |
|---|
| | 1640 | getMessages().getMessage("org.bedework.role.owner"))) { |
|---|
| | 1641 | access += UserAuth.publicEventUser; |
|---|
| | 1642 | } |
|---|
| | 1643 | |
|---|
| | 1644 | /** This is how it ought to look |
|---|
| | 1645 | if (req.isUserInRole("admin")) { |
|---|
| | 1646 | access += UserAuth.superUser; |
|---|
| | 1647 | } |
|---|
| | 1648 | |
|---|
| | 1649 | if (req.isUserInRole("contentadmin")) { |
|---|
| | 1650 | access += UserAuth.contentAdminUser; |
|---|
| | 1651 | } |
|---|
| | 1652 | |
|---|
| | 1653 | if (req.isUserInRole("alert")) { |
|---|
| | 1654 | access += UserAuth.alertUser; |
|---|
| | 1655 | } |
|---|
| | 1656 | |
|---|
| | 1657 | if (req.isUserInRole("owner")) { |
|---|
| | 1658 | access += UserAuth.publicEventUser; |
|---|
| | 1659 | } */ |
|---|
| | 1660 | |
|---|
| | 1661 | return access; |
|---|
| | 1662 | } |
|---|
| | 1663 | |
|---|
| | 1664 | private Collection findAllCalSuites(CalSvcI svc, |
|---|
| | 1665 | BwAdminGroup adg, |
|---|
| | 1666 | Groups adgrps) throws Throwable { |
|---|
| | 1667 | ArrayList al = new ArrayList(); |
|---|
| | 1668 | |
|---|
| | 1669 | BwCalSuiteWrapper cs = svc.getCalSuite(adg); |
|---|
| | 1670 | if (cs != null) { |
|---|
| | 1671 | al.add(cs); |
|---|
| | 1672 | } |
|---|
| | 1673 | |
|---|
| | 1674 | Iterator parents = ((AdminGroups)adgrps).findGroupParents(adg).iterator(); |
|---|
| | 1675 | |
|---|
| | 1676 | while (parents.hasNext()) { |
|---|
| | 1677 | al.addAll(findAllCalSuites(svc, (BwAdminGroup)parents.next(), adgrps)); |
|---|
| | 1678 | } |
|---|
| | 1679 | |
|---|
| | 1680 | return al; |
|---|
| | 1681 | } |
|---|
| | 1682 | |
|---|