diff --git a/README.md b/README.md index 2227cac..b93aee0 100644 --- a/README.md +++ b/README.md @@ -37,9 +37,15 @@ Once the dependencies are properly installed, follow the steps below: postgres=# \conninfo postgres=# CREATE DATABASE wpdauth; postgres=# \c wpdauth + wpdauth=# CREATE SCHEMA auth; wpdauth=# CREATE EXTENSION "uuid-ossp"; wpdauth=# \i db/ddl.sql wpdauth=# \i db/sys_config.sql + wpdauth=# create user uwpdauth; + wpdauth=# alter user uwpdauth with encrypted password ''; + wpdauth=# GRANT USAGE ON SCHEMA auth TO uwpdauth; + wpdauth=# GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA auth TO uwpdauth; + wpdauth=# GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA auth TO uwpdauth; wpdauth=# \q ``` diff --git a/db/ddl.sql b/db/ddl.sql index 2066019..62a5368 100644 --- a/db/ddl.sql +++ b/db/ddl.sql @@ -1,4 +1,4 @@ -CREATE TABLE IF NOT EXISTS users ( +CREATE TABLE IF NOT EXISTS auth.users ( id SERIAL PRIMARY KEY, username VARCHAR(255) UNIQUE NOT NULL, password VARCHAR(255) NOT NULL, @@ -9,50 +9,50 @@ CREATE TABLE IF NOT EXISTS users ( active INT NOT NULL ); -CREATE TABLE IF NOT EXISTS forgotpassword_keys ( +CREATE TABLE IF NOT EXISTS auth.forgotpassword_keys ( id SERIAL PRIMARY KEY, email VARCHAR(255) NOT NULL, key VARCHAR(4) NOT NULL, created_at TIMESTAMP DEFAULT NOW() ); -CREATE TABLE IF NOT EXISTS forgotpassword_questions ( +CREATE TABLE IF NOT EXISTS auth.forgotpassword_questions ( id SERIAL PRIMARY KEY, question VARCHAR(255) NOT NULL, active INT NOT NULL ); -CREATE TABLE IF NOT EXISTS forgotpassword_questions_users_answers ( +CREATE TABLE IF NOT EXISTS auth.forgotpassword_questions_users_answers ( id SERIAL PRIMARY KEY, forgotpassword_questions_id INT NOT NULL, users_id INT NOT NULL, answer VARCHAR(255) NOT NULL, - FOREIGN KEY (forgotpassword_questions_id) REFERENCES forgotpassword_questions (id), - FOREIGN KEY (users_id) REFERENCES users (id) + FOREIGN KEY (forgotpassword_questions_id) REFERENCES auth.forgotpassword_questions (id), + FOREIGN KEY (users_id) REFERENCES auth.users (id) ); -CREATE TABLE IF NOT EXISTS roles ( +CREATE TABLE IF NOT EXISTS auth.roles ( id SERIAL PRIMARY KEY, name VARCHAR(100) NOT NULL, active INT NOT NULL ); -CREATE TABLE IF NOT EXISTS users_roles ( +CREATE TABLE IF NOT EXISTS auth.users_roles ( users_id INT NOT NULL, roles INT NOT NULL, - FOREIGN KEY (users_id) REFERENCES users (id) + FOREIGN KEY (users_id) REFERENCES auth.users (id) ); -CREATE TABLE IF NOT EXISTS users_rolesprovider_activationkey ( +CREATE TABLE IF NOT EXISTS auth.users_rolesprovider_activationkey ( id SERIAL PRIMARY KEY, users_id INT NOT NULL, roles_id INT NOT NULL, activationkey uuid NOT NULL, - FOREIGN KEY (users_id) REFERENCES users (id), - FOREIGN KEY (roles_id) REFERENCES roles (id) + FOREIGN KEY (users_id) REFERENCES auth.users (id), + FOREIGN KEY (roles_id) REFERENCES auth.roles (id) ); -CREATE TABLE IF NOT EXISTS educemaden_organizations ( +CREATE TABLE IF NOT EXISTS auth.educemaden_organizations ( id INT NOT NULL, active VARCHAR(20) NULL, name VARCHAR(255) NOT NULL, @@ -66,10 +66,10 @@ CREATE TABLE IF NOT EXISTS educemaden_organizations ( responsible varchar(50) NULL ); -CREATE TABLE IF NOT EXISTS users_educemaden_organizations ( +CREATE TABLE IF NOT EXISTS auth.users_educemaden_organizations ( id SERIAL PRIMARY KEY, users_id INT NOT NULL, educemaden_organizations_id INT NOT NULL, activationkey uuid NOT NULL, - FOREIGN KEY (users_id) REFERENCES users (id) + FOREIGN KEY (users_id) REFERENCES auth.users (id) ); diff --git a/db/sys_config.sql b/db/sys_config.sql index 9bfcb6a..4bca27d 100644 --- a/db/sys_config.sql +++ b/db/sys_config.sql @@ -1,15 +1,15 @@ DO $$ BEGIN - INSERT INTO roles(name, active) VALUES ('ROLE_ADMIN', 1); - INSERT INTO roles(name, active) VALUES ('ROLE_INSTITUTION', 1); - INSERT INTO roles(name, active) VALUES ('ROLE_CLIENT', 1); + INSERT INTO auth.roles(name, active) VALUES ('ROLE_ADMIN', 1); + INSERT INTO auth.roles(name, active) VALUES ('ROLE_INSTITUTION', 1); + INSERT INTO auth.roles(name, active) VALUES ('ROLE_CLIENT', 1); - INSERT INTO forgotpassword_questions(question, active) VALUES ('Qual a sua cor predileta?', 1); - INSERT INTO forgotpassword_questions(question, active) VALUES ('Qual foi o seu livro predileto?', 1); - INSERT INTO forgotpassword_questions(question, active) VALUES ('Qual o nome da rua em que você cresceu?', 1); - INSERT INTO forgotpassword_questions(question, active) VALUES ('Qual o nome do seu bicho de estimação predileto?', 1); - INSERT INTO forgotpassword_questions(question, active) VALUES ('Qual a sua comida predileta?', 1); - INSERT INTO forgotpassword_questions(question, active) VALUES ('Qual cidade você nasceu?', 1); - INSERT INTO forgotpassword_questions(question, active) VALUES ('Qual é o seu país preferido?', 1); - INSERT INTO forgotpassword_questions(question, active) VALUES ('Qual é a sua marca de carro predileto?', 1); + INSERT INTO auth.forgotpassword_questions(question, active) VALUES ('Qual a sua cor predileta?', 1); + INSERT INTO auth.forgotpassword_questions(question, active) VALUES ('Qual foi o seu livro predileto?', 1); + INSERT INTO auth.forgotpassword_questions(question, active) VALUES ('Qual o nome da rua em que você cresceu?', 1); + INSERT INTO auth.forgotpassword_questions(question, active) VALUES ('Qual o nome do seu bicho de estimação predileto?', 1); + INSERT INTO auth.forgotpassword_questions(question, active) VALUES ('Qual a sua comida predileta?', 1); + INSERT INTO auth.forgotpassword_questions(question, active) VALUES ('Qual cidade você nasceu?', 1); + INSERT INTO auth.forgotpassword_questions(question, active) VALUES ('Qual é o seu país preferido?', 1); + INSERT INTO auth.forgotpassword_questions(question, active) VALUES ('Qual é a sua marca de carro predileto?', 1); END $$; diff --git a/load/README.md b/load/README.md index af95474..6886ff8 100644 --- a/load/README.md +++ b/load/README.md @@ -12,7 +12,7 @@ Once the project setup was finished successfully, follow the steps below: - Start the PostgreSQL and run the scripts to create the database and get the load data. ```console - $ psql -d wpdauth -c "TRUNCATE TABLE educemaden_organizations;" - $ psql -d wpdauth -c "COPY educemaden_organizations FROM '//educacao.cemaden-organization-dump.csv' DELIMITER ',' CSV HEADER;" - $ psql -d wpdauth -c "SELECT * FROM educemaden_organizations;" + $ psql -d wpdauth -c "TRUNCATE TABLE auth.educemaden_organizations;" + $ psql -d wpdauth -c "COPY auth.educemaden_organizations FROM '//educacao.cemaden-organization-dump.csv' DELIMITER ',' CSV HEADER;" + $ psql -d wpdauth -c "SELECT * FROM auth.educemaden_organizations;" ``` \ No newline at end of file diff --git a/src/main/java/org/waterproofingdata/wpdauth/repository/EduCemadenOrganizationsRepository.java b/src/main/java/org/waterproofingdata/wpdauth/repository/EduCemadenOrganizationsRepository.java index ea51f49..be6c1fd 100644 --- a/src/main/java/org/waterproofingdata/wpdauth/repository/EduCemadenOrganizationsRepository.java +++ b/src/main/java/org/waterproofingdata/wpdauth/repository/EduCemadenOrganizationsRepository.java @@ -10,10 +10,10 @@ import org.waterproofingdata.wpdauth.model.EduCemadenOrganizations; public interface EduCemadenOrganizationsRepository extends JpaRepository { EduCemadenOrganizations findByPhone(String phone); - @Query(value = "SELECT e.*, ueo.activationkey FROM educemaden_organizations e INNER JOIN users_educemaden_organizations ueo ON e.id = ueo.educemaden_organizations_id WHERE ueo.users_id = ?1", nativeQuery = true) + @Query(value = "SELECT e.*, ueo.activationkey FROM auth.educemaden_organizations e INNER JOIN auth.users_educemaden_organizations ueo ON e.id = ueo.educemaden_organizations_id WHERE ueo.users_id = ?1", nativeQuery = true) EduCemadenOrganizations findByUserId(Integer userid); @Transactional - @Query(value = "INSERT INTO users_educemaden_organizations(id, users_id, educemaden_organizations_id, activationkey) VALUES (DEFAULT, ?1, ?2, ?3)", nativeQuery = true) + @Query(value = "INSERT INTO auth.users_educemaden_organizations(id, users_id, educemaden_organizations_id, activationkey) VALUES (DEFAULT, ?1, ?2, ?3)", nativeQuery = true) void insertUsersEduCemadenOrganizations(Integer userid, Integer eduCemadenOrganizationsid, String activationkey); } diff --git a/src/main/java/org/waterproofingdata/wpdauth/repository/ForgotPasswordsKeysRepository.java b/src/main/java/org/waterproofingdata/wpdauth/repository/ForgotPasswordsKeysRepository.java index 9048f53..63175ae 100644 --- a/src/main/java/org/waterproofingdata/wpdauth/repository/ForgotPasswordsKeysRepository.java +++ b/src/main/java/org/waterproofingdata/wpdauth/repository/ForgotPasswordsKeysRepository.java @@ -5,6 +5,6 @@ import org.springframework.data.jpa.repository.Query; import org.waterproofingdata.wpdauth.model.ForgotPasswordsKeys; public interface ForgotPasswordsKeysRepository extends JpaRepository { - @Query(value = "SELECT f.* FROM forgotpassword_keys f WHERE f.email = ?1 AND f.key = ?2 AND f.created_at >= current_date::timestamp AND f.created_at < current_date::timestamp + interval '1 day' ORDER BY f.id DESC LIMIT 1", nativeQuery = true) + @Query(value = "SELECT f.* FROM auth.forgotpassword_keys f WHERE f.email = ?1 AND f.key = ?2 AND f.created_at >= current_date::timestamp AND f.created_at < current_date::timestamp + interval '1 day' ORDER BY f.id DESC LIMIT 1", nativeQuery = true) ForgotPasswordsKeys findTodayRecordByEmailANDKey(String email, String key); } diff --git a/src/main/java/org/waterproofingdata/wpdauth/repository/ForgotPasswordsQuestionsUsersAnswersRepository.java b/src/main/java/org/waterproofingdata/wpdauth/repository/ForgotPasswordsQuestionsUsersAnswersRepository.java index c3b17e2..ee1719f 100644 --- a/src/main/java/org/waterproofingdata/wpdauth/repository/ForgotPasswordsQuestionsUsersAnswersRepository.java +++ b/src/main/java/org/waterproofingdata/wpdauth/repository/ForgotPasswordsQuestionsUsersAnswersRepository.java @@ -5,6 +5,6 @@ import org.springframework.data.jpa.repository.Query; import org.waterproofingdata.wpdauth.model.ForgotPasswordsQuestionsUsersAnswers; public interface ForgotPasswordsQuestionsUsersAnswersRepository extends JpaRepository { - @Query(value = "SELECT fqua.* FROM forgotpassword_questions_users_answers fqua WHERE fqua.forgotpassword_questions_id = ?1 AND fqua.users_id = ?2", nativeQuery = true) + @Query(value = "SELECT fqua.* FROM auth.forgotpassword_questions_users_answers fqua WHERE fqua.forgotpassword_questions_id = ?1 AND fqua.users_id = ?2", nativeQuery = true) ForgotPasswordsQuestionsUsersAnswers findByForgotPasswordQuestionsAndUserid(Integer forgotpasswordquestionsid, Integer usersid); } diff --git a/src/main/java/org/waterproofingdata/wpdauth/repository/UsersRepository.java b/src/main/java/org/waterproofingdata/wpdauth/repository/UsersRepository.java index 1d41e30..49df281 100644 --- a/src/main/java/org/waterproofingdata/wpdauth/repository/UsersRepository.java +++ b/src/main/java/org/waterproofingdata/wpdauth/repository/UsersRepository.java @@ -17,6 +17,6 @@ public interface UsersRepository extends JpaRepository { Users findByEmail(String email); @Transactional - @Query(value = "UPDATE users SET active = ?2 WHERE username = ?1", nativeQuery = true) + @Query(value = "UPDATE auth.users SET active = ?2 WHERE username = ?1", nativeQuery = true) void activateByUsername(String username, Integer active); } diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties index 4fe772e..02e19ea 100644 --- a/src/main/resources/application.properties +++ b/src/main/resources/application.properties @@ -4,21 +4,22 @@ spring.datasource.hikari.maximumPoolSize=5 ## PostgreSQL spring.datasource.url=jdbc:postgresql://localhost:5432/wpdauth -spring.datasource.username=username -spring.datasource.password=password +spring.datasource.username=uwpdauth +spring.datasource.password= spring.jpa.hibernate.ddl-auto=none +spring.jpa.properties.hibernate.default_schema=auth server.port=8080 -security.jwt.token.secret-key=secret-key +security.jwt.token.secret-key= ##5 minutes duration by default: 5 minutes * 60 seconds * 1000 miliseconds security.jwt.token.expire-length=300000 -spring.mail.host=smtp.gmail.com -spring.mail.port=587 -spring.mail.username=username@gmail.com -spring.mail.password=password +spring.mail.host= +spring.mail.port= +spring.mail.username= +spring.mail.password= spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.enable=true