Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] CODENVY-627 Check for reserved usernames on user creation #1511

Closed
wants to merge 6 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
*******************************************************************************/
package org.eclipse.che.api.user.server;

import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;

import org.eclipse.che.api.core.ConflictException;
import org.eclipse.che.api.core.NotFoundException;
import org.eclipse.che.api.core.ServerException;
Expand All @@ -22,9 +25,12 @@
import org.slf4j.LoggerFactory;

import javax.inject.Inject;
import javax.inject.Named;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import static com.google.common.base.MoreObjects.firstNonNull;
import static java.lang.String.format;
Expand All @@ -45,15 +51,17 @@ public class UserManager {
private final UserDao userDao;
private final UserProfileDao profileDao;
private final PreferenceDao preferenceDao;

private final Set<String> reservedNames;

@Inject
public UserManager(UserDao userDao,
UserProfileDao profileDao,
PreferenceDao preferenceDao) {
PreferenceDao preferenceDao,
@Named("user.reserved_names") String[] reservedNames) {
this.userDao = userDao;
this.profileDao = profileDao;
this.preferenceDao = preferenceDao;
this.reservedNames = Sets.newHashSet(reservedNames);
}


Expand All @@ -68,6 +76,9 @@ public UserManager(UserDao userDao,
* when any other error occurs
*/
public void create(User user, boolean isTemporary) throws ConflictException, ServerException {
if (isNameReserved(user.getName())) {
throw new ConflictException("Username is reserved");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add name to exception message

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should report in exception the username that is conflicting

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

}
user.withId(generate("user", ID_LENGTH))
.withPassword(firstNonNull(user.getPassword(), generate("", PASSWORD_LENGTH)));
userDao.create(user);
Expand Down Expand Up @@ -164,4 +175,8 @@ public User getByName(String userName) throws NotFoundException, ServerException
public void remove(String id) throws NotFoundException, ServerException, ConflictException {
userDao.remove(id);
}

private boolean isNameReserved(String name) {
return reservedNames.contains(name);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inline

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok

}
}