From 697c349f2909de380b141ea53d8ddd67ecfaec8f Mon Sep 17 00:00:00 2001 From: DOHA Date: Wed, 30 Mar 2016 19:15:58 +0200 Subject: [PATCH] remove role from hierarchy --- .../org/baeldung/persistence/SetupData.java | 27 +--- .../persistence/dao/RoleRepository.java | 9 -- .../org/baeldung/persistence/model/Role.java | 121 ------------------ .../org/baeldung/persistence/model/User.java | 87 ++++++------- 4 files changed, 46 insertions(+), 198 deletions(-) delete mode 100644 spring-security-custom-permission/src/main/java/org/baeldung/persistence/dao/RoleRepository.java delete mode 100644 spring-security-custom-permission/src/main/java/org/baeldung/persistence/model/Role.java diff --git a/spring-security-custom-permission/src/main/java/org/baeldung/persistence/SetupData.java b/spring-security-custom-permission/src/main/java/org/baeldung/persistence/SetupData.java index fa6d4c42ee..47616ca61a 100644 --- a/spring-security-custom-permission/src/main/java/org/baeldung/persistence/SetupData.java +++ b/spring-security-custom-permission/src/main/java/org/baeldung/persistence/SetupData.java @@ -7,11 +7,9 @@ import javax.annotation.PostConstruct; import org.baeldung.persistence.dao.OrganizationRepository; import org.baeldung.persistence.dao.PrivilegeRepository; -import org.baeldung.persistence.dao.RoleRepository; import org.baeldung.persistence.dao.UserRepository; import org.baeldung.persistence.model.Organization; import org.baeldung.persistence.model.Privilege; -import org.baeldung.persistence.model.Role; import org.baeldung.persistence.model.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; @@ -21,9 +19,6 @@ public class SetupData { @Autowired private UserRepository userRepository; - @Autowired - private RoleRepository roleRepository; - @Autowired private PrivilegeRepository privilegeRepository; @@ -33,26 +28,25 @@ public class SetupData { @PostConstruct public void init() { initPrivileges(); - initRoles(); initOrganizations(); initUsers(); } private void initUsers() { - final Role role1 = roleRepository.findByName("USER_ROLE"); - final Role role2 = roleRepository.findByName("ADMIN_ROLE"); + final Privilege privilege1 = privilegeRepository.findByName("FOO_READ_PRIVILEGE"); + final Privilege privilege2 = privilegeRepository.findByName("FOO_WRITE_PRIVILEGE"); // final User user1 = new User(); user1.setUsername("john"); user1.setPassword("123"); - user1.setRoles(new HashSet(Arrays.asList(role1))); + user1.setPrivileges(new HashSet(Arrays.asList(privilege1))); user1.setOrganization(organizationRepository.findByName("FirstOrg")); userRepository.save(user1); // final User user2 = new User(); user2.setUsername("tom"); user2.setPassword("111"); - user2.setRoles(new HashSet(Arrays.asList(role2))); + user2.setPrivileges(new HashSet(Arrays.asList(privilege1, privilege2))); user2.setOrganization(organizationRepository.findByName("SecondOrg")); userRepository.save(user2); } @@ -66,19 +60,6 @@ public class SetupData { } - private void initRoles() { - final Privilege privilege1 = privilegeRepository.findByName("FOO_READ_PRIVILEGE"); - final Privilege privilege2 = privilegeRepository.findByName("FOO_WRITE_PRIVILEGE"); - // - final Role role1 = new Role("USER_ROLE"); - role1.setPrivileges(new HashSet(Arrays.asList(privilege1))); - roleRepository.save(role1); - // - final Role role2 = new Role("ADMIN_ROLE"); - role2.setPrivileges(new HashSet(Arrays.asList(privilege1, privilege2))); - roleRepository.save(role2); - } - private void initPrivileges() { final Privilege privilege1 = new Privilege("FOO_READ_PRIVILEGE"); privilegeRepository.save(privilege1); diff --git a/spring-security-custom-permission/src/main/java/org/baeldung/persistence/dao/RoleRepository.java b/spring-security-custom-permission/src/main/java/org/baeldung/persistence/dao/RoleRepository.java deleted file mode 100644 index 408720fe9c..0000000000 --- a/spring-security-custom-permission/src/main/java/org/baeldung/persistence/dao/RoleRepository.java +++ /dev/null @@ -1,9 +0,0 @@ -package org.baeldung.persistence.dao; - -import org.baeldung.persistence.model.Role; -import org.springframework.data.jpa.repository.JpaRepository; - -public interface RoleRepository extends JpaRepository { - public Role findByName(String name); - -} diff --git a/spring-security-custom-permission/src/main/java/org/baeldung/persistence/model/Role.java b/spring-security-custom-permission/src/main/java/org/baeldung/persistence/model/Role.java deleted file mode 100644 index f4589315b9..0000000000 --- a/spring-security-custom-permission/src/main/java/org/baeldung/persistence/model/Role.java +++ /dev/null @@ -1,121 +0,0 @@ -package org.baeldung.persistence.model; - -import java.util.Set; - -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.JoinColumn; -import javax.persistence.JoinTable; -import javax.persistence.ManyToMany; - -@Entity -public class Role { - - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - private Long id; - - @Column(nullable = false, unique = true) - private String name; - - @ManyToMany(fetch = FetchType.EAGER) - @JoinTable(name = "roles_privileges", joinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "privilege_id", referencedColumnName = "id")) - private Set privileges; - - // - - public Role() { - super(); - } - - public Role(String name) { - super(); - this.name = name; - } - - // - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public Set getPrivileges() { - return privileges; - } - - public void setPrivileges(Set privileges) { - this.privileges = privileges; - } - - // - - @Override - public String toString() { - final StringBuilder builder = new StringBuilder(); - builder.append("Role [id=").append(id).append(", name=").append(name).append(", privileges=").append(privileges).append("]"); - return builder.toString(); - } - - @Override - public int hashCode() { - final int prime = 31; - int result = 1; - result = (prime * result) + ((id == null) ? 0 : id.hashCode()); - result = (prime * result) + ((name == null) ? 0 : name.hashCode()); - result = (prime * result) + ((privileges == null) ? 0 : privileges.hashCode()); - return result; - } - - @Override - public boolean equals(Object obj) { - if (this == obj) { - return true; - } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { - return false; - } - final Role other = (Role) obj; - if (id == null) { - if (other.id != null) { - return false; - } - } else if (!id.equals(other.id)) { - return false; - } - if (name == null) { - if (other.name != null) { - return false; - } - } else if (!name.equals(other.name)) { - return false; - } - if (privileges == null) { - if (other.privileges != null) { - return false; - } - } else if (!privileges.equals(other.privileges)) { - return false; - } - return true; - } - -} \ No newline at end of file diff --git a/spring-security-custom-permission/src/main/java/org/baeldung/persistence/model/User.java b/spring-security-custom-permission/src/main/java/org/baeldung/persistence/model/User.java index 995c62d08f..86b81cdcee 100644 --- a/spring-security-custom-permission/src/main/java/org/baeldung/persistence/model/User.java +++ b/spring-security-custom-permission/src/main/java/org/baeldung/persistence/model/User.java @@ -35,8 +35,8 @@ public class User implements UserDetails { private String password; @ManyToMany(fetch = FetchType.EAGER) - @JoinTable(name = "users_roles", joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "role_id", referencedColumnName = "id")) - private Set roles; + @JoinTable(name = "users_privileges", joinColumns = @JoinColumn(name = "user_id", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "privilege_id", referencedColumnName = "id")) + private Set privileges; @ManyToOne(fetch = FetchType.EAGER) @JoinColumn(name = "organization_id", referencedColumnName = "id") @@ -75,12 +75,12 @@ public class User implements UserDetails { this.password = password; } - public Set getRoles() { - return roles; + public Set getPrivileges() { + return privileges; } - public void setRoles(Set roles) { - this.roles = roles; + public void setPrivileges(Set privileges) { + this.privileges = privileges; } public Organization getOrganization() { @@ -93,10 +93,41 @@ public class User implements UserDetails { // + @Override + public Collection getAuthorities() { + final List authorities = new ArrayList(); + for (final Privilege privilege : this.getPrivileges()) { + authorities.add(new SimpleGrantedAuthority(privilege.getName())); + } + return authorities; + } + + @Override + public boolean isAccountNonExpired() { + return true; + } + + @Override + public boolean isAccountNonLocked() { + return true; + } + + @Override + public boolean isCredentialsNonExpired() { + return true; + } + + @Override + public boolean isEnabled() { + return true; + } + + // + @Override public String toString() { final StringBuilder builder = new StringBuilder(); - builder.append("User [id=").append(id).append(", username=").append(username).append(", password=").append(password).append(", roles=").append(roles).append(", organization=").append(organization).append("]"); + builder.append("User [id=").append(id).append(", username=").append(username).append(", password=").append(password).append(", privileges=").append(privileges).append(", organization=").append(organization).append("]"); return builder.toString(); } @@ -107,7 +138,7 @@ public class User implements UserDetails { result = (prime * result) + ((id == null) ? 0 : id.hashCode()); result = (prime * result) + ((organization == null) ? 0 : organization.hashCode()); result = (prime * result) + ((password == null) ? 0 : password.hashCode()); - result = (prime * result) + ((roles == null) ? 0 : roles.hashCode()); + result = (prime * result) + ((privileges == null) ? 0 : privileges.hashCode()); result = (prime * result) + ((username == null) ? 0 : username.hashCode()); return result; } @@ -145,11 +176,11 @@ public class User implements UserDetails { } else if (!password.equals(other.password)) { return false; } - if (roles == null) { - if (other.roles != null) { + if (privileges == null) { + if (other.privileges != null) { return false; } - } else if (!roles.equals(other.roles)) { + } else if (!privileges.equals(other.privileges)) { return false; } if (username == null) { @@ -161,38 +192,4 @@ public class User implements UserDetails { } return true; } - - // - - @Override - public Collection getAuthorities() { - final List authorities = new ArrayList(); - for (final Role role : this.getRoles()) { - for (final Privilege privilege : role.getPrivileges()) { - authorities.add(new SimpleGrantedAuthority(privilege.getName())); - } - } - return authorities; - } - - @Override - public boolean isAccountNonExpired() { - return true; - } - - @Override - public boolean isAccountNonLocked() { - return true; - } - - @Override - public boolean isCredentialsNonExpired() { - return true; - } - - @Override - public boolean isEnabled() { - return true; - } - }