1 17 package org.alfresco.web.bean.users; 18 19 import java.text.MessageFormat ; 20 import java.util.ArrayList ; 21 import java.util.Collections ; 22 import java.util.HashMap ; 23 import java.util.List ; 24 import java.util.Map ; 25 import java.util.Set ; 26 27 import javax.faces.application.FacesMessage; 28 import javax.faces.component.UISelectOne; 29 import javax.faces.context.FacesContext; 30 import javax.faces.event.ActionEvent; 31 import javax.faces.event.ValueChangeEvent; 32 import javax.faces.model.DataModel; 33 import javax.faces.model.ListDataModel; 34 import javax.transaction.UserTransaction ; 35 36 import org.alfresco.model.ContentModel; 37 import org.alfresco.service.cmr.repository.InvalidNodeRefException; 38 import org.alfresco.service.cmr.repository.NodeRef; 39 import org.alfresco.service.cmr.repository.NodeService; 40 import org.alfresco.service.cmr.search.SearchService; 41 import org.alfresco.service.cmr.security.AccessPermission; 42 import org.alfresco.service.cmr.security.AccessStatus; 43 import org.alfresco.service.cmr.security.AuthorityType; 44 import org.alfresco.service.cmr.security.OwnableService; 45 import org.alfresco.service.cmr.security.PermissionService; 46 import org.alfresco.service.cmr.security.PersonService; 47 import org.alfresco.web.app.Application; 48 import org.alfresco.web.app.context.IContextListener; 49 import org.alfresco.web.app.context.UIContextService; 50 import org.alfresco.web.bean.BrowseBean; 51 import org.alfresco.web.bean.repository.MapNode; 52 import org.alfresco.web.bean.repository.Node; 53 import org.alfresco.web.bean.repository.Repository; 54 import org.alfresco.web.ui.common.Utils; 55 import org.alfresco.web.ui.common.component.UIActionLink; 56 import org.alfresco.web.ui.common.component.data.UIRichList; 57 import org.alfresco.web.ui.repo.WebResources; 58 59 62 public abstract class UserMembersBean implements IContextListener 63 { 64 private static final String MSG_SUCCESS_INHERIT_NOT = "success_not_inherit_permissions"; 65 private static final String MSG_SUCCESS_INHERIT = "success_inherit_permissions"; 66 67 private static final String ERROR_DELETE = "error_remove_user"; 68 69 private static final String OUTCOME_FINISH = "finish"; 70 71 72 protected NodeService nodeService; 73 74 75 protected SearchService searchService; 76 77 78 protected PermissionService permissionService; 79 80 81 protected PersonService personService; 82 83 84 protected BrowseBean browseBean; 85 86 87 protected OwnableService ownableService; 88 89 90 private UIRichList usersRichList; 91 92 93 private String personAuthority = null; 94 95 96 private String personName = null; 97 98 99 private DataModel personRolesDataModel = null; 100 101 102 private List <PermissionWrapper> personRoles = null; 103 104 105 108 public UserMembersBean() 109 { 110 UIContextService.getInstance(FacesContext.getCurrentInstance()).registerBean(this); 111 } 112 113 114 117 122 public abstract Node getNode(); 123 124 125 128 131 public void setNodeService(NodeService nodeService) 132 { 133 this.nodeService = nodeService; 134 } 135 136 139 public void setSearchService(SearchService searchService) 140 { 141 this.searchService = searchService; 142 } 143 144 147 public void setPermissionService(PermissionService permissionService) 148 { 149 this.permissionService = permissionService; 150 } 151 152 155 public void setOwnableService(OwnableService ownableService) 156 { 157 this.ownableService = ownableService; 158 } 159 160 163 public void setPersonService(PersonService personService) 164 { 165 this.personService = personService; 166 } 167 168 171 public void setBrowseBean(BrowseBean browseBean) 172 { 173 this.browseBean = browseBean; 174 } 175 176 179 public UIRichList getUsersRichList() 180 { 181 return this.usersRichList; 182 } 183 184 187 public void setUsersRichList(UIRichList usersRichList) 188 { 189 this.usersRichList = usersRichList; 190 } 191 192 197 public DataModel getPersonRolesDataModel() 198 { 199 if (this.personRolesDataModel == null) 200 { 201 this.personRolesDataModel = new ListDataModel(); 202 } 203 204 this.personRolesDataModel.setWrappedData(this.personRoles); 205 206 return this.personRolesDataModel; 207 } 208 209 212 public String getPersonAuthority() 213 { 214 return this.personAuthority; 215 } 216 217 220 public void setPersonAuthority(String person) 221 { 222 this.personAuthority = person; 223 } 224 225 228 public String getPersonName() 229 { 230 return this.personName; 231 } 232 233 236 public void setPersonName(String personName) 237 { 238 this.personName = personName; 239 } 240 241 244 public boolean getHasChangePermissions() 245 { 246 return getNode().hasPermission(PermissionService.CHANGE_PERMISSIONS); 247 } 248 249 252 public boolean isInheritPermissions() 253 { 254 return this.permissionService.getInheritParentPermissions(getNode().getNodeRef()); 255 } 256 257 260 public void setInheritPermissions(boolean inheritPermissions) 261 { 262 } 264 265 268 public String getOwner() 269 { 270 return this.ownableService.getOwner(getNode().getNodeRef()); 271 } 272 273 276 public List <Map > getUsers() 277 { 278 FacesContext context = FacesContext.getCurrentInstance(); 279 280 List <Map > personNodes = null; 281 282 UserTransaction tx = null; 283 try 284 { 285 tx = Repository.getUserTransaction(context, true); 286 tx.begin(); 287 288 Map <String , List <String >> permissionMap = new HashMap <String , List <String >>(13, 1.0f); 292 Set <AccessPermission> permissions = permissionService.getAllSetPermissions(getNode().getNodeRef()); 293 if (permissions != null) 294 { 295 for (AccessPermission permission : permissions) 296 { 297 if (permission.getAccessStatus() == AccessStatus.ALLOWED && 299 (permission.getAuthorityType() == AuthorityType.USER || 300 permission.getAuthorityType() == AuthorityType.GROUP || 301 permission.getAuthorityType() == AuthorityType.GUEST || 302 permission.getAuthorityType() == AuthorityType.EVERYONE)) 303 { 304 String authority = permission.getAuthority(); 305 306 List <String > userPermissions = permissionMap.get(authority); 307 if (userPermissions == null) 308 { 309 userPermissions = new ArrayList <String >(4); 311 permissionMap.put(authority, userPermissions); 312 } 313 userPermissions.add(permission.getPermission()); 315 } 316 } 317 } 318 319 personNodes = new ArrayList <Map >(permissionMap.size()); 322 for (String authority : permissionMap.keySet()) 323 { 324 if (AuthorityType.getAuthorityType(authority) == AuthorityType.GUEST || 326 personService.personExists(authority)) 327 { 328 NodeRef nodeRef = personService.getPerson(authority); 329 if (nodeRef != null) 330 { 331 MapNode node = new MapNode(nodeRef); 333 334 Map <String , Object > props = node.getProperties(); 338 props.put("fullName", ((String )props.get("firstName")) + ' ' + ((String )props.get("lastName"))); 339 props.put("roles", listToString(context, permissionMap.get(authority))); 340 props.put("icon", WebResources.IMAGE_PERSON); 341 342 personNodes.add(node); 343 } 344 } 345 else 346 { 347 Map <String , Object > node = new HashMap <String , Object >(5, 1.0f); 349 if (authority.startsWith(PermissionService.GROUP_PREFIX) == true) 350 { 351 node.put("fullName", authority.substring(PermissionService.GROUP_PREFIX.length())); 352 } 353 else 354 { 355 node.put("fullName", authority); 356 } 357 node.put("userName", authority); 358 node.put("id", authority); 359 node.put("roles", listToString(context, permissionMap.get(authority))); 360 node.put("icon", WebResources.IMAGE_GROUP); 361 personNodes.add(node); 362 } 363 } 364 365 tx.commit(); 367 } 368 catch (InvalidNodeRefException refErr) 369 { 370 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 371 context, Repository.ERROR_NODEREF), new Object [] {"root"}) ); 372 personNodes = Collections.<Map >emptyList(); 373 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 374 } 375 catch (Throwable err) 376 { 377 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 378 context, Repository.ERROR_GENERIC), err.getMessage()), err ); 379 personNodes = Collections.<Map >emptyList(); 380 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 381 } 382 383 return personNodes; 384 } 385 386 private static String listToString(FacesContext context, List <String > list) 387 { 388 StringBuilder buf = new StringBuilder (); 389 390 if (list != null) 391 { 392 for (int i=0; i<list.size(); i++) 393 { 394 if (buf.length() != 0) 395 { 396 buf.append(", "); 397 } 398 buf.append(Application.getMessage(context, list.get(i))); 399 } 400 } 401 402 return buf.toString(); 403 } 404 405 406 409 412 public void contextUpdated() 413 { 414 if (this.usersRichList != null) 415 { 416 this.usersRichList.setValue(null); 417 } 418 } 419 420 421 424 427 public void close(ActionEvent event) 428 { 429 UIContextService.getInstance(FacesContext.getCurrentInstance()).notifyBeans(); 430 } 431 432 438 public void setupUserAction(ActionEvent event) 439 { 440 FacesContext context = FacesContext.getCurrentInstance(); 441 442 UIActionLink link = (UIActionLink) event.getComponent(); 443 Map <String , String > params = link.getParameterMap(); 444 String authority = params.get("userName"); 445 if (authority != null && authority.length() != 0) 446 { 447 try 448 { 449 if (this.personService.personExists(authority)) 450 { 451 NodeRef ref = personService.getPerson(authority); 453 Node node = new Node(ref); 454 455 setPersonName((String )node.getProperties().get(ContentModel.PROP_FIRSTNAME) + ' ' + 457 (String )node.getProperties().get(ContentModel.PROP_LASTNAME)); 458 } 459 else 460 { 461 setPersonName(authority); 462 } 463 464 List <PermissionWrapper> userPermissions = new ArrayList <PermissionWrapper>(4); 466 Set <AccessPermission> permissions = permissionService.getAllSetPermissions(getNode().getNodeRef()); 467 if (permissions != null) 468 { 469 for (AccessPermission permission : permissions) 470 { 471 if (permission.getAccessStatus() == AccessStatus.ALLOWED) 473 { 474 if (authority.equals(permission.getAuthority())) 475 { 476 PermissionWrapper wrapper = new PermissionWrapper( 478 permission.getPermission(), 479 Application.getMessage(context, permission.getPermission())); 480 userPermissions.add(wrapper); 481 } 482 } 483 } 484 } 485 this.personRoles = userPermissions; 487 setPersonAuthority(authority); 488 } 489 catch (Exception err) 490 { 491 Utils.addErrorMessage(MessageFormat.format(Application.getMessage(FacesContext 492 .getCurrentInstance(), Repository.ERROR_GENERIC), new Object [] { err.getMessage() })); 493 } 494 } 495 else 496 { 497 setPersonAuthority(null); 498 } 499 500 contextUpdated(); 502 } 503 504 507 public void inheritPermissionsValueChanged(ValueChangeEvent event) 508 { 509 try 510 { 511 boolean inheritPermissions = (Boolean )event.getNewValue(); 513 this.permissionService.setInheritParentPermissions(getNode().getNodeRef(), inheritPermissions); 514 515 FacesContext context = FacesContext.getCurrentInstance(); 517 String msg; 518 if (inheritPermissions) 519 { 520 msg = Application.getMessage(context, MSG_SUCCESS_INHERIT); 521 } 522 else 523 { 524 msg = Application.getMessage(context, MSG_SUCCESS_INHERIT_NOT); 525 } 526 FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, msg, msg); 527 context.addMessage(event.getComponent().getClientId(context), facesMsg); 528 } 529 catch (Throwable e) 530 { 531 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 532 FacesContext.getCurrentInstance(), Repository.ERROR_GENERIC), e.getMessage()), e); 533 } 534 } 535 536 539 public void addRole(ActionEvent event) 540 { 541 UISelectOne rolePicker = (UISelectOne)event.getComponent().findComponent("roles"); 542 543 String role = (String )rolePicker.getValue(); 544 if (role != null) 545 { 546 FacesContext context = FacesContext.getCurrentInstance(); 547 PermissionWrapper wrapper = new PermissionWrapper(role, Application.getMessage(context, role)); 548 this.personRoles.add(wrapper); 549 } 550 551 contextUpdated(); 553 } 554 555 558 public void removeRole(ActionEvent event) 559 { 560 PermissionWrapper wrapper = (PermissionWrapper)this.personRolesDataModel.getRowData(); 561 if (wrapper != null) 562 { 563 this.personRoles.remove(wrapper); 564 } 565 566 contextUpdated(); 568 } 569 570 573 public String finishOK() 574 { 575 String outcome = OUTCOME_FINISH; 576 577 FacesContext context = FacesContext.getCurrentInstance(); 578 579 if (this.personRoles != null && getPersonAuthority() != null) 581 { 582 UserTransaction tx = null; 583 try 584 { 585 tx = Repository.getUserTransaction(context); 586 tx.begin(); 587 588 NodeRef nodeRef = getNode().getNodeRef(); 591 this.permissionService.clearPermission(nodeRef, getPersonAuthority()); 592 for (PermissionWrapper wrapper : personRoles) 593 { 594 this.permissionService.setPermission( 595 nodeRef, 596 getPersonAuthority(), 597 wrapper.getPermission(), 598 true); 599 } 600 601 tx.commit(); 602 } 603 catch (Exception err) 604 { 605 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 606 context, Repository.ERROR_GENERIC), err.getMessage()), err ); 607 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 608 609 outcome = null; 610 } 611 } 612 613 return outcome; 614 } 615 616 619 public String removeOK() 620 { 621 UserTransaction tx = null; 622 623 try 624 { 625 FacesContext context = FacesContext.getCurrentInstance(); 626 tx = Repository.getUserTransaction(context); 627 tx.begin(); 628 629 if (getPersonAuthority() != null) 631 { 632 this.permissionService.clearPermission(getNode().getNodeRef(), getPersonAuthority()); 634 } 635 636 tx.commit(); 638 } 639 catch (Exception e) 640 { 641 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 643 Utils.addErrorMessage(MessageFormat.format(Application.getMessage(FacesContext 644 .getCurrentInstance(), ERROR_DELETE), e.getMessage()), e); 645 } 646 647 return OUTCOME_FINISH; 648 } 649 650 651 654 657 public static class PermissionWrapper 658 { 659 public PermissionWrapper(String permission, String label) 660 { 661 this.permission = permission; 662 this.label = label; 663 } 664 665 public String getRole() 666 { 667 return this.label; 668 } 669 670 public String getPermission() 671 { 672 return this.permission; 673 } 674 675 private String label; 676 private String permission; 677 } 678 } 679 | Popular Tags |