Adding exist check and creation time for addUser in UserManager

This commit is contained in:
Martin Stockhammer 2020-11-08 14:21:32 +01:00
parent 14ebb1e898
commit 5caf69ba8c
4 changed files with 84 additions and 9 deletions

View File

@ -0,0 +1,42 @@
package org.apache.archiva.redback.users;/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/**
* @author Martin Stockhammer <martin_s@apache.org>
*/
public class UserExistsException extends UserManagerException
{
public UserExistsException( )
{
}
public UserExistsException( String message, Throwable cause )
{
super( message, cause );
}
public UserExistsException( String message )
{
super( message );
}
public UserExistsException( Throwable cause )
{
super( cause );
}
}

View File

@ -24,6 +24,7 @@
import org.apache.archiva.redback.users.Messages;
import org.apache.archiva.redback.users.PermanentUserException;
import org.apache.archiva.redback.users.User;
import org.apache.archiva.redback.users.UserExistsException;
import org.apache.archiva.redback.users.UserManagerException;
import org.apache.archiva.redback.users.UserNotFoundException;
import org.apache.archiva.redback.users.UserQuery;
@ -127,6 +128,10 @@ public User addUser(User user) throws UserManagerException {
throw new IllegalStateException(
Messages.getString( "user.manager.cannot.add.user.without.username" ) ); //$NON-NLS-1$
}
if (userExists( user.getUsername() )) {
log.debug( "User exists already " + user.getUsername( ) );
throw new UserExistsException( "User exists already " + user.getUsername( ) );
}
userSecurityPolicy.extensionChangePassword( user );
@ -147,6 +152,8 @@ public User addUser(User user) throws UserManagerException {
if (user.getLastPasswordChange()==null) {
user.setLastPasswordChange(new Date());
}
user.setAccountCreationDate( new Date( ) );
log.debug( "Adding new user " + user.getUsername( ) );
em.persist( user );
return user;
}
@ -282,7 +289,7 @@ public void deleteUser(String username) throws UserManagerException {
@Transactional
@Override
public void addUserUnchecked(User user) throws UserManagerException {
log.info("addUserUnchecked "+user.getUsername());
log.debug("addUserUnchecked "+user.getUsername());
if ( !( user instanceof JpaUser ) )
{
throw new UserManagerException( "Unable to Add User. User object " + user.getClass().getName() +
@ -294,12 +301,7 @@ public void addUserUnchecked(User user) throws UserManagerException {
throw new IllegalStateException(
Messages.getString( "user.manager.cannot.add.user.without.username" ) ); //$NON-NLS-1$
}
TypedQuery<JpaUser> q = em.createQuery("SELECT u FROM JpaUser u", JpaUser.class);
for (JpaUser u : q.getResultList()) {
log.info("USER FOUND: "+u.getUsername());
}
log.info("NEW USER "+user.getUsername());
user.setAccountCreationDate( new Date( ) );
em.persist( user );
}

View File

@ -28,6 +28,7 @@
import org.apache.archiva.redback.configuration.UserConfigurationKeys;
import org.apache.archiva.redback.users.AbstractUserManager;
import org.apache.archiva.redback.users.User;
import org.apache.archiva.redback.users.UserExistsException;
import org.apache.archiva.redback.users.UserManager;
import org.apache.archiva.redback.users.UserManagerException;
import org.apache.archiva.redback.users.UserNotFoundException;
@ -46,6 +47,7 @@
import javax.naming.directory.DirContext;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
/**
@ -123,6 +125,16 @@ private User addUser( User user, boolean checked )
{
return null;
}
try
{
if (checked && userExists( user.getUsername() )){
throw new UserExistsException( "User exists already " + user.getUsername( ) );
}
}
catch ( UserManagerException e )
{
throw new LdapException( "Unexpected LDAP error " + e.getMessage( ) );
}
if ( isReadOnly() && GUEST_USERNAME.equals( user.getUsername() ) )
{
@ -130,6 +142,8 @@ private User addUser( User user, boolean checked )
return guestUser;
}
user.setAccountCreationDate( new Date( ) );
LdapConnection ldapConnection = getLdapConnection();
try
{

View File

@ -23,6 +23,7 @@
import org.apache.archiva.redback.users.AbstractUserManager;
import org.apache.archiva.redback.users.PermanentUserException;
import org.apache.archiva.redback.users.User;
import org.apache.archiva.redback.users.UserExistsException;
import org.apache.archiva.redback.users.UserManager;
import org.apache.archiva.redback.users.UserManagerException;
import org.apache.archiva.redback.users.UserNotFoundException;
@ -34,6 +35,7 @@
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
@ -96,8 +98,16 @@ public List<User> findUsersByQuery( UserQuery query )
private Map<Object, User> users = new HashMap<Object, User>();
public User addUser( User user )
public User addUser(User user) throws UserManagerException {
return addUser( user, false );
}
private User addUser( User user, boolean checked ) throws UserManagerException
{
if (checked && userExists( user.getUsername() )) {
throw new UserExistsException( "User exists already " + user.getUsername( ) );
}
user.setAccountCreationDate( new Date( ) );
saveUser( user );
fireUserManagerUserAdded( user );
@ -176,7 +186,14 @@ public void deleteUser( String username )
public void addUserUnchecked( User user )
{
addUser( user );
try
{
addUser( user, false );
}
catch ( UserManagerException e )
{
log.error( "User manager exception " + e.getMessage( ) );
}
}
public void eraseDatabase()