]> rtime.felk.cvut.cz Git - coffee/coffee-flask.git/commit
Add support for multiple identifiers per user
authorJaroslav Klapalek <klapajar@fel.cvut.cz>
Wed, 14 Aug 2019 11:29:21 +0000 (13:29 +0200)
committerMichal Sojka <michal.sojka@cvut.cz>
Sun, 18 Aug 2019 20:12:03 +0000 (22:12 +0200)
commitc38692694b0b480020d4b8eacdcc690572f78c13
tree3e7fdef70201f215474761a4d1f9450bb4b382af
parent3e9c1dd43cdbfb3441a584cfe24e9d1251c68670
Add support for multiple identifiers per user

Users can register multiple identifiers for a single account. Also
they can rename identifiers (each identifier has its own name) and
unregister/remove them from the account.

Required: Create new table in database (will be created automatically
on first startup)

CREATE TABLE if not exists identifiers (
    `userid` varchar ( 24 ) NOT NULL,
    `id` varchar ( 24 ) PRIMARY KEY NOT NULL,
    `name` varchar ( 24 ),
    `status` INTEGER NOT NULL DEFAULT 1,
    FOREIGN KEY(`userid`) REFERENCES `users`(`id`)
);

Note: `status` is used for activating/deactivating associations.

This table creates 1:N relation (users:identifiers), where 'userid'
is marked as foreign key from 'users' table. There is no reason for
changing 'users.id', but it could be done.

Column 'id' is marked as primary key to avoid creating M:N relation
instead. Each time a relation is removed, it is marked as deactivated
and it is deleted from the table next time, when a new relation with
the same identifier is created.

Each coffee is now saved with 'identifiers.id' instead of 'users.id'.
Because we were not using custom id in 'users' table, we do not
have to change anything in currently recorded data/design of tables.

On user creation / first login 'users.id' (or id of identifier) and
id of identifier are added to 'identifiers' table.

Selected changes:
> app.py:login()
From now on we expect that received variable is 'iid' (id of identi-
fier). At first this identifier is checked if it belongs to a user.
If True: 'uid' (users.id) is found in database,
Otherwise: 'iid' is registred as 'uid' for a new user.

> app.py:user_XXX_identifier()
Functions add/rename/disable modifies identifiers for current user.
Note: 'session["uid"]' is used because client side (.js) does not
know the difference between 'uid' and 'iid' (and there is no reason
to change this). This solves a problem of invalid ids when trying to
modify ID#1 of account ID#0 while logged by ID#2 in case we are
assuming that id of identifier ID#0 is the same as 'users.id'.

> app.py:coffee_add()
Add consistency check to avoid situations when user without valid
identifier is trying to register a coffee.

> coffee_db.py
Add functions to match new functions in app.py. Also modify current
functions to use 'identifiers' table in SQL queries.

> coffee_db.py:add_user_identifier()
Creates relation between user and identifier. It is also deleting
old (and deactivated!) relation from the database before registering
a new one.

> templates/main.js:addIdentifier_start() and addIdentifier_finish()
Modifies UI for the user and changes 'identifier_registration' variable.

> templates/user.html
Add table of identifiers for current user, which contains names,
ids and buttons to rename/remove identifier from current account.

Possible TODOs:
 - add option to merge two identifiers (e. g. if one is lost, it
should be possible to transfer all coffees to different one),
 - divide identifiers to 2+ groups -- 'master key' and 'regular',
so the public ones (e. g. mugs) cannot remove identifiers from an
account.
app.py
coffee_db.py
coffee_db.sql
templates/main.js
templates/user.html