View Javadoc

1   package org.ogce.purse.impl;
2   
3   import java.util.Calendar;
4   import java.util.Iterator;
5   import java.util.Set;
6   
7   import org.apache.log4j.Logger;
8   import org.globus.purse.registration.UserData;
9   import org.gridlab.gridsphere.portlet.PortletGroup;
10  import org.gridlab.gridsphere.portlet.PortletRequest;
11  import org.gridlab.gridsphere.portlet.PortletRole;
12  import org.gridlab.gridsphere.portlet.User;
13  import org.gridlab.gridsphere.portlet.impl.SportletUser;
14  import org.gridlab.gridsphere.portlet.service.PortletServiceNotFoundException;
15  import org.gridlab.gridsphere.portlet.service.PortletServiceUnavailableException;
16  import org.gridlab.gridsphere.portlet.service.spi.PortletServiceFactory;
17  import org.gridlab.gridsphere.portlet.service.spi.impl.SportletServiceFactory;
18  import org.gridlab.gridsphere.portlets.core.user.ProfileManagerPortlet;
19  import org.gridlab.gridsphere.provider.event.FormEvent;
20  import org.gridlab.gridsphere.services.core.portal.PortalConfigService;
21  import org.gridlab.gridsphere.services.core.security.acl.AccessControlManagerService;
22  import org.gridlab.gridsphere.services.core.security.acl.GroupRequest;
23  import org.gridlab.gridsphere.services.core.security.password.InvalidPasswordException;
24  import org.gridlab.gridsphere.services.core.security.password.PasswordEditor;
25  import org.gridlab.gridsphere.services.core.security.password.PasswordManagerService;
26  import org.gridlab.gridsphere.services.core.user.UserManagerService;
27  import org.ogce.purse.api.PurseEvent;
28  import org.ogce.purse.api.PurseEventException;
29  import org.ogce.purse.api.RegistrationModule;
30  
31  public class GridSphereRegistrationModule implements RegistrationModule {
32  
33      private static final Logger log = Logger.getLogger(GridSphereRegistrationModule.class);
34      private static GridSphereRegistrationModule module = null;
35      private static UserManagerService userManagerService;
36      private static PasswordManagerService passwordManagerService;
37      private static PortalConfigService portalConfigService;
38      private static AccessControlManagerService aclManagerService;
39      
40      private GridSphereRegistrationModule() {
41          PortletServiceFactory serviceFactory = SportletServiceFactory.getInstance();
42          try {
43              userManagerService = (UserManagerService)
44                  serviceFactory.createPortletService(UserManagerService.class, null, true);
45              passwordManagerService = (PasswordManagerService)
46                  serviceFactory.createPortletService(PasswordManagerService.class, null, true);
47              portalConfigService = (PortalConfigService)
48                  serviceFactory.createPortletService(PortalConfigService.class, null, true);
49              aclManagerService = (AccessControlManagerService)
50                  serviceFactory.createPortletService(AccessControlManagerService.class, null, true);
51          } catch (PortletServiceNotFoundException e) {
52              e.printStackTrace();
53          } catch (PortletServiceUnavailableException e) {
54              e.printStackTrace();
55          }
56      }
57      public static GridSphereRegistrationModule getInstance() {
58          if (module == null) {
59              module = new GridSphereRegistrationModule();
60          } 
61          return module;
62      }
63  
64      private void createUser(UserData user) {
65          SportletUser newUser = userManagerService.createUser();
66  
67          // Edit account attributes
68          newUser.setUserName(user.getUserName());
69          newUser.setFullName(user.getFirstName() + " " + user.getLastName());
70          newUser.setEmailAddress(user.getEmailAddress());
71          newUser.setOrganization(user.getInstitution());
72          userManagerService.saveUser(newUser);
73          log.debug("Saved new user [" + newUser.getUserName() + "]");
74          
75          // Save password
76          PasswordEditor editor = passwordManagerService.editPassword(newUser);
77          editor.setValue(user.getPassword());
78          editor.setDateLastModified(Calendar.getInstance().getTime());
79          passwordManagerService.savePassword(editor);
80          
81          // Set default role and group
82          Set groups = portalConfigService.getPortalConfigSettings().getDefaultGroups();
83          Iterator it = groups.iterator();
84          while (it.hasNext()) {
85              PortletGroup group = (PortletGroup) it.next();
86              GroupRequest groupRequest = aclManagerService.createGroupEntry();
87              groupRequest.setUser(newUser);
88              groupRequest.setGroup(group);
89              groupRequest.setRole(aclManagerService.getRoleByName(PortletRole.USER.getName()));
90  
91              // Submit changes
92              aclManagerService.saveGroupEntry(groupRequest);
93          }
94      }
95  
96      public void onEvent(PurseEvent event) throws PurseEventException {
97          UserData userData = event.getUserData();
98          switch (event.getEventType()) {
99          case PurseEvent.CONFIRMED_EVENT:
100         case PurseEvent.ACCEPTED_EVENT:
101             if (! accountExists(userData.getUserName())) {
102                 createUser(userData);
103             }
104             break;
105         case PurseEvent.PASSWORD_UPDATED_EVENT:
106             log.debug("Got a password update event");
107             updatePassword(event);
108         default:
109             break;
110         }
111     }
112     
113     private void updatePassword(PurseEvent event) throws PurseEventException {
114         boolean successful = false;
115         UserData userData = event.getUserData();
116         User user = userManagerService.getUserByUserName(userData.getUserName());
117         String origPasswd = event.getOldPassword();
118         try {
119             passwordManagerService.validateSuppliedPassword(user, origPasswd);
120             log.debug("Verified user password");
121         } catch (InvalidPasswordException e) {
122             log.error("Unable to verify GridSphere password", e);
123             return;
124         }
125 
126         String passwordValue = event.getNewPassword();
127 
128         if (passwordValue == null) {
129             log.error("Old password is null");
130         } else
131         // Otherwise, password must match confirmation
132         if (passwordValue.length() == 0) {
133             log.error("Password is zero length");
134         } else if (passwordValue.length() < 5) {
135             log.error("Password is too short");
136         } else {
137             // save password
138             PasswordEditor editPasswd = passwordManagerService.editPassword(user);
139             editPasswd.setValue(passwordValue);
140             editPasswd.setDateLastModified(Calendar.getInstance().getTime());
141             passwordManagerService.savePassword(editPasswd);
142             log.debug("Successfully saved new password");
143             successful = true;
144         }
145         if (!successful) {
146             throw new PurseEventException("Unable to update GridSphere password.");
147         }
148     }
149     
150     public String getName() {
151         return "GridSphere";
152     }
153     public int getStatus(UserData userData) {
154         if (accountExists(userData.getUserName())) {
155             return RegistrationModule.STATUS_ACCOUNT_OK;
156         } else {
157             return RegistrationModule.STATUS_ACCOUNT_NONE;
158         }
159     }
160     
161     public boolean accountExists(String username) {
162         return userManagerService.existsUserName(username);
163     }
164 
165 }