1 64 65 package com.jcorporate.expresso.services.controller; 66 67 72 73 import com.jcorporate.expresso.core.controller.Block; 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.ErrorCollection; 79 import com.jcorporate.expresso.core.controller.Input; 80 import com.jcorporate.expresso.core.controller.NonHandleableException; 81 import com.jcorporate.expresso.core.controller.Output; 82 import com.jcorporate.expresso.core.controller.State; 83 import com.jcorporate.expresso.core.controller.Transition; 84 import com.jcorporate.expresso.core.db.DBException; 85 import com.jcorporate.expresso.core.dbobj.Schema; 86 import com.jcorporate.expresso.core.dbobj.SchemaFactory; 87 import com.jcorporate.expresso.core.dbobj.SecuredDBObject; 88 import com.jcorporate.expresso.core.dbobj.ValidValue; 89 import com.jcorporate.expresso.core.job.Job; 90 import com.jcorporate.expresso.core.misc.StringUtil; 91 import com.jcorporate.expresso.services.dbobj.JobSecurity; 92 import com.jcorporate.expresso.services.dbobj.SchemaList; 93 import com.jcorporate.expresso.services.dbobj.UserGroup; 94 import org.apache.log4j.Logger; 95 96 import java.util.Enumeration ; 97 import java.util.Hashtable ; 98 import java.util.Iterator ; 99 import java.util.Vector ; 100 101 102 110 public class JobSecurityMatrix 111 extends DBController { 112 private static final String thisClass = JobSecurityMatrix.class.getName() + "."; 113 private static Logger log = Logger.getLogger(JobSecurityMatrix.class); 114 115 119 public JobSecurityMatrix() { 120 State prompt = new State("prompt", "Prompt for Schema"); 121 addState(prompt); 122 setInitialState("prompt"); 123 124 State seljob = new State("seljob", "Select Job"); 125 seljob.addRequiredParameter("GroupName"); 126 seljob.addRequiredParameter("SchemaClass"); 127 addState(seljob); 128 129 State setjob = new State("setjob", "Set Allowed Jobs"); 130 setjob.addRequiredParameter("SchemaClass"); 131 setjob.addRequiredParameter("GroupName"); 132 addState(setjob); 133 134 State updjob = new State("updjob", "Update Allowed Jobs"); 135 updjob.addRequiredParameter("SchemaClass"); 136 updjob.addRequiredParameter("GroupName"); 137 addState(updjob); 138 139 State selfunctions = new State("selfunctions", 140 "Select Allowed Functions"); 141 selfunctions.addRequiredParameter("GroupName"); 142 selfunctions.addRequiredParameter("JobClass"); 143 addState(selfunctions); 144 145 State updfunctions = new State("updfunctions", 146 "Update Allowed Functions"); 147 updfunctions.addRequiredParameter("GroupName"); 148 updfunctions.addRequiredParameter("JobClass"); 149 addState(updfunctions); 150 this.setSchema(com.jcorporate.expresso.core.ExpressoSchema.class); 151 } 152 153 160 private Job getJob(String className) 161 throws ControllerException { 162 String myName = (thisClass + "getController(String)"); 163 Job myController; 164 165 try { 166 myController = (Job) Class.forName(className).newInstance(); 167 } catch (IllegalAccessException ie) { 168 throw new ControllerException(myName + ":Illegal Access " + 169 "Exception loading Job class " + 170 className + ":" + ie.getMessage()); 171 } catch (InstantiationException ie) { 172 throw new ControllerException(myName + ":Can't instantiate " + 173 "Job class " + className + ":" + 174 ie.getMessage()); 175 } catch (ClassNotFoundException se) { 176 throw new ControllerException(myName + ":Can't find a Job " + 177 "class called " + className + ":" + 178 se.getMessage()); 179 } catch (Exception eo) { 180 log.error(eo); 181 throw new ControllerException(myName + ":Exception loading " + 182 "Job" + className + 183 "- see detailed message in server log:" + 184 eo.getMessage()); 185 } 186 187 return myController; 188 } 189 190 191 199 private Schema getSchema(String className, boolean failIfNotLoaded) 200 throws ControllerException { 201 Schema mySchema = SchemaFactory.getInstance().getSchema(className); 202 203 if (mySchema == null && failIfNotLoaded) { 204 if (failIfNotLoaded) { 205 throw new ControllerException("Cannot access schema '" + 206 className + "'"); 207 } 208 } 209 210 return mySchema; 211 } 212 213 214 222 private void getSelJobMatrix(ControllerResponse myResponse, 223 ControllerRequest params) 224 throws ControllerException { 225 String myName = (thisClass + "getDBObjMatrix()"); 226 Schema mySchema = getSchema(params.getParameter("SchemaClass"), true); 227 228 try { 229 Block matrix = new Block("matrix"); 230 matrix.setAttribute("table", "Y"); 231 matrix.setAttribute("header-row", ("Job|Allowed?")); 232 myResponse.add(matrix); 233 234 Job oneController = null; 235 JobSecurity secur = new JobSecurity(SecuredDBObject.SYSTEM_ACCOUNT); 236 secur.setDataContext(params.getDataContext()); 237 238 Input cb = null; 239 Block oneRow = null; 240 241 for (Enumeration e = mySchema.getJobs(); e.hasMoreElements();) { 242 oneRow = new Block("oneRow"); 243 oneRow.setAttribute("row", "Y"); 244 oneController = (Job) e.nextElement(); 245 oneRow.add(new Output(oneController.getTitle())); 246 247 248 249 250 boolean conAllowed = false; 251 secur.clear(); 252 secur.setField("JobClass", oneController.getClass().getName()); 253 secur.setField("GroupName", params.getParameter("GroupName")); 254 255 if (secur.find()) { 256 conAllowed = true; 257 } 258 259 cb = new Input(oneController.getClass().getName()); 260 cb.setType("boolean"); 261 cb.setAttribute("checkbox", ""); 262 263 if (conAllowed) { 264 cb.setDefaultValue("Y"); 265 } else { 266 cb.setDefaultValue("N"); 267 } 268 269 oneRow.add(cb); 270 matrix.add(oneRow); 271 } 272 273 } catch (DBException de) { 274 throw new ControllerException(myName + 275 ":Database exception reading " + 276 "security info:" + de.getMessage()); 277 } 278 } 279 280 281 289 private void getSelFunctionsMatrix(ControllerResponse myResponse, 290 ControllerRequest params) 291 throws ControllerException { 292 String myName = (thisClass + "getSelFunctionsMatrix()"); 293 Job con = getJob(params.getParameter("JobClass")); 294 295 296 try { 297 298 299 Block matrix = new Block("matrix"); 300 matrix.setAttribute("table", "Y"); 301 302 String head = ("Function|Allowed?"); 303 matrix.setAttribute("header-row", head); 304 myResponse.add(matrix); 305 306 JobSecurity secur = new JobSecurity(SecuredDBObject.SYSTEM_ACCOUNT); 307 secur.setDataContext(params.getDataContext()); 308 secur.setField("JobClass", con.getClass().getName()); 309 secur.setField("GroupName", params.getParameter("GroupName")); 310 311 String currentSecurity = (""); 312 313 if (secur.find()) { 314 currentSecurity = secur.getField("Functions"); 315 } 316 317 Input cb = null; 318 Block oneRow = null; 319 Hashtable allStates = con.getFunctions(); 320 String oneStateName; 321 State oneState; 322 323 for (Enumeration e = allStates.keys(); e.hasMoreElements();) { 324 oneRow = new Block("oneRow"); 325 oneRow.setAttribute("row", "Y"); 326 oneStateName = (String ) e.nextElement(); 327 oneState = (State) allStates.get(oneStateName); 328 oneRow.add(new Output((String ) oneState.getDescription())); 329 330 331 332 333 boolean stateAllowed = false; 334 335 if (currentSecurity.indexOf(oneStateName) != -1) { 336 stateAllowed = true; 337 } 338 if (currentSecurity.indexOf("*") != -1) { 339 stateAllowed = true; 340 } 341 342 cb = new Input(oneStateName); 343 cb.setType("boolean"); 344 cb.setAttribute("checkbox", ""); 345 346 if (stateAllowed) { 347 cb.setDefaultValue("Y"); 348 } else { 349 cb.setDefaultValue("N"); 350 } 351 352 oneRow.add(cb); 353 matrix.add(oneRow); 354 } 355 356 } catch (DBException de) { 357 throw new ControllerException(myName + 358 ":Database exception reading " + 359 "security info:" + de.getMessage()); 360 } 361 } 362 363 364 369 public String getTitle() { 370 return ("Job Security Matrix"); 371 } 372 373 382 public ControllerResponse newState(String newState, 383 ControllerRequest params) 384 throws ControllerException, 385 NonHandleableException { 386 ControllerResponse myResponse = super.newState(newState, params); 387 388 if (newState.equals("prompt")) { 389 promptState(myResponse, params); 390 } else if (newState.equals("seljob")) { 391 selJobState(myResponse, params); 392 } else if (newState.equals("setjob")) { 393 setJobState(myResponse, params); 394 } else if (newState.equals("updjob")) { 395 updJobState(myResponse, params); 396 } else if (newState.equals("selfunctions")) { 397 selFunctionsState(myResponse, params); 398 } else if (newState.equals("updstates")) { 399 updFunctionsState(myResponse, params); 400 } 401 if (!newState.equals("prompt")) { 402 Transition again = new Transition("Start Again", 403 getClass().getName()); 404 again.setName("again"); 405 again.addParam(STATE_PARAM_KEY, "prompt"); 406 myResponse.addTransition(again); 407 } 408 409 return myResponse; 410 } 411 412 413 419 private void promptState(ControllerResponse myResponse, 420 ControllerRequest params) 421 throws ControllerException { 422 String myName = (thisClass + "promptState()"); 423 424 try { 425 Input chooseGroup = new Input(); 426 chooseGroup.setLabel("Choose Group"); 427 chooseGroup.setName("GroupName"); 428 429 Vector vg = new Vector (2); 430 UserGroup gl = new UserGroup(SecuredDBObject.SYSTEM_ACCOUNT); 431 gl.setDataContext(params.getDataContext()); 432 433 UserGroup oneGroup = null; 434 435 for (Iterator eg = gl.searchAndRetrieveList().iterator(); 436 eg.hasNext();) { 437 oneGroup = (UserGroup) eg.next(); 438 vg.addElement(new ValidValue(oneGroup.getField("GroupName"), 439 oneGroup.getField("Descrip"))); 440 } 441 442 if (vg.size() == 0) { 443 throw new ControllerException(myName + 444 ":There are no groups " + 445 "defined."); 446 } 447 448 chooseGroup.setValidValues(vg); 449 myResponse.addInput(chooseGroup); 450 451 452 Input chooseSchema = new Input(); 453 chooseSchema.setLabel("Choose Schema"); 454 chooseSchema.setName("SchemaClass"); 455 456 Vector vs = new Vector (2); 457 vs.addElement(new ValidValue("com.jcorporate.expresso." + "core.ExpressoSchema", 458 "General")); 459 460 Schema oneSchemaObj; 461 SchemaList sl = new SchemaList(SecuredDBObject.SYSTEM_ACCOUNT); 462 sl.setDataContext(params.getDataContext()); 463 464 SchemaList oneSchema = null; 465 466 for (Iterator es = sl.searchAndRetrieveList().iterator(); 467 es.hasNext();) { 468 oneSchema = (SchemaList) es.next(); 469 oneSchemaObj = getSchema(oneSchema.getField("SchemaClass"), 470 false); 471 472 if (oneSchemaObj != null) { 473 if (oneSchemaObj.getJobs().hasMoreElements()) { 474 vs.addElement(new ValidValue(oneSchema.getField("SchemaClass"), 475 oneSchema.getField("Descrip"))); 476 } 477 } 478 } 479 480 481 chooseSchema.setValidValues(vs); 482 myResponse.addInput(chooseSchema); 483 } catch (DBException de) { 484 throw new ControllerException(myName + ":Unable to retrieve " + 485 "schema information:" + 486 de.getMessage()); 487 } 488 489 Transition setjob = new Transition("Select Allowed Jobs", 490 getClass().getName()); 491 setjob.setName("setjob"); 492 setjob.setState("setjob"); 493 myResponse.addTransition(setjob); 494 495 Transition seljob = new Transition("Select Allowed Functions", 496 getClass().getName()); 497 seljob.setName("seljob"); 498 seljob.setState("seljob"); 499 myResponse.addTransition(seljob); 501 } 502 503 504 510 private void selJobState(ControllerResponse myResponse, 511 ControllerRequest params) 512 throws ControllerException, NonHandleableException { 513 String myName = (thisClass + "selJobState()"); 514 Schema mySchema = getSchema(params.getParameter("SchemaClass"), true); 515 Job oneController; 516 Input chooseController = new Input(); 517 chooseController.setLabel("Choose Job"); 518 chooseController.setName("JobClass"); 519 520 Vector v = new Vector (2); 521 522 for (Enumeration e = mySchema.getJobs(); e.hasMoreElements();) { 523 oneController = (Job) e.nextElement(); 524 525 526 527 if (oneController.getFunctions().size() != 0) { 528 v.addElement(new ValidValue(oneController.getClass().getName(), 529 oneController.getTitle())); 530 } 531 } 532 533 if (v.size() == 0) { 534 ErrorCollection ec = new ErrorCollection(); 535 ec.addError(myName + ":There are no jobs " + 536 "defined in the selected schema with functions defined." + 537 " Use 'Select Allowed Jobs' to enable access to jobs."); 538 myResponse.saveErrors(ec); 539 transition("prompt", params, myResponse); 540 return; 541 } 542 543 chooseController.setValidValues(v); 544 myResponse.addInput(chooseController); 545 546 try { 547 UserGroup myGroup = new UserGroup(SecuredDBObject.SYSTEM_ACCOUNT); 548 myGroup.setDataContext(params.getDataContext()); 549 myGroup.setField("GroupName", params.getParameter("GroupName")); 550 myGroup.retrieve(); 551 myResponse.addOutput(new Output("Choose a Job to administer functions " + "for group '" + 552 myGroup.getField("Descrip") + 553 "'")); 554 } catch (DBException de) { 555 throw new ControllerException(myName + 556 ":Unable to retrieve group:" + 557 de.getMessage()); 558 } 559 560 Transition selfunctions = new Transition("Select Allowed Functions", 561 getClass().getName()); 562 selfunctions.setName("selfunctions"); 563 selfunctions.addParam(STATE_PARAM_KEY, "selfunctions"); 564 selfunctions.addParam("GroupName", params.getParameter("GroupName")); 565 selfunctions.addParam("SchemaClass", 566 params.getParameter("SchemaClass")); 567 myResponse.addTransition(selfunctions); 568 } 569 570 571 578 private void selFunctionsState(ControllerResponse myResponse, 579 ControllerRequest params) 580 throws ControllerException { 581 String myName = (thisClass + "selFunctionsState()"); 582 Job oneController = getJob(params.getParameter("JobClass")); 583 584 try { 585 UserGroup oneGroup = new UserGroup(SecuredDBObject.SYSTEM_ACCOUNT); 586 oneGroup.setDataContext(params.getDataContext()); 587 oneGroup.setField("GroupName", params.getParameter("GroupName")); 588 oneGroup.retrieve(); 589 myResponse.addOutput(new Output("Select allowed Functions for Job '" + oneController.getTitle() + 590 "' and group '" + 591 oneGroup.getField("Descrip") + 592 "'")); 593 } catch (DBException de) { 594 throw new ControllerException(myName + 595 ":Unable to locate group '" + 596 params.getParameter("GroupName")); 597 } 598 599 getSelFunctionsMatrix(myResponse, params); 600 601 Transition updstates = new Transition("Update", getClass().getName()); 602 updstates.setName("updstates"); 603 updstates.addParam(STATE_PARAM_KEY, "updstates"); 604 updstates.addParam("GroupName", params.getParameter("GroupName")); 605 updstates.addParam("JobClass", params.getParameter("JobClass")); 606 myResponse.addTransition(updstates); 607 } 608 609 610 616 private void setJobState(ControllerResponse myResponse, 617 ControllerRequest params) 618 throws ControllerException { 619 String myName = (thisClass + "setControllerState()"); 620 621 622 623 try { 624 UserGroup myGroup = new UserGroup(SecuredDBObject.SYSTEM_ACCOUNT); 625 myGroup.setDataContext(params.getDataContext()); 626 myGroup.setField("GroupName", params.getParameter("GroupName")); 627 myGroup.retrieve(); 628 629 String schemaName; 630 631 if (params.getParameter("SchemaClass").equals("com.jcorporate." + "expresso.core.ExpressoSchema")) { 632 schemaName = ("General"); 633 } else { 634 SchemaList mySL = new SchemaList(SecuredDBObject.SYSTEM_ACCOUNT); 635 mySL.setDataContext(params.getDataContext()); 636 mySL.setField("SchemaClass", 637 params.getParameter("SchemaClass")); 638 mySL.retrieve(); 639 schemaName = mySL.getField("Descrip"); 640 } 641 642 myResponse.addOutput(new Output("Select allowed Jobs for group '" + myGroup.getField("Descrip") + 643 "' in Schema '" + schemaName + 644 "'")); 645 getSelJobMatrix(myResponse, params); 646 } catch (DBException de) { 647 throw new ControllerException(myName + 648 ":Unable to read state or group " + 649 "information:" + de.getMessage()); 650 } 651 652 Transition updjob = new Transition("Update", getClass().getName()); 653 updjob.setName("updjob"); 654 updjob.addParam(STATE_PARAM_KEY, "updjob"); 655 updjob.addParam("GroupName", params.getParameter("GroupName")); 656 updjob.addParam("SchemaClass", params.getParameter("SchemaClass")); 657 myResponse.addTransition(updjob); 658 } 659 660 661 668 private void updJobState(ControllerResponse myResponse, 669 ControllerRequest params) 670 throws ControllerException { 671 String myName = (thisClass + "updJobState()"); 672 Schema mySchema = getSchema(params.getParameter("SchemaClass"), true); 673 674 try { 675 JobSecurity secur = new JobSecurity(SecuredDBObject.SYSTEM_ACCOUNT); 676 secur.setDataContext(params.getDataContext()); 677 678 Job checkController; 679 int changes = 0; 680 681 for (Enumeration pe = mySchema.getJobs(); pe.hasMoreElements();) { 682 checkController = (Job) pe.nextElement(); 683 secur.clear(); 684 secur.setField("JobClass", 685 checkController.getClass().getName()); 686 secur.setField("GroupName", params.getParameter("GroupName")); 687 688 if (StringUtil.notNull(params.getParameter(checkController.getClass().getName())).equals("Y")) { 689 690 691 if (!secur.find()) { 692 secur.setField("Functions", "*"); 693 secur.add(); 694 myResponse.addOutput(new Output("Permission granted for " + "Job " + 695 checkController.getTitle())); 696 changes++; 697 } 698 } else { 699 700 701 if (secur.find()) { 702 secur.delete(); 703 myResponse.addOutput(new Output("Permission removed for " + "Job " + 704 checkController.getTitle())); 705 changes++; 706 } 707 } 708 709 } 710 711 if (changes == 0) { 712 myResponse.addOutput(new Output("No changes required")); 713 } 714 } catch (DBException de) { 715 throw new ControllerException(myName + 716 ":Unable to update security " + 717 "info", de); 718 } 719 } 720 721 722 728 private void updFunctionsState(ControllerResponse myResponse, 729 ControllerRequest params) 730 throws ControllerException { 731 String myName = (thisClass + "updStatesState()"); 732 Job con = getJob(params.getParameter("JobClass")); 733 734 try { 735 Hashtable allStates = con.getFunctions(); 736 String oneStateName; 737 State oneState; 738 StringBuffer newSecurity = new StringBuffer (""); 739 boolean allAllowed = true; 740 741 for (Enumeration e = allStates.keys(); e.hasMoreElements();) { 742 oneStateName = (String ) e.nextElement(); 743 oneState = (State) allStates.get(oneStateName); 744 745 if (StringUtil.notNull(params.getParameter(oneStateName)).equals("Y")) { 746 newSecurity.append(oneStateName); 747 newSecurity.append(", "); 748 myResponse.addOutput(new Output("Access granted to function '" + oneState.getDescription() + 749 "'")); 750 } else { 751 myResponse.addOutput(new Output("Access denied to function '" + oneState.getDescription() + 752 "'")); 753 allAllowed = false; 754 } 755 } 756 757 758 JobSecurity secur = new JobSecurity(SecuredDBObject.SYSTEM_ACCOUNT); 759 secur.setDataContext(params.getDataContext()); 760 secur.setField("JobClass", con.getClass().getName()); 761 secur.setField("GroupName", params.getParameter("GroupName")); 762 763 if (allAllowed) { 764 newSecurity = new StringBuffer ("*"); 765 } 766 if (secur.find()) { 767 secur.setField("Functions", newSecurity.toString()); 768 secur.update(); 769 } else { 770 secur.setField("Functions", newSecurity.toString()); 771 secur.add(); 772 } 773 } catch (DBException de) { 774 throw new ControllerException(myName + 775 ":Database exception reading " + 776 "security info:" + de.getMessage()); 777 } 778 } 779 780 781 } 782 | Popular Tags |