-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
104 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
src/main/java/sysc4806/project/services/ApplicationUserService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package sysc4806.project.services; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.stereotype.Service; | ||
import sysc4806.project.models.*; | ||
import sysc4806.project.repositories.ApplicationUserRepository; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
import static sysc4806.project.util.AuthenticationHelper.PROFESSOR_ROLE; | ||
import static sysc4806.project.util.AuthenticationHelper.getUserRole; | ||
|
||
@Service | ||
public class ApplicationUserService { | ||
@Autowired | ||
ApplicationUserRepository applicationUserRepository; | ||
|
||
/** | ||
* Gets the current user | ||
* @return The current application user | ||
*/ | ||
public ApplicationUser getCurrentUser() { | ||
UserDetails user = (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); | ||
return applicationUserRepository.findApplicationUserByEmail(user.getUsername()); | ||
} | ||
|
||
|
||
/** | ||
* Check if the current user is Professor | ||
* @return True if the current user is Professor | ||
*/ | ||
public boolean isCurrentUserProfessor() throws Exception { | ||
ApplicationUser currentUser = getCurrentUser(); | ||
return Objects.equals(getUserRole(currentUser), PROFESSOR_ROLE); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters