Changeset 8b875e6 for src


Ignore:
Timestamp:
05/05/24 01:27:16 (7 months ago)
Author:
ppaunovski <paunovskipavel@…>
Branches:
master
Children:
baf4cc4
Parents:
4d026a5
Message:

Added roles to Users and some minor changes

Location:
src/main
Files:
1 added
13 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/mk/ukim/finki/busngo/model/entities/Kondukter.java

    r4d026a5 r8b875e6  
    33import jakarta.persistence.*;
    44import lombok.Data;
     5import lombok.EqualsAndHashCode;
    56
    67import java.util.List;
    78import java.util.Objects;
    89
     10@EqualsAndHashCode(callSuper = true)
    911@Data
    1012@Entity
  • src/main/java/mk/ukim/finki/busngo/model/entities/Korisnik.java

    r4d026a5 r8b875e6  
    5050    private String kLozinka;
    5151
     52    @Basic
     53    @Column(name = "k_role")
     54    @Enumerated(EnumType.STRING)
     55    private Role kRole;
     56
    5257//    @OneToMany(mappedBy = "korisnikByPatnikKId")
    5358//    private List<Bilet> biletsByKId;
     
    6065    @Override
    6166    public Collection<? extends GrantedAuthority> getAuthorities() {
    62         return kIsAdmin ? Collections.singletonList(Role.ROLE_ADMIN) : Collections.singletonList(Role.ROLE_USER);
     67        return Collections.singletonList(kRole);
    6368    }
    6469
  • src/main/java/mk/ukim/finki/busngo/model/entities/Patnik.java

    r4d026a5 r8b875e6  
    33import jakarta.persistence.*;
    44import lombok.Data;
     5import lombok.EqualsAndHashCode;
    56
    67import java.util.List;
    78import java.util.Objects;
    89
     10@EqualsAndHashCode(callSuper = true)
    911@Data
    1012@Entity
  • src/main/java/mk/ukim/finki/busngo/model/entities/Vozac.java

    r4d026a5 r8b875e6  
    33import jakarta.persistence.*;
    44import lombok.Data;
     5import lombok.EqualsAndHashCode;
    56
    67import java.util.List;
    78import java.util.Objects;
    89
     10@EqualsAndHashCode(callSuper = true)
    911@Data
    1012@Entity
  • src/main/java/mk/ukim/finki/busngo/model/entities/Vraboten.java

    r4d026a5 r8b875e6  
    33import jakarta.persistence.*;
    44import lombok.Data;
     5import lombok.EqualsAndHashCode;
    56import lombok.Getter;
    67
     
    89import java.util.Objects;
    910
     11@EqualsAndHashCode(callSuper = true)
    1012@Data
    1113@Entity
  • src/main/java/mk/ukim/finki/busngo/service/AuthService.java

    r4d026a5 r8b875e6  
    22
    33import mk.ukim.finki.busngo.model.entities.Korisnik;
     4import mk.ukim.finki.busngo.model.enums.Role;
    45import mk.ukim.finki.busngo.model.enums.VrabotenType;
    56
     
    910    Korisnik registerVraboten(String ime, String email, String password, String confirmPassword, String address, String telefon, VrabotenType type, Double salary);
    1011
     12    Korisnik register(String name, String email, String password, String confirmPassword, String address, String phone, Role role, Double salary);
    1113}
  • src/main/java/mk/ukim/finki/busngo/service/KorisnikService.java

    r4d026a5 r8b875e6  
    22
    33import mk.ukim.finki.busngo.model.entities.Korisnik;
     4import mk.ukim.finki.busngo.model.enums.Role;
    45
    56
     
    1011    Korisnik findById(Long id);
    1112    Korisnik loadUserByEmail(String email);
     13
    1214}
  • src/main/java/mk/ukim/finki/busngo/service/impl/AuthServiceImpl.java

    r4d026a5 r8b875e6  
    55import mk.ukim.finki.busngo.model.entities.Patnik;
    66import mk.ukim.finki.busngo.model.entities.Vozac;
     7import mk.ukim.finki.busngo.model.enums.Role;
    78import mk.ukim.finki.busngo.model.enums.VrabotenType;
    89import mk.ukim.finki.busngo.model.exceptions.InvalidCredentialsException;
     
    128129    }
    129130
     131    @Override
     132    public Korisnik register(String name, String email, String password, String confirmPassword, String address, String phone, Role role, Double salary) {
     133        if (email == null || password == null || email.isEmpty() || password.isEmpty()) {
     134            throw new InvalidCredentialsException();
     135        }
     136
     137        if(!password.equals(confirmPassword)){
     138            throw new InvalidCredentialsException();
     139        }
     140
     141        if(role.equals(Role.ROLE_DRIVER)){
     142            Vozac vozac = new Vozac();
     143            vozac.setKIme(name);
     144            vozac.setKAdresa(address);
     145            vozac.setKLozinka(passwordEncoder.encode(password));
     146            vozac.setKEmail(email);
     147            vozac.setKTelefon(phone);
     148            vozac.setKIsAdmin(role.equals(Role.ROLE_ADMIN));
     149            vozac.setKRole(role);
     150            vozac.setVPlata(salary != null ? salary : 0.0);
     151            vozac.setVDatumNaVrabotuvanje(Date.valueOf(LocalDate.now()));
     152            this.korisnikRepository.save(vozac);
     153            return vozacRepository.save(vozac);
     154        } else if(role.equals(Role.ROLE_CONDUCTOR)){
     155            Kondukter kondukter = new Kondukter();
     156            kondukter.setKIme(name);
     157            kondukter.setKAdresa(address);
     158            kondukter.setKLozinka(passwordEncoder.encode(password));
     159            kondukter.setKEmail(email);
     160            kondukter.setKTelefon(phone);
     161            kondukter.setKIsAdmin(role.equals(Role.ROLE_ADMIN));
     162            kondukter.setKRole(role);
     163            kondukter.setVPlata(salary != null ? salary : 0.0);
     164            kondukter.setVDatumNaVrabotuvanje(Date.valueOf(LocalDate.now()));
     165            this.korisnikRepository.save(kondukter);
     166            return kondukterRepository.save(kondukter);
     167        } else {
     168            Patnik patnik = new Patnik();
     169            patnik.setKIme(name);
     170            patnik.setKAdresa(address);
     171            patnik.setKLozinka(passwordEncoder.encode(password));
     172            patnik.setKEmail(email);
     173            patnik.setKTelefon(phone);
     174            patnik.setKIsAdmin(role.equals(Role.ROLE_ADMIN));
     175            patnik.setKRole(role);
     176            this.korisnikRepository.save(patnik);
     177            return patnikRepository.save(patnik);
     178        }
     179    }
     180
    130181
    131182}
  • src/main/java/mk/ukim/finki/busngo/service/impl/KorisnikServiceImpl.java

    r4d026a5 r8b875e6  
    22
    33import mk.ukim.finki.busngo.model.entities.Korisnik;
     4import mk.ukim.finki.busngo.model.enums.Role;
    45import mk.ukim.finki.busngo.model.exceptions.InvalidUserId;
    56import mk.ukim.finki.busngo.repository.KorisnikRepository;
  • src/main/java/mk/ukim/finki/busngo/web/RegisterController.java

    r4d026a5 r8b875e6  
    22
    33import mk.ukim.finki.busngo.model.entities.Korisnik;
     4import mk.ukim.finki.busngo.model.enums.Role;
    45import mk.ukim.finki.busngo.model.enums.VrabotenType;
    56import mk.ukim.finki.busngo.model.exceptions.InvalidCredentialsException;
     
    3536        }
    3637
    37         model.addAttribute("types", VrabotenType.values());
     38        model.addAttribute("roles", Role.values());
    3839
    3940        model.addAttribute("bodyContent", "register");
     
    4849                           @RequestParam String address,
    4950                           @RequestParam String phone,
    50                            @RequestParam(required = false)VrabotenType vrabotenType,
     51                           @RequestParam Role role,
    5152                           @RequestParam(required = false) Double salary
    5253                           ) {
    5354        try{
    54             Korisnik korisnik;
    55             if(vrabotenType != null){
    56                  korisnik = this.authService.registerVraboten(name, email, password, confirmPassword, address, phone, vrabotenType, salary);
     55            Korisnik korisnik = authService.register(name, email, password, confirmPassword, address, phone, role, salary);
    5756
    58             }
    59             else
    60                 korisnik = this.authService.registerPatnik(name, email, password, confirmPassword, address, phone);
    6157            return "redirect:/login";
    6258        } catch (InvalidCredentialsException | UserAlreadyExistsException exception) {
  • src/main/resources/application.properties

    r4d026a5 r8b875e6  
    11#
    2 spring.datasource.url=jdbc:postgresql://localhost:8089/db_202324z_va_prj_busngo
     2spring.datasource.url=jdbc:postgresql://localhost:8081/db_202324z_va_prj_busngo
    33spring.datasource.username=db_202324z_va_prj_busngo_owner
    44spring.datasource.password=d9f924580e7
     
    1111#spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
    1212spring.jpa.properties.hibernate.jdbc.non_contextual_creation=true
    13 spring.jpa.properties.hibernate.default_schema=project
     13spring.jpa.properties.hibernate.default_schema=project_new
    1414
    1515spring.jpa.hibernate.ddl-auto=validate
  • src/main/resources/templates/fragments/header.html

    r4d026a5 r8b875e6  
    1010      <div class="collapse navbar-collapse justify-content-end" id="navbarsExampleDefault">
    1111        <ul class="navbar-nav m-auto">
    12           <li sec:hasRole class="nav-item m-auto">
     12          <li sec:authorize="hasRole('ROLE_PASSENGER')" class="nav-item m-auto">
    1313            <a class="nav-link active" href="/bilet">Bileti</a>
    1414          </li>
    15           <li class="nav-item m-auto">
     15          <li sec:authorize="hasAnyRole('ROLE_PASSENGER')" class="nav-item m-auto">
    1616            <a class="nav-link active" href="/vozenje">Vozenja</a>
    1717          </li>
    18           <li class="nav-item m-auto">
     18          <li sec:authorize="hasRole('ROLE_PASSENGER')" class="nav-item m-auto">
    1919            <a class="nav-link active" href="/kazna">Kazni</a>
    2020          </li>
    21           <li class="nav-item m-auto">
     21          <li sec:authorize="hasAnyRole('ROLE_CONDUCTOR', 'ROLE_DRIVER')" class="nav-item m-auto">
    2222            <a class="nav-link active" href="/instanca-na-linija">Instanci</a>
    2323          </li>
  • src/main/resources/templates/register.html

    r4d026a5 r8b875e6  
    2929      </p>
    3030
    31       <p sec:authorize="hasRole('ROLE_ADMIN')">
    32         <label for="vrabotenType" class="sr-only">Vraboten Type</label>
    33         <select name="vrabotenType" id="vrabotenType">
    34           <option th:each="type : ${types}"
    35                   th:text="${type.name()}"
    36                   th:value="${type}" ></option>
     31<!--      <p sec:authorize="hasRole('ROLE_ADMIN')">-->
     32        <label for="role" class="sr-only">Role</label>
     33        <select name="role" id="role">
     34          <option th:each="role : ${roles}"
     35                  th:text="${role.name()}"
     36                  th:value="${role}" ></option>
    3737        </select>
    38       </p>
     38<!--      </p>-->
    3939
    4040      <p sec:authorize="hasRole('ROLE_ADMIN')">
Note: See TracChangeset for help on using the changeset viewer.