1 110 package com.ivata.groupware.business.addressbook.struts; 111 112 import java.rmi.RemoteException ; 113 import java.util.Arrays ; 114 import java.util.Collection ; 115 import java.util.Iterator ; 116 import java.util.Set ; 117 import java.util.TreeSet ; 118 import java.util.Vector ; 119 120 import javax.servlet.http.HttpServletRequest ; 121 import javax.servlet.http.HttpServletResponse ; 122 import javax.servlet.http.HttpSession ; 123 124 import org.apache.struts.action.ActionErrors; 125 import org.apache.struts.action.ActionForm; 126 import org.apache.struts.action.ActionMapping; 127 import org.apache.struts.action.ActionMessage; 128 import org.apache.struts.action.ActionMessages; 129 import org.picocontainer.PicoContainer; 130 131 import com.ivata.groupware.admin.security.server.SecuritySession; 132 import com.ivata.groupware.admin.security.user.UserDO; 133 import com.ivata.groupware.business.addressbook.AddressBook; 134 import com.ivata.groupware.business.addressbook.person.group.GroupConstants; 135 import com.ivata.groupware.business.addressbook.person.group.GroupDO; 136 import com.ivata.groupware.business.addressbook.person.group.right.RightConstants; 137 import com.ivata.groupware.business.addressbook.right.AddressBookRights; 138 import com.ivata.mask.MaskFactory; 139 import com.ivata.mask.util.StringHandling; 140 import com.ivata.mask.util.SystemException; 141 import com.ivata.mask.web.struts.MaskAction; 142 import com.ivata.mask.web.struts.MaskAuthenticator; 143 144 145 154 public class GroupRightAction extends MaskAction { 155 AddressBook addressBook; 156 AddressBookRights addressBookRights; 157 158 170 public GroupRightAction(AddressBook addressBook, AddressBookRights 171 addressBookRights, MaskFactory maskFactory, 172 MaskAuthenticator authenticator) { 173 super(maskFactory, authenticator); 174 this.addressBook = addressBook; 175 this.addressBookRights = addressBookRights; 176 } 177 197 public void clear(final ActionMapping mapping, 198 final ActionErrors errors, 199 final ActionForm form, 200 final HttpServletRequest request, 201 final HttpServletResponse response, 202 final HttpSession session) 203 throws SystemException { 204 GroupRightForm groupRightForm = (GroupRightForm) form; 205 groupRightForm.setGroup(new GroupDO()); 206 207 groupRightForm.setGroupRightTab_activeTab(new Integer (0)); 208 groupRightForm.setSelected(new String [0]); 209 210 groupRightForm.setIncludePage("/addressBook/groupRightGeneral.jsp"); 211 212 groupRightForm.setGroupRightsAdd(new String [0]); 213 groupRightForm.setGroupRightsAmend(new String [0]); 214 groupRightForm.setGroupRightsRemove(new String [0]); 215 groupRightForm.setGroupRightsView(new String [0]); 216 217 groupRightForm.setGroupIds(new Vector ()); 218 groupRightForm.setRowNames(new Vector ()); 219 groupRightForm.setRows(new Vector ()); 220 221 session.removeAttribute("groupRightTab_activeTab"); 222 } 223 224 231 private String [] convertGroupCollectionToStringArray(final Collection collection) { 232 String [] stringArray = new String [collection.size()]; 233 int index = 0; 234 235 for (Iterator i = collection.iterator(); i.hasNext(); ++index) { 236 stringArray[index] = ((Integer ) i.next()).toString(); 237 } 238 239 return stringArray; 240 } 241 242 264 public String execute(final ActionMapping mapping, 265 final ActionErrors errors, 266 final ActionForm form, 267 final HttpServletRequest request, 268 final HttpServletResponse response, 269 final HttpSession session) 270 throws SystemException { 271 GroupRightForm groupRightForm = (GroupRightForm) form; 272 SecuritySession securitySession = (SecuritySession) session.getAttribute("securitySession"); 273 274 if ((request.getParameter("reset") != null) && 275 request.getParameter("reset").equals("true")) { 276 groupRightForm.setGroup(new GroupDO()); 277 groupRightForm.setAddGroup(null); 278 groupRightForm.setType(request.getParameter("type")); 279 groupRightForm.setIncludePage("/addressBook/groupRightGeneral.jsp"); 280 groupRightForm.setHelpKey("addressbook.groupRightUser"); 281 groupRightForm.setReadOnly(false); 282 groupRightForm.setCanRemove(false); 283 session.removeAttribute("groupRightTab_activeTab"); 284 } 285 286 if (groupRightForm == null) { 287 PicoContainer container = securitySession.getContainer(); 288 groupRightForm = (GroupRightForm) container.getComponentInstance(GroupRightForm.class); 289 session.setAttribute("addressBookGroupRightForm", groupRightForm); 290 } 291 292 Integer requestGroupId = StringHandling.integerValue(request.getParameter( 294 "requestGroupId")); 295 296 if (requestGroupId != null) { 297 GroupDO requestGroup = addressBook.findGroupByPrimaryKey( 298 securitySession, 299 requestGroupId); 300 groupRightForm.setGroup(requestGroup); 301 302 Set allGroups = new TreeSet (); 304 305 Collection addressBookView = addressBookRights.findRightsForGroup(securitySession, 306 requestGroup, RightConstants.ACCESS_VIEW); 307 allGroups.addAll(addressBookView); 308 groupRightForm.setGroupRightsView(convertGroupCollectionToStringArray( 309 addressBookView)); 310 311 Collection addressBookAdd = addressBookRights.findRightsForGroup(securitySession, 312 requestGroup, RightConstants.ACCESS_ADD); 313 allGroups.addAll(addressBookAdd); 314 groupRightForm.setGroupRightsAdd(convertGroupCollectionToStringArray( 315 addressBookAdd)); 316 317 Collection addressBookAmend = addressBookRights.findRightsForGroup(securitySession, 318 requestGroup, RightConstants.ACCESS_AMEND); 319 allGroups.addAll(addressBookAmend); 320 groupRightForm.setGroupRightsAmend(convertGroupCollectionToStringArray( 321 addressBookAmend)); 322 323 Collection addressBookRemove = addressBookRights.findRightsForGroup(securitySession, 324 requestGroup, RightConstants.ACCESS_REMOVE); 325 allGroups.addAll(addressBookRemove); 326 groupRightForm.setGroupRightsRemove(convertGroupCollectionToStringArray( 327 addressBookRemove)); 328 329 Vector groupIds = new Vector (); 330 Vector groupNames = new Vector (); 331 332 for (Iterator i = allGroups.iterator(); i.hasNext();) { 333 Integer userGroupId = (Integer ) i.next(); 334 groupIds.add(userGroupId); 335 groupNames.add(addressBook.findGroupByPrimaryKey( 336 securitySession, userGroupId).getName()); 337 } 338 339 groupRightForm.setGroupIds(groupIds); 340 groupRightForm.setGroupNames(groupNames); 341 342 groupRightForm.setReadOnly(!addressBookRights.canAmendInGroup( 344 securitySession, requestGroup)); 345 346 groupRightForm.setCanRemove(addressBookRights.canRemoveFromGroup( 348 securitySession, requestGroup)); 349 350 session.removeAttribute("groupRightTab_activeTab"); 351 groupRightForm.setGroupRightTab_activeTab(null); 352 } 353 354 if (!groupRightForm.getGroupIds().contains(GroupConstants.USER_GROUP)) { 356 groupRightForm.getGroupIds().add(GroupConstants.USER_GROUP); 357 groupRightForm.getGroupNames().add("everyone"); 358 } 359 360 int activeTab = (groupRightForm.getGroupRightTab_activeTab() == null) 363 ? 0 : groupRightForm.getGroupRightTab_activeTab().intValue(); 364 365 if (groupRightForm.getType() == null) { 366 groupRightForm.setType(request.getParameter("type")); 367 } 368 369 if (activeTab == 1) { 372 groupRightForm.setIncludePage("/addressBook/groupRightDetail.jsp"); 373 groupRightForm.setHelpKey("addressbook.groupRightAddressBook"); 374 groupRightForm.setRows(groupRightForm.getGroupIds()); 375 groupRightForm.setRowNames(groupRightForm.getGroupNames()); 376 377 if (groupRightForm.getType().equals("addressBook")) { 378 groupRightForm.setEmptyListMessage( 379 "groupRight.addressBook.label.list.empty"); 380 } else { 381 groupRightForm.setEmptyListMessage( 382 "groupRight.userGroup.label.list.empty"); 383 } 384 } else { 385 groupRightForm.setIncludePage("/addressBook/groupRightGeneral.jsp"); 386 387 if (groupRightForm.getType().equals("addressBook")) { 388 groupRightForm.setHelpKey("addressbook.groupRightUser"); 389 } 390 } 391 392 Integer addGroup = groupRightForm.getAddGroup(); 394 395 if ((addGroup != null) && !addGroup.equals(new Integer (0))) { 396 GroupDO group = addressBook.findGroupByPrimaryKey(securitySession, 397 addGroup); 398 399 if (!groupRightForm.getGroupIds().contains(group.getId())) { 401 groupRightForm.getGroupIds().add(group.getId()); 402 groupRightForm.getGroupNames().add(group.getName()); 403 } 404 } 405 406 if (!StringHandling.isNullOrEmpty(groupRightForm.getRemove())) { 408 String [] selected = groupRightForm.getSelected(); 409 410 for (int i = 0; i < selected.length; i++) { 411 Integer selectedId = StringHandling.integerValue(selected[i]); 412 413 for (int index = 0; index < groupRightForm.getRows().size(); 414 ++index) { 415 if (groupRightForm.getRows().get(index).equals(selectedId)) { 417 groupRightForm.getRows().remove(index); 418 groupRightForm.getRowNames().remove(index); 419 420 break; 421 } 422 } 423 } 424 } 425 426 return "addressBookGroupRight"; 427 } 428 429 452 public String onConfirm(final ActionMapping mapping, 453 final ActionErrors errors, 454 final ActionForm form, 455 final HttpServletRequest request, 456 final HttpServletResponse response, 457 final HttpSession session, 458 final String defaultForward) 459 throws SystemException { 460 GroupRightForm groupRightForm = (GroupRightForm) form; 461 GroupDO group = groupRightForm.getGroup(); 462 SecuritySession securitySession = (SecuritySession) session.getAttribute("securitySession"); 463 UserDO user = securitySession.getUser(); 464 465 if (group.getId() == null) { 467 group.setCreatedBy(user); 468 469 if (groupRightForm.getType().equals("addressBook")) { 471 group = addressBook.addAddressBook(securitySession, group); 472 } else { 473 group = addressBook.addUserGroup(securitySession, group); 474 } 475 476 groupRightForm.setGroup(group); 477 groupRightForm.setReadOnly(false); 478 groupRightForm.setCanRemove(true); 479 } else { 480 try { 481 if (groupRightForm.getType().equals("addressBook")) { 483 group = addressBook.amendGroup(securitySession, group); 484 } else { 485 group = addressBook.amendGroup(securitySession, group); 486 } 487 488 groupRightForm.setGroup(group); 489 } catch (SystemException e) { 490 errors.add(ActionMessages.GLOBAL_MESSAGE, 491 new ActionMessage("errors.addressBook.group.amend", 492 group.getName())); 493 } 494 } 495 496 if ((groupRightForm.getGroupRightsAdd() != null) && 498 (groupRightForm.getGroupRightsAmend() != null) && 499 (groupRightForm.getGroupRightsRemove() != null) && 500 (groupRightForm.getGroupRightsView() != null)) { 501 updateGroupRights(securitySession, groupRightForm, groupRightForm.getGroup()); 503 504 Set allGroups = new TreeSet (); 507 Collection addressBookView = addressBookRights.findRightsForGroup(securitySession, 508 group, RightConstants.ACCESS_VIEW); 509 allGroups.addAll(addressBookView); 510 groupRightForm.setGroupRightsView(convertGroupCollectionToStringArray( 511 addressBookView)); 512 513 Collection addressBookAdd = addressBookRights.findRightsForGroup(securitySession, 514 group, RightConstants.ACCESS_ADD); 515 allGroups.addAll(addressBookAdd); 516 groupRightForm.setGroupRightsAdd(convertGroupCollectionToStringArray( 517 addressBookAdd)); 518 519 Collection addressBookAmend = addressBookRights.findRightsForGroup(securitySession, 520 group, RightConstants.ACCESS_AMEND); 521 allGroups.addAll(addressBookAmend); 522 groupRightForm.setGroupRightsAmend(convertGroupCollectionToStringArray( 523 addressBookAmend)); 524 525 Collection addressBookRemove = addressBookRights.findRightsForGroup(securitySession, 526 group, RightConstants.ACCESS_REMOVE); 527 allGroups.addAll(addressBookRemove); 528 groupRightForm.setGroupRightsRemove(convertGroupCollectionToStringArray( 529 addressBookRemove)); 530 531 Vector groupIds = new Vector (); 532 Vector groupNames = new Vector (); 533 534 for (Iterator i = allGroups.iterator(); i.hasNext();) { 535 Integer userGroupId = (Integer ) i.next(); 536 groupIds.add(userGroupId); 537 groupNames.add(addressBook.findGroupByPrimaryKey( 538 securitySession, userGroupId).getName()); 539 } 540 541 groupRightForm.setGroupIds(groupIds); 542 groupRightForm.setGroupNames(groupNames); 543 544 groupRightForm.setRows(groupRightForm.getGroupIds()); 545 groupRightForm.setRowNames(groupRightForm.getGroupNames()); 546 547 groupRightForm.setReadOnly(!addressBookRights.canAmendInGroup( 549 securitySession, group)); 550 551 groupRightForm.setCanRemove(addressBookRights.canRemoveFromGroup( 553 securitySession, group)); 554 } 555 556 if (!groupRightForm.getGroupIds().contains(GroupConstants.USER_GROUP)) { 558 groupRightForm.getGroupIds().add(GroupConstants.USER_GROUP); 559 groupRightForm.getGroupNames().add("everyone"); 560 } 561 562 if (groupRightForm.getOk() != null) { 566 session.removeAttribute("addressBookGroupRightForm"); 568 569 return "utilClosePopUp"; 570 } 571 572 return defaultForward; 573 } 574 575 597 public String onDelete(final ActionMapping mapping, 598 final ActionErrors errors, 599 final ActionForm form, 600 final HttpServletRequest request, 601 final HttpServletResponse response, 602 final HttpSession session, final String defaultForward) 603 throws SystemException { 604 SecuritySession securitySession = (SecuritySession) session.getAttribute("securitySession"); 605 GroupRightForm groupRightForm = (GroupRightForm) form; 606 GroupDO group = groupRightForm.getGroup(); 607 608 try { 609 if (groupRightForm.getType().equals("addressBook")) { 611 addressBook.removeGroup(securitySession, group.getId()); 612 } else { 613 addressBook.removeGroup(securitySession, group.getId()); 614 } 615 } catch (SystemException e) { 616 errors.add(ActionMessages.GLOBAL_MESSAGE, 617 new ActionMessage("errors.addressBook.group.remove", 618 group.getName())); 619 } 620 621 return null; 622 } 623 624 636 private void updateGroupRights(final SecuritySession securitySession, 637 final GroupRightForm groupRightForm, 638 final GroupDO group) 639 throws SystemException { 640 String [][] rightsArray = { 642 groupRightForm.getGroupRightsAdd(), 643 groupRightForm.getGroupRightsAmend(), 644 groupRightForm.getGroupRightsRemove(), 645 groupRightForm.getGroupRightsView() 646 }; 647 Integer [] accessArray = { 648 RightConstants.ACCESS_ADD, RightConstants.ACCESS_AMEND, 649 RightConstants.ACCESS_REMOVE, RightConstants.ACCESS_VIEW 650 }; 651 652 for (Iterator i = groupRightForm.getGroupIds().iterator(); i.hasNext();) { 653 Integer id = (Integer ) i.next(); 654 655 Vector rightsForGroup = new Vector (); 656 boolean hasRightNow; 657 658 for (int index = 0; index < accessArray.length; index++) { 659 Integer access = accessArray[index]; 660 String [] rights = rightsArray[index]; 661 662 if (access.equals(RightConstants.ACCESS_VIEW) && 664 groupRightForm.getType().equals("userGroup") && 665 GroupConstants.equals(id, GroupConstants.USER_GROUP)) { 666 hasRightNow = true; 667 } else { 668 Arrays.sort(rights); 669 hasRightNow = Arrays.binarySearch(rights, id.toString()) >= 0; 670 } 671 672 rightsForGroup.addAll(addressBookRights.findRightsForGroup( 673 securitySession, group, access)); 674 675 if (!rightsForGroup.contains(id) && hasRightNow) { 677 rightsForGroup.add(id); 678 addressBookRights.amendRightsForGroup(securitySession, 679 group, rightsForGroup, access); 680 681 } else if (rightsForGroup.contains(id) && !hasRightNow) { 683 rightsForGroup.remove(id); 684 addressBookRights.amendRightsForGroup(securitySession, 685 group, rightsForGroup, access); 686 } 687 688 rightsForGroup.clear(); 689 } 690 } 691 } 692 } 693 | Popular Tags |