Browse Source

Implement authentication (& authorization) properties: user & roles

Signed-off-by: Pekka Helenius <fincer89@hotmail.com>
v0.0.3-alpha
Pekka Helenius 4 years ago
parent
commit
7c51eb38ca
7 changed files with 533 additions and 0 deletions
  1. +112
    -0
      bookstore/src/main/java/com/fjordtek/bookstore/model/auth/Role.java
  2. +26
    -0
      bookstore/src/main/java/com/fjordtek/bookstore/model/auth/RoleRepository.java
  3. +156
    -0
      bookstore/src/main/java/com/fjordtek/bookstore/model/auth/User.java
  4. +24
    -0
      bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRepository.java
  5. +102
    -0
      bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRole.java
  6. +92
    -0
      bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRoleCompositeKey.java
  7. +21
    -0
      bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRoleRepository.java

+ 112
- 0
bookstore/src/main/java/com/fjordtek/bookstore/model/auth/Role.java View File

@ -0,0 +1,112 @@
//Pekka Helenius <fincer89@hotmail.com>, 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.
* <p>
* 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<UserRole> userRoles;
////////////////////
// Attribute setters
public void setId(Long id) {
this.id = id;
}
public void setName(String name) {
this.name = name;
}
/*
public void setUserroles(List<UserRole> userRoles) {
this.userRoles = userRoles;
}
*/
////////////////////
// Attribute getters
public Long getId() {
return id;
}
public String getName() {
return name;
}
/*
public List<UserRole> 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 + "]";
}
}

+ 26
- 0
bookstore/src/main/java/com/fjordtek/bookstore/model/auth/RoleRepository.java View File

@ -0,0 +1,26 @@
//Pekka Helenius <fincer89@hotmail.com>, 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<Role, Long> {
List<Role> findByName(String string);
}

+ 156
- 0
bookstore/src/main/java/com/fjordtek/bookstore/model/auth/User.java View File

@ -0,0 +1,156 @@
//Pekka Helenius <fincer89@hotmail.com>, 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.
* <p>
* 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<UserRole> 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<UserRole> 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<UserRole> 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 + "]";
}
}

+ 24
- 0
bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRepository.java View File

@ -0,0 +1,24 @@
//Pekka Helenius <fincer89@hotmail.com>, 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, Long> {
User findByUsername(String username);
}

+ 102
- 0
bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRole.java View File

@ -0,0 +1,102 @@
//Pekka Helenius <fincer89@hotmail.com>, 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.
* <p>
* 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.
* <p>
* Primary key of this entity is a composite primary key.
* <p>
* This implementation supports having additional columns in the join table
* if needed, contrary to the direct Spring @ManyToMany annotation.
* <p>
* 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 + "]";
}
}

+ 92
- 0
bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRoleCompositeKey.java View File

@ -0,0 +1,92 @@
//Pekka Helenius <fincer89@hotmail.com>, 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.
* <p>
* 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 + "]";
}
}

+ 21
- 0
bookstore/src/main/java/com/fjordtek/bookstore/model/auth/UserRoleRepository.java View File

@ -0,0 +1,21 @@
//Pekka Helenius <fincer89@hotmail.com>, 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<UserRole, Long> {
}

Loading…
Cancel
Save