Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(User): Change role to roles #258

Merged
merged 2 commits into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading