diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/model/auth/Role.java b/bookstore/src/main/java/com/fjordtek/bookstore/model/auth/Role.java new file mode 100644 index 0000000..2c09550 --- /dev/null +++ b/bookstore/src/main/java/com/fjordtek/bookstore/model/auth/Role.java @@ -0,0 +1,112 @@ +//Pekka Helenius , Fjordtek 2020 + +package com.fjordtek.bookstore.model.auth; + +import java.util.List; + +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToMany; +import javax.persistence.SequenceGenerator; +import javax.persistence.Transient; + +/** + * This class implements Role entity which forms + * core structure for the corresponding ROLE table in a database. + *

+ * Additionally, Role entity objects are Java objects having + * methods, attributes and other class-related additions within them. + * + * @author Pekka Helenius + */ + +@Entity +public class Role { + + //////////////////// + // Primary key value in database + + @Id + @GeneratedValue( + strategy = GenerationType.IDENTITY, + generator = "roleIdGenerator" + ) + @SequenceGenerator( + name = "roleIdGenerator", + sequenceName = "roleIdSequence" + ) + private Long id; + + @Column( + unique = true, + nullable = false, + columnDefinition = "VARCHAR(20)" + ) + private String name; + + @OneToMany( + mappedBy = "role", + cascade = CascadeType.ALL, + fetch = FetchType.LAZY, + orphanRemoval = true + ) + // Role objects do not have any UserRoles when initializing them + @Transient + private List userRoles; + + //////////////////// + // Attribute setters + + public void setId(Long id) { + this.id = id; + } + + public void setName(String name) { + this.name = name; + } +/* + public void setUserroles(List userRoles) { + this.userRoles = userRoles; + } +*/ + //////////////////// + // Attribute getters + + public Long getId() { + return id; + } + + public String getName() { + return name; + } +/* + public List getUserroles() { + return userRoles; + } +*/ + //////////////////// + // Class constructors + + public Role() { + } + + public Role(String name) { + // super(); + this.name = name; + } + + //////////////////// + // Class overrides + + @Override + public String toString() { + return "[" + "id: " + this.id + ", " + + "name: " + this.name + "]"; + } + +} \ No newline at end of file diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/model/auth/RoleRepository.java b/bookstore/src/main/java/com/fjordtek/bookstore/model/auth/RoleRepository.java new file mode 100644 index 0000000..2eb39a1 --- /dev/null +++ b/bookstore/src/main/java/com/fjordtek/bookstore/model/auth/RoleRepository.java @@ -0,0 +1,26 @@ +//Pekka Helenius , Fjordtek 2020 + +package com.fjordtek.bookstore.model.auth; + +import java.util.List; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.data.rest.core.annotation.RepositoryRestResource; + +/** + * This interface extends CrudRepository interface, implementing + * custom methods for a repository containing Role entities. + * + * @author Pekka Helenius + */ + +@RepositoryRestResource( + path = "roles", + itemResourceRel = "roles", + exported = true + ) +public interface RoleRepository extends CrudRepository { + + List findByName(String string); + +} \ No newline at end of file diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/model/auth/User.java b/bookstore/src/main/java/com/fjordtek/bookstore/model/auth/User.java new file mode 100644 index 0000000..31196b9 --- /dev/null +++ b/bookstore/src/main/java/com/fjordtek/bookstore/model/auth/User.java @@ -0,0 +1,156 @@ +//Pekka Helenius , Fjordtek 2020 + +package com.fjordtek.bookstore.model.auth; + +import java.util.List; + +import javax.persistence.CascadeType; +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToMany; +import javax.persistence.SequenceGenerator; +import javax.persistence.Transient; +import javax.validation.constraints.Email; + +import com.fasterxml.jackson.annotation.JsonIgnore; + +/** + * This class implements User entity which forms + * core structure for the corresponding USER table in a database. + *

+ * Additionally, User entity objects are Java objects having + * methods, attributes and other class-related additions within them. + * + * @author Pekka Helenius + */ + +@Entity +public class User { + + // TODO define universally in a single place + private static final int strMax = 100; + private static final int strMaxPasswd = 1024; + + //////////////////// + // Primary key value in database + + @Id + @GeneratedValue( + strategy = GenerationType.IDENTITY, + generator = "userIdGenerator" + ) + @SequenceGenerator( + name = "userIdGenerator", + sequenceName = "userIdSequence" + ) + private Long id; + + //////////////////// + + @Column( + unique = true, + nullable = false, + columnDefinition = "NVARCHAR(" + strMax + ")" + ) + private String username; + + @Column( + nullable = false, + columnDefinition = "NVARCHAR(" + strMaxPasswd + ")" + ) + @JsonIgnore + private String password; + + @Email + @Column( + nullable = false, + columnDefinition = "NVARCHAR(" + strMax + ")" + ) + private String email; + + @OneToMany( + mappedBy = "user", + cascade = CascadeType.ALL, + fetch = FetchType.LAZY, + orphanRemoval = true + ) + // User objects do not have any UserRoles when initializing them + @Transient + private List userRoles; + + //////////////////// + // Attribute setters + + public void setId(Long id) { + this.id = id; + } + + public void setUsername(String username) { + this.username = username; + } + + public void setPassword(String password) { + this.password = password; + } + + public void setEmail(String email) { + this.email = email; + } +/* + public void setUserroles(List userRoles) { + this.userRoles = userRoles; + } +*/ + //////////////////// + // Attribute getters + + public Long getId() { + return id; + } + + public String getUsername() { + return username; + } + + public String getPassword() { + return password; + } + + public String getEmail() { + return email; + } +/* + public List getUserroles() { + return userRoles; + } +*/ + //////////////////// + // Class constructors + + public User() { + } + + public User(String username, String password, String email) { + // super(); + this.username = username; + this.password = password; + this.email = email; + } + + //////////////////// + // Class overrides + + @Override + public String toString() { + return "[" + "id: " + this.id + ", " + + "username: " + this.username + ", " + + "password: " + this.password + ", " + + "email: " + this.email + "]"; + } + + +} \ No newline at end of file diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRepository.java b/bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRepository.java new file mode 100644 index 0000000..cd12f1c --- /dev/null +++ b/bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRepository.java @@ -0,0 +1,24 @@ +//Pekka Helenius , Fjordtek 2020 + +package com.fjordtek.bookstore.model.auth; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.data.rest.core.annotation.RepositoryRestResource; + +/** + * This interface extends CrudRepository interface, implementing + * custom methods for a repository containing User entities. + * + * @author Pekka Helenius + */ + +@RepositoryRestResource( + path = "users", + itemResourceRel = "users", + exported = true + ) +public interface UserRepository extends CrudRepository { + + User findByUsername(String username); + +} \ No newline at end of file diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRole.java b/bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRole.java new file mode 100644 index 0000000..ecce5fe --- /dev/null +++ b/bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRole.java @@ -0,0 +1,102 @@ +//Pekka Helenius , Fjordtek 2020 + +package com.fjordtek.bookstore.model.auth; + +import javax.persistence.EmbeddedId; +import javax.persistence.Entity; +import javax.persistence.ManyToOne; +import javax.persistence.MapsId; + +/** + * This class implements UserRole entity which forms + * core structure for the corresponding USER_ROLE join table in a database. + *

+ * Main reason for the join table is to allow more sophisticated + * role policy for users. For instance, a user may have multiple roles like + * FINANCE and MARKETING, whereas another user may only have MARKETING + * role. In other words, this back-end implementation allows fine-grained + * user policy for the web service and better business logic. + *

+ * Primary key of this entity is a composite primary key. + *

+ * This implementation supports having additional columns in the join table + * if needed, contrary to the direct Spring @ManyToMany annotation. + *

+ * Additionally, UserRole entity objects are Java objects having + * methods, attributes and other class-related additions within them. + * + * @author Pekka Helenius + */ + +@Entity +public class UserRole { + + //////////////////// + // Primary key value in database + + @EmbeddedId + private UserRoleCompositeKey id = new UserRoleCompositeKey(); + + @ManyToOne + @MapsId("roleId") + private User user; + + @ManyToOne + @MapsId("userId") + private Role role; + + //////////////////// + // Other attributes + + //////////////////// + // Attribute setters + + public void setId(UserRoleCompositeKey id) { + this.id = id; + } + + public void setUser(User user) { + this.user = user; + } + + public void setRole(Role role) { + this.role = role; + } + + //////////////////// + // Attribute getters + + public UserRoleCompositeKey getId() { + return id; + } + + public User getUser() { + return user; + } + + public Role getRole() { + return role; + } + + //////////////////// + // Class constructors + + public UserRole() { + } + + public UserRole(User user, Role role) { + //super(); + this.user = user; + this.role = role; + } + + //////////////////// + // Class overrides + + @Override + public String toString() { + return "[" + "user: " + this.user + ", " + + "role: " + this.role + "]"; + } + +} \ No newline at end of file diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRoleCompositeKey.java b/bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRoleCompositeKey.java new file mode 100644 index 0000000..fa15aa2 --- /dev/null +++ b/bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRoleCompositeKey.java @@ -0,0 +1,92 @@ +//Pekka Helenius , Fjordtek 2020 + +package com.fjordtek.bookstore.model.auth; + +import java.io.Serializable; +import java.util.Objects; + +import javax.persistence.Embeddable; + +/** + * This class implements a composite primary key based on + * primary keys of User & Role entities. + *

+ * This class overrides several JPA specific methods. + * + * @author Pekka Helenius + */ + +@Embeddable +public class UserRoleCompositeKey implements Serializable { + + private static final long serialVersionUID = 2889337731246989510L; + + private Long userId; + + private Long roleId; + + //////////////////// + // Attribute setters + + public void setUserId(Long userId) { + this.userId = userId; + } + + public void setRoleId(Long roleId) { + this.roleId = roleId; + } + + //////////////////// + // Attribute getters + + public Long getUserId() { + return userId; + } + + public Long getRoleId() { + return roleId; + } + + //////////////////// + // Class constructors + + public UserRoleCompositeKey() { + } + + public UserRoleCompositeKey(Long userId, Long roleId) { + // super(); + this.userId = userId; + this.roleId = roleId; + } + + //////////////////// + // Class overrides + + @Override + public boolean equals(Object obj) { + + if (this == obj) { + return true; + } + if (obj == null || getClass() != obj.getClass()) { + return false; + } + + UserRoleCompositeKey that = (UserRoleCompositeKey) obj; + + return Objects.equals(userId, that.userId) && + Objects.equals(roleId, that.roleId); + } + + @Override + public int hashCode() { + return Objects.hash(userId, roleId); + } + + @Override + public String toString() { + return "[" + "user_id: " + this.userId + ", " + + "role_id: " + this.roleId + "]"; + } + +} diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRoleRepository.java b/bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRoleRepository.java new file mode 100644 index 0000000..e9e5fa1 --- /dev/null +++ b/bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRoleRepository.java @@ -0,0 +1,21 @@ +//Pekka Helenius , Fjordtek 2020 + +package com.fjordtek.bookstore.model.auth; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.data.rest.core.annotation.RepositoryRestResource; + +/** + * This interface extends CrudRepository interface, implementing + * custom methods for a repository containing UserRole entities. + * + * @author Pekka Helenius + */ + +@RepositoryRestResource( + path = "userroles", + itemResourceRel = "userroles", + exported = true + ) +public interface UserRoleRepository extends CrudRepository { +} \ No newline at end of file