diff --git a/pom.xml b/pom.xml index 6705522..e921743 100644 --- a/pom.xml +++ b/pom.xml @@ -71,6 +71,10 @@ postgresql runtime + + org.flywaydb + flyway-core + org.springframework.boot spring-boot-configuration-processor diff --git a/src/main/kotlin/uk/co/neviyn/booru/Controller.kt b/src/main/kotlin/uk/co/neviyn/booru/Controller.kt index 2e487c0..8887e45 100644 --- a/src/main/kotlin/uk/co/neviyn/booru/Controller.kt +++ b/src/main/kotlin/uk/co/neviyn/booru/Controller.kt @@ -84,7 +84,7 @@ class UploadController @Autowired constructor( val imageRepository: ImageRepository, val tagRepository: TagRepository, - val userRepository: UserRepository, + val memberRepository: MemberRepository, val imageConfigurationProperties: ImageConfigurationProperties, val storage: FileSystemStorage ) { @@ -101,7 +101,7 @@ class UploadController if (file.isEmpty) return "upload" // TODO: Show error on page, can't upload nothing val extension = java.io.File(file.originalFilename!!).extension 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 tagData = tags.split(" ").map { tagRepository.findByTagIs(it) ?: tagRepository.save(Tag(it)) }.toMutableSet() storage.addImageFile(hash, extension, file.bytes) diff --git a/src/main/kotlin/uk/co/neviyn/booru/Entity.kt b/src/main/kotlin/uk/co/neviyn/booru/Entity.kt index 56fec1e..beff2e1 100644 --- a/src/main/kotlin/uk/co/neviyn/booru/Entity.kt +++ b/src/main/kotlin/uk/co/neviyn/booru/Entity.kt @@ -14,7 +14,7 @@ import javax.persistence.ManyToOne import javax.validation.constraints.Pattern @Entity -open class User( +open class Member( @Column(unique = true) open var name: String = "", open var email: String = "", @@ -25,8 +25,8 @@ open class User( @JsonIgnore @ManyToMany(fetch = FetchType.EAGER) @JoinTable( - name = "user_roles", - joinColumns = [JoinColumn(name = "user_id", referencedColumnName = "id")], + name = "member_roles", + joinColumns = [JoinColumn(name = "member_id", referencedColumnName = "id")], inverseJoinColumns = [JoinColumn(name = "role_id", referencedColumnName = "id")] ) open var roles: MutableSet = mutableSetOf(), @@ -46,7 +46,7 @@ open class Image( open var filename: String = "", @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "uploader") - open var uploader: User = User(), + open var uploader: Member = Member(), @ManyToMany @JoinTable( name = "tag_image", diff --git a/src/main/kotlin/uk/co/neviyn/booru/Repository.kt b/src/main/kotlin/uk/co/neviyn/booru/Repository.kt index 0c04cc2..804231d 100644 --- a/src/main/kotlin/uk/co/neviyn/booru/Repository.kt +++ b/src/main/kotlin/uk/co/neviyn/booru/Repository.kt @@ -6,8 +6,8 @@ import org.springframework.data.jpa.repository.JpaRepository import org.springframework.data.jpa.repository.Query import org.springframework.data.repository.CrudRepository -interface UserRepository : CrudRepository { - fun findByName(name: String): User? +interface MemberRepository : CrudRepository { + fun findByName(name: String): Member? } interface RoleRepository : CrudRepository diff --git a/src/main/kotlin/uk/co/neviyn/booru/Security.kt b/src/main/kotlin/uk/co/neviyn/booru/Security.kt index d334fe4..6d17b2a 100644 --- a/src/main/kotlin/uk/co/neviyn/booru/Security.kt +++ b/src/main/kotlin/uk/co/neviyn/booru/Security.kt @@ -35,13 +35,13 @@ class SecurityConfig open class CustomUserDetails constructor( - private val user: User + private val member: Member ) : UserDetails { - override fun getAuthorities(): MutableCollection = user.roles.map { SimpleGrantedAuthority(it.name) }.toMutableList() + override fun getAuthorities(): MutableCollection = 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 @@ -49,15 +49,15 @@ constructor( override fun isCredentialsNonExpired(): Boolean = true - override fun isEnabled(): Boolean = user.enabled + override fun isEnabled(): Boolean = member.enabled } @Service class CustomUserDetailsService @Autowired constructor( - val userRepository: UserRepository + val memberRepository: MemberRepository ) : 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 diff --git a/src/main/resources/data.sql b/src/main/resources/data.sql deleted file mode 100644 index 0d94ffd..0000000 --- a/src/main/resources/data.sql +++ /dev/null @@ -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 ; \ No newline at end of file diff --git a/src/main/resources/db/migration/V1_0__Database_Init.sql b/src/main/resources/db/migration/V1_0__Database_Init.sql new file mode 100644 index 0000000..0f5bd08 --- /dev/null +++ b/src/main/resources/db/migration/V1_0__Database_Init.sql @@ -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); \ No newline at end of file diff --git a/src/test/resources/application.properties b/src/test/resources/application.properties index 8876b0f..5799647 100644 --- a/src/test/resources/application.properties +++ b/src/test/resources/application.properties @@ -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.jpa.hibernate.ddl-auto=update -spring.jpa.properties.hibernate.default_schema=booru images.directory=./images \ No newline at end of file