Skip to content

Commit

Permalink
feat(User): Change role to roles (#258)
Browse files Browse the repository at this point in the history
  • Loading branch information
geoffreykwan authored Feb 29, 2024
1 parent 102aa23 commit 6e84932
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
4 changes: 4 additions & 0 deletions src/main/java/org/wise/portal/domain/user/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
*/
package org.wise.portal.domain.user;

import java.util.List;

import org.wise.portal.domain.Persistable;
import org.wise.portal.domain.authentication.MutableUserDetails;

Expand Down Expand Up @@ -49,4 +51,6 @@ public interface User extends Persistable, Comparable<User> {
boolean isTeacher();

boolean isTrustedAuthor();

List<String> getRoles();
}
30 changes: 26 additions & 4 deletions src/main/java/org/wise/portal/domain/user/impl/UserImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
*/
package org.wise.portal.domain.user.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
Expand Down Expand Up @@ -91,13 +94,31 @@ public boolean isTrustedAuthor() {
return userDetails.hasGrantedAuthority(UserDetailsService.TRUSTED_AUTHOR_ROLE);
}

public List<String> getRoles() {
List<String> roles = new ArrayList<String>();
if (this.isAdmin()) {
roles.add("admin");
}
if (this.isResearcher()) {
roles.add("researcher");
}
if (this.isTrustedAuthor()) {
roles.add("trustedAuthor");
}
if (this.isTeacher()) {
roles.add("teacher");
}
if (this.isStudent()) {
roles.add("student");
}
return roles;
}

@Override
public int hashCode() {
final int PRIME = 31;
int result = 1;
result = PRIME
* result
+ ((userDetails == null) ? 0 : userDetails.hashCode());
result = PRIME * result + ((userDetails == null) ? 0 : userDetails.hashCode());
return result;
}

Expand All @@ -108,7 +129,8 @@ public boolean equals(Object obj) {
if (obj == null)
return false;
if (obj instanceof HibernateProxy) {
if (getClass() != (( HibernateProxy) obj).getHibernateLazyInitializer().getImplementation().getClass()) {
if (getClass() != ((HibernateProxy) obj).getHibernateLazyInitializer().getImplementation()
.getClass()) {
return false;
}
} else if (getClass() != obj.getClass())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,8 @@ HashMap<String, Object> getUserInfo(Authentication auth,
info.put("isPreviousAdmin", isPreviousAdmin(auth));
info.put("language", ud.getLanguage());
info.put("isGoogleUser", ud.isGoogleUser());

if (user.isStudent()) {
info.put("role", "student");
} else {
if (user.isAdmin()) {
info.put("role", "admin");
} else if (user.isResearcher()) {
info.put("role", "researcher");
} else if (user.isTeacher()) {
info.put("role", "teacher");
}
info.put("roles", user.getRoles());
if (user.isTeacher()) {
TeacherUserDetails tud = (TeacherUserDetails) ud;
info.put("displayName", tud.getDisplayname());
info.put("email", tud.getEmailAddress());
Expand Down

0 comments on commit 6e84932

Please sign in to comment.