Skip to content

Commit

Permalink
Add username
Browse files Browse the repository at this point in the history
GitOrigin-RevId: 202eb92d99fac782c2df0468b4cd7e4f9c92a2cb
  • Loading branch information
pdesgarets authored and Gitlab-CI committed Dec 4, 2024
1 parent 83e9bdb commit 0966ef1
Show file tree
Hide file tree
Showing 19 changed files with 782 additions and 57 deletions.
49 changes: 28 additions & 21 deletions back/.gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -1,28 +1,42 @@
variables:
MAVEN_OPTS: "-Dmaven.repo.local=.m2"

image: openjdk:17
image: eclipse-temurin:17

build_job:
stage: build
lint:
tags:
- docker
script:
- ./mvnw clean compile -DskipTests
artifacts:
expire_in: 1 hour
paths:
- target/
- .m2/
- ./mvnw checkstyle:check
except:
- tags
- master

lint:
stage: test
test:
variables:
SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/mydocker
SPRING_DATASOURCE_USERNAME: mydocker
SPRING_DATASOURCE_PASSWORD: mydocker
services:
- name: postgres:13
alias: postgres
entrypoint:
- /bin/sh
command:
- -c
- echo "CREATE EXTENSION citext;" > /docker-entrypoint-initdb.d/citext.sql && exec docker-entrypoint.sh postgres
variables:
POSTGRES_DB: mydocker
POSTGRES_USER: mydocker
POSTGRES_PASSWORD: mydocker
tags:
- docker
dependencies:
- build_job
before_script:
- openssl genrsa -traditional -out src/main/resources/private_key.pem 4096
- openssl rsa -traditional -pubout -in src/main/resources/private_key.pem -out src/main/resources/public_key.pem
script:
- ./mvnw checkstyle:check
- ./mvnw generate-sources
- ./mvnw test || { [ "$DEBUG" = "true" ] && sleep 3600; }
except:
- tags
- master
Expand All @@ -31,8 +45,6 @@ lint:
stage: deploy
tags:
- docker
dependencies:
- build_job
script:
- "[[ -z \"${CI_COMMIT_TAG}\" ]] && export VERSION=\"${CI_COMMIT_SHA}-SNAPSHOT\" || export VERSION=\"${CI_COMMIT_TAG}\""
- "echo \"Setting version to: ${VERSION}\""
Expand Down Expand Up @@ -69,8 +81,3 @@ deploy_github:
- gh release --repo CentraleSupelec/mydocker upload back-${CI_COMMIT_TAG} artifacts.zip
tags:
- docker

stages:
- build
- test
- deploy
4 changes: 3 additions & 1 deletion back/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@ services:
environment:
POSTGRES_USER: thuv
POSTGRES_PASSWORD: password
TEST_DB: thuvtest
volumes:
- $PWD/docker/citext.sql:/docker-entrypoint-initdb.d/citext.sql
- $PWD/docker/setup.sh:/docker-entrypoint-initdb.d/setup.sh
- db-data:/var/lib/postgresql/data

cas:
image: apereo/cas:7.0.1
environment:
CAS_SERVICE_REGISTRY_CORE_INIT_FROM_JSON: "true"
CAS_LOGOUT_FOLLOW_SERVICE_REDIRECTS: "true"
SERVER_SSL_ENABLED: "false"
volumes:
- $PWD/docker/cas-service-1.json:/tmp/cas/cas-service-1.json
Expand Down
1 change: 0 additions & 1 deletion back/docker/citext.sql

This file was deleted.

10 changes: 10 additions & 0 deletions back/docker/setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/sh
set -e

psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" -c "CREATE EXTENSION IF NOT EXISTS citext;"
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" <<-EOSQL
CREATE USER $TEST_DB;
CREATE DATABASE $TEST_DB;
GRANT ALL PRIVILEGES ON DATABASE $TEST_DB TO $TEST_DB;
EOSQL
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$TEST_DB" -c "CREATE EXTENSION IF NOT EXISTS citext;"
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package fr.centralesupelec.thuv.exception;

public class UserUpsertException extends RuntimeException {
public UserUpsertException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ public ResponseEntity<String> launch(
DecodedJWT jwt = this.ltiService.getValidatedJWT(idToken, state, origin, session);

User user = myUserDetailsService.upsertUser(
null,
jwt.getClaim("email").asString(),
jwt.getClaim("given_name").asString(),
jwt.getClaim("family_name").asString()
Expand Down
12 changes: 11 additions & 1 deletion back/src/main/java/fr/centralesupelec/thuv/model/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
import lombok.EqualsAndHashCode;
import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import lombok.ToString;

import java.util.*;

@Entity
@Table(name = "users", indexes = {
@Index(columnList = "email", unique = true)
@Index(columnList = "username", unique = true)
})
@Data
@EqualsAndHashCode(of = {"id"})
Expand All @@ -23,19 +24,26 @@ public class User {
@Column(columnDefinition = "CITEXT")
private String email;

@NotBlank
@Column(columnDefinition = "CITEXT")
private String username;

@NotBlank
private String name;

@NotBlank
private String lastname;

private Boolean enabled = true;

public Set<UserCourse> getUserCourses() {
return userCourses;
}

/*
* Relationships
*/
@ToString.Exclude
@OneToMany(
cascade = { CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.MERGE },
fetch = FetchType.LAZY,
Expand All @@ -56,6 +64,7 @@ public Set<UserCourse> getUserCourses() {
)
private Collection<Role> roles = new ArrayList<>();

@ToString.Exclude
@OneToMany(
fetch = FetchType.LAZY,
mappedBy = "creator",
Expand Down Expand Up @@ -97,6 +106,7 @@ public void removeUserCourse(UserCourse userCourse) {
userCourse.setUser(null);
}

@ToString.Exclude
@OneToMany(mappedBy = "user", fetch = FetchType.LAZY)
private List<ActivityLogRecord> logs = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@ public class UpdateUserDto {
private String lastname;
@Pattern(regexp = "ROLE_TEACHER|ROLE_ADMIN")
private String role;
private Boolean enabled;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
public class UserDto {
private Long id;
private String email;
private String username;
private String name;
private String lastname;
private String role;
private Boolean enabled;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ public UserDto convertToDto(User user) {
.setEmail(
user.getEmail()
)
.setUsername(
user.getUsername()
)
.setEnabled(
user.getEnabled()
)
.setRole(
user.getRoles().stream().findAny().orElseGet(Role::new).getName()
);
Expand All @@ -36,12 +42,18 @@ public void applyChange(User user, UpdateUserDto updateUserDto) {
.setEmail(
updateUserDto.getEmail()
)
.setUsername(
user.getUsername()
)
.setLastname(
updateUserDto.getLastname()
)
.setName(
updateUserDto.getName()
);
if (updateUserDto.getEnabled() != null) {
user.setEnabled(updateUserDto.getEnabled());
}
user.getRoles().clear();
user.getRoles().add(
roleRepository.getByName(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
@Repository
@Transactional(readOnly = true)
public interface UserRepository extends JpaRepository<User, Long> {
Optional<User> findByEmail(String email);
List<User> findByEmailIn(Collection<String> emails);
Optional<User> findByUsername(String email);
Optional<User> findByEnabledTrueAndUsername(String email);
List<User> findByEnabledTrueAndEmail(String email);
List<User> findDistinctByEmailContainingAndRolesNameIsNot(@NotBlank String email, String rolesName);
Page<User> findDistinctByEmailContainingAndRolesNameIn(
@NotBlank String email, Collection<String> rolesName, Pageable pageable
Expand Down
Loading

0 comments on commit 0966ef1

Please sign in to comment.