Browse Source

Start creating users and authentication

master
Alfred 1 year ago
parent
commit
cada41db00
2 changed files with 67 additions and 29 deletions
  1. 41
    28
      pgsql/fake_data.pgsql
  2. 26
    1
      pgsql/schema.pgsql

+ 41
- 28
pgsql/fake_data.pgsql View File

@@ -1,32 +1,45 @@
1
-insert into macarrodes.t_bookmarks (title, url, date_add, site, favicon, nclicks) values 
2
-    ('Hacker News', 'https://news.ycombinator.com/active', '1963-09-01', 'news.ycombinator.com', 'https://news.ycombinator.com/y18.gif', 5), 
3
-    ('Meneame', 'https://www.meneame.net/', '2017-10-01', 'www.meneame.net', 'https://mnmstatic.net/v_144/favicon.ico?v=E6bENepwgd', 2),
4
-    ('Hipertextual', 'https://hipertextual.com/', '2017-05-10', 'hipertextual.com', 'https://hipertextual.com/files/2016/07/cropped-fav-120x120.png', 10),
5
-    ('Espinof', 'https://www.espinof.com/', '2018-09-01', 'www.espinof.com', 'https://img.weblogssl.com/css/espinof/p/skin-site-default-d/images/favicon.ico', 3),
6
-    ('Fetlife', 'https://fetlife.com', '1963-09-01', 'fetlife.com', 'https://pbs.twimg.com/profile_images/1004502321387188224/0u7PBn0l_400x400.jpg', 3);
1
+insert into macarrodes.t_users (username) values 
2
+    ('user1'),
3
+    ('user2');
7 4
 
8
-insert into macarrodes.t_tags (name) values 
9
-    ('Read it later'),
10
-    ('Favorite'),
11
-    ('In initial page'),
12
-    ('Useful');
5
+CREATE ROLE user1 NOLOGIN;
6
+grant usage on schema macarrodes to user1;
7
+grant all PRIVILEGES ON TABLE macarrodes.t_users TO user1;
8
+GRANT user1 TO postgres;
13 9
 
14
-insert into macarrodes.t_folders (tag, folder_type) values 
15
-    (1, 'readitlater'),
16
-    (2, 'buttons'),
17
-    (3, 'bookmarkslist');
10
+CREATE ROLE user2 NOLOGIN;
11
+GRANT user2 TO postgres;
12
+grant usage on schema macarrodes to user2;
18 13
 
19
-insert into macarrodes.t_tags (name) values 
20
-    ('Aggregator'),
21
-    ('News'),
22
-    ('Geek');
14
+insert into macarrodes.t_bookmarks (user_id, title, url, date_add, site, favicon, nclicks) values 
15
+    (1, 'Hacker News', 'https://news.ycombinator.com/active', '1963-09-01', 'news.ycombinator.com', 'https://news.ycombinator.com/y18.gif', 5), 
16
+    (1, 'Meneame', 'https://www.meneame.net/', '2017-10-01', 'www.meneame.net', 'https://mnmstatic.net/v_144/favicon.ico?v=E6bENepwgd', 2),
17
+    (1, 'Hipertextual', 'https://hipertextual.com/', '2017-05-10', 'hipertextual.com', 'https://hipertextual.com/files/2016/07/cropped-fav-120x120.png', 10),
18
+    (1, 'Espinof', 'https://www.espinof.com/', '2018-09-01', 'www.espinof.com', 'https://img.weblogssl.com/css/espinof/p/skin-site-default-d/images/favicon.ico', 3),
19
+    (1, 'Fetlife', 'https://fetlife.com', '1963-09-01', 'fetlife.com', 'https://pbs.twimg.com/profile_images/1004502321387188224/0u7PBn0l_400x400.jpg', 3);
23 20
 
24
-insert into macarrodes.t_bookmarks_tags (bookmark, tag) values 
25
-    (1, 4),
26
-    (2, 4),
27
-    (2, 5),
28
-    (2, 3),
29
-    (3, 5),
30
-    (3, 6),
31
-    (4, 2),
32
-    (4, 5);
21
+insert into macarrodes.t_tags (user_id, name) values 
22
+    (1, 'Read it later'),
23
+    (1, 'Favorite'),
24
+    (1, 'In initial page'),
25
+    (1, 'Useful');
26
+
27
+insert into macarrodes.t_folders (user_id, tag, folder_type) values 
28
+    (1, 1, 'readitlater'),
29
+    (1, 2, 'buttons'),
30
+    (1, 3, 'bookmarkslist');
31
+
32
+insert into macarrodes.t_tags (user_id, name) values 
33
+    (1, 'Aggregator'),
34
+    (1, 'News'),
35
+    (1, 'Geek');
36
+
37
+insert into macarrodes.t_bookmarks_tags (user_id, bookmark, tag) values 
38
+    (1, 1, 4),
39
+    (1, 2, 4),
40
+    (1, 2, 5),
41
+    (1, 2, 3),
42
+    (1, 3, 5),
43
+    (1, 3, 6),
44
+    (1, 4, 2),
45
+    (1, 4, 5);

+ 26
- 1
pgsql/schema.pgsql View File

@@ -1,7 +1,14 @@
1 1
 create schema macarrodes;
2 2
 
3
+create table macarrodes.t_users (
4
+  id serial primary key,
5
+  username text unique,
6
+  password text
7
+);
8
+
3 9
 create table macarrodes.t_bookmarks (
4 10
   id serial primary key,
11
+  user_id integer references macarrodes.t_users(id),
5 12
   title text,
6 13
   site text,
7 14
   url text not null,
@@ -16,12 +23,14 @@ create table macarrodes.t_bookmarks (
16 23
 
17 24
 create table macarrodes.t_tags (
18 25
   id serial primary key,
26
+  user_id integer references macarrodes.t_users(id),
19 27
   name text unique,
20 28
   stem text unique
21 29
 );
22 30
 
23 31
 create table macarrodes.t_folders (
24 32
   id serial primary key,
33
+  user_id integer references macarrodes.t_users(id),
25 34
   tag integer references macarrodes.t_tags(id),
26 35
   folder_order integer default 0,
27 36
   folder_type text default ''
@@ -29,9 +38,25 @@ create table macarrodes.t_folders (
29 38
 
30 39
 create table macarrodes.t_bookmarks_tags (
31 40
   bookmark integer references macarrodes.t_bookmarks,
32
-  tag integer references macarrodes.t_tags
41
+  tag integer references macarrodes.t_tags,
42
+  user_id integer references macarrodes.t_users(id)
33 43
 );
34 44
 
45
+CREATE OR REPLACE FUNCTION macarrodes.user_id() RETURNS INTEGER AS
46
+$$
47
+  DECLARE
48
+    identifier INTEGER;
49
+  BEGIN
50
+    SELECT id 
51
+    INTO identifier
52
+    FROM macarrodes.t_users
53
+    WHERE macarrodes.t_users.username = current_user
54
+    LIMIT 1;
55
+
56
+    RETURN identifier;
57
+  END
58
+$$ LANGUAGE plpgsql;
59
+
35 60
 create view macarrodes.tags as 
36 61
   select 
37 62
     t_tags.id,

Loading…
Cancel
Save