Browse Source

Adding auth db schema

main
ddangelorb 3 years ago
parent
commit
e53925eeb1
  1. 6
      README.md
  2. 30
      db/ddl.sql
  3. 22
      db/sys_config.sql
  4. 6
      load/README.md
  5. 4
      src/main/java/org/waterproofingdata/wpdauth/repository/EduCemadenOrganizationsRepository.java
  6. 2
      src/main/java/org/waterproofingdata/wpdauth/repository/ForgotPasswordsKeysRepository.java
  7. 2
      src/main/java/org/waterproofingdata/wpdauth/repository/ForgotPasswordsQuestionsUsersAnswersRepository.java
  8. 2
      src/main/java/org/waterproofingdata/wpdauth/repository/UsersRepository.java
  9. 15
      src/main/resources/application.properties

6
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 '<your really secure 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
```

30
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)
);

22
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 $$;

6
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 '/<absolute path>/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 '/<absolute path>/educacao.cemaden-organization-dump.csv' DELIMITER ',' CSV HEADER;"
$ psql -d wpdauth -c "SELECT * FROM auth.educemaden_organizations;"
```

4
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, Integer> {
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);
}

2
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<ForgotPasswordsKeys, Integer> {
@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);
}

2
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<ForgotPasswordsQuestionsUsersAnswers, Integer> {
@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);
}

2
src/main/java/org/waterproofingdata/wpdauth/repository/UsersRepository.java

@ -17,6 +17,6 @@ public interface UsersRepository extends JpaRepository<Users, Integer> {
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);
}

15
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=<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=<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=<host>
spring.mail.port=<port>
spring.mail.username=<username>
spring.mail.password=<password>
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

Loading…
Cancel
Save