1 64 65 70 package com.jcorporate.expresso.services.controller; 71 72 import com.jcorporate.expresso.core.controller.Block; 73 import com.jcorporate.expresso.core.controller.Controller; 74 import com.jcorporate.expresso.core.controller.ControllerException; 75 import com.jcorporate.expresso.core.controller.ControllerRequest; 76 import com.jcorporate.expresso.core.controller.ControllerResponse; 77 import com.jcorporate.expresso.core.controller.DBController; 78 import com.jcorporate.expresso.core.controller.Input; 79 import com.jcorporate.expresso.core.controller.NonHandleableException; 80 import com.jcorporate.expresso.core.controller.Output; 81 import com.jcorporate.expresso.core.controller.State; 82 import com.jcorporate.expresso.core.controller.Transition; 83 import com.jcorporate.expresso.core.db.DBException; 84 import com.jcorporate.expresso.core.dbobj.Schema; 85 import com.jcorporate.expresso.core.dbobj.SchemaFactory; 86 import com.jcorporate.expresso.core.dbobj.SecuredDBObject; 87 import com.jcorporate.expresso.core.dbobj.ValidValue; 88 import com.jcorporate.expresso.core.misc.ConfigManager; 89 import com.jcorporate.expresso.core.misc.StringUtil; 90 import com.jcorporate.expresso.core.servlet.StdServlet; 91 import com.jcorporate.expresso.services.dbobj.ControllerSecurity; 92 import com.jcorporate.expresso.services.dbobj.SchemaList; 93 import com.jcorporate.expresso.services.dbobj.UserGroup; 94 95 import java.util.Enumeration ; 96 import java.util.Hashtable ; 97 import java.util.Iterator ; 98 import java.util.Vector ; 99 100 101 108 public class ControllerSecurityMatrix 109 extends DBController { 110 private static final String thisClass = ControllerSecurityMatrix.class.getName() + "."; 111 112 116 public ControllerSecurityMatrix() { 117 State prompt = new State("prompt", "Choose Schema and User Group"); 118 addState(prompt); 119 120 State selcon = new State("selcon", "Select Controller"); 121 selcon.addRequiredParameter("GroupName"); 122 selcon.addRequiredParameter("SchemaClass"); 123 addState(selcon); 124 125 State setcon = new State("setcon", "Set Allowed Controllers"); 126 setcon.addRequiredParameter("SchemaClass"); 127 setcon.addRequiredParameter("GroupName"); 128 addState(setcon); 129 130 State updcon = new State("updcon", "Update Allowed Controllers"); 131 updcon.addRequiredParameter("SchemaClass"); 132 updcon.addRequiredParameter("GroupName"); 133 addState(updcon); 134 135 State selstates = new State("selstates", "Select Allowed States"); 136 selstates.addRequiredParameter("GroupName"); 137 selstates.addRequiredParameter("ControllerClass"); 138 addState(selstates); 139 140 State updstates = new State("updstates", "Update Allowed States"); 141 updstates.addRequiredParameter("GroupName"); 142 updstates.addRequiredParameter("ControllerClass"); 143 addState(updstates); 144 145 this.setInitialState("prompt"); 146 this.setSchema(com.jcorporate.expresso.core.ExpressoSchema.class); 147 148 } 149 150 151 159 private void addStartAgainButton(ControllerRequest request, 160 ControllerResponse response) throws ControllerException { 161 162 Transition again = new Transition("Start Again", 163 getClass().getName()); 164 again.setName("again"); 165 again.addParam(STATE_PARAM_KEY, "prompt"); 166 response.addTransition(again); 167 } 168 169 170 177 private Schema getSchema(String className) 178 throws ControllerException { 179 return SchemaFactory.getInstance().getSchema(className); 180 } 181 182 183 191 private void getSelControllerMatrix(ControllerRequest params, 192 ControllerResponse myResponse) 193 throws ControllerException { 194 String myName = (thisClass + "getDBObjMatrix()"); 195 Schema mySchema = getSchema(params.getParameter("SchemaClass")); 196 197 if (mySchema == null) { 198 throw new ControllerException(myName + 199 ":Unable to instantiate the '" + 200 params.getParameter("SchemaClass") + 201 "' schema."); 202 } 203 try { 204 205 206 Block matrix = new Block("matrix"); 207 matrix.setAttribute("table", "Y"); 208 209 String head = ("Controller|Allowed?"); 210 matrix.setAttribute("header-row", head); 211 212 Block oneRow = new Block("row"); 213 oneRow.setAttribute("row", "Y"); 214 myResponse.add(matrix); 215 216 Controller oneController = null; 217 ControllerSecurity secur = new ControllerSecurity(SecuredDBObject.SYSTEM_ACCOUNT); 218 secur.setDataContext(params.getDataContext()); 219 220 Input cb = null; 221 222 for (Iterator e = mySchema.getControllerList().iterator(); 223 e.hasNext();) { 224 oneRow = new Block("row"); 225 oneRow.setAttribute("row", "Y"); 226 oneController = (Controller) e.next(); 227 oneRow.add(new Output(oneController.getTitle())); 228 229 230 231 232 boolean conAllowed = false; 233 secur.clear(); 234 secur.setField("ControllerClass", 235 oneController.getClass().getName()); 236 secur.setField("GroupName", params.getParameter("GroupName")); 237 238 if (secur.find()) { 239 conAllowed = true; 240 } 241 242 cb = new Input(oneController.getClass().getName()); 243 cb.setType("boolean"); 244 cb.setAttribute("checkbox", ""); 245 246 if (conAllowed) { 247 cb.setDefaultValue("Y"); 248 } else { 249 cb.setDefaultValue("N"); 250 } 251 252 oneRow.add(cb); 253 matrix.add(oneRow); 254 } 255 256 257 StdServlet oneServlet; 258 259 260 for (Enumeration se = mySchema.getServlets(); 261 se.hasMoreElements();) { 262 oneRow = new Block("row"); 263 oneRow.setAttribute("row", "Y"); 264 oneServlet = (StdServlet) se.nextElement(); 265 oneRow.add(new Output(oneServlet.getTitle())); 266 267 268 269 270 boolean conAllowed = false; 271 secur.clear(); 272 secur.setField("ControllerClass", 273 oneServlet.getClass().getName()); 274 secur.setField("GroupName", params.getParameter("GroupName")); 275 276 if (secur.find()) { 277 conAllowed = true; 278 } 279 280 cb = new Input(oneServlet.getClass().getName()); 281 cb.setType("boolean"); 282 cb.setAttribute("checkbox", ""); 283 284 if (conAllowed) { 285 cb.setDefaultValue("Y"); 286 } else { 287 cb.setDefaultValue("N"); 288 } 289 290 oneRow.add(cb); 291 matrix.add(oneRow); 292 } 293 294 } catch (DBException de) { 295 throw new ControllerException(myName + 296 ":Database exception reading " + 297 "security info", de); 298 } 299 } 300 301 302 310 private void getSelStatesMatrix(ControllerResponse myResponse, 311 ControllerRequest params) 312 throws ControllerException, 313 NonHandleableException { 314 Controller con = ConfigManager.getControllerFactory().getController(params.getParameter("ControllerClass")); 315 316 317 try { 318 319 320 Block matrix = new Block("matrix"); 321 matrix.setAttribute("table", "Y"); 322 323 String head = ("State|Allowed?"); 324 matrix.setAttribute("header-row", head); 325 326 Block oneRow = new Block("row"); 327 oneRow.setAttribute("row", "Y"); 328 myResponse.add(matrix); 329 330 ControllerSecurity secur = new ControllerSecurity(SecuredDBObject.SYSTEM_ACCOUNT); 331 secur.setDataContext(params.getDataContext()); 332 secur.setField("ControllerClass", con.getClass().getName()); 333 secur.setField("GroupName", params.getParameter("GroupName")); 334 335 String currentSecurity = (""); 336 337 if (secur.find()) { 338 currentSecurity = secur.getField("States"); 339 } 340 341 Input cb = null; 342 Hashtable allStates = con.getStates(); 343 String oneStateName; 344 State oneState; 345 346 for (Enumeration e = allStates.keys(); e.hasMoreElements();) { 347 oneRow = new Block("row"); 348 oneRow.setAttribute("row", "Y"); 349 oneStateName = (String ) e.nextElement(); 350 oneState = (State) allStates.get(oneStateName); 351 oneRow.add(new Output((String ) oneState.getDescription())); 352 353 354 355 356 boolean stateAllowed = false; 357 358 if (currentSecurity.indexOf(oneStateName) != -1) { 359 stateAllowed = true; 360 } 361 if (currentSecurity.indexOf("*") != -1) { 362 stateAllowed = true; 363 } 364 365 cb = new Input(oneStateName); 366 cb.setType("boolean"); 367 cb.setAttribute("checkbox", ""); 368 369 if (stateAllowed) { 370 cb.setDefaultValue("Y"); 371 } else { 372 cb.setDefaultValue("N"); 373 } 374 375 oneRow.add(cb); 376 matrix.add(oneRow); 377 } 378 379 } catch (DBException de) { 380 throw new ControllerException("Database exception reading " + 381 "security info", de); 382 } 383 } 384 385 386 391 public String getTitle() { 392 return ("Administer Controller Security"); 393 } 394 395 401 private void runPromptState(ControllerRequest params, 402 ControllerResponse myResponse) 403 throws ControllerException { 404 { 405 Input chooseGroup = new Input(); 406 chooseGroup.setLabel("Choose Group"); 407 chooseGroup.setName("GroupName"); 408 409 Vector v = new Vector (2); 410 411 try { 412 UserGroup gl = new UserGroup(SecuredDBObject.SYSTEM_ACCOUNT); 413 gl.setDataContext(params.getDataContext()); 414 415 UserGroup oneGroup = null; 416 417 for (Iterator e = gl.searchAndRetrieveList().iterator(); 418 e.hasNext();) { 419 oneGroup = (UserGroup) e.next(); 420 v.addElement(new ValidValue(oneGroup.getField("GroupName"), 421 oneGroup.getField("Descrip"))); 422 } 423 } catch (DBException de) { 424 throw new ControllerException("Unable to retrieve " + 425 "group information", de); 426 } 427 if (v.size() == 0) { 428 throw new ControllerException("There are no groups " + 429 "defined."); 430 } 431 432 chooseGroup.setValidValues(v); 433 myResponse.addInput(chooseGroup); 434 } 435 436 437 { 438 Input chooseSchema = new Input(); 439 chooseSchema.setLabel("Choose Schema"); 440 chooseSchema.setName("SchemaClass"); 441 442 Vector v = new Vector (2); 443 v.addElement(new ValidValue("com.jcorporate.expresso.core." + "ExpressoSchema", 444 "General")); 445 446 try { 447 Schema oneSchemaObj; 448 SchemaList sl = new SchemaList(SecuredDBObject.SYSTEM_ACCOUNT); 449 sl.setDataContext(params.getDataContext()); 450 451 SchemaList oneSchema = null; 452 453 for (Iterator e = sl.searchAndRetrieveList().iterator(); 454 e.hasNext();) { 455 oneSchema = (SchemaList) e.next(); 456 oneSchemaObj = getSchema(oneSchema.getField("SchemaClass")); 457 458 if (oneSchemaObj != null) { 459 if (oneSchemaObj.getControllerList().iterator().hasNext()) { 460 v.addElement(new ValidValue(oneSchema.getField("SchemaClass"), 461 oneSchema.getField("Descrip"))); 462 } 463 } 464 } 465 466 } catch (DBException de) { 467 throw new ControllerException("Unable to retrieve " + 468 "schema information", de); 469 } 470 471 chooseSchema.setValidValues(v); 472 myResponse.addInput(chooseSchema); 473 } 474 475 476 Transition setcon = new Transition("Select Allowed Controllers", 477 getClass().getName()); 478 setcon.setName("setcon"); 479 setcon.addParam(STATE_PARAM_KEY, "setcon"); 480 myResponse.addTransition(setcon); 481 482 Transition selcon = new Transition("Select Allowed States", 483 getClass().getName()); 484 selcon.setName("selcon"); 485 selcon.addParam(STATE_PARAM_KEY, "selcon"); 486 myResponse.addTransition(selcon); 487 } 488 489 490 496 private void runSelconState(ControllerRequest params, 497 ControllerResponse myResponse) 498 throws ControllerException { 499 String myName = (thisClass + "selControllerState()"); 500 Schema mySchema = getSchema(params.getParameter("SchemaClass")); 501 502 if (mySchema == null) { 503 throw new ControllerException(myName + 504 ":Can't find the schema class '" + 505 params.getParameter("SchemaClass") + 506 "'"); 507 } 508 509 Controller oneController; 510 Input chooseController = new Input(); 511 chooseController.setLabel("Choose Controller"); 512 chooseController.setName("ControllerClass"); 513 514 Vector v = new Vector (2); 515 516 for (Iterator e = mySchema.getControllerList().iterator(); e.hasNext();) { 517 oneController = (Controller) e.next(); 518 v.addElement(new ValidValue(oneController.getClass().getName(), 519 oneController.getTitle())); 520 } 521 522 if (v.size() == 0) { 523 throw new ControllerException(myName + 524 ":There are no controllers " + 525 "defined in the selected schema."); 526 } 527 528 chooseController.setValidValues(v); 529 myResponse.addInput(chooseController); 530 531 try { 532 UserGroup myGroup = new UserGroup(SecuredDBObject.SYSTEM_ACCOUNT); 533 myGroup.setDataContext(params.getDataContext()); 534 myGroup.setField("GroupName", params.getParameter("GroupName")); 535 myGroup.retrieve(); 536 myResponse.addOutput(new Output("Choose a Controller to administer states " + "for group '" + 537 myGroup.getField("Descrip") + 538 "'")); 539 } catch (DBException de) { 540 throw new ControllerException("Unable to retrieve group", de); 541 } 542 543 Transition selstates = new Transition("Select Allowed States", 544 getClass().getName()); 545 selstates.setName("selstates"); 546 selstates.addParam(STATE_PARAM_KEY, "selstates"); 547 selstates.addParam("GroupName", params.getParameter("GroupName")); 548 selstates.addParam("SchemaClass", params.getParameter("SchemaClass")); 549 myResponse.addTransition(selstates); 550 } 551 552 553 561 private void runSelstatesState(ControllerRequest params, 562 ControllerResponse myResponse) 563 throws ControllerException, NonHandleableException { 564 String myName = (thisClass + "selStatesState()"); 565 Controller oneController = ConfigManager.getControllerFactory().getController(params.getParameter( 566 "ControllerClass")); 567 568 try { 569 UserGroup oneGroup = new UserGroup(SecuredDBObject.SYSTEM_ACCOUNT); 570 oneGroup.setDataContext(params.getDataContext()); 571 oneGroup.setField("GroupName", params.getParameter("GroupName")); 572 oneGroup.retrieve(); 573 myResponse.addOutput(new Output("Select allowed States for Controller'" + oneController.getTitle() + 574 "' and group '" + 575 oneGroup.getField("Descrip") + 576 "'")); 577 getSelStatesMatrix(myResponse, params); 578 } catch (DBException de) { 579 throw new ControllerException(myName + 580 ":Unable to locate group '" + 581 params.getParameter("GroupName")); 582 } 583 584 Transition updstates = new Transition("Update", getClass().getName()); 585 updstates.setName("updstates"); 586 updstates.addParam(STATE_PARAM_KEY, "updstates"); 587 updstates.addParam("GroupName", params.getParameter("GroupName")); 588 updstates.addParam("ControllerClass", 589 params.getParameter("ControllerClass")); 590 myResponse.addTransition(updstates); 591 } 592 593 594 600 private void runSetconState(ControllerRequest params, 601 ControllerResponse myResponse) 602 throws ControllerException, 603 NonHandleableException { 604 String myName = (thisClass + "setControllerState()"); 605 606 607 608 try { 609 UserGroup myGroup = new UserGroup(SecuredDBObject.SYSTEM_ACCOUNT); 610 myGroup.setDataContext(params.getDataContext()); 611 myGroup.setField("GroupName", params.getParameter("GroupName")); 612 myGroup.retrieve(); 613 614 String schemaName; 615 616 if (params.getParameter("SchemaClass").equals("com.jcorporate." + "expresso.core.ExpressoSchema")) { 617 schemaName = ("General"); 618 } else { 619 SchemaList mySL = new SchemaList(SecuredDBObject.SYSTEM_ACCOUNT); 620 mySL.setDataContext(params.getDataContext()); 621 mySL.setField("SchemaClass", 622 params.getParameter("SchemaClass")); 623 mySL.retrieve(); 624 schemaName = mySL.getField("Descrip"); 625 } 626 627 myResponse.addOutput(new Output("Select allowed Controllers for group '" + myGroup.getField("Descrip") + 628 "' in Schema '" + schemaName + 629 "'")); 630 getSelControllerMatrix(params, myResponse); 631 } catch (DBException de) { 632 throw new ControllerException(myName + 633 ":Unable to read state, group or schema " + 634 "information", de); 635 } 636 637 Transition updcon = new Transition("Update", getClass().getName()); 638 updcon.setName("updcon"); 639 updcon.addParam(STATE_PARAM_KEY, "updcon"); 640 updcon.addParam("GroupName", params.getParameter("GroupName")); 641 updcon.addParam("SchemaClass", params.getParameter("SchemaClass")); 642 myResponse.addTransition(updcon); 643 } 644 645 646 653 private void runUpdconState(ControllerRequest request, 654 ControllerResponse response) 655 throws ControllerException { 656 String myName = (thisClass + "updControllerState()"); 657 Schema mySchema = getSchema(request.getParameter("SchemaClass")); 658 659 if (mySchema == null) { 660 throw new ControllerException(myName + 661 ":Can't find the schema class '" + 662 request.getParameter("SchemaClass") + 663 "'"); 664 } 665 666 try { 667 ControllerSecurity secur = new ControllerSecurity(SecuredDBObject.SYSTEM_ACCOUNT); 668 secur.setDataContext(request.getDataContext()); 669 670 Controller checkController; 671 int changes = 0; 672 673 for (Iterator pe = mySchema.getControllerList().iterator(); 674 pe.hasNext();) { 675 checkController = (Controller) pe.next(); 676 secur.clear(); 677 secur.setField("ControllerClass", 678 checkController.getClass().getName()); 679 secur.setField("GroupName", request.getParameter("GroupName")); 680 681 if (StringUtil.notNull(request.getParameter(checkController.getClass().getName())).equals("Y")) { 682 683 684 if (!secur.find()) { 685 secur.setField("States", "*"); 686 secur.add(); 687 response.addOutput(new Output("Permission granted for " + "Controller" + 688 checkController.getTitle())); 689 changes++; 690 } 691 } else { 692 693 694 if (secur.find()) { 695 secur.delete(); 696 response.addOutput(new Output("Permission removed for " + "Controller" + 697 checkController.getTitle())); 698 changes++; 699 } 700 } 701 702 } 703 704 705 706 StdServlet checkServlet; 707 708 for (Enumeration se = mySchema.getServlets(); 709 se.hasMoreElements();) { 710 checkServlet = (StdServlet) se.nextElement(); 711 secur.clear(); 712 secur.setField("ControllerClass", 713 checkServlet.getClass().getName()); 714 secur.setField("GroupName", request.getParameter("GroupName")); 715 716 if (StringUtil.notNull(request.getParameter(checkServlet.getClass().getName())).equals("Y")) { 717 718 719 if (!secur.find()) { 720 secur.setField("States", "*"); 721 secur.add(); 722 response.addOutput(new Output("Permission granted for " + "Servlet " + 723 checkServlet.getTitle())); 724 changes++; 725 } 726 } else { 727 728 729 if (secur.find()) { 730 secur.delete(); 731 response.addOutput(new Output("Permission removed for " + "Servlet " + 732 checkServlet.getTitle())); 733 changes++; 734 } 735 } 736 737 } 738 739 if (changes == 0) { 740 response.addOutput(new Output("No changes required")); 741 } 742 } catch (DBException de) { 743 throw new ControllerException(myName + 744 ":Unable to update security " + 745 "info", de); 746 } 747 } 748 749 750 757 private void runUpdstatesState(ControllerRequest request, 758 ControllerResponse response) 759 throws ControllerException, NonHandleableException { 760 String myName = (thisClass + "updStatesState()"); 761 Controller con = ConfigManager.getControllerFactory().getController(request.getParameter("ControllerClass")); 762 763 try { 764 Hashtable allStates = con.getStates(); 765 String oneStateName; 766 State oneState; 767 StringBuffer newSecurity = new StringBuffer (""); 768 boolean allAllowed = true; 769 770 for (Enumeration e = allStates.keys(); e.hasMoreElements();) { 771 oneStateName = (String ) e.nextElement(); 772 oneState = (State) allStates.get(oneStateName); 773 774 if (StringUtil.notNull(request.getParameter(oneStateName)).equals("Y")) { 775 newSecurity.append(oneStateName); 776 newSecurity.append(", "); 777 response.addOutput(new Output("Access granted to state '" + oneState.getDescription() + 778 "'")); 779 } else { 780 response.addOutput(new Output("Access denied to state '" + oneState.getDescription() + 781 "'")); 782 allAllowed = false; 783 } 784 } 785 786 787 ControllerSecurity secur = new ControllerSecurity(SecuredDBObject.SYSTEM_ACCOUNT); 788 secur.setDataContext(request.getDataContext()); 789 secur.setField("ControllerClass", con.getClass().getName()); 790 secur.setField("GroupName", request.getParameter("GroupName")); 791 792 if (allAllowed) { 793 newSecurity = new StringBuffer ("*"); 794 } 795 if (secur.find()) { 796 secur.setField("States", newSecurity.toString()); 797 secur.update(); 798 } else { 799 secur.setField("States", newSecurity.toString()); 800 secur.add(); 801 } 802 } catch (DBException de) { 803 throw new ControllerException(myName + 804 ":Database exception reading " + 805 "security info", de); 806 } 807 } 808 809 810 } 811 | Popular Tags |