Hi, Laurent, hi, all! Below please find snippets of the code you've asked for. It is not the final "production" version and has not been thoroughly tested yet so please don't blame me for possible bugs/stupid things I've done. :-) Some comments: User is just a JavaBean, Constants class holds some constants used in the code. I don't include them (and exceptions) here because it will further increase the size of this message and you will most likely use your own classes anyway. :-) If you need all this stuff just sent me a mail, my address is [EMAIL PROTECTED]
Sincerely yours, Andrey. PS: comments/suggestions concerning this code are welcome :-) > -----Original Message----- > From: Slide Users Mailing List [mailto:[EMAIL PROTECTED] > Sent: Thursday, March 04, 2004 11:53 PM > To: [EMAIL PROTECTED] > Subject: RE: User addition problem > Importance: Low > > Hi Andrey, > > Would you share your user addition code ? > This obvious feature looks not so easy to implement. > First difficulty comes from the Configuration class: its > documentation says it provides only read methods. > Then share this code with the community (or at least with me > :P) would be greatly appreciated. > > Best regards, > > Laurent Sauvage. //ADD/CHANGE/REMOVE USER METHODS: /** * Adds a new User instance to the system. * @param newUser - a new User object. * @param currentUser - currently working user * @throws ConfigurationException - When can't access namespace * @throws DocumentStoreException - When any Slide exception occurs * @throws ArgumentException - When any of the arguments is equal to null or password is empty */ public void addUser(User newUser, String newUserPassword, User currentUser) throws ConfigurationException, DocumentStoreException, ArgumentException { boolean shouldEndTransaction = true; String invalidArguments = ""; if (null == currentUser) { invalidArguments = invalidArguments + " " + "current user"; } if (null == newUser) { invalidArguments = invalidArguments + " " + "new user"; } if ( (null == newUserPassword) || (newUserPassword.trim().equals("")) ) { invalidArguments = invalidArguments + " " + "new user password"; } if (!invalidArguments.equals("")) { throw new ArgumentException(Constants.ERROR_INVALID_ARGUMENT + invalidArguments); } slideToken = SlideSecurityHelper.getSlideToken(currentUser); namespaceAccessToken = SlideSecurityHelper.getNamespaceAccessToken(this); if (namespaceAccessToken != null) { // get the users path NamespaceConfig nc = namespaceAccessToken.getNamespaceConfig(); String strUri = nc.getUsersPath() + "/" + newUser.getName(); // get the helpers Structure structure = namespaceAccessToken.getStructureHelper(); Content content = namespaceAccessToken.getContentHelper(); Security security = namespaceAccessToken.getSecurityHelper(); // do the actual transaction try { if (namespaceAccessToken.getStatus() == Status.STATUS_NO_TRANSACTION) { namespaceAccessToken.begin(); // Transaction has been started here => should end here shouldEndTransaction = true; } else { // Transaction has been started somehere else => shouldn't end here shouldEndTransaction = false; } // create the node in the structure ObjectNode object; object = new SubjectNode(); structure.create(slideToken, object, strUri); // create a revision with the appropriate properties set NodeRevisionDescriptor revision = new NodeRevisionDescriptor(0); revision.setProperty( new NodeProperty("resourcetype", "<collection/>", true)); revision.setCreationDate(new Date()); revision.setLastModified(new Date()); revision.setProperty( new NodeProperty("getcontentlength", "0", true)); revision.setProperty( new NodeProperty("source", "", true)); revision.setProperty( new NodeProperty("password", newUserPassword) ), NodeProperty.SLIDE_NAMESPACE)); revision.setProperty( new NodeProperty("id", newUser.getId(), NodeProperty.DEFAULT_NAMESPACE)); revision.setProperty( new NodeProperty("firstName", newUser.getFirstName(), NodeProperty.DEFAULT_NAMESPACE)); revision.setProperty( new NodeProperty("lastName", newUser.getLastName(), NodeProperty.DEFAULT_NAMESPACE)); revision.setProperty( new NodeProperty("description", newUser.getDescription(), NodeProperty.DEFAULT_NAMESPACE)); revision.setProperty( new NodeProperty("businessGroup", newUser.getBusinessGroup(), NodeProperty.DEFAULT_NAMESPACE)); revision.setProperty( new NodeProperty("email", newUser.getEmail(), NodeProperty.DEFAULT_NAMESPACE)); revision.setProperty( new NodeProperty("phoneNumber", newUser.getPhoneNumber(), NodeProperty.DEFAULT_NAMESPACE)); content.create(slideToken, strUri, revision, null); security.grantPermission(slideToken, object, new SubjectNode("self"), new ActionNode("all"), true); if (newUser.getHasRootRole()) { // Link to the root role Role rootRole = getRole(SlideConstants.ROOT_ROLE); addUserRoleRelationship(newUser, rootRole, currentUser); // Link to the user role Role userRole = getRole(SlideConstants.USER_ROLE); addUserRoleRelationship(newUser, userRole, currentUser); } else { // Link to the user role Role userRole = getRole(SlideConstants.USER_ROLE); addUserRoleRelationship(newUser, userRole, currentUser); } if (shouldEndTransaction) { namespaceAccessToken.commit(); } } catch (ArgumentException ae) { ae.printStackTrace(System.out); // rollback the transaction try { if (shouldEndTransaction) { namespaceAccessToken.rollback(); } } catch (SystemException se) { // ignore } throw ae; } catch (DocumentStoreException dse) { dse.printStackTrace(System.out); // rollback the transaction try { if (shouldEndTransaction) { namespaceAccessToken.rollback(); } } catch (SystemException se) { // ignore } throw dse; } catch (ObjectAlreadyExistsException oaee) { oaee.printStackTrace(System.out); // rollback the transaction try { if (shouldEndTransaction) { namespaceAccessToken.rollback(); } } catch (SystemException se) { // ignore } // duplicate username throw new ConfigurationException(SlideConstants.USER_EXISTS, oaee); } catch (ServiceAccessException sae) { sae.printStackTrace(System.out); // rollback the transaction try { if (shouldEndTransaction) { namespaceAccessToken.rollback(); } } catch (SystemException se) { // ignore } // low level service access failed throw new DocumentStoreException(SlideConstants.SERVICE_FAILED, sae); } catch (Exception e) { e.printStackTrace(System.out); // rollback the transaction try { if (shouldEndTransaction) { namespaceAccessToken.rollback(); } } catch (SystemException se) { // ignore } // any other errors are unanticipated throw new DocumentStoreException(SlideConstants.UNKNOWN_EXCEPTION, e); } } else { // Namespace can't be accessed! throw new ConfigurationException(SlideConstants.NAMESPACE_ACCESS_FAILED); } } /** * Changes an existing User info. * @param changedUser - a User object to be changed. * @param currentUser - currently working user * @throws ConfigurationException - When can't access namespace * @throws DocumentStoreException - When any Slide exception occurs * @throws ArgumentException - When any of the arguments is equal to null */ public void changeUser(User changedUser, String newPassword, User currentUser) throws ConfigurationException, DocumentStoreException, ArgumentException { boolean shouldEndTransaction = true; String invalidArguments = ""; if (null == currentUser) { invalidArguments = invalidArguments + " " + "current user"; } if (null == changedUser) { invalidArguments = invalidArguments + " " + "old user"; } if (!invalidArguments.equals("")) { throw new ArgumentException(Constants.ERROR_INVALID_ARGUMENT + invalidArguments); } slideToken = SlideSecurityHelper.getSlideToken(currentUser); NamespaceAccessToken namespaceAccessToken = SlideSecurityHelper.getNamespaceAccessToken(this); if (namespaceAccessToken != null) { // get the helpers Structure structure = namespaceAccessToken.getStructureHelper(); Content content = namespaceAccessToken.getContentHelper(); Macro macro = namespaceAccessToken.getMacroHelper(); Security security = namespaceAccessToken.getSecurityHelper(); // get the users path NamespaceConfig namespaceConfig = namespaceAccessToken.getNamespaceConfig(); String usersDirUri = namespaceConfig.getUsersPath(); String oldUserPathUri = ""; boolean oldHasRootRole = false; UserCollection users = getUserCollection(); for (int i = 0; i < users.size(); i++) { User u = (User)users.get(i); if (u.equals(changedUser)) { oldUserPathUri = usersDirUri + "/" + u.getName(); oldHasRootRole = u.getHasRootRole(); } } String newUserPathUri = usersDirUri + "/" + changedUser.getName(); // do the actual transaction try { if (namespaceAccessToken.getStatus() == Status.STATUS_NO_TRANSACTION) { namespaceAccessToken.begin(); // Transaction has been started here => should end here shouldEndTransaction = true; } else { // Transaction has been started somehere else => shouldn't end here shouldEndTransaction = false; } if ( (!newUserPathUri.equals(oldUserPathUri)) && (!oldUserPathUri.equals("")) ) { // move the user node macro.move(slideToken, oldUserPathUri, newUserPathUri, new MacroParameters(true, false)); } NodeRevisionDescriptor revision = null; NodeRevisionDescriptors revisions = content.retrieve(slideToken, newUserPathUri); if (revisions != null) { revision = content.retrieve(slideToken, revisions); } else { revision = new NodeRevisionDescriptor(0); revision.setProperty( new NodeProperty("resourcetype", "<collection/>", true)); revision.setCreationDate(new Date()); revision.setLastModified(new Date()); revision.setProperty( new NodeProperty("getcontentlength", "0", true)); revision.setProperty( new NodeProperty("source", "", true)); } // Set the new password if ( (newPassword != null) && (!newPassword.equals("")) ) { // Password required for a new user only // If existing user lefts the Password field blank // we simply don't change it! revision.setProperty( new NodeProperty("password",newPassword, NodeProperty.SLIDE_NAMESPACE)); } revision.setProperty( new NodeProperty("firstName", changedUser.getFirstName(), NodeProperty.DEFAULT_NAMESPACE)); revision.setProperty( new NodeProperty("lastName", changedUser.getLastName(), NodeProperty.DEFAULT_NAMESPACE)); revision.setProperty( new NodeProperty("description", changedUser.getDescription(), NodeProperty.DEFAULT_NAMESPACE)); revision.setProperty( new NodeProperty("businessGroup", changedUser.getBusinessGroup(), NodeProperty.DEFAULT_NAMESPACE)); revision.setProperty( new NodeProperty("email", changedUser.getEmail(), NodeProperty.DEFAULT_NAMESPACE)); revision.setProperty( new NodeProperty("phoneNumber", changedUser.getPhoneNumber(), NodeProperty.DEFAULT_NAMESPACE)); content.store(slideToken, newUserPathUri, revision, null); if (oldHasRootRole != changedUser.getHasRootRole()) { // Basic role (user or root) change! if (changedUser.getHasRootRole()) { // Link to the root role Role rootRole = getRole(SlideConstants.ROOT_ROLE); addUserRoleRelationship(changedUser, rootRole, currentUser); } else { // Remove link to the root role Role rootRole = getRole(SlideConstants.ROOT_ROLE); removeUserRoleRelationship(changedUser, rootRole, currentUser); } } if (shouldEndTransaction) { namespaceAccessToken.commit(); } } catch (ArgumentException ae) { ae.printStackTrace(System.out); // rollback the transaction try { if (shouldEndTransaction) { namespaceAccessToken.rollback(); } } catch (SystemException se) { // ignore } throw ae; } catch (DocumentStoreException dse) { dse.printStackTrace(System.out); try { if (shouldEndTransaction) { namespaceAccessToken.rollback(); } } catch (SystemException se) { // ignore } throw dse; } catch (MacroException me) { me.printStackTrace(System.out); // some aspect of the move operation failed // rollback the transaction try { if (shouldEndTransaction) { namespaceAccessToken.rollback(); } } catch (SystemException se) { // ignore } throw new DocumentStoreException(SlideConstants.SERVICE_FAILED, me); } catch (ObjectLockedException ole) { ole.printStackTrace(System.out); // one or more of the affected objects is currently locked // rollback the transaction try { if (shouldEndTransaction) { namespaceAccessToken.rollback(); } } catch (SystemException se) { // ignore } throw new DocumentStoreException(SlideConstants.OBJECT_LOCKED, ole); } catch (ServiceAccessException sae) { sae.printStackTrace(System.out); // low level service access failed // rollback the transaction try { if (shouldEndTransaction) { namespaceAccessToken.rollback(); } } catch (SystemException se) { // ignore } throw new DocumentStoreException(SlideConstants.SERVICE_FAILED, sae); } catch (Exception e) { e.printStackTrace(System.out); // any other errors are unanticipated // rollback the transaction try { if (shouldEndTransaction) { namespaceAccessToken.rollback(); } } catch (SystemException se) { // ignore } throw new DocumentStoreException(SlideConstants.SERVICE_FAILED, e); } } else { // Namespace can't be accessed! throw new ConfigurationException(SlideConstants.SERVICE_FAILED); } } /** * Permanently removes a given User from the system. * @param oldUser - a User object to be removed. * @param currentUser - currently working user * @throws ConfigurationException - When can't access namespace * @throws DocumentStoreException - When any Slide exception occurs * @throws ArgumentException - When any of the arguments is equal to null */ public void removeUser(User recedingUser, User currentUser) throws ConfigurationException, DocumentStoreException, ArgumentException { boolean shouldEndTransaction = true; if (null == currentUser) { throw new ArgumentException(Constants.ERROR_INVALID_ARGUMENT + "currentUser"); } if (null == recedingUser) { throw new ArgumentException(Constants.ERROR_INVALID_ARGUMENT + "currentUser"); } slideToken = SlideSecurityHelper.getSlideToken(currentUser); NamespaceAccessToken namespaceAccessToken = SlideSecurityHelper.getNamespaceAccessToken(this); if (namespaceAccessToken != null) { // get the helpers Macro macro = namespaceAccessToken.getMacroHelper(); Security security = namespaceAccessToken.getSecurityHelper(); // get the users path NamespaceConfig namespaceConfig = namespaceAccessToken.getNamespaceConfig(); String usersDirUri = namespaceConfig.getUsersPath(); String recedingUserPathUri = usersDirUri + "/" + recedingUser.getName(); // do the actual transaction try { if (namespaceAccessToken.getStatus() == Status.STATUS_NO_TRANSACTION) { namespaceAccessToken.begin(); // Transaction has been started here => should end here shouldEndTransaction = true; } else { // Transaction has been started somehere else => shouldn't end here shouldEndTransaction = false; } // Remove all user-role relationships RoleCollection allRoles = getRoleCollection(); for (int i = 0; i < allRoles.size(); i++) { Role r = (Role)allRoles.get(i); //System.out.println("Shao; removeUser: " + recedingUser.getName() + "; check role: " + r.getName()); if (security.hasRole( SlideSecurityHelper.getSlideToken(recedingUser), r.getName())) { //System.out.println("Shao; removeUser: " + recedingUser.getName() + "; remove relation to role: " + r.getName()); removeUserRoleRelationship(recedingUser, r, currentUser); } } // TODO Find and remove links to the deleted user node ? // do a recursive delete macro.delete(slideToken, recedingUserPathUri, new MacroParameters(true, false)); if (shouldEndTransaction) { namespaceAccessToken.commit(); } } catch (ArgumentException ae) { ae.printStackTrace(System.out); // rollback the transaction try { if (shouldEndTransaction) { namespaceAccessToken.rollback(); } } catch (SystemException se) { // ignore } throw ae; } catch (Exception e) { e.printStackTrace(System.out); // some aspect of the remove operation failed // rollback the transaction try { if (shouldEndTransaction) { namespaceAccessToken.rollback(); } } catch (SystemException se) { // ignore } throw new DocumentStoreException(SlideConstants.SERVICE_FAILED, e); } } else { // Namespace can't be accessed! throw new ConfigurationException(SlideConstants.SERVICE_FAILED); } } //HELPER METHODS FROM the SlideSecurityHelper Class: /** * Acquires a NamespaceAccessToken. * @param token - an object referenced * by security context of NamespaceAccessToken. * @return - a NamespaceAccessToken instance. */ static NamespaceAccessToken getNamespaceAccessToken(Object token) { SecurityToken securityToken = new SecurityToken(token); String namespaceName = SlideConfigurationManager. getSetting(SlideConstants. QUERY_NAMESPACE_NAME); if (namespaceName == null) { // The default namespace namespaceName = Domain.getDefaultNamespace(); } NamespaceAccessToken namespaceToken = Domain.accessNamespace( securityToken, namespaceName); return namespaceToken; } /** * Gets a Slide security token, which represents a current user. * @param user - a system User. * @return slide security token. */ static SlideToken getSlideToken(User user) { CredentialsToken credentials = new CredentialsToken(user); return new SlideTokenImpl(credentials); } --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
