-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Changes from 2 commits
7a4f7f5
f855f8e
df25c1e
da69f5d
957742f
017da28
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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); | ||
} | ||
|
||
|
||
|
@@ -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"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should report in exception the username that is conflicting There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. inline There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok |
||
} | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok