Skip to content
This repository has been archived by the owner on Dec 16, 2019. It is now read-only.

Commit

Permalink
No users in database, just files and errors
Browse files Browse the repository at this point in the history
  • Loading branch information
Frédéric Haziza authored and Frédéric Haziza committed Jan 22, 2018
1 parent 23c7809 commit 8556ec1
Show file tree
Hide file tree
Showing 3 changed files with 1 addition and 98 deletions.
83 changes: 1 addition & 82 deletions extras/db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,93 +7,12 @@ CREATE TYPE hash_algo AS ENUM ('md5', 'sha256');

CREATE EXTENSION pgcrypto;


-- ##################################################
-- USERS
-- ##################################################
CREATE TABLE users (
id SERIAL, PRIMARY KEY(id), UNIQUE(id),
elixir_id TEXT NOT NULL, UNIQUE(elixir_id),
password_hash TEXT,
pubkey TEXT,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT clock_timestamp(),
last_accessed TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT clock_timestamp(),
expiration INTERVAL NOT NULL,
CHECK (password_hash IS NOT NULL OR pubkey IS NOT NULL)
);

CREATE FUNCTION sanitize_id(elixir_id users.elixir_id%TYPE)
RETURNS users.elixir_id%TYPE AS $sanitize_id$
DECLARE
eid users.elixir_id%TYPE;
BEGIN
-- eid := trim(trailing '@elixir-europe.org' from elixir_id);
eid := regexp_replace(elixir_id, '@.*', '');
RETURN eid;
END;
$sanitize_id$ LANGUAGE plpgsql;

CREATE FUNCTION insert_user(elixir_id users.elixir_id%TYPE,
password_hash users.password_hash%TYPE,
public_key users.pubkey%TYPE,
exp_int users.expiration%TYPE DEFAULT INTERVAL '1' MONTH)

RETURNS users.id%TYPE AS $insert_user$
#variable_conflict use_column
DECLARE
user_id users.elixir_id%TYPE;
eid users.elixir_id%TYPE;
BEGIN
eid := sanitize_id(elixir_id);
INSERT INTO users (elixir_id,password_hash,pubkey,expiration) VALUES(eid,password_hash,public_key,exp_int)
ON CONFLICT (elixir_id) DO UPDATE SET last_accessed = DEFAULT, expiration = exp_int
RETURNING users.id INTO user_id;
RETURN user_id;
END;
$insert_user$ LANGUAGE plpgsql;

-- Delete other user entries that are too old
CREATE FUNCTION refresh_user(elixir_id users.elixir_id%TYPE)

RETURNS void AS $refresh_user$
#variable_conflict use_column
DECLARE
eid users.elixir_id%TYPE;
BEGIN
eid := sanitize_id(elixir_id);
UPDATE users SET last_accessed = DEFAULT WHERE elixir_id = eid;
RETURN;
END;
$refresh_user$ LANGUAGE plpgsql;

CREATE FUNCTION update_users()
RETURNS trigger AS $update_users$
BEGIN
DELETE FROM users WHERE last_accessed < current_timestamp - expiration;
RETURN NEW;
END;
$update_users$ LANGUAGE plpgsql;

CREATE TRIGGER delete_expired_users_trigger AFTER UPDATE ON users EXECUTE PROCEDURE update_users();

CREATE FUNCTION flush_user(elixir_id users.elixir_id%TYPE)
RETURNS void AS $flush_user$
#variable_conflict use_column
DECLARE
eid users.elixir_id%TYPE;
BEGIN
eid := sanitize_id(elixir_id);
DELETE FROM users WHERE elixir_id = eid; -- Future: and ega_user is true
RETURN;
END;
$flush_user$ LANGUAGE plpgsql;

-- ##################################################
-- FILES
-- ##################################################
CREATE TABLE files (
id SERIAL, PRIMARY KEY(id), UNIQUE (id),
elixir_id TEXT REFERENCES users (elixir_id) ON DELETE CASCADE,
elixir_id TEXT NOT NULL,
filename TEXT NOT NULL,
enc_checksum TEXT,
enc_checksum_algo hash_algo,
Expand Down
11 changes: 0 additions & 11 deletions lega/frontend.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
| [LocalEGA-URL]/ | GET | Frontpage |
| [LocalEGA-URL]/file?user=&name= | GET | Information on a file for a given user |
| [LocalEGA-URL]/user/<name> | GET | JSON array of all files information |
| [LocalEGA-URL]/user/<name> | DELETE | Revoking inbox access |
|-----------------------------------|------------|----------------------------------------|
:author: Frédéric Haziza
Expand Down Expand Up @@ -75,16 +74,6 @@ async def index(request):
'''
return { 'country': 'Sweden', 'text' : '<p>There should be some info here.</p>' }

@only_central_ega
async def flush_user(request):
'''Flush an EGA user from the database'''
name = request.match_info['name']
LOG.info(f'Flushing user {name} from the database')
res = await db.flush_user(request.app['db'], name)
if not res:
raise web.HTTPBadRequest(text=f'An error occured for user {name}\n')
return web.Response(text=f'Success')

@only_central_ega
async def status_file(request):
'''Status endpoint for a given file'''
Expand Down
5 changes: 0 additions & 5 deletions lega/utils/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,6 @@ async def get_user_info(conn, username):
await cur.execute(query, {'username': username})
return await cur.fetchone()

async def flush_user(conn, name):
with (await conn.cursor()) as cur:
await cur.execute('SELECT flush_user(%(name)s);', { 'name': name })
return await cur.fetchone()

######################################
## "Classic" code ##
######################################
Expand Down

0 comments on commit 8556ec1

Please sign in to comment.