added a check whether a given user identified by his token is an admin or not, returns true if yes
This commit is contained in:
parent
2123a9177e
commit
ddf3bb3a5d
@ -10,6 +10,7 @@ import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.security.crypto.bcrypt.BCrypt;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.Random;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
@ -148,7 +149,7 @@ public class Controller {
|
||||
Random r = new Random();
|
||||
int low = 100000;
|
||||
int high = 1000000;
|
||||
int code = r.nextInt(high-low) + low;
|
||||
int code = r.nextInt(high - low) + low;
|
||||
station.setCode(code);
|
||||
|
||||
try {
|
||||
@ -180,4 +181,18 @@ public class Controller {
|
||||
}
|
||||
}
|
||||
|
||||
@CrossOrigin(origins = "http://localhost:8081") // only for dev purpose
|
||||
@RequestMapping("/api/checkAdmin")
|
||||
@ResponseBody
|
||||
boolean checkAdmin(@RequestParam String token) {
|
||||
User user = userRepository.findByUsername(token.substring(0, token.indexOf("$")));
|
||||
List<Role> roles = user.getRoles();
|
||||
for (Role role : roles) {
|
||||
if (role.getId() == 0) { // is admin
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user