Guest User

Untitled

a guest
Jan 6th, 2018
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1.  
  2.  
  3.  
  4.  
  5. import java.security.SecureRandom;
  6.  
  7.  
  8.  
  9. import org.alfresco.model.ContentModel;
  10. import org.alfresco.repo.node.NodeServicePolicies;
  11. import org.alfresco.repo.policy.Behaviour;
  12. import org.alfresco.repo.policy.JavaBehaviour;
  13. import org.alfresco.repo.policy.PolicyComponent;
  14. import org.alfresco.service.cmr.repository.ChildAssociationRef;
  15. import org.alfresco.service.cmr.repository.NodeRef;
  16. import org.alfresco.service.cmr.repository.NodeService;
  17. import org.alfresco.service.cmr.security.AuthenticationService;
  18. import org.alfresco.service.cmr.security.MutableAuthenticationService;
  19. import org.alfresco.service.cmr.security.PersonService;
  20. import org.alfresco.util.PropertyCheck;
  21. import org.apache.commons.lang.RandomStringUtils;
  22. import org.apache.log4j.Logger;
  23. import org.springframework.beans.factory.InitializingBean;
  24.  
  25.  
  26.  
  27. public class GenerateNewUserWelcomeEmail implements NodeServicePolicies.OnCreateNodePolicy, InitializingBean {
  28.  
  29. private char[] possibleCharacters = (new String("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~!@#$%*-_)")).toCharArray();
  30. private PolicyComponent policyComponent;
  31. private NodeService nodeService;
  32. private PersonService personService;
  33. private MutableAuthenticationService authenticationService;
  34.  
  35.  
  36.  
  37. private static Logger logger = Logger.getLogger(GenerateNewUserWelcomeEmail.class);
  38.  
  39.  
  40.  
  41. public void setPolicyComponent(PolicyComponent policyComponent) {
  42. this.policyComponent = policyComponent;
  43. }
  44.  
  45.  
  46.  
  47. public void setNodeService(NodeService nodeService) {
  48. this.nodeService = nodeService;
  49. }
  50.  
  51.  
  52.  
  53. public void setPersonService(PersonService personService) {
  54. this.personService = personService;
  55. }
  56.  
  57.  
  58.  
  59. public void setAuthenticationService(AuthenticationService authenticationService) {
  60. if (authenticationService instanceof MutableAuthenticationService) {
  61. this.authenticationService = (MutableAuthenticationService) authenticationService;
  62. }
  63. }
  64.  
  65.  
  66.  
  67. @Override
  68. public void onCreateNode(ChildAssociationRef childAssocRef) {
  69. logger.debug("\n\n--------------------------------- Initializing GenerateNewUserWelcomeEmail behaviour -------------------------------------");
  70. notifyUser(childAssocRef);
  71. }
  72.  
  73.  
  74.  
  75. private void notifyUser(ChildAssociationRef childAssocRef) {
  76. NodeRef personRef = childAssocRef.getChildRef();
  77. // get the user name
  78. String username = (String) this.nodeService.getProperty(personRef, ContentModel.PROP_USERNAME); // generate the new password (Alfresco's rules)
  79. String newPassword = RandomStringUtils.random( 10, 0, possibleCharacters.length-1, false, false, possibleCharacters, new SecureRandom() ); // set the new password
  80. authenticationService.setAuthentication(username, newPassword.toCharArray());
  81. // send default notification to the user
  82. personService.notifyPerson(username, newPassword);
  83. }
  84.  
  85. @Override
  86. public void afterPropertiesSet() throws Exception {
  87. PropertyCheck.mandatory(this, "policyComponent", policyComponent);
  88. PropertyCheck.mandatory(this, "nodeService", nodeService);
  89. PropertyCheck.mandatory(this, "authenticationService", authenticationService);
  90. PropertyCheck.mandatory(this, "personService", personService);
  91. this.policyComponent.bindClassBehaviour(
  92. NodeServicePolicies.OnCreateNodePolicy.QNAME,
  93. ContentModel.TYPE_PERSON,
  94. new JavaBehaviour(this, NodeServicePolicies.OnCreateNodePolicy.QNAME.getLocalName()
  95. , Behaviour.NotificationFrequency.TRANSACTION_COMMIT));
  96. }
  97. }
Add Comment
Please, Sign In to add comment