Skip to content

Commit

Permalink
Feature/gap 1850 spadmin (#52)
Browse files Browse the repository at this point in the history
feat (super admin dashboard): adds functionality for super admin dashboard

---------

Co-authored-by: john-tco <[email protected]>
Co-authored-by: Dylan Wright <[email protected]>
Co-authored-by: Dominic West <[email protected]>
  • Loading branch information
4 people authored Jul 17, 2023
1 parent c393bab commit 92ef925
Show file tree
Hide file tree
Showing 40 changed files with 903 additions and 41 deletions.
56 changes: 51 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
Expand Down Expand Up @@ -39,6 +39,19 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct</artifactId>
<version>1.5.2.Final</version>
</dependency>
<dependency>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.5.2.Final</version>
</dependency>


<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-core</artifactId>
Expand Down Expand Up @@ -181,9 +194,9 @@
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<configuration>
<url>jdbc:postgresql://localhost:5433/gapuserlocaldb</url>
<user>root</user>
<password>root</password>
<url>jdbc:postgresql://localhost:5432/gapuserlocaldb</url>
<user>postgres</user>
<password>postgres</password>
<cleanDisabled>false</cleanDisabled>
</configuration>
</plugin>
Expand All @@ -197,6 +210,39 @@
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>17</source>
<target>17</target>
<annotationProcessorPaths>
<path>
<groupId>org.mapstruct</groupId>
<artifactId>mapstruct-processor</artifactId>
<version>1.5.1.Final</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.24</version>
</path>
<path>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-mapstruct-binding</artifactId>
<version>0.2.0</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>







<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-antrun-plugin</artifactId>
Expand Down Expand Up @@ -245,7 +291,7 @@
</execution>
</executions>
</plugin>
</plugins>
</plugins>
</build>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package gov.cabinetofice.gapuserservice.dto;

import gov.cabinetofice.gapuserservice.model.User;
import lombok.Builder;
import lombok.Data;

import java.util.List;

@Data
@Builder
public class ChangeDepartmentPageDto {
private User user;
private List<DepartmentDto> departments;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package gov.cabinetofice.gapuserservice.dto;

import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class DepartmentDto {
private String id;
private String name;
private String ggisID;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@
@Data
@Builder
public class OneLoginUserInfoDto {
private String email;
private String emailAddress;
private String sub;
}
12 changes: 12 additions & 0 deletions src/main/java/gov/cabinetofice/gapuserservice/dto/RoleDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package gov.cabinetofice.gapuserservice.dto;

import lombok.Builder;
import lombok.Data;

@Data
@Builder
public class RoleDto {
private String id;
private String name;
private String description;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package gov.cabinetofice.gapuserservice.dto;

import lombok.Builder;
import lombok.Data;

import java.util.List;

@Data
@Builder
public class SuperAdminDashboardPageDto {
private List<RoleDto> roles;
private List<DepartmentDto> departments;
private List<UserDto> users;
private long userCount;
}
17 changes: 17 additions & 0 deletions src/main/java/gov/cabinetofice/gapuserservice/dto/UserDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package gov.cabinetofice.gapuserservice.dto;

import gov.cabinetofice.gapuserservice.model.Department;
import lombok.Builder;
import lombok.Data;

import java.util.List;

@Builder
@Data
public class UserDto {
private String gapUserId;
private String emailAddress;
private String sub;
private List<RoleDto> roles;
private Department department;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package gov.cabinetofice.gapuserservice.exceptions;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(HttpStatus.NOT_FOUND)
public class DepartmentNotFoundException extends RuntimeException {
public DepartmentNotFoundException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package gov.cabinetofice.gapuserservice.exceptions;

import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ResponseStatus;

@ResponseStatus(HttpStatus.NOT_FOUND)
public class UserNotFoundException extends RuntimeException {
public UserNotFoundException(String message) {
super(message);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package gov.cabinetofice.gapuserservice.mappers;

import gov.cabinetofice.gapuserservice.dto.DepartmentDto;
import gov.cabinetofice.gapuserservice.model.Department;
import org.mapstruct.Mapper;

@Mapper(componentModel = "spring")
public interface DepartmentMapper {
DepartmentDto departmentToDepartmentDto(Department dept);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package gov.cabinetofice.gapuserservice.mappers;
import gov.cabinetofice.gapuserservice.model.Role;

import gov.cabinetofice.gapuserservice.dto.RoleDto;
import org.mapstruct.Mapper;

@Mapper(componentModel = "spring")
public interface RoleMapper {
RoleDto roleToRoleDto(Role role);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package gov.cabinetofice.gapuserservice.mappers;

import gov.cabinetofice.gapuserservice.dto.UserDto;
import gov.cabinetofice.gapuserservice.model.User;
import org.mapstruct.Mapper;

@Mapper(componentModel = "spring")
public interface UserMapper {
UserDto userToUserDto(User user);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gov.cabinetofice.gapuserservice.model;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.*;
import lombok.*;
Expand Down Expand Up @@ -29,9 +30,10 @@ public class Department {
private String ggisID;

@Column(name = "users")
@OneToMany(mappedBy="id", cascade = { CascadeType.MERGE, CascadeType.PERSIST }, fetch = FetchType.LAZY)
@OneToMany(mappedBy="gapUserId", cascade = { CascadeType.MERGE, CascadeType.PERSIST }, fetch = FetchType.LAZY)
@ToString.Exclude
@JsonIgnoreProperties({ "hibernateLazyInitializer" })
@JsonBackReference
@Builder.Default
private List<User> users = new ArrayList<>();
}
6 changes: 6 additions & 0 deletions src/main/java/gov/cabinetofice/gapuserservice/model/Role.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package gov.cabinetofice.gapuserservice.model;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import jakarta.persistence.*;
import lombok.*;
Expand All @@ -26,15 +27,20 @@ public class Role {
@Enumerated(EnumType.STRING)
private RoleEnum name;

@Column(name = "description")
private String description;

@ManyToMany(cascade = { CascadeType.MERGE, CascadeType.PERSIST }, fetch = FetchType.LAZY)
@JoinColumn(name = "id", nullable = false)
@ToString.Exclude
@JsonIgnoreProperties({ "hibernateLazyInitializer" })
@JsonBackReference
@Builder.Default
private List<User> users = new ArrayList<>();

public void addUser(User user) {
this.users.add(user);
}
public void removeUser( User user) { this.users.remove(user); }
}

21 changes: 16 additions & 5 deletions src/main/java/gov/cabinetofice/gapuserservice/model/User.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gov.cabinetofice.gapuserservice.model;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import jakarta.persistence.*;
import lombok.*;

Expand All @@ -20,23 +21,25 @@ public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "gap_user_id")
private Integer id;
private Integer gapUserId;

@Column(name = "email")
private String email;
private String emailAddress;

@Column(name = "sub")
private String sub;

@ManyToMany(cascade = { CascadeType.MERGE, CascadeType.PERSIST }, fetch = FetchType.EAGER, mappedBy = "users")
@ManyToMany(cascade = { CascadeType.MERGE, CascadeType.PERSIST }, fetch = FetchType.LAZY, mappedBy = "users")
@ToString.Exclude
@JsonIgnoreProperties({ "hibernateLazyInitializer" })
@JsonManagedReference
@Builder.Default
private List<Role> roles = new ArrayList<>();

@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.PERSIST }, fetch = FetchType.EAGER)
@ManyToOne(cascade = { CascadeType.MERGE, CascadeType.PERSIST }, fetch = FetchType.LAZY)
@JoinColumn(name = "dept_id")
@ToString.Exclude
@JsonManagedReference
@JsonIgnoreProperties({ "hibernateLazyInitializer" })
private Department department;

Expand All @@ -50,7 +53,7 @@ public boolean hasSub() {
}

public boolean hasEmail() {
return this.email != null;
return this.emailAddress != null;
}

public boolean hasDepartment() {
Expand All @@ -68,4 +71,12 @@ public boolean isAdmin() {
public boolean isSuperAdmin() {
return this.roles.stream().anyMatch((role) -> role.getName().equals(RoleEnum.SUPER_ADMIN));
}

public User removeAllRoles() {
for (Role role : this.roles) {
role.removeUser(this);
}
this.roles.clear();
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package gov.cabinetofice.gapuserservice.repository;

import gov.cabinetofice.gapuserservice.model.Department;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface DepartmentRepository extends JpaRepository<Department, Integer> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
@Repository
public interface RoleRepository extends JpaRepository<Role, Integer> {
Optional<Role> findByName(RoleEnum name);

}

Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package gov.cabinetofice.gapuserservice.repository;

import gov.cabinetofice.gapuserservice.model.User;
import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface UserRepository extends JpaRepository<User, Integer> {
Optional<User> findByEmail(String email);

@EntityGraph(attributePaths = {"department", "roles"})
Optional<User> findByEmailAddress(String email);
@EntityGraph(attributePaths = {"department", "roles"})
Optional<User> findBySub(String sub);

@EntityGraph(attributePaths = {"department", "roles"})
Optional<User> findById(int id);
}

Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ protected void doFilterInternal(final @NonNull HttpServletRequest request,
}

//TODO set the Security context, so we can access user details in rest of app
final UsernamePasswordAuthenticationToken authentication = new UsernamePasswordAuthenticationToken(
final UsernamePasswordAuthenticationToken user_authentication = new UsernamePasswordAuthenticationToken(
"Placeholder",
null,
Collections.singletonList(new SimpleGrantedAuthority("ROLE_USER")));

SecurityContextHolder.getContext().setAuthentication(authentication);
SecurityContextHolder.getContext().setAuthentication(user_authentication);
chain.doFilter(request, response);
}

Expand Down
Loading

0 comments on commit 92ef925

Please sign in to comment.