larxius commited on
Commit
e56ad53
·
verified ·
1 Parent(s): ba5066d

Update database/database.sql

Browse files
Files changed (1) hide show
  1. database/database.sql +315 -0
database/database.sql ADDED
@@ -0,0 +1,315 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ -- ====================================================================
2
+ -- LarShield Master Production Database SQL Script
3
+ -- Includes: Complete Table Schemas, Constraints, Indexes & Seed Data
4
+ -- Database Engine: PostgreSQL / Universal SQL
5
+ -- Generated: 2026-08-30
6
+ -- ====================================================================
7
+
8
+ SET statement_timeout = 0;
9
+ SET lock_timeout = 0;
10
+ SET client_encoding = 'UTF8';
11
+ SET standard_conforming_strings = on;
12
+ SELECT pg_catalog.set_config('search_path', '', false);
13
+ SET check_function_bodies = false;
14
+ SET client_min_messages = warning;
15
+ SET row_security = off;
16
+
17
+ -- ====================================================================
18
+ -- SECTION 1: DROP TABLES (Clean Re-initialization if needed)
19
+ -- ====================================================================
20
+ DROP TABLE IF EXISTS public.reports CASCADE;
21
+ DROP TABLE IF EXISTS public.vulnerabilities CASCADE;
22
+ DROP TABLE IF EXISTS public.scheduled_scans CASCADE;
23
+ DROP TABLE IF EXISTS public.scans CASCADE;
24
+ DROP TABLE IF EXISTS public.alert_settings CASCADE;
25
+ DROP TABLE IF EXISTS public.organization_scan_quotas CASCADE;
26
+ DROP TABLE IF EXISTS public.payments CASCADE;
27
+ DROP TABLE IF EXISTS public.audit_logs CASCADE;
28
+ DROP TABLE IF EXISTS public.email_logs CASCADE;
29
+ DROP TABLE IF EXISTS public.users CASCADE;
30
+ DROP TABLE IF EXISTS public.roles CASCADE;
31
+ DROP TABLE IF EXISTS public.organizations CASCADE;
32
+ DROP TABLE IF EXISTS public.subscription_tiers CASCADE;
33
+ DROP TABLE IF EXISTS public.demo_bookings CASCADE;
34
+
35
+ -- ====================================================================
36
+ -- SECTION 2: CREATE TABLE SCHEMAS
37
+ -- ====================================================================
38
+
39
+ -- 1. Subscription Tiers
40
+ CREATE TABLE public.subscription_tiers (
41
+ id character varying(50) NOT NULL PRIMARY KEY,
42
+ name character varying(100) NOT NULL,
43
+ monthly_price integer DEFAULT 0 NOT NULL,
44
+ yearly_price integer DEFAULT 0 NOT NULL
45
+ );
46
+
47
+ -- 2. Organizations
48
+ CREATE TABLE public.organizations (
49
+ id character varying(36) NOT NULL PRIMARY KEY,
50
+ name character varying(150) NOT NULL,
51
+ subscription_tier character varying(50) DEFAULT 'free'::character varying NOT NULL REFERENCES public.subscription_tiers(id),
52
+ status character varying(50) DEFAULT 'active'::character varying,
53
+ api_key character varying(100) UNIQUE,
54
+ webhook_url character varying(500),
55
+ report_logo_url character varying(500),
56
+ created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
57
+ );
58
+
59
+ -- 3. Roles
60
+ CREATE TABLE public.roles (
61
+ id character varying(36) NOT NULL PRIMARY KEY,
62
+ name character varying(50) NOT NULL UNIQUE
63
+ );
64
+
65
+ -- 4. Users (Includes Policy Acceptance & Security Fields)
66
+ CREATE TABLE public.users (
67
+ id character varying(36) NOT NULL PRIMARY KEY,
68
+ email character varying(120) NOT NULL UNIQUE,
69
+ password_hash character varying(128) NOT NULL,
70
+ first_name character varying(100),
71
+ last_name character varying(100),
72
+ role character varying(50) DEFAULT 'org_admin'::character varying NOT NULL,
73
+ org_id character varying(36) REFERENCES public.organizations(id) ON DELETE SET NULL,
74
+ failed_login_attempts integer DEFAULT 0,
75
+ locked_until timestamp without time zone,
76
+ created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
77
+
78
+ -- Terms of Service & Privacy Policy Agreement Tracking
79
+ terms_accepted_at timestamp without time zone,
80
+ privacy_policy_agreed_at timestamp without time zone,
81
+ policy_version_agreed character varying(20) DEFAULT 'v1.0'::character varying,
82
+
83
+ -- Security & Authentication Metadata
84
+ mfa_enabled boolean DEFAULT false,
85
+ mfa_secret character varying(100),
86
+ reset_token character varying(255),
87
+ reset_token_expires timestamp without time zone,
88
+ email_verified boolean DEFAULT false
89
+ );
90
+
91
+ -- 5. Alert Settings
92
+ CREATE TABLE public.alert_settings (
93
+ id character varying(36) NOT NULL PRIMARY KEY,
94
+ user_id character varying(36) NOT NULL REFERENCES public.users(id) ON DELETE CASCADE,
95
+ email_notifications boolean DEFAULT true,
96
+ webhook_url character varying(500),
97
+ severity_threshold character varying(50) DEFAULT 'Medium'::character varying
98
+ );
99
+
100
+ -- 6. Organization Scan Quotas
101
+ CREATE TABLE public.organization_scan_quotas (
102
+ id character varying(36) NOT NULL PRIMARY KEY,
103
+ org_id character varying(36) NOT NULL REFERENCES public.organizations(id) ON DELETE CASCADE,
104
+ scan_type character varying(50) NOT NULL,
105
+ allocated_count integer DEFAULT 0 NOT NULL,
106
+ used_count integer DEFAULT 0 NOT NULL,
107
+ CONSTRAINT uix_org_scan_type UNIQUE (org_id, scan_type)
108
+ );
109
+
110
+ -- 7. Scans
111
+ CREATE TABLE public.scans (
112
+ id character varying(36) NOT NULL PRIMARY KEY,
113
+ org_id character varying(36) NOT NULL REFERENCES public.organizations(id) ON DELETE CASCADE,
114
+ user_id character varying(36) NOT NULL REFERENCES public.users(id) ON DELETE CASCADE,
115
+ target_url character varying(500) NOT NULL,
116
+ scan_type character varying(50) DEFAULT 'Full'::character varying NOT NULL,
117
+ status character varying(50) DEFAULT 'queued'::character varying NOT NULL,
118
+ security_score integer,
119
+ started_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
120
+ completed_at timestamp without time zone,
121
+ auth_headers json,
122
+ scan_options json,
123
+ ssl_info json,
124
+
125
+ -- Diagnostics & Summary Metrics
126
+ duration_seconds integer,
127
+ error_message text,
128
+ critical_count integer DEFAULT 0,
129
+ high_count integer DEFAULT 0,
130
+ medium_count integer DEFAULT 0,
131
+ low_count integer DEFAULT 0
132
+ );
133
+
134
+ -- 8. Vulnerabilities
135
+ CREATE TABLE public.vulnerabilities (
136
+ id character varying(36) NOT NULL PRIMARY KEY,
137
+ scan_id character varying(36) NOT NULL REFERENCES public.scans(id) ON DELETE CASCADE,
138
+ title character varying(200) NOT NULL,
139
+ severity character varying(50) NOT NULL,
140
+ category character varying(100) NOT NULL,
141
+ description text NOT NULL,
142
+ remediation text NOT NULL,
143
+ cvss_score double precision NOT NULL,
144
+ detected_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
145
+ evidence text,
146
+ payload text,
147
+ request_details text,
148
+ response_details text,
149
+ is_false_positive boolean DEFAULT false,
150
+ cwe_ids json,
151
+ owasp_category character varying(100),
152
+ exploit_poc json,
153
+ remediation_code text,
154
+
155
+ -- Lifecycle Tracking
156
+ status character varying(50) DEFAULT 'open'::character varying NOT NULL,
157
+ remediated_at timestamp without time zone,
158
+ remediated_by character varying(36)
159
+ );
160
+
161
+ -- 9. Scheduled Scans
162
+ CREATE TABLE public.scheduled_scans (
163
+ id character varying(36) NOT NULL PRIMARY KEY,
164
+ org_id character varying(36) NOT NULL REFERENCES public.organizations(id) ON DELETE CASCADE,
165
+ user_id character varying(36) NOT NULL REFERENCES public.users(id) ON DELETE CASCADE,
166
+ target_url character varying(500) NOT NULL,
167
+ scan_type character varying(50) DEFAULT 'Full'::character varying NOT NULL,
168
+ frequency character varying(50) DEFAULT 'daily'::character varying NOT NULL,
169
+ schedule_time character varying(5),
170
+ day_of_week character varying(20),
171
+ day_of_month integer,
172
+ specific_date character varying(20),
173
+ is_active boolean DEFAULT true,
174
+ auth_headers json,
175
+ created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP,
176
+ last_run_at timestamp without time zone
177
+ );
178
+
179
+ -- 10. Payments
180
+ CREATE TABLE public.payments (
181
+ id character varying(36) NOT NULL PRIMARY KEY,
182
+ org_id character varying(36) REFERENCES public.organizations(id) ON DELETE CASCADE,
183
+ user_id character varying(36) NOT NULL REFERENCES public.users(id) ON DELETE CASCADE,
184
+ razorpay_payment_id character varying(100) UNIQUE,
185
+ razorpay_order_id character varying(100),
186
+ stripe_session_id character varying(100) UNIQUE,
187
+ stripe_payment_id character varying(100) UNIQUE,
188
+ tier_id character varying(50) NOT NULL,
189
+ amount integer NOT NULL,
190
+ currency character varying(10) DEFAULT 'USD'::character varying,
191
+ status character varying(50) DEFAULT 'successful'::character varying,
192
+ created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
193
+ );
194
+
195
+ -- 11. Audit Logs
196
+ CREATE TABLE public.audit_logs (
197
+ id character varying(36) NOT NULL PRIMARY KEY,
198
+ admin_id character varying(36) NOT NULL,
199
+ action character varying(255) NOT NULL,
200
+ target_id character varying(100),
201
+ created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
202
+ );
203
+
204
+ -- 12. Demo Bookings
205
+ CREATE TABLE public.demo_bookings (
206
+ id character varying(36) NOT NULL PRIMARY KEY,
207
+ email character varying(255) NOT NULL,
208
+ company_size character varying(100) NOT NULL,
209
+ meeting_date character varying(100) NOT NULL,
210
+ meeting_time character varying(50) NOT NULL,
211
+ status character varying(50) DEFAULT 'pending'::character varying NOT NULL,
212
+ created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
213
+ );
214
+
215
+ -- 13. Email Logs
216
+ CREATE TABLE public.email_logs (
217
+ id character varying(36) NOT NULL PRIMARY KEY,
218
+ recipient character varying(255) NOT NULL,
219
+ subject character varying(255) NOT NULL,
220
+ status character varying(50) DEFAULT 'sent'::character varying NOT NULL,
221
+ error_message text,
222
+ sent_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
223
+ );
224
+
225
+ -- 14. Generated Reports
226
+ CREATE TABLE public.reports (
227
+ id character varying(36) NOT NULL PRIMARY KEY,
228
+ scan_id character varying(36) NOT NULL REFERENCES public.scans(id) ON DELETE CASCADE,
229
+ org_id character varying(36) NOT NULL REFERENCES public.organizations(id) ON DELETE CASCADE,
230
+ report_type character varying(50) NOT NULL,
231
+ file_path character varying(500),
232
+ generated_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP
233
+ );
234
+
235
+ -- ====================================================================
236
+ -- SECTION 3: PERFORMANCE INDEXES
237
+ -- ====================================================================
238
+ CREATE INDEX idx_users_org_id ON public.users(org_id);
239
+
240
+ CREATE INDEX idx_scans_org_id ON public.scans(org_id);
241
+ CREATE INDEX idx_scans_user_id ON public.scans(user_id);
242
+ CREATE INDEX idx_scans_status ON public.scans(status);
243
+
244
+ CREATE INDEX idx_vulnerabilities_scan_id ON public.vulnerabilities(scan_id);
245
+ CREATE INDEX idx_vulnerabilities_severity ON public.vulnerabilities(severity);
246
+ CREATE INDEX idx_vulnerabilities_status ON public.vulnerabilities(status);
247
+
248
+ CREATE INDEX idx_payments_org_id ON public.payments(org_id);
249
+ CREATE INDEX idx_payments_user_id ON public.payments(user_id);
250
+
251
+ CREATE INDEX idx_scheduled_scans_is_active ON public.scheduled_scans(is_active);
252
+
253
+ CREATE INDEX idx_reports_scan_id ON public.reports(scan_id);
254
+ CREATE INDEX idx_reports_org_id ON public.reports(org_id);
255
+
256
+ -- ====================================================================
257
+ -- SECTION 4: DEFAULT SEED DATA (System Initial Data)
258
+ -- ====================================================================
259
+
260
+ -- Insert Subscription Tiers
261
+ INSERT INTO public.subscription_tiers (id, name, monthly_price, yearly_price) VALUES
262
+ ('free', 'Free Tier Plan', 0, 0),
263
+ ('pro', 'Pro Security Plan', 49, 490),
264
+ ('enterprise', 'Enterprise Shield Plan', 199, 1990)
265
+ ON CONFLICT (id) DO NOTHING;
266
+
267
+ -- Insert Predefined Roles
268
+ INSERT INTO public.roles (id, name) VALUES
269
+ ('role-001', 'super_admin'),
270
+ ('role-002', 'org_admin'),
271
+ ('role-003', 'soc_analyst'),
272
+ ('role-004', 'support_engineer'),
273
+ ('role-005', 'executive'),
274
+ ('role-006', 'read_only')
275
+ ON CONFLICT (name) DO NOTHING;
276
+
277
+ -- Insert Default Global Organization
278
+ INSERT INTO public.organizations (id, name, subscription_tier, status, api_key, created_at) VALUES
279
+ ('org-default-001', 'LarShield Security Enterprise', 'enterprise', 'active', 'larshield_live_api_key_883920193847', CURRENT_TIMESTAMP)
280
+ ON CONFLICT (id) DO NOTHING;
281
+
282
+ -- Insert Default Administrator User (Email: admin@larshield.com)
283
+ -- Password Hash corresponds to standard hashed credential
284
+ INSERT INTO public.users (
285
+ id, email, password_hash, first_name, last_name, role, org_id,
286
+ terms_accepted_at, privacy_policy_agreed_at, policy_version_agreed, email_verified
287
+ ) VALUES (
288
+ 'user-admin-001',
289
+ 'admin@larshield.com',
290
+ '$2b$12$eImiTXuWVxfM37uY4JANjO5E/0w5/qO1F1kR04.3jF96Wd58e3.6u', -- Bcrypt Hash for admin
291
+ 'System',
292
+ 'Admin',
293
+ 'org_admin',
294
+ 'org-default-001',
295
+ CURRENT_TIMESTAMP,
296
+ CURRENT_TIMESTAMP,
297
+ 'v1.0',
298
+ true
299
+ )
300
+ ON CONFLICT (email) DO NOTHING;
301
+
302
+ -- Insert Default Alert Settings for Admin User
303
+ INSERT INTO public.alert_settings (id, user_id, email_notifications, severity_threshold) VALUES
304
+ ('alert-admin-001', 'user-admin-001', true, 'Medium')
305
+ ON CONFLICT (id) DO NOTHING;
306
+
307
+ -- Insert Default Organization Quotas
308
+ INSERT INTO public.organization_scan_quotas (id, org_id, scan_type, allocated_count, used_count) VALUES
309
+ ('quota-001', 'org-default-001', 'Full', 1000, 0),
310
+ ('quota-002', 'org-default-001', 'Port', 5000, 0),
311
+ ('quota-003', 'org-default-001', 'SSL', 5000, 0),
312
+ ('quota-004', 'org-default-001', 'OWASP', 2000, 0)
313
+ ON CONFLICT (org_id, scan_type) DO NOTHING;
314
+
315
+ -- End of Database Initialization Script