Compare commits
No commits in common. "583682106df898d618c50be46aa01b238c11a247" and "5d3664d1480e2afaf14df1798ba93e4a63003389" have entirely different histories.
583682106d
...
5d3664d148
14 changed files with 12 additions and 177 deletions
2
pom.xml
2
pom.xml
|
@ -5,7 +5,7 @@
|
|||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>3.2.1</version>
|
||||
<version>3.1.4</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>ru.redrise</groupId>
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package ru.redrise.marinesco.security;
|
||||
package ru.redrise.marinesco;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
@ -8,7 +8,7 @@ import jakarta.validation.constraints.NotEmpty;
|
|||
import jakarta.validation.constraints.NotNull;
|
||||
import jakarta.validation.constraints.Size;
|
||||
import lombok.Data;
|
||||
import ru.redrise.marinesco.User;
|
||||
import ru.redrise.marinesco.security.UserRole;
|
||||
|
||||
//TODO: refactor along with RegistrationForm.java
|
||||
@Data
|
|
@ -1,8 +1,10 @@
|
|||
package ru.redrise.marinesco;
|
||||
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
//@PreAuthorize("hasRole('USER')")
|
||||
@Controller
|
||||
public class RootController {
|
||||
|
||||
|
|
|
@ -13,19 +13,16 @@ import lombok.extern.slf4j.Slf4j;
|
|||
import ru.redrise.marinesco.data.RolesRepository;
|
||||
import ru.redrise.marinesco.data.UserRepository;
|
||||
import ru.redrise.marinesco.security.UserRole;
|
||||
import ru.redrise.marinesco.settings.ApplicationSettings;
|
||||
|
||||
@Slf4j
|
||||
@Configuration
|
||||
public class ShinyApplicationRunner {
|
||||
private UserRepository users;
|
||||
private RolesRepository roles;
|
||||
private ApplicationSettings settings;
|
||||
|
||||
public ShinyApplicationRunner(UserRepository users, RolesRepository roles, ApplicationSettings settings) {
|
||||
public ShinyApplicationRunner(UserRepository users, RolesRepository roles) {
|
||||
this.users = users;
|
||||
this.roles = roles;
|
||||
this.settings = settings;
|
||||
}
|
||||
|
||||
@Bean
|
||||
|
@ -35,7 +32,6 @@ public class ShinyApplicationRunner {
|
|||
log.info("Application first run");
|
||||
setRoles();
|
||||
setAdmin(args, encoder);
|
||||
settings.setAllowRegistraion(true);
|
||||
} else
|
||||
log.info("Regular run");
|
||||
};
|
||||
|
|
|
@ -16,6 +16,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
|
|||
|
||||
import jakarta.validation.Valid;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import ru.redrise.marinesco.AdministatorAddUserForm;
|
||||
import ru.redrise.marinesco.User;
|
||||
import ru.redrise.marinesco.UserGenerified;
|
||||
import ru.redrise.marinesco.data.RolesRepository;
|
||||
|
|
|
@ -15,8 +15,6 @@ import lombok.extern.slf4j.Slf4j;
|
|||
import ru.redrise.marinesco.User;
|
||||
import ru.redrise.marinesco.data.RolesRepository;
|
||||
import ru.redrise.marinesco.data.UserRepository;
|
||||
import ru.redrise.marinesco.settings.ApplicationSettings;
|
||||
import ru.redrise.marinesco.settings.KeyValueRepository;
|
||||
|
||||
@Slf4j
|
||||
@Controller
|
||||
|
@ -27,18 +25,14 @@ public class RegistrationController {
|
|||
private PasswordEncoder passwordEncoder;
|
||||
private HttpServletRequest request;
|
||||
|
||||
private ApplicationSettings applicationSettings;
|
||||
|
||||
public RegistrationController(UserRepository userRepo,
|
||||
RolesRepository rolesRepo,
|
||||
PasswordEncoder passwordEncoder,
|
||||
HttpServletRequest request,
|
||||
ApplicationSettings applicationSettings) {
|
||||
HttpServletRequest request) {
|
||||
this.userRepo = userRepo;
|
||||
this.rolesRepo = rolesRepo;
|
||||
this.passwordEncoder = passwordEncoder;
|
||||
this.request = request;
|
||||
this.applicationSettings = applicationSettings;
|
||||
}
|
||||
|
||||
@ModelAttribute(name = "registrationForm")
|
||||
|
@ -48,15 +42,11 @@ public class RegistrationController {
|
|||
|
||||
@GetMapping
|
||||
public String registerForm() {
|
||||
if (applicationSettings.isRegistrationAllowed())
|
||||
return "registration";
|
||||
return "registration_forbidden";
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
public String postMethodName(@Valid RegistrationForm form, Errors errors, Model model) {
|
||||
if (!applicationSettings.isRegistrationAllowed())
|
||||
return "redirect:/";
|
||||
if (form.isPasswordsNotEqual()) {
|
||||
model.addAttribute("passwordsMismatch", "Passwords must be the same.");
|
||||
return "registration";
|
||||
|
|
|
@ -54,8 +54,8 @@ public class SecurityConfig {
|
|||
.requestMatchers(mvc.pattern("/login")).anonymous()
|
||||
.requestMatchers(mvc.pattern("/error")).permitAll()
|
||||
.requestMatchers(PathRequest.toH2Console()).permitAll()
|
||||
.requestMatchers(mvc.pattern("/")).authenticated()
|
||||
.requestMatchers(mvc.pattern("/profile/**")).authenticated()//.hasAnyRole("ADMIN", "USER")
|
||||
.requestMatchers(mvc.pattern("/")).hasAnyRole("ADMIN", "USER")
|
||||
.requestMatchers(mvc.pattern("/profile/**")).hasAnyRole("ADMIN", "USER")
|
||||
//.requestMatchers(mvc.pattern("/design/**")).hasRole("USER")
|
||||
.anyRequest().authenticated())
|
||||
//.anyRequest().permitAll())
|
||||
|
|
|
@ -1,37 +0,0 @@
|
|||
package ru.redrise.marinesco.settings;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class ApplicationSettings {
|
||||
private static final String ALLOW_REGISTRATION = "allow_registration";
|
||||
|
||||
private KeyValueRepository keyValueRepository;
|
||||
|
||||
private boolean registrationAllowed;
|
||||
|
||||
public ApplicationSettings(KeyValueRepository keyValueRepository) {
|
||||
this.keyValueRepository = keyValueRepository;
|
||||
initAllowRegistraionValue();
|
||||
}
|
||||
|
||||
private void initAllowRegistraionValue() {
|
||||
try{
|
||||
this.registrationAllowed = keyValueRepository.findById(ApplicationSettings.ALLOW_REGISTRATION).get().getM_value();
|
||||
}
|
||||
catch (Exception e){
|
||||
// Application first run, thus no data on the DB
|
||||
registrationAllowed = true;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public synchronized void setAllowRegistraion(boolean value) {
|
||||
keyValueRepository.save(new KeyValue(ApplicationSettings.ALLOW_REGISTRATION, value));
|
||||
this.registrationAllowed = value;
|
||||
}
|
||||
|
||||
public synchronized boolean isRegistrationAllowed() {
|
||||
return registrationAllowed;
|
||||
}
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
package ru.redrise.marinesco.settings;
|
||||
|
||||
import jakarta.persistence.Entity;
|
||||
import jakarta.persistence.Id;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Data
|
||||
@Entity
|
||||
@AllArgsConstructor
|
||||
@NoArgsConstructor(access = AccessLevel.PRIVATE, force = true)
|
||||
public class KeyValue {
|
||||
|
||||
@Id
|
||||
private String m_key;
|
||||
private Boolean m_value;
|
||||
}
|
|
@ -1,11 +0,0 @@
|
|||
package ru.redrise.marinesco.settings;
|
||||
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
|
||||
@Repository
|
||||
public interface KeyValueRepository extends CrudRepository<KeyValue, String>{
|
||||
|
||||
}
|
|
@ -1,42 +0,0 @@
|
|||
package ru.redrise.marinesco.settings;
|
||||
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.ModelAttribute;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
//@Slf4j
|
||||
@Controller
|
||||
@RequestMapping("/settings")
|
||||
@PreAuthorize("hasRole('ADMIN')")
|
||||
public class SettingsController {
|
||||
private KeyValueRepository keyValueRepository;
|
||||
private ApplicationSettings applicationSettings;
|
||||
|
||||
public SettingsController(KeyValueRepository keyValueRepository, ApplicationSettings applicationSettings){
|
||||
this.keyValueRepository = keyValueRepository;
|
||||
this.applicationSettings = applicationSettings;
|
||||
}
|
||||
|
||||
@GetMapping
|
||||
public String getPage() {
|
||||
|
||||
return "settings";
|
||||
}
|
||||
@ModelAttribute(name = "allowRegistration")
|
||||
public Boolean setRegistrationSetting(){
|
||||
return applicationSettings.isRegistrationAllowed();
|
||||
//return keyValueRepository.findById(ApplicationSettings.ALLOW_REGISTRATION).get().getM_value();
|
||||
}
|
||||
|
||||
@GetMapping("/allow_registration/{sw}")
|
||||
public String switchRegistration(@PathVariable("sw") Boolean sw){
|
||||
//log.info("{}", sw);
|
||||
//keyValueRepository.save(new KeyValue(ApplicationSettings.ALLOW_REGISTRATION, sw));
|
||||
applicationSettings.setAllowRegistraion(sw);
|
||||
|
||||
return "redirect:/settings";
|
||||
}
|
||||
}
|
|
@ -1,22 +0,0 @@
|
|||
<!DOCTYPE html>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
|
||||
|
||||
<head>
|
||||
<title>Marinesco - registration form</title>
|
||||
<link rel="icon" href="/favicon.svg" type="image/svg+xml">
|
||||
<link rel="alternate icon" href="/favicon.png" type="image/png">
|
||||
<link rel="stylesheet" th:href="@{/styles/styles.css}" />
|
||||
<script src="/jquery.js"></script>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div class="page">
|
||||
<div th:replace="~{fragments/header :: 'header'}"></div>
|
||||
<div class="container base">
|
||||
<h1>Currently we're close to new registrations</h1>
|
||||
</div>
|
||||
</div>
|
||||
<div th:replace="~{fragments/footer :: 'footer'}"></div>
|
||||
</body>
|
||||
|
||||
</html>
|
|
@ -15,7 +15,6 @@
|
|||
<br /><a href="/login">Login</a>
|
||||
<br /><a href="/profile">/profile</a>
|
||||
<br /><a href="/manage_users">/manage_users</a>
|
||||
<br /><a href="/settings">/settings</a>
|
||||
<br /><a href="/h2">H2</a>
|
||||
</div>
|
||||
</div>
|
||||
|
|
|
@ -1,22 +0,0 @@
|
|||
<!DOCTYPE html>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
|
||||
|
||||
<head>
|
||||
<title>Marinesco - Application settings</title>
|
||||
<link rel="icon" href="/favicon.svg" type="image/svg+xml">
|
||||
<link rel="alternate icon" href="/favicon.png" type="image/png">
|
||||
<link rel="stylesheet" th:href="@{/styles/styles.css}" />
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div class="page">
|
||||
<div th:replace="~{fragments/header :: 'header'}"></div>
|
||||
<div class="container base">
|
||||
<span th:text="${'New users registration is now ' + (allowRegistration ? 'enabled. ' : 'disabled. ' )}"></span>
|
||||
<a th:href="${'/settings/allow_registration/' + !allowRegistration }" th:text="${'Click here to ' + (allowRegistration ? 'disable' : 'enable' )}"></a>
|
||||
</div>
|
||||
</div>
|
||||
<div th:replace="~{fragments/footer :: 'footer'}"></div>
|
||||
</body>
|
||||
|
||||
</html>
|
Loading…
Reference in a new issue