Renamed users to members, added flyway database versioning

This commit is contained in:
neviyn 2021-05-06 22:47:53 +01:00
parent f97358aeb4
commit e4dd1c243e
8 changed files with 79 additions and 20 deletions

View File

@ -71,6 +71,10 @@
<artifactId>postgresql</artifactId> <artifactId>postgresql</artifactId>
<scope>runtime</scope> <scope>runtime</scope>
</dependency> </dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId> <artifactId>spring-boot-configuration-processor</artifactId>

View File

@ -84,7 +84,7 @@ class UploadController
@Autowired constructor( @Autowired constructor(
val imageRepository: ImageRepository, val imageRepository: ImageRepository,
val tagRepository: TagRepository, val tagRepository: TagRepository,
val userRepository: UserRepository, val memberRepository: MemberRepository,
val imageConfigurationProperties: ImageConfigurationProperties, val imageConfigurationProperties: ImageConfigurationProperties,
val storage: FileSystemStorage val storage: FileSystemStorage
) { ) {
@ -101,7 +101,7 @@ class UploadController
if (file.isEmpty) return "upload" // TODO: Show error on page, can't upload nothing if (file.isEmpty) return "upload" // TODO: Show error on page, can't upload nothing
val extension = java.io.File(file.originalFilename!!).extension val extension = java.io.File(file.originalFilename!!).extension
if (!imageConfigurationProperties.types.contains(extension)) return "upload" // TODO: Show error on page, unrecognised file type if (!imageConfigurationProperties.types.contains(extension)) return "upload" // TODO: Show error on page, unrecognised file type
val user = userRepository.findByName(userDetails.username)!! val user = memberRepository.findByName(userDetails.username)!!
val hash: String = generateFileHash(file) val hash: String = generateFileHash(file)
val tagData = tags.split(" ").map { tagRepository.findByTagIs(it) ?: tagRepository.save(Tag(it)) }.toMutableSet() val tagData = tags.split(" ").map { tagRepository.findByTagIs(it) ?: tagRepository.save(Tag(it)) }.toMutableSet()
storage.addImageFile(hash, extension, file.bytes) storage.addImageFile(hash, extension, file.bytes)

View File

@ -14,7 +14,7 @@ import javax.persistence.ManyToOne
import javax.validation.constraints.Pattern import javax.validation.constraints.Pattern
@Entity @Entity
open class User( open class Member(
@Column(unique = true) @Column(unique = true)
open var name: String = "", open var name: String = "",
open var email: String = "", open var email: String = "",
@ -25,8 +25,8 @@ open class User(
@JsonIgnore @JsonIgnore
@ManyToMany(fetch = FetchType.EAGER) @ManyToMany(fetch = FetchType.EAGER)
@JoinTable( @JoinTable(
name = "user_roles", name = "member_roles",
joinColumns = [JoinColumn(name = "user_id", referencedColumnName = "id")], joinColumns = [JoinColumn(name = "member_id", referencedColumnName = "id")],
inverseJoinColumns = [JoinColumn(name = "role_id", referencedColumnName = "id")] inverseJoinColumns = [JoinColumn(name = "role_id", referencedColumnName = "id")]
) )
open var roles: MutableSet<Role> = mutableSetOf(), open var roles: MutableSet<Role> = mutableSetOf(),
@ -46,7 +46,7 @@ open class Image(
open var filename: String = "", open var filename: String = "",
@ManyToOne(fetch = FetchType.LAZY) @ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "uploader") @JoinColumn(name = "uploader")
open var uploader: User = User(), open var uploader: Member = Member(),
@ManyToMany @ManyToMany
@JoinTable( @JoinTable(
name = "tag_image", name = "tag_image",

View File

@ -6,8 +6,8 @@ import org.springframework.data.jpa.repository.JpaRepository
import org.springframework.data.jpa.repository.Query import org.springframework.data.jpa.repository.Query
import org.springframework.data.repository.CrudRepository import org.springframework.data.repository.CrudRepository
interface UserRepository : CrudRepository<User, Long> { interface MemberRepository : CrudRepository<Member, Long> {
fun findByName(name: String): User? fun findByName(name: String): Member?
} }
interface RoleRepository : CrudRepository<Role, Long> interface RoleRepository : CrudRepository<Role, Long>

View File

@ -35,13 +35,13 @@ class SecurityConfig
open class CustomUserDetails open class CustomUserDetails
constructor( constructor(
private val user: User private val member: Member
) : UserDetails { ) : UserDetails {
override fun getAuthorities(): MutableCollection<out GrantedAuthority> = user.roles.map { SimpleGrantedAuthority(it.name) }.toMutableList() override fun getAuthorities(): MutableCollection<out GrantedAuthority> = member.roles.map { SimpleGrantedAuthority(it.name) }.toMutableList()
override fun getPassword(): String = user.password override fun getPassword(): String = member.password
override fun getUsername(): String = user.name override fun getUsername(): String = member.name
override fun isAccountNonExpired(): Boolean = true override fun isAccountNonExpired(): Boolean = true
@ -49,15 +49,15 @@ constructor(
override fun isCredentialsNonExpired(): Boolean = true override fun isCredentialsNonExpired(): Boolean = true
override fun isEnabled(): Boolean = user.enabled override fun isEnabled(): Boolean = member.enabled
} }
@Service @Service
class CustomUserDetailsService class CustomUserDetailsService
@Autowired constructor( @Autowired constructor(
val userRepository: UserRepository val memberRepository: MemberRepository
) : UserDetailsService { ) : UserDetailsService {
override fun loadUserByUsername(p0: String): UserDetails = CustomUserDetails(userRepository.findByName(p0) ?: throw UsernameNotFoundException(p0)) override fun loadUserByUsername(p0: String): UserDetails = CustomUserDetails(memberRepository.findByName(p0) ?: throw UsernameNotFoundException(p0))
} }
@Bean @Bean

View File

@ -1,2 +0,0 @@
INSERT INTO booru.role (id, name) VALUES (1, 'ADMIN') ON CONFLICT DO NOTHING ;
INSERT INTO booru.role (id, name) VALUES (2, 'USER') ON CONFLICT DO NOTHING ;

View File

@ -0,0 +1,59 @@
DROP SCHEMA IF EXISTS booru CASCADE ;
CREATE SCHEMA booru;
CREATE TABLE booru.member
(
id bigint NOT NULL PRIMARY KEY,
name varchar NOT NULL UNIQUE,
email varchar NOT NULL,
password varchar NOT NULL,
enabled boolean
);
CREATE TABLE booru.image
(
id bigint NOT NULL PRIMARY KEY,
filename varchar NOT NULL UNIQUE,
uploader bigint NOT NULL,
CONSTRAINT uploader FOREIGN KEY (uploader) REFERENCES booru.member (id)
);
CREATE TABLE booru.role
(
id bigint NOT NULL PRIMARY KEY,
name varchar NOT NULL
);
CREATE TABLE booru.tag
(
id bigint NOT NULL PRIMARY KEY,
tag varchar NOT NULL UNIQUE
);
CREATE TABLE booru.tag_image
(
tag_id bigint NOT NULL,
image_id bigint NOT NULL,
CONSTRAINT tag_image_pk PRIMARY KEY (tag_id, image_id),
CONSTRAINT image FOREIGN KEY (image_id) REFERENCES booru.image (id),
CONSTRAINT tag FOREIGN KEY (tag_id) REFERENCES booru.tag (id)
);
CREATE TABLE booru.member_roles
(
member_id bigint NOT NULL,
role_id bigint NOT NULL,
CONSTRAINT role FOREIGN KEY (role_id) REFERENCES booru.role (id),
CONSTRAINT member FOREIGN KEY (member_id) REFERENCES booru.member (id)
);
-- Populate default values
INSERT INTO booru.role (id, name)
VALUES (1, 'ADMIN'),
(2, 'USER');
INSERT INTO booru.member (id, name, email, password, enabled)
VALUES (1, 'root', 'root@example.org', '$2a$10$XiMxQ1Qe.AprOdJKUPum6O2URy2b4ogffujVZVpUT8CJPksfSyNZK', TRUE);
INSERT INTO booru.member_roles (member_id, role_id)
VALUES (1, 1),
(1, 2);

View File

@ -1,5 +1,3 @@
spring.datasource.url=jdbc:h2:mem:testdb;MODE=PostgreSQL;INIT=CREATE SCHEMA IF NOT EXISTS booru spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driverClassName=org.h2.Driver spring.datasource.driverClassName=org.h2.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.default_schema=booru
images.directory=./images images.directory=./images