эм... регистрация?... работает? куда коммит делся?...

This commit is contained in:
Елена Бакальская 2024-05-13 02:18:21 +04:00
parent b6fabeec1b
commit 63fa78d2b7
7 changed files with 8 additions and 43 deletions

View File

@ -12,7 +12,6 @@ public class UserPrincipal implements UserDetails {
private final long id;
private final String username;
private final String password;
private final boolean isAdmin;
private final Set<? extends GrantedAuthority> roles;
private final boolean active;
@ -20,7 +19,6 @@ public class UserPrincipal implements UserDetails {
this.id = user.getId();
this.username = user.getLogin();
this.password = user.getPassword();
this.isAdmin = user.getIsAdmin();
this.roles = Set.of(user.getRole());
this.active = true;
}
@ -39,10 +37,6 @@ public class UserPrincipal implements UserDetails {
return password;
}
public boolean getIsAdmin() {
return isAdmin;
}
@Override
public Collection<? extends GrantedAuthority> getAuthorities() {
return roles;

View File

@ -23,9 +23,6 @@ public class UserDTO {
@NotNull
private UserRole role;
@NotNull
private boolean isAdmin;
@JsonProperty(access = JsonProperty.Access.READ_ONLY)
public Integer getId() {
return id;
@ -58,12 +55,4 @@ public class UserDTO {
public void setRole(UserRole role) {
this.role = role;
}
public boolean getIsAdmin() {
return isAdmin;
}
public void setIsAdmin(boolean isAdmin) {
this.isAdmin = isAdmin;
}
}

View File

@ -13,6 +13,7 @@ import org.springframework.web.bind.annotation.RequestMapping;
import com.example.backend.core.configurations.Constants;
import com.example.backend.users.model.UserEntity;
import com.example.backend.users.model.UserRole;
import com.example.backend.users.service.UserService;
import jakarta.validation.Valid;
@ -26,17 +27,19 @@ public class UserSignupController {
private static final String USER_ATTRIBUTE = "user";
private final UserService userService;
private final ModelMapper modelMapper;
public UserSignupController(
UserService userService,
ModelMapper modelMapper) {
this.userService = userService;
this.modelMapper = modelMapper;
}
private UserEntity toEntity(UserSignupDTO dto) {
return modelMapper.map(dto, UserEntity.class);
final UserEntity entity = new UserEntity();
entity.setLogin(dto.getLogin());
entity.setPassword(dto.getPassword());
entity.setRole(dto.getIsAdmin() ? UserRole.ADMIN : UserRole.USER);
return entity;
}
@GetMapping

View File

@ -4,7 +4,6 @@ import java.util.Objects;
import com.example.backend.core.model.BaseEntity;
import io.micrometer.common.lang.NonNull;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
@ -19,19 +18,15 @@ public class UserEntity extends BaseEntity {
@Column(nullable = false, length = 90)
private String password;
@NonNull
private boolean isAdmin;
private UserRole role;
public UserEntity() {
}
public UserEntity(Integer id, String login, String password, boolean isAdmin) {
public UserEntity(Integer id, String login, String password) {
this.login = login;
this.password = password;
this.isAdmin = isAdmin;
this.role = UserRole.USER;
}
@ -51,14 +46,6 @@ public class UserEntity extends BaseEntity {
this.password = password;
}
public boolean getIsAdmin() {
return isAdmin;
}
public void setIsAdmin(boolean isAdmin) {
this.isAdmin = isAdmin;
}
public UserRole getRole() {
return role;
}
@ -82,7 +69,6 @@ public class UserEntity extends BaseEntity {
return Objects.equals(other.getId(), id) &&
Objects.equals(other.getLogin(), login) &&
Objects.equals(other.getRole(), role) &&
Objects.equals(other.getIsAdmin(), isAdmin) &&
Objects.equals(other.getPassword(), password);
}
}

View File

@ -17,7 +17,6 @@ import com.example.backend.core.configurations.Constants;
import com.example.backend.core.errors.NotFoundException;
import com.example.backend.core.security.UserPrincipal;
import com.example.backend.users.model.UserEntity;
import com.example.backend.users.model.UserRole;
import com.example.backend.users.repository.UserRepository;
@Service
@ -59,16 +58,10 @@ public class UserService implements UserDetailsService {
if (entity == null) {
throw new IllegalArgumentException("Entity is null");
}
entity.setLogin(null);
checkLogin(null, entity.getLogin());
final String password = Optional.ofNullable(entity.getPassword()).orElse("");
entity.setPassword(
passwordEncoder.encode(StringUtils.hasText(password.strip()) ? password : Constants.DEFAULT_PASSWORD));
if (entity.getIsAdmin() == true) {
entity.setRole(UserRole.ADMIN);
} else {
entity.setRole(UserRole.USER);
}
repository.save(entity);
return repository.save(entity);
}

View File

@ -46,7 +46,7 @@
</div>
<div class="check-admin mt-4 d-flex flex-row justify-content-center mb-4">
<input th:field="*{isAdmin}" class='isAdmin-check' type="checkbox" />
<input th:field="*{isAdmin}" class='isAdmin-check' type="checkbox" name="isAdmin" />
<label for="isAdmin" class='are-you-admin'> Вы админ? </label>
</div>

Binary file not shown.