CAS Part 1

Signed-off-by: Marcin Woźniak <y0rune@aol.com>
This commit is contained in:
Marcin Woźniak 2020-12-03 16:23:39 +01:00
parent b6c2e43975
commit 3ebfda5316
Signed by: y0rune
GPG Key ID: F204C385F57EB348
5 changed files with 56 additions and 9 deletions

View File

@ -24,10 +24,12 @@ public class User {
private String name; private String name;
private String surname; private String surname;
private String email; private String email;
private String usosId;
private UserRoles role; private UserRoles role;
private String token; private String token;
private Timestamp tokenUsageDate; private Timestamp tokenUsageDate;
public User() { public User() {
} }
@ -46,6 +48,29 @@ public class User {
this.role = role; this.role = role;
} }
/*
* User
*
* @param name name given to the user
* @param surname surname given to the user
* @param email mail given to the user
* @param usosId id in the USOS system
* @param role role given to the user
*/
public User(String name, String surname, String mail, String usosId, UserRoles role){
this(name,surname,mail,role);
this.usosId = usosId;
}
/*
* getusosId
*
* @return usosId
*/
public String getUsosId() {
return usosId;
}
/* /*
* getEmail * getEmail
* *

View File

@ -50,4 +50,7 @@ public interface UserRepository extends JpaRepository<User, Long> {
@Query("FROM User WHERE (name LIKE %?1% OR surname LIKE %?1%) AND role=?2") @Query("FROM User WHERE (name LIKE %?1% OR surname LIKE %?1%) AND role=?2")
List<User> searchForUsers(@Param("query") String query, @Param("role") UserRoles role); List<User> searchForUsers(@Param("query") String query, @Param("role") UserRoles role);
@Query("FROM User WHERE usosId = ?1")
Optional<User> getByUsosId(@Param("usosId") String usosId);
} }

View File

@ -24,6 +24,31 @@ public class UserService {
super(); super();
} }
public User checkForUser(String email, String usosId) {
if (usosId == null) {
Optional <User> user = this.repo.getByAuthority(email.replace("\n", "").trim());
if (user.isPresent()){
return user.get();
}
else {
final User newUser = new User(null,null,email.replace("\n", "").trim(),UserRoles.STUDENT);
this.repo.save(newUser);
return newUser;
}
}
else {
Optional <User> user = this.repo.getByUsosId(usosId.replace("\n", "").trim());
if (user.isPresent()){
return user.get();
}
else {
final User newUser = new User(null,null,email.replace("\n", "").trim(),usosId,UserRoles.STUDENT);
this.repo.save(newUser);
return newUser;
}
}
}
public String login(String authority) throws UserNotFoundException { public String login(String authority) throws UserNotFoundException {
User user = this.repo.getByAuthority(authority.replace("\n", "").trim()) User user = this.repo.getByAuthority(authority.replace("\n", "").trim())
.orElseThrow(() -> new UserNotFoundException("Can not find user with given authority")); .orElseThrow(() -> new UserNotFoundException("Can not find user with given authority"));

View File

@ -49,13 +49,5 @@ public class App {
mac.setSurname("Głowacki"); mac.setSurname("Głowacki");
mac.setRole(UserRoles.STUDENT); mac.setRole(UserRoles.STUDENT);
this.userService.save(mac); this.userService.save(mac);
User mar = new User();
mar.setEmail("marwoz16@st.amu.edu.pl");
mar.setName("Marcin");
mar.setSurname("Woźniak");
mar.setRole(UserRoles.ADMIN);
this.userService.save(mar);
} }
} }

View File

@ -36,7 +36,9 @@ public class TokenController {
try { try {
CasUserIdentity casUserIdentity = validator.validate(); CasUserIdentity casUserIdentity = validator.validate();
String usosId = casUserIdentity.getUsosId();
String authority = casUserIdentity.getEmail(); String authority = casUserIdentity.getEmail();
this.userService.checkForUser(authority, usosId);
String token = this.userService.login(authority); String token = this.userService.login(authority);
return new ResponseEntity<>(token, HttpStatus.OK); return new ResponseEntity<>(token, HttpStatus.OK);
} catch (CasValidationExcepiton e) { } catch (CasValidationExcepiton e) {