From abe49c372f3aa29f517895a17afb884f44eb69ee Mon Sep 17 00:00:00 2001 From: joalhelk <joalhelk@jyu.fi> Date: Tue, 15 Mar 2022 16:55:52 +0200 Subject: [PATCH] admin modify users and new datatypes --- WatcherGui/.classpath | 2 +- .../.settings/org.eclipse.jdt.core.prefs | 8 +- .../joelhelkala/watcherGui/Colors/Colors.java | 9 ++ .../watcherGui/Datatypes/UserType.java | 30 ++++++ .../watcherGui/frames/LoginPage.java | 9 +- .../watcherGui/frames/WelcomePage.java | 65 +++++++----- .../frames/panels/LineChartPanel.java | 2 + .../frames/panels/ProgressBarCirclePanel.java | 2 - .../frames/subframes/AdminFrame.java | 99 +++++++++++++++++++ .../frames/subframes/FriendsFrame.java | 5 +- .../frames/subframes/NodeSettingsFrame.java | 4 +- .../watcherGui/httpRequests/HttpRequests.java | 18 ++++ .../appuser/AppUserController.java | 8 +- .../appuser/AppUserRepository.java | 4 + .../watcherServer/appuser/AppUserService.java | 12 ++- .../response/UserGetResponse.java | 37 +++++++ .../watcherServer/response/UserType.java | 30 ++++++ .../security/config/WebSecurityConfig.java | 2 +- 18 files changed, 300 insertions(+), 46 deletions(-) create mode 100644 WatcherGui/src/main/java/com/joelhelkala/watcherGui/Colors/Colors.java create mode 100644 WatcherGui/src/main/java/com/joelhelkala/watcherGui/Datatypes/UserType.java create mode 100644 WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/AdminFrame.java create mode 100644 watcherServer/src/main/java/com/joelhelkala/watcherServer/response/UserGetResponse.java create mode 100644 watcherServer/src/main/java/com/joelhelkala/watcherServer/response/UserType.java diff --git a/WatcherGui/.classpath b/WatcherGui/.classpath index 002ad57..d786ebb 100644 --- a/WatcherGui/.classpath +++ b/WatcherGui/.classpath @@ -24,7 +24,7 @@ <attribute name="test" value="true"/> </attributes> </classpathentry> - <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-10"> <attributes> <attribute name="maven.pomderived" value="true"/> </attributes> diff --git a/WatcherGui/.settings/org.eclipse.jdt.core.prefs b/WatcherGui/.settings/org.eclipse.jdt.core.prefs index db24ee7..b12b227 100644 --- a/WatcherGui/.settings/org.eclipse.jdt.core.prefs +++ b/WatcherGui/.settings/org.eclipse.jdt.core.prefs @@ -1,8 +1,8 @@ eclipse.preferences.version=1 org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.targetPlatform=10 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.compliance=10 org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate @@ -11,5 +11,5 @@ org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore -org.eclipse.jdt.core.compiler.release=disabled -org.eclipse.jdt.core.compiler.source=1.8 +org.eclipse.jdt.core.compiler.release=enabled +org.eclipse.jdt.core.compiler.source=10 diff --git a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/Colors/Colors.java b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/Colors/Colors.java new file mode 100644 index 0000000..7af800a --- /dev/null +++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/Colors/Colors.java @@ -0,0 +1,9 @@ +package com.joelhelkala.watcherGui.Colors; + +import java.awt.Color; + +public abstract class Colors { + public static final Color gray = new Color(45, 45, 45); + public static final Color errorText = Color.red; + public static final Color buttonColor = Color.orange; +} diff --git a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/Datatypes/UserType.java b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/Datatypes/UserType.java new file mode 100644 index 0000000..116976a --- /dev/null +++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/Datatypes/UserType.java @@ -0,0 +1,30 @@ +package com.joelhelkala.watcherGui.Datatypes; + + +import com.joelhelkala.watcherGui.User.Roles.Role; + +public class UserType { + private String name; + private String email; + private Boolean enabled; + private Boolean locked; + private Role role; + private Long id; + + public UserType(String name, String email, Boolean enabled, Boolean locked, Role role, Long id) { + this.name = name; + this.email = email; + this.enabled = enabled; + this.locked = locked; + this.role = role; + this.id = id; + } + + // GETTERS + public String getName() { return name; } + public String getEmail() { return email; } + public Boolean getEnabled() { return enabled; } + public Boolean getLocked() { return locked; } + public Role getRole() { return role; } + public Long getId() { return id; } +} diff --git a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/LoginPage.java b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/LoginPage.java index 277a3f3..4df81eb 100644 --- a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/LoginPage.java +++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/LoginPage.java @@ -10,6 +10,7 @@ import javax.swing.border.Border; import org.json.JSONObject; +import com.joelhelkala.watcherGui.Colors.Colors; import com.joelhelkala.watcherGui.User.User; import com.joelhelkala.watcherGui.frames.panels.LoginPanel; import com.joelhelkala.watcherGui.frames.panels.RegisterPanel; @@ -65,24 +66,24 @@ public class LoginPage implements ActionListener, MouseListener { messageLabel.setBounds(width/2-50, height-29*4, 250, 35); messageLabel.setFont(new Font(null, Font.ITALIC, 15)); - messageLabel.setForeground(Color.red); + messageLabel.setForeground(Colors.errorText); retryLabel.setBounds(width/2, height-29*3, 250, 35); retryLabel.setFont(new Font(null, Font.ITALIC, 15)); - retryLabel.setForeground(Color.red); + retryLabel.setForeground(Colors.errorText); imageLabel.setBackground(Color.black); imageLabel.setBounds(80,30, width, height-100); newButton.setBounds(width/4-117/2, height-29*3, 117, 29); newButton.setBorder(new RoundedBorder(10)); - newButton.setForeground(Color.orange); + newButton.setForeground(Colors.buttonColor); newButton.addActionListener(this); newButton.addMouseListener(this); newButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); actionButton.setBounds(width-180, height-29*3, 117, 29); actionButton.setBorder(new RoundedBorder(10)); - actionButton.setForeground(Color.orange); + actionButton.setForeground(Colors.buttonColor); actionButton.addActionListener(this); actionButton.addMouseListener(this); actionButton.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); diff --git a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/WelcomePage.java b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/WelcomePage.java index 7dc5ae6..ba59216 100644 --- a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/WelcomePage.java +++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/WelcomePage.java @@ -7,7 +7,6 @@ import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.util.ArrayList; -import java.util.Date; import java.util.List; import java.util.Timer; import java.util.TimerTask; @@ -16,6 +15,7 @@ import com.joelhelkala.watcherGui.Nodes.Nodes; import com.joelhelkala.watcherGui.Nodes.Node.Node; import com.joelhelkala.watcherGui.User.User; import com.joelhelkala.watcherGui.User.Roles.Role; +import com.joelhelkala.watcherGui.frames.subframes.AdminFrame; import com.joelhelkala.watcherGui.frames.subframes.FriendsFrame; import com.joelhelkala.watcherGui.frames.subframes.NodeDataFrame; import com.joelhelkala.watcherGui.frames.subframes.NodeSettingsFrame; @@ -135,18 +135,6 @@ public class WelcomePage implements MouseListener, ActionListener { friendsLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); leftPanel.add(friendsLabel); - if(User.getRole() == Role.ADMIN) { - adminLabel.setOpaque(true); - adminLabel.setBackground(dark); - adminLabel.setHorizontalAlignment(SwingConstants.CENTER); - adminLabel.setForeground(Color.WHITE); - adminLabel.setFont(new Font("Apple SD Gothic Neo", Font.PLAIN, 16)); - adminLabel.setBounds(0, 192, 240, 53); - adminLabel.addMouseListener(this); - adminLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); - leftPanel.add(adminLabel); - } - helpLabel.setHorizontalAlignment(SwingConstants.CENTER); helpLabel.setForeground(Color.WHITE); helpLabel.setFont(new Font("Apple SD Gothic Neo", Font.PLAIN, 16)); @@ -185,20 +173,36 @@ public class WelcomePage implements MouseListener, ActionListener { stagePanel = new JPanel(); stagePanel.setBounds(leftPanelWidth, topPanelHeight, width-leftPanelWidth, height-topPanelHeight-bottomPanelHeight); stagePanel.setBackground(Color.red); - JPanel first = nodeDataFrame; - JPanel second = nodeSettingFrame; - JPanel third = new FriendsFrame(leftPanelWidth, topPanelHeight, width-leftPanelWidth, height-topPanelHeight-bottomPanelHeight); + + JPanel friendsFrame = new FriendsFrame(leftPanelWidth, topPanelHeight, width-leftPanelWidth, height-topPanelHeight-bottomPanelHeight); stagePanel.setLayout(cl); - stagePanel.add(first, "1"); - stagePanel.add(second, "2"); - stagePanel.add(third, "3"); - cl.show(stagePanel, "1"); + stagePanel.add(nodeDataFrame, "data"); + stagePanel.add(nodeSettingFrame, "setting"); + stagePanel.add(friendsFrame, "friends"); + if(User.getRole() == Role.ADMIN) { + AddAdminTab(leftPanel); + stagePanel.add(new AdminFrame(), "admin"); + } + cl.show(stagePanel, "data"); frame.add(stagePanel); frame.setVisible(true); PollData(); } + // Add a admin navigationtab to given panel + private void AddAdminTab(JPanel panel) { + adminLabel.setOpaque(true); + adminLabel.setBackground(dark); + adminLabel.setHorizontalAlignment(SwingConstants.CENTER); + adminLabel.setForeground(Color.WHITE); + adminLabel.setFont(new Font("Apple SD Gothic Neo", Font.PLAIN, 16)); + adminLabel.setBounds(0, 192, 240, 53); + adminLabel.addMouseListener(this); + adminLabel.setCursor(Cursor.getPredefinedCursor(Cursor.HAND_CURSOR)); + panel.add(adminLabel); + } + /* * Polls the backend for new data every given interval */ @@ -224,32 +228,39 @@ public class WelcomePage implements MouseListener, ActionListener { @Override public void mouseClicked(MouseEvent arg0) { if(arg0.getSource() == nodeSettingsLabel && arg0.getSource() != chosen_label) { - cl.show(stagePanel, "2"); + cl.show(stagePanel, "setting"); setActiveScene(nodeSettingsLabel); } else if(arg0.getSource() == nodeDataLabel && arg0.getSource() != chosen_label) { - cl.show(stagePanel, "1"); + cl.show(stagePanel, "data"); setActiveScene(nodeDataLabel); } else if(arg0.getSource() == friendsLabel && arg0.getSource() != chosen_label) { - cl.show(stagePanel, "3"); + cl.show(stagePanel, "friends"); setActiveScene(friendsLabel); } else if(arg0.getSource() == helpLabel) { showHelpDialog(); } else if(arg0.getSource() == settingsLabel) { // TODO: make settings for user information updating + } else if(arg0.getSource() == adminLabel) { + cl.show(stagePanel, "admin"); + setActiveScene(adminLabel); } } + /* + * Shows a very basic dialog + */ private static void showHelpDialog() { - // TODO: + JOptionPane.showMessageDialog(frame, + "For questions about the app, contact the developer at joel.helkala@gmail.com", + "Help!", + JOptionPane.PLAIN_MESSAGE); } /* * Sets a navbar item as chosen and changes its colors accordingly */ private void setActiveScene(JLabel chosen) { - nodeSettingsLabel.setBackground(dark); - friendsLabel.setBackground(dark); - nodeDataLabel.setBackground(dark); + chosen_label.setBackground(dark); chosen.setBackground(gray); chosen_label = chosen; diff --git a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/panels/LineChartPanel.java b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/panels/LineChartPanel.java index 5775cc2..fcfc9fb 100644 --- a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/panels/LineChartPanel.java +++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/panels/LineChartPanel.java @@ -1,6 +1,8 @@ package com.joelhelkala.watcherGui.frames.panels; import java.awt.Dimension; +import java.time.format.DateTimeFormatter; +import java.time.format.FormatStyle; import javax.swing.JPanel; diff --git a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/panels/ProgressBarCirclePanel.java b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/panels/ProgressBarCirclePanel.java index b94d1e5..dace458 100644 --- a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/panels/ProgressBarCirclePanel.java +++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/panels/ProgressBarCirclePanel.java @@ -10,8 +10,6 @@ import javax.swing.Timer; import javax.swing.plaf.basic.BasicProgressBarUI; import org.jfree.chart.ChartColor; -import javafx.scene.paint.Color; - import java.awt.BorderLayout; import java.awt.Dimension; import java.awt.Graphics; diff --git a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/AdminFrame.java b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/AdminFrame.java new file mode 100644 index 0000000..d6e9149 --- /dev/null +++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/AdminFrame.java @@ -0,0 +1,99 @@ +package com.joelhelkala.watcherGui.frames.subframes; + +import java.awt.BorderLayout; +import java.awt.Color; +import java.util.List; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.Insets; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.Box; +import javax.swing.JButton; +import javax.swing.JCheckBox; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JTextField; +import javax.swing.SwingConstants; + +import com.joelhelkala.watcherGui.Colors.Colors; +import com.joelhelkala.watcherGui.Datatypes.UserType; +import com.joelhelkala.watcherGui.httpRequests.HttpRequests; + +public class AdminFrame extends JPanel implements ActionListener { + private JPanel panel; + private GridBagConstraints c; + + public AdminFrame() { + List<UserType> users = HttpRequests.GetUsers(); + + setLayout(new BorderLayout()); + setOpaque(true); + setBackground(Colors.gray); + JLabel label = new JLabel("Admin"); + label.setForeground(Color.white); + label.setHorizontalAlignment(SwingConstants.CENTER); + add(label, BorderLayout.NORTH); + panel = new JPanel(new GridBagLayout()); + panel.setBackground(Colors.gray); + add(panel, BorderLayout.CENTER); + c = new GridBagConstraints(); + c.insets = new Insets(4, 4, 4, 4); + + for(UserType user : users) { + CreateUserField(user); + } + c.weighty = 1.0; + panel.add(Box.createGlue(), c); + } + + private void CreateUserField(UserType user) { + JLabel name = new JLabel(user.getName()); + name.setForeground(Color.white); + panel.add(name, c); + + JTextField email = new JTextField(20); + email.setText(user.getEmail()); + panel.add(email, c); + + JCheckBox enabled = new JCheckBox("Enabled", user.getEnabled()); + enabled.setForeground(Color.white); + panel.add(enabled, c); + + JCheckBox locked = new JCheckBox("Locked", user.getLocked()); + locked.setForeground(Color.white); + panel.add(locked, c); + + JButton delete = new JButton("Delete"); + delete.addActionListener( evt->{ + performActionOnId( user.getId() ); + }); + panel.add(delete, c); + + c.gridwidth = GridBagConstraints.REMAINDER; + JButton save = new JButton("Save"); + save.addActionListener( evt -> { + performActionOnSave(name, email, enabled, locked, user.getId()); + }); + panel.add(save, c); + c.gridwidth = 1; + } + + + private void performActionOnSave(JLabel name, JTextField email, JCheckBox enabled, JCheckBox locked, Long id) { + System.out.println(email.getText()); + + } + + // Delete user with given id + private void performActionOnId(Long id) { + System.out.println("Delete : " + id); + } + + @Override + public void actionPerformed(ActionEvent e) { + // TODO Auto-generated method stub + + } +} diff --git a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/FriendsFrame.java b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/FriendsFrame.java index 4b7a2f5..afed169 100644 --- a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/FriendsFrame.java +++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/FriendsFrame.java @@ -6,16 +6,17 @@ import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.SwingConstants; +import com.joelhelkala.watcherGui.Colors.Colors; + import java.awt.BorderLayout; public class FriendsFrame extends JPanel { - private static final Color gray = new Color(45, 45, 45); public FriendsFrame(int x, int y, int width, int height) { setLayout(new BorderLayout()); setBounds(x,y,width,height); setOpaque(true); - setBackground(gray); + setBackground(Colors.gray); JLabel label = new JLabel("Friends"); label.setForeground(Color.white); label.setHorizontalAlignment(SwingConstants.CENTER); diff --git a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/NodeSettingsFrame.java b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/NodeSettingsFrame.java index 643e0e4..30e46e8 100644 --- a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/NodeSettingsFrame.java +++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/frames/subframes/NodeSettingsFrame.java @@ -10,6 +10,7 @@ import javax.swing.JPanel; import javax.swing.JSplitPane; import javax.swing.JTextField; +import com.joelhelkala.watcherGui.Colors.Colors; import com.joelhelkala.watcherGui.Nodes.Node.Node; import com.joelhelkala.watcherGui.User.User; import com.joelhelkala.watcherGui.frames.LoginPage; @@ -21,7 +22,6 @@ import java.awt.BorderLayout; import java.awt.event.*; public class NodeSettingsFrame extends JPanel implements ActionListener{ - private static final Color gray = new Color(45, 45, 45); private JTextField descField = new JTextField(); private JTextField locationField = new JTextField(); private JTextField latField = new JTextField(); @@ -37,7 +37,7 @@ public class NodeSettingsFrame extends JPanel implements ActionListener{ setLayout(null); setBounds(0,0,width,height); setOpaque(true); - setBackground(gray); + setBackground(Colors.gray); JLabel descLabel = new JLabel("Description"); descLabel.setForeground(Color.white); diff --git a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/httpRequests/HttpRequests.java b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/httpRequests/HttpRequests.java index 3e815b0..5e6b7d0 100644 --- a/WatcherGui/src/main/java/com/joelhelkala/watcherGui/httpRequests/HttpRequests.java +++ b/WatcherGui/src/main/java/com/joelhelkala/watcherGui/httpRequests/HttpRequests.java @@ -16,6 +16,7 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import com.joelhelkala.watcherGui.Datatypes.UserType; import com.joelhelkala.watcherGui.Nodes.Node.Node; import com.joelhelkala.watcherGui.User.User; import com.joelhelkala.watcherGui.User.Roles.Role; @@ -48,6 +49,23 @@ public class HttpRequests { return false; } + // Gets all the users from the server + public static List<UserType> GetUsers() { + JSONArray arr = basicGetRequest("/appuser"); + List<UserType> userList = new ArrayList<>(); + for(int i = 0; i < arr.length(); i++) { + JSONObject obj = arr.getJSONObject(i); + userList.add(new UserType(obj.getString("name"), + obj.getString("email"), + obj.getBoolean("enabled"), + obj.getBoolean("locked"), + obj.getString("role").equals("ADMIN") ? Role.ADMIN : Role.USER, + obj.getLong("id"))); + } + return userList; + + } + /* * Sends a POST request to register a new user to database * Server sends a token if the information given are valid diff --git a/watcherServer/src/main/java/com/joelhelkala/watcherServer/appuser/AppUserController.java b/watcherServer/src/main/java/com/joelhelkala/watcherServer/appuser/AppUserController.java index 5a3a81f..298b36c 100644 --- a/watcherServer/src/main/java/com/joelhelkala/watcherServer/appuser/AppUserController.java +++ b/watcherServer/src/main/java/com/joelhelkala/watcherServer/appuser/AppUserController.java @@ -1,6 +1,11 @@ package com.joelhelkala.watcherServer.appuser; +import com.joelhelkala.watcherServer.response.UserGetResponse; +import com.joelhelkala.watcherServer.response.UserType; +import org.apache.catalina.User; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import java.util.List; @@ -18,8 +23,9 @@ public class AppUserController { // Endpoint for person GET request which returns all the users @GetMapping - public List<AppUser> getUsers() { + public List<UserType> getUsers() { return userService.getUsers(); + //return new UserGetResponse(HttpStatus.OK, users).getResponse(); } // Endpoint for person POST request which creates a new user diff --git a/watcherServer/src/main/java/com/joelhelkala/watcherServer/appuser/AppUserRepository.java b/watcherServer/src/main/java/com/joelhelkala/watcherServer/appuser/AppUserRepository.java index 6129d35..8efde3a 100644 --- a/watcherServer/src/main/java/com/joelhelkala/watcherServer/appuser/AppUserRepository.java +++ b/watcherServer/src/main/java/com/joelhelkala/watcherServer/appuser/AppUserRepository.java @@ -6,6 +6,7 @@ import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; import javax.transaction.Transactional; +import java.util.List; import java.util.Optional; @Repository @@ -19,4 +20,7 @@ public interface AppUserRepository extends JpaRepository<AppUser, Long> { @Query("UPDATE AppUser a " + "SET a.enabled = TRUE WHERE a.email = ?1") int enableAppUser(String email); + + @Query("SELECT user FROM AppUser user") + List<AppUser> getUsers(); } diff --git a/watcherServer/src/main/java/com/joelhelkala/watcherServer/appuser/AppUserService.java b/watcherServer/src/main/java/com/joelhelkala/watcherServer/appuser/AppUserService.java index 282e411..ea76b8b 100644 --- a/watcherServer/src/main/java/com/joelhelkala/watcherServer/appuser/AppUserService.java +++ b/watcherServer/src/main/java/com/joelhelkala/watcherServer/appuser/AppUserService.java @@ -3,8 +3,10 @@ package com.joelhelkala.watcherServer.appuser; import com.joelhelkala.watcherServer.exception.ApiRequestException; import com.joelhelkala.watcherServer.registration.token.ConfirmationToken; import com.joelhelkala.watcherServer.registration.token.ConfirmationTokenService; +import com.joelhelkala.watcherServer.response.UserType; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; +import org.springframework.security.core.userdetails.User; import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.core.userdetails.UserDetailsService; import org.springframework.security.core.userdetails.UsernameNotFoundException; @@ -36,8 +38,14 @@ public class AppUserService implements UserDetailsService { } // Returns all the users in the database - public List<AppUser> getUsers() { - return appUserRepository.findAll(); + public List<UserType> getUsers() { + log.info("Getting all users"); + List<AppUser> users = appUserRepository.getUsers(); + List<UserType> userTypeList = new ArrayList<>(); + for(AppUser user : users) { + userTypeList.add(new UserType(user)); + } + return userTypeList; } /* diff --git a/watcherServer/src/main/java/com/joelhelkala/watcherServer/response/UserGetResponse.java b/watcherServer/src/main/java/com/joelhelkala/watcherServer/response/UserGetResponse.java new file mode 100644 index 0000000..1f5707b --- /dev/null +++ b/watcherServer/src/main/java/com/joelhelkala/watcherServer/response/UserGetResponse.java @@ -0,0 +1,37 @@ +package com.joelhelkala.watcherServer.response; + +import com.joelhelkala.watcherServer.appuser.AppUser; +import lombok.AllArgsConstructor; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class UserGetResponse implements IResponse { + private final HttpStatus httpStatus; + private List<AppUser> users; + + public UserGetResponse(HttpStatus status, List<AppUser> users) { + this.users = users; + httpStatus = status; + } + + @Override + public ResponseEntity<Object> getResponse() { + List<UserType> body = createList(); + ResponseEntity<Object> response = new ResponseEntity<>(body, httpStatus); + return response; + } + + private List<UserType> createList() { + List<UserType> userTypeList = new ArrayList<>(); + for(int i = 0; i < users.size(); i++) { + UserType user = new UserType(users.get(i)); + userTypeList.add(user); + } + return userTypeList; + } +} diff --git a/watcherServer/src/main/java/com/joelhelkala/watcherServer/response/UserType.java b/watcherServer/src/main/java/com/joelhelkala/watcherServer/response/UserType.java new file mode 100644 index 0000000..9d2a9f8 --- /dev/null +++ b/watcherServer/src/main/java/com/joelhelkala/watcherServer/response/UserType.java @@ -0,0 +1,30 @@ +package com.joelhelkala.watcherServer.response; + +import com.joelhelkala.watcherServer.appuser.AppUser; +import com.joelhelkala.watcherServer.appuser.AppUserRole; +import lombok.Getter; +import lombok.Setter; + +import javax.persistence.EnumType; +import javax.persistence.Enumerated; + +@Getter +@Setter +public class UserType { + private String name; + private String email; + @Enumerated(EnumType.STRING) + private AppUserRole role; + private Long id; + private Boolean locked = false; + private Boolean enabled = false; + + public UserType(AppUser user) { + name = user.getName(); + email = user.getEmail(); + role = user.getRole(); + locked = user.getLocked(); + enabled = user.getEnabled(); + id = user.getId(); + } +} diff --git a/watcherServer/src/main/java/com/joelhelkala/watcherServer/security/config/WebSecurityConfig.java b/watcherServer/src/main/java/com/joelhelkala/watcherServer/security/config/WebSecurityConfig.java index 181b489..009f2d7 100644 --- a/watcherServer/src/main/java/com/joelhelkala/watcherServer/security/config/WebSecurityConfig.java +++ b/watcherServer/src/main/java/com/joelhelkala/watcherServer/security/config/WebSecurityConfig.java @@ -50,7 +50,7 @@ public class WebSecurityConfig extends WebSecurityConfigurerAdapter { // GET requests to appuser endpoint should have ADMIN roles // TODO: This does not work I think - http.authorizeRequests().antMatchers(GET, "/api/v*/appuser/**").hasAnyAuthority("ADMIN"); + http.authorizeRequests().antMatchers(GET, "/api/v*/appuser").hasAnyAuthority("ADMIN"); // Every request should be authenticated http.authorizeRequests().anyRequest().authenticated(); http.addFilter(customAuthenticationFilter); -- GitLab