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

Edit user info admin service #2631

Merged
merged 49 commits into from
Jul 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
26c2fb4
Add beginnings of register participant admin command
denniscunningham May 11, 2023
e6d740a
Merge with develop
denniscunningham Jun 15, 2023
c228632
WIP
denniscunningham Jun 16, 2023
c68c779
WIP
denniscunningham Jun 19, 2023
a1ababa
Merge with develop
denniscunningham Jun 20, 2023
57e80dc
Fix checkstyle violation
denniscunningham Jun 20, 2023
d1e1e99
WIP
denniscunningham Jun 20, 2023
d9fdd4a
WIP
denniscunningham Jun 20, 2023
959400d
Merge branch 'develop' into register_ptp
denniscunningham Jun 20, 2023
e436d11
WIP
denniscunningham Jun 21, 2023
cdf1147
Merge branch 'develop' into register_ptp
denniscunningham Jun 21, 2023
70711a6
WIP
denniscunningham Jun 22, 2023
f1a4e7a
WIP
denniscunningham Jun 22, 2023
094b162
WIP
denniscunningham Jun 22, 2023
97b5ee7
WIP
denniscunningham Jun 22, 2023
68bed51
Merge branch 'develop' into register_ptp
denniscunningham Jun 22, 2023
c9c94fc
WIP
denniscunningham Jun 23, 2023
4b774ae
Merge branch 'develop' into register_ptp
denniscunningham Jun 23, 2023
e432b5f
WIP
denniscunningham Jun 23, 2023
e87acd1
WIP
denniscunningham Jun 24, 2023
473ad9c
Merge with Develop
denniscunningham Jun 27, 2023
75db27e
Add support for multiple users per request
denniscunningham Jun 27, 2023
9d25d08
WIP
denniscunningham Jun 27, 2023
1e2ab65
Add user create and remove support
denniscunningham Jun 30, 2023
d4d5000
Merge branch 'develop' into register_ptp
denniscunningham Jun 30, 2023
9c28c2c
WIP
denniscunningham Jul 5, 2023
4e3583a
WIP
denniscunningham Jul 5, 2023
ac287d7
Merge branch 'develop' into register_ptp
denniscunningham Jul 11, 2023
dda96f2
Merge branch 'develop' into register_ptp
denniscunningham Jul 11, 2023
3c82589
WIP
denniscunningham Jul 12, 2023
915f834
WIP
denniscunningham Jul 14, 2023
40347c7
WIP
denniscunningham Jul 14, 2023
2be0cd2
Merge branch 'develop' into register_ptp
denniscunningham Jul 14, 2023
9a72968
WIP
denniscunningham Jul 18, 2023
a6dd55d
Remove participant registration feature
denniscunningham Jul 18, 2023
b444c9f
Initial implementation of DSM user administration
denniscunningham Jul 18, 2023
be5eadc
WIP
denniscunningham Jul 20, 2023
6119f4b
WIP
denniscunningham Jul 20, 2023
a164c6c
Updates due to code review, mostly reworked UserDao
denniscunningham Jul 20, 2023
ee54a07
WIP
denniscunningham Jul 21, 2023
66693ed
Merge branch 'pepper-970' into pepper-973
denniscunningham Jul 21, 2023
a038961
WIP
denniscunningham Jul 21, 2023
5e9cecd
WIP
denniscunningham Jul 24, 2023
1497361
Create and delete user settings; improve user add and remove
denniscunningham Jul 25, 2023
fd34526
Merge with develop
denniscunningham Jul 26, 2023
8cb0db6
Remove unused subclass
denniscunningham Jul 26, 2023
6d2581c
Fix typo
denniscunningham Jul 27, 2023
43e6360
Add support for adding existing users to study
denniscunningham Jul 27, 2023
af780c0
Merge branch 'develop' into pepper-973
denniscunningham Jul 28, 2023
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 @@ -6,9 +6,11 @@
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import lombok.Data;
import lombok.NonNull;
import org.broadinstitute.dsm.exception.DsmInternalError;
import org.broadinstitute.dsm.statics.DBConstants;
import org.broadinstitute.dsm.statics.QueryExtension;
import org.broadinstitute.lddp.db.SimpleResult;
Expand All @@ -27,6 +29,8 @@ public class UserSettings {
+ "WHERE user.user_id = settings.user_id AND user.is_active = 1";
private static final String SQL_INSERT_USER_SETTINGS = "INSERT INTO user_settings SET user_id = ?";

private static final String SQL_DELETE_USER_SETTINGS = "DELETE FROM user_settings WHERE user_id = ?";

private static final String USER_ID = "userId";

private int rowsOnPage;
Expand Down Expand Up @@ -104,12 +108,38 @@ public static UserSettings getUserSettings(@NonNull String email) {
return us;
}

public static void insertUserSetting(Connection conn, int userId) {
try (PreparedStatement insertKit = conn.prepareStatement(SQL_INSERT_USER_SETTINGS)) {
insertKit.setInt(1, userId);
insertKit.executeUpdate();
} catch (SQLException e) {
throw new RuntimeException("Error inserting new user_settings ", e);
public static int createUserSettings(int userId) {
return inTransaction(conn -> insertUserSetting(conn, userId));
}

public static int insertUserSetting(Connection conn, int userId) {
int id = -1;
try (PreparedStatement stmt = conn.prepareStatement(SQL_INSERT_USER_SETTINGS, Statement.RETURN_GENERATED_KEYS)) {
stmt.setInt(1, userId);
int result = stmt.executeUpdate();
if (result != 1) {
throw new DsmInternalError("Error inserting user setting. Result count was " + result);
}
try (ResultSet rs = stmt.getGeneratedKeys()) {
if (rs.next()) {
id = rs.getInt(1);
}
}
} catch (SQLException ex) {
throw new DsmInternalError("Error inserting user setting", ex);
}
return id;
}

public static int deleteUserSettings(int userId) {
return inTransaction(conn -> {
try (PreparedStatement stmt = conn.prepareStatement(SQL_DELETE_USER_SETTINGS)) {
stmt.setInt(1, userId);
return stmt.executeUpdate();
} catch (SQLException ex) {
String msg = String.format("Error deleting user settings: userId=%d", userId);
throw new DsmInternalError(msg, ex);
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class UserDao implements Dao<UserDto> {
public static final String PHONE_NUMBER = "phone_number";
public static final String IS_ACTIVE = "is_active";
private static final String SQL_INSERT_USER = "INSERT INTO access_user (name, email, phone_number, is_active) VALUES (?,?,?,?)";
private static final String SQL_UPDATE_USER =
"UPDATE access_user SET name = ?, phone_number = ?, is_active = ? WHERE user_id = ?";
private static final String SQL_DELETE_USER_BY_ID = "DELETE FROM access_user WHERE user_id = ?";
private static final String SQL_SELECT_USER_BY_EMAIL =
"SELECT user.user_id, user.name, user.email, user.phone_number, user.is_active FROM access_user user "
Expand Down Expand Up @@ -87,7 +89,7 @@ public Optional<UserDto> get(long userId) {

@Override
public int create(UserDto userDto) {
String email = userDto.getEmail().orElse(null);
String email = userDto.getEmailOrThrow();
if (StringUtils.isBlank(email)) {
throw new DsmInternalError("Error inserting user: email is blank");
}
Expand All @@ -110,6 +112,26 @@ public int create(UserDto userDto) {
});
}

public static void update(int userId, UserDto userDto) {
String email = userDto.getEmailOrThrow();
String errorMsg = "Error updating user " + email;
int res = inTransaction(conn -> {
try (PreparedStatement stmt = conn.prepareStatement(SQL_UPDATE_USER)) {
stmt.setString(1, userDto.getName().orElse(null));
ssettipalli marked this conversation as resolved.
Show resolved Hide resolved
stmt.setString(2, userDto.getPhoneNumber().orElse(null));
ssettipalli marked this conversation as resolved.
Show resolved Hide resolved
stmt.setInt(3, userDto.getIsActive().orElse(1));
stmt.setInt(4, userId);
int result = stmt.executeUpdate();
if (result != 1) {
throw new DsmInternalError(errorMsg + " Result count was " + result);
}
return result;
} catch (SQLException ex) {
throw new DsmInternalError(errorMsg, ex);
}
});
}

@Override
public int delete(int id) {
SimpleResult simpleResult = DaoUtil.deleteById(id, SQL_DELETE_USER_BY_ID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import lombok.AllArgsConstructor;
import lombok.Setter;
import org.broadinstitute.dsm.exception.DsmInternalError;

@Setter
@AllArgsConstructor
Expand Down Expand Up @@ -36,10 +37,20 @@ public Optional<String> getEmail() {
return Optional.ofNullable(email);
}

// TODO: getEmail should always throw on a missing email since it is not nullable
// but there are a lot of callers. Feel free to chase them all down and rename this method -DC
public String getEmailOrThrow() {
return getEmail().orElseThrow(() -> new DsmInternalError("User email cannot be null"));
ssettipalli marked this conversation as resolved.
Show resolved Hide resolved
}

public Optional<String> getPhoneNumber() {
return Optional.ofNullable(phoneNumber);
}
public Optional<Integer> getIsActive() {
return Optional.ofNullable(isActive);
}

public boolean isActive() {
return getIsActive().orElse(0) == 1;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected Object processRequest(Request request, Response response, String userI
String oncHistoryUserId;
try {
UserDto user = new UserDao().get(Integer.parseInt(userId)).orElseThrow();
oncHistoryUserId = user.getEmail().orElseThrow();
oncHistoryUserId = user.getEmailOrThrow();
if (oncHistoryUserId.isEmpty()) {
throw new DsmInternalError("Empty email address");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.apache.commons.lang3.StringUtils;
import org.broadinstitute.dsm.security.RequestHandler;
import org.broadinstitute.dsm.service.admin.AddUserRequest;
import org.broadinstitute.dsm.service.admin.UpdateUserRequest;
import org.broadinstitute.dsm.service.admin.UserAdminService;
import org.broadinstitute.dsm.service.admin.UserRequest;
import org.broadinstitute.dsm.statics.RoutePath;
Expand Down Expand Up @@ -47,6 +48,20 @@ public Object processRequest(Request request, Response response, String userId)
} catch (Exception e) {
return UserRoleRoute.handleError(e, "adding user", response);
}
} else if (requestMethod.equals(RoutePath.RequestMethod.PUT.toString())) {
UpdateUserRequest req;
try {
req = new Gson().fromJson(body, UpdateUserRequest.class);
} catch (Exception e) {
log.info("Invalid request format for {}", body);
response.status(400);
return "Invalid request format";
}
try {
adminService.updateUser(req);
} catch (Exception e) {
return UserRoleRoute.handleError(e, "updating user", response);
}
} else if (requestMethod.equals(RoutePath.RequestMethod.DELETE.toString())) {
UserRequest req;
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import java.util.List;

import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.broadinstitute.dsm.db.dto.user.UserDto;
import org.broadinstitute.dsm.exception.DsmInternalError;

@Data
public class AddUserRequest {
Expand Down Expand Up @@ -31,5 +33,18 @@ public User(String email, String name, String phone, List<String> roles) {
public UserDto asUserDto() {
return new UserDto(name, email, phone);
}

public UserDto asUpdatedUserDto(UserDto userDto) {
if (!email.equalsIgnoreCase(userDto.getEmailOrThrow())) {
throw new DsmInternalError("Assert: email addresses do not match");
}
if (!StringUtils.isBlank(name)) {
userDto.setName(name);
}
if (!StringUtils.isBlank(phone)) {
userDto.setPhoneNumber(phone);
}
return userDto;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.broadinstitute.dsm.service.admin;

import java.util.List;

import lombok.Data;
import org.broadinstitute.dsm.db.dto.user.UserDto;

@Data
public class UpdateUserRequest {

private final List<UpdateUserRequest.User> users;

public UpdateUserRequest(List<UpdateUserRequest.User> users) {
this.users = users;
}

@Data
public static class User {
private final String email;
private final String name;
private final String phone;

public User(String email, String name, String phone) {
this.email = email;
this.name = name;
this.phone = phone;
}

public UserDto asUserDto() {
return new UserDto(name, email, phone);
}
}
}
Loading
Loading