Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IP filtering #102

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions create_all_tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
CREATE OR REPLACE FUNCTION update_modified_timestamp() RETURNS TRIGGER LANGUAGE plpgsql AS
$$
BEGIN
new.updated_at := current_timestamp;
RETURN new;
END;
$$;

CREATE TABLE domains(
domain_id SERIAL PRIMARY KEY,
domain_name VARCHAR(250) NOT NULL UNIQUE,
forum_name VARCHAR(250) NOT NULL DEFAULT 'Orange Forum',
no_regular_signup_msg VARCHAR(250) NOT NULL DEFAULT '',
signup_token VARCHAR(250) NOT NULL DEFAULT '',
edit_window INTEGER DEFAULT 20,
auto_topic_close_days INTEGER DEFAULT 60,
user_activity_window INTEGER DEFAULT 3,
max_num_activity INTEGER DEFAULT 20,
is_regular_signup_enabled BOOL NOT NULL DEFAULT false,
is_readonly BOOL NOT NULL DEFAULT false,
enable_group_sub BOOL NOT NULL DEFAULT false,
enable_topic_autosub BOOL NOT NULL DEFAULT false,
enable_comment_autosub BOOL NOT NULL DEFAULT false,
archived_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT current_timestamp,
updated_at TIMESTAMPTZ NOT NULL DEFAULT current_timestamp);

CREATE TRIGGER update_timestamp BEFORE UPDATE ON domains FOR EACH ROW EXECUTE PROCEDURE update_modified_timestamp();
CREATE UNIQUE INDEX domains_domain_index ON domains(domain_name);

CREATE TABLE users(
user_id SERIAL PRIMARY KEY,
domain_id INTEGER NOT NULL REFERENCES domains(domain_id) ON DELETE CASCADE,
email VARCHAR(250) NOT NULL,
username VARCHAR(32) NOT NULL,
passwd_hash VARCHAR(250) NOT NULL,
about TEXT NOT NULL DEFAULT '',
is_superadmin BOOL NOT NULL DEFAULT false,
is_topic_autosubscribe BOOL NOT NULL DEFAULT true,
is_comment_autosubscribe BOOL NOT NULL DEFAULT true,
is_email_notifications_disabled BOOL NOT NULL DEFAULT false,
num_topics INTEGER NOT NULL DEFAULT 0,
num_comments INTEGER NOT NULL DEFAULT 0,
num_activity INTEGER NOT NULL DEFAULT 0,
onetime_login_token VARCHAR(250) NOT NULL DEFAULT '',
onetime_login_token_at TIMESTAMPTZ NOT NULL DEFAULT to_timestamp(0),
reset_token VARCHAR(250) NOT NULL DEFAULT '',
last_ip VARCHAR(50) NOT NULL DEFAULT '',
activity_at TIMESTAMPTZ NOT NULL DEFAULT current_timestamp,
reset_at TIMESTAMPTZ NOT NULL DEFAULT to_timestamp(0),
logout_at TIMESTAMPTZ NOT NULL DEFAULT to_timestamp(0),
banned_at TIMESTAMPTZ,
archived_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT current_timestamp,
updated_at TIMESTAMPTZ NOT NULL DEFAULT current_timestamp);

CREATE TRIGGER update_timestamp BEFORE UPDATE ON users FOR EACH ROW EXECUTE PROCEDURE update_modified_timestamp();
CREATE UNIQUE INDEX users_domain_username_index ON users(domain_id, username);
CREATE UNIQUE INDEX users_domain_email_index ON users(domain_id, email);
CREATE INDEX users_otp_token_index ON users(onetime_login_token);
CREATE INDEX users_reset_token_index ON users(reset_token);
CREATE INDEX users_created_index ON users(created_at);


CREATE TABLE configs(
name VARCHAR(250) NOT NULL PRIMARY KEY,
val VARCHAR(250) NOT NULL DEFAULT '');

INSERT INTO configs(name, val) VALUES('` + DBVersion + `', '1');

CREATE TABLE bannedips (
id SERIAL PRIMARY KEY,
domain_id INTEGER NOT NULL REFERENCES domains(domain_id) ON DELETE CASCADE,
ip VARCHAR(50) NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT current_timestamp
);
94 changes: 94 additions & 0 deletions create_tables_and_insert_test_data_in_db.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
DROP TABLE IF EXISTS configs;
DROP TABLE IF EXISTS bannedips;
DROP TABLE IF EXISTS users;
DROP TABLE IF EXISTS domains;

CREATE OR REPLACE FUNCTION update_modified_timestamp() RETURNS TRIGGER LANGUAGE plpgsql AS
$$
BEGIN
new.updated_at := current_timestamp;
RETURN new;
END;
$$;

CREATE TABLE domains(
domain_id SERIAL PRIMARY KEY,
domain_name VARCHAR(250) NOT NULL UNIQUE,
forum_name VARCHAR(250) NOT NULL DEFAULT 'Orange Forum',
no_regular_signup_msg VARCHAR(250) NOT NULL DEFAULT '',
signup_token VARCHAR(250) NOT NULL DEFAULT '',
edit_window INTEGER DEFAULT 20,
auto_topic_close_days INTEGER DEFAULT 60,
user_activity_window INTEGER DEFAULT 3,
max_num_activity INTEGER DEFAULT 20,
is_regular_signup_enabled BOOL NOT NULL DEFAULT false,
is_readonly BOOL NOT NULL DEFAULT false,
enable_group_sub BOOL NOT NULL DEFAULT false,
enable_topic_autosub BOOL NOT NULL DEFAULT false,
enable_comment_autosub BOOL NOT NULL DEFAULT false,
archived_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT current_timestamp,
updated_at TIMESTAMPTZ NOT NULL DEFAULT current_timestamp);

CREATE TRIGGER update_timestamp BEFORE UPDATE ON domains FOR EACH ROW EXECUTE PROCEDURE update_modified_timestamp();
CREATE UNIQUE INDEX domains_domain_index ON domains(domain_name);

CREATE TABLE users(
user_id SERIAL PRIMARY KEY,
domain_id INTEGER NOT NULL REFERENCES domains(domain_id) ON DELETE CASCADE,
email VARCHAR(250) NOT NULL,
username VARCHAR(32) NOT NULL,
passwd_hash VARCHAR(250) NOT NULL,
about TEXT NOT NULL DEFAULT '',
is_superadmin BOOL NOT NULL DEFAULT false,
is_topic_autosubscribe BOOL NOT NULL DEFAULT true,
is_comment_autosubscribe BOOL NOT NULL DEFAULT true,
is_email_notifications_disabled BOOL NOT NULL DEFAULT false,
num_topics INTEGER NOT NULL DEFAULT 0,
num_comments INTEGER NOT NULL DEFAULT 0,
num_activity INTEGER NOT NULL DEFAULT 0,
onetime_login_token VARCHAR(250) NOT NULL DEFAULT '',
onetime_login_token_at TIMESTAMPTZ NOT NULL DEFAULT to_timestamp(0),
reset_token VARCHAR(250) NOT NULL DEFAULT '',
last_ip VARCHAR(50) NOT NULL DEFAULT '',
activity_at TIMESTAMPTZ NOT NULL DEFAULT current_timestamp,
reset_at TIMESTAMPTZ NOT NULL DEFAULT to_timestamp(0),
logout_at TIMESTAMPTZ NOT NULL DEFAULT to_timestamp(0),
banned_at TIMESTAMPTZ,
archived_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT current_timestamp,
updated_at TIMESTAMPTZ NOT NULL DEFAULT current_timestamp);

CREATE TRIGGER update_timestamp BEFORE UPDATE ON users FOR EACH ROW EXECUTE PROCEDURE update_modified_timestamp();
CREATE UNIQUE INDEX users_domain_username_index ON users(domain_id, username);
CREATE UNIQUE INDEX users_domain_email_index ON users(domain_id, email);
CREATE INDEX users_otp_token_index ON users(onetime_login_token);
CREATE INDEX users_reset_token_index ON users(reset_token);
CREATE INDEX users_created_index ON users(created_at);


CREATE TABLE configs(
name VARCHAR(250) NOT NULL PRIMARY KEY,
val VARCHAR(250) NOT NULL DEFAULT '');

INSERT INTO configs(name, val) VALUES('` + DBVersion + `', '1');

CREATE TABLE bannedips (
id SERIAL PRIMARY KEY,
domain_id INTEGER NOT NULL REFERENCES domains(domain_id) ON DELETE CASCADE,
ip VARCHAR(50) NOT NULL,
created_at TIMESTAMPTZ NOT NULL DEFAULT current_timestamp
);

--####################INSERTING TEST DATA #####################
INSERT INTO domains(domain_name,forum_name,archived_at)VALUES('domain1','forum1',current_timestamp);
INSERT INTO domains(domain_name,forum_name,archived_at)VALUES('domain2','forum2',current_timestamp);
INSERT INTO domains(domain_name,forum_name,archived_at)VALUES('domain3','forum3',current_timestamp);

INSERT INTO users(domain_id,email,username,passwd_hash,onetime_login_token)VALUES(1,'user1@example.com','user1','pass1','token1');
INSERT INTO users(domain_id,email,username,passwd_hash,onetime_login_token)VALUES(2,'user2@example.com','user2','pass2','token2');
INSERT INTO users(domain_id,email,username,passwd_hash,onetime_login_token)VALUES(3,'user3@example.com','user3','pass3','token3');

INSERT INTO bannedips(domain_id,ip)VALUES(1,'192.168.1.1');
INSERT INTO bannedips(domain_id,ip)VALUES(2,'10.23.45.56');
INSERT INTO bannedips(domain_id,ip)VALUES(3,'134.45.67.187');
4 changes: 4 additions & 0 deletions drop_all_tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DROP TABLE IF EXISTS configs;
DROP TABLE IF EXISTS bannedips;
DROP TABLE IF EXISTS users;
DROP TABLE IF EXISTS domains;
19 changes: 9 additions & 10 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ module github.com/s-gv/orangeforum
go 1.16

require (
github.com/alexedwards/scs/v2 v2.4.0 // indirect
github.com/go-chi/chi v1.5.2 // indirect
github.com/go-chi/jwtauth v1.2.0 // indirect
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b // indirect
github.com/google/uuid v1.2.0 // indirect
github.com/gorilla/csrf v1.7.0 // indirect
github.com/jackc/pgx/v4 v4.11.0 // indirect
github.com/alexedwards/scs/v2 v2.4.0
github.com/go-chi/chi v1.5.2
github.com/go-chi/jwtauth v1.2.0
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b
github.com/google/uuid v1.2.0
github.com/gorilla/csrf v1.7.0
github.com/jackc/pgx/v4 v4.11.0
github.com/jmoiron/sqlx v1.3.1
github.com/lestrrat-go/jwx v1.1.0 // indirect
github.com/mattn/go-sqlite3 v1.14.6 // indirect
golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e // indirect
github.com/lestrrat-go/jwx v1.1.0
golang.org/x/crypto v0.0.0-20210513164829-c07d793c2f9a
)
Loading