admin panel start

This commit is contained in:
Michael Wain 2024-05-27 02:37:44 +03:00
parent 400e7fea33
commit 51256367f5
4 changed files with 39 additions and 12 deletions

View File

@ -0,0 +1,27 @@
package com.alterdekim.game.controller;
import com.alterdekim.game.entities.User;
import com.alterdekim.game.service.UserServiceImpl;
import com.alterdekim.game.util.AuthenticationUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
@Slf4j
@Controller
public class AdminController {
@Autowired
private UserServiceImpl userService;
@GetMapping("/admin")
public String adminPanel(Model model) {
User u = AuthenticationUtil.authProfile(model, userService);
if( !u.getRoles().get(0).getName().equals("ROLE_ADMINISTRATOR") ) {
return "redirect:/games";
}
return "admin";
}
}

View File

@ -34,15 +34,15 @@ public class SpringSecurity {
.authorizeHttpRequests((authorize) ->
authorize
.requestMatchers("/async/**").permitAll()
.requestMatchers("/image/**").hasAnyAuthority("ROLE_ADMIN")
.requestMatchers("/game").hasAnyAuthority("ROLE_ADMIN")
.requestMatchers("/games").hasAnyAuthority("ROLE_ADMIN")
.requestMatchers("/profile/**").hasAnyAuthority("ROLE_ADMIN")
.requestMatchers("/api/**").hasAnyAuthority("ROLE_ADMIN")
.requestMatchers("/friends").hasAnyAuthority("ROLE_ADMIN")
.requestMatchers("/followers").hasAnyAuthority("ROLE_ADMIN")
.requestMatchers("/settings").hasAnyAuthority("ROLE_ADMIN")
.requestMatchers("/websocket/**").hasAnyAuthority("ROLE_ADMIN")
.requestMatchers("/image/**").hasAnyAuthority("ROLE_USER")
.requestMatchers("/game").hasAnyAuthority("ROLE_USER")
.requestMatchers("/games").hasAnyAuthority("ROLE_USER")
.requestMatchers("/profile/**").hasAnyAuthority("ROLE_USER")
.requestMatchers("/api/**").hasAnyAuthority("ROLE_USER")
.requestMatchers("/friends").hasAnyAuthority("ROLE_USER")
.requestMatchers("/followers").hasAnyAuthority("ROLE_USER")
.requestMatchers("/settings").hasAnyAuthority("ROLE_USER")
.requestMatchers("/websocket/**").hasAnyAuthority("ROLE_USER")
.requestMatchers("/static/**").permitAll()
.requestMatchers("/access-denied").permitAll()
.requestMatchers("/signup").permitAll()

View File

@ -14,6 +14,6 @@ public class WebSocketSecurityConfig {
@Bean
AuthorizationManager<Message<?>> messageAuthorizationManager(MessageMatcherDelegatingAuthorizationManager.Builder messages) {
return AuthorityAuthorizationManager.hasAnyAuthority("ROLE_ADMIN");
return AuthorityAuthorizationManager.hasAnyAuthority("ROLE_USER");
}
}

View File

@ -35,7 +35,7 @@ public class UserServiceImpl implements UserService {
user.setDisplayName(userDto.getUsername());
user.setAvatarId(0L);
user.setPassword(passwordEncoder.encode(userDto.getPassword()));
Role role = roleRepository.findByName("ROLE_ADMIN");
Role role = roleRepository.findByName("ROLE_USER");
if(role == null){
role = checkRoleExist();
}
@ -68,7 +68,7 @@ public class UserServiceImpl implements UserService {
private Role checkRoleExist() {
Role role = new Role();
role.setName("ROLE_ADMIN");
role.setName("ROLE_USER");
return roleRepository.save(role);
}