Skip to content

Commit

Permalink
fix MeineHardwareView not realoading grid
Browse files Browse the repository at this point in the history
show grids in dashboard only if storageadmin or fieldtechnician
add hashing on registration
fix exception when lastlogedin is null
fix UserManagementview not reloading on usercreation
fix activate/deactivate button.
add block login on if user is deactivated
make all users in data.sql activated

known issues: If user is deactivated login screen shows wrong message.

Signed-off-by: Aleksandar Zivkovic <[email protected]>
  • Loading branch information
anywaywayany committed Feb 27, 2024
1 parent 6331983 commit 18a3b7c
Show file tree
Hide file tree
Showing 7 changed files with 2,478 additions and 1,295 deletions.
141 changes: 75 additions & 66 deletions src/main/java/com/samic/samic/data/entity/User.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,25 @@
package com.samic.samic.data.entity;

import com.fasterxml.jackson.annotation.JsonIgnore;
import jakarta.persistence.*;
import jakarta.persistence.CascadeType;
import jakarta.persistence.Column;
import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.FetchType;
import jakarta.persistence.OneToMany;
import jakarta.persistence.Table;
import jakarta.validation.constraints.Email;
import jakarta.validation.constraints.PastOrPresent;
import lombok.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

@EqualsAndHashCode(callSuper = true)
@Data
Expand All @@ -17,93 +28,91 @@
@Builder
@Entity
@Table(name = "users")
public class User extends AbstractIdentityClass<Long>{


/*
relations
*/
@OneToMany(mappedBy = "storedAtUser",
fetch = FetchType.LAZY,
cascade = {CascadeType.PERSIST})
// @JoinColumn(name = "fk_storageObject")
private List<StorageObject> storageObject = new ArrayList<>();
public class User extends AbstractIdentityClass<Long> {

// @OneToOne(targetEntity = StorageObject.class,fetch = FetchType.LAZY)
// @JoinColumn(name = "storage_object" )
// private StorageObject storageObject;

/*
relations
*/
@OneToMany(mappedBy = "storedAtUser",
fetch = FetchType.LAZY,
cascade = {CascadeType.PERSIST})
// @JoinColumn(name = "fk_storageObject")
private List<StorageObject> storageObject = new ArrayList<>();

@OneToMany(mappedBy = "reservedFrom",
fetch = FetchType.LAZY,
cascade = {CascadeType.REMOVE, CascadeType.PERSIST})
private List<Reservation> reservation = new ArrayList<>();
// @OneToOne(targetEntity = StorageObject.class,fetch = FetchType.LAZY)
// @JoinColumn(name = "storage_object" )
// private StorageObject storageObject;


// @JsonIgnore
/* @NotBlank*/
// @Column(name = "password")
// private String password;
@OneToMany(mappedBy = "reservedFrom",
fetch = FetchType.LAZY,
cascade = {CascadeType.REMOVE, CascadeType.PERSIST})
private List<Reservation> reservation = new ArrayList<>();

/*
attributes
*/
@Embedded
private Profile profile;
// @JsonIgnore
/* @NotBlank*/
// @Column(name = "password")
// private String password;

@Enumerated(EnumType.STRING)
@Column(name = "role")
private Role role;
/*
attributes
*/
@Embedded
private Profile profile;

@PastOrPresent
@Column(name = "created_At")
private LocalDateTime createdAt;
@Enumerated(EnumType.STRING)
@Column(name = "role")
private Role role;

@PastOrPresent
@Column(name = "last_login")
private LocalDateTime lastLogin;
@PastOrPresent
@Column(name = "created_At")
private LocalDateTime createdAt;

@Column(name = "activated")
private Boolean activated;
@PastOrPresent
@Column(name = "last_login")
private LocalDateTime lastLogin;

@Email
@Column(name = "email")
private String mail;
@Column(name = "activated")
private Boolean activated;

@JsonIgnore
@Column(name = "hashed_password")
private String hashedPassword;
@Email
@Column(name = "email")
private String mail;

// @OneToOne(mappedBy = "user", fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST})
// private ApplicationUser applicationUser;
@JsonIgnore
@Column(name = "hashed_password")
private String hashedPassword;

// @OneToOne(mappedBy = "user", fetch = FetchType.LAZY, cascade = {CascadeType.MERGE, CascadeType.PERSIST})
// private ApplicationUser applicationUser;

// @OneToOne
// @JoinColumn(foreignKey = @ForeignKey(name = "FK_User_2_Profile"))
// @OneToOne
// @JoinColumn(foreignKey = @ForeignKey(name = "FK_User_2_Profile"))

@Override
public String toString(){
StringBuilder builder = new StringBuilder();
builder.append("User:\n")
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
builder.append("User:\n")
// .append("profile=")
// .append(profile.getLastName())
.append("\n")
.append("role=")
.append(role.getLongVersion())
.append("\n")
.append("role=")
.append(role.getLongVersion())
// .append('\'')
// .append("createdAt=")
// .append(createdAt)
// .append('\'')
// .append("lastLogin=")
// .append(lastLogin)
// .append('\'')
.append("activated=")
.append(activated)
.append("\n")
.append("mail='")
.append(mail)
.append("\n");
return builder.toString();
}
.append("activated=")
.append(activated)
.append("\n")
.append("mail='")
.append(mail)
.append("\n");
return builder.toString();
}

}
31 changes: 17 additions & 14 deletions src/main/java/com/samic/samic/security/SecurityConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,26 @@
@Configuration
public class SecurityConfiguration extends VaadinWebSecurity {

@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

@Override
protected void configure(HttpSecurity http) throws Exception {
@Bean
public PasswordEncoder passwordEncoder() {
return new BCryptPasswordEncoder();
}

http.authorizeHttpRequests(
authorize -> authorize.requestMatchers(new AntPathRequestMatcher("/images/*.png")).permitAll());
@Override
protected void configure(HttpSecurity http) throws Exception {

// Icons from the line-awesome addon
http.authorizeHttpRequests(authorize -> authorize
.requestMatchers(new AntPathRequestMatcher("/line-awesome/**/*.svg")).permitAll());
http.authorizeHttpRequests(
authorize -> authorize.requestMatchers(new AntPathRequestMatcher("/images/*.png"))
.permitAll());

// Icons from the line-awesome addon
http.authorizeHttpRequests(authorize -> authorize
.requestMatchers(new AntPathRequestMatcher("/line-awesome/**/*.svg")).permitAll());

super.configure(http);
setLoginView(http, LoginView.class);
}

super.configure(http);
setLoginView(http, LoginView.class);
}

}
63 changes: 37 additions & 26 deletions src/main/java/com/samic/samic/security/UserDetailsServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,69 @@

import com.samic.samic.data.entity.Role;
import com.samic.samic.data.entity.User;

import com.samic.samic.data.repositories.RepositoryUser;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

import com.samic.samic.data.repositories.RepositoryUser;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@AllArgsConstructor
public class UserDetailsServiceImpl implements UserDetailsService {

// @Autowired
// @Autowired
// private final ServiceUser serviceUser;
@Autowired
private final RepositoryUser repositoryUser;



@Override
@Transactional
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
// User user1 = serviceUser.findByUsername(username);
// User user = serviceUser.findUser(username);
User user1 = repositoryUser.findByProfile_Username(username);

if (user1 == null) {
throw new UsernameNotFoundException("No user present with username: " + username);
} else {
return new org.springframework.security.core.userdetails.User(user1.getProfile().getUsername(), user1.getHashedPassword(),
getAuthorities(user1));
}
}
@Autowired
private final RepositoryUser repositoryUser;

@Autowired
private final PasswordEncoder passwordEncoder;

private static List<GrantedAuthority> getAuthorities(User user) {
Role userRole = user.getRole();
return Collections.singletonList(new SimpleGrantedAuthority("ROLE_" + userRole));
}

// private static List<GrantedAuthority> getAuthorities(User user) {
// return user.getRoles().stream().map(role -> new SimpleGrantedAuthority("ROLE_" + role))
// .collect(Collectors.toList());
//
// }

private static List<GrantedAuthority> getAuthorities(User user){
Role userRole = user.getRole();
return Collections.singletonList(new SimpleGrantedAuthority("ROLE_"+userRole));
@Override
@Transactional
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
// User user1 = serviceUser.findByUsername(username);
// User user = serviceUser.findUser(username);
User user1 = repositoryUser.findByProfile_Username(username);

if (user1 == null) {
throw new UsernameNotFoundException("No user present with username: " + username);
} else if (!user1.getActivated()) {
throw new UsernameNotFoundException("User is not activated");
} else {
user1.setLastLogin(LocalDateTime.now());
return new org.springframework.security.core.userdetails.User(
user1.getProfile().getUsername(), user1.getHashedPassword(),
getAuthorities(user1));
}
}

public void register(User user) {
var hashedPassword = passwordEncoder.encode(user.getHashedPassword());
System.out.println(hashedPassword);
user.setHashedPassword(hashedPassword);
System.out.println(user.getHashedPassword());
repositoryUser.save(user);
}

}
Loading

0 comments on commit 18a3b7c

Please sign in to comment.