Commit 0d503270 authored by deepdey's avatar deepdey

chore: restructure user-info

parent f2ea403b
...@@ -25,8 +25,6 @@ class Role(db.Model, CustomSerializerMixin): ...@@ -25,8 +25,6 @@ class Role(db.Model, CustomSerializerMixin):
updated_by = db.Column(db.BigInteger) updated_by = db.Column(db.BigInteger)
is_deleted = db.Column(db.Boolean, default=False) is_deleted = db.Column(db.Boolean, default=False)
role_type = db.relationship('RoleType')
__table_args__ = ( __table_args__ = (
db.UniqueConstraint('project_id', 'role_name', name='uq_project_id_role_name'), db.UniqueConstraint('project_id', 'role_name', name='uq_project_id_role_name'),
) )
......
...@@ -19,6 +19,7 @@ class User(db.Model, CustomSerializerMixin): ...@@ -19,6 +19,7 @@ class User(db.Model, CustomSerializerMixin):
password = db.Column(db.String(80), nullable=False) password = db.Column(db.String(80), nullable=False)
organization = db.Column(db.String(255), nullable=False) organization = db.Column(db.String(255), nullable=False)
project_id = db.Column(db.BigInteger, nullable=True) project_id = db.Column(db.BigInteger, nullable=True)
role_type_id = db.Column(db.BigInteger, db.ForeignKey('role_type.id'), nullable=False)
gstin = db.Column(db.String(50), nullable=False) gstin = db.Column(db.String(50), nullable=False)
email = db.Column(db.String(120), nullable=False, unique=True) email = db.Column(db.String(120), nullable=False, unique=True)
phone_number = db.Column(db.String(20), nullable=False, unique=True) phone_number = db.Column(db.String(20), nullable=False, unique=True)
...@@ -33,5 +34,6 @@ class User(db.Model, CustomSerializerMixin): ...@@ -33,5 +34,6 @@ class User(db.Model, CustomSerializerMixin):
is_deleted = db.Column(db.Boolean, default=False) is_deleted = db.Column(db.Boolean, default=False)
user_roles = db.relationship('UserRoleMap') user_roles = db.relationship('UserRoleMap')
role_type = db.relationship('RoleType')
serialize_rules = ('-password',) serialize_rules = ('-password',)
...@@ -3,6 +3,7 @@ from app.app_config.response_message import * ...@@ -3,6 +3,7 @@ from app.app_config.response_message import *
from app.routes import api from app.routes import api
from flask_restx import fields from flask_restx import fields
from app.routes.role_type.role_type_response import ROLE_TYPE_RESPONSE_MODEL
from app.utils.api_response import prepare_swagger_response from app.utils.api_response import prepare_swagger_response
USER_RESPONSE_MODEL: any = api.model( USER_RESPONSE_MODEL: any = api.model(
...@@ -18,6 +19,9 @@ USER_RESPONSE_MODEL: any = api.model( ...@@ -18,6 +19,9 @@ USER_RESPONSE_MODEL: any = api.model(
'email_verified': fields.Boolean, 'email_verified': fields.Boolean,
'phone_verified': fields.Boolean, 'phone_verified': fields.Boolean,
'status': fields.String, 'status': fields.String,
'role_type_id': fields.Integer,
'project_id': fields.Integer,
'role_type': fields.Nested(ROLE_TYPE_RESPONSE_MODEL),
'user_roles': fields.List( 'user_roles': fields.List(
fields.String(default='{}', description='user roles object in json') fields.String(default='{}', description='user roles object in json')
), ),
......
from typing import List from typing import List
from app import orm as ORM from app import orm as ORM
from app.app_config.constant import EndpointTagEnum
from app.utils.redis import set_value from app.utils.redis import set_value
from app import db from app import db
def process_valid_loging(user_info: ORM.User, api_key: str) -> None: def process_valid_loging(user_info: ORM.User, api_key: str) -> None:
set_value(f'{user_info.id}_un', user_info.username, 60 * 60 * 24) DEFAULT_EXPIRES: int = 60 * 60 * 24
set_value(f'{api_key}_id', user_info.id, 60 * 60 * 24)
set_value(f'{user_info.id}_un', user_info.username, DEFAULT_EXPIRES)
set_value(f'{user_info.id}_pid', user_info.project_id, DEFAULT_EXPIRES)
set_value(f'{api_key}_id', user_info.id, DEFAULT_EXPIRES)
# TODO: currently we consider user have only one role # TODO: currently we consider user have only one role
if len(user_info.user_roles) > 0: if len(user_info.user_roles) > 0:
set_value(f'{api_key}_rid', user_info.user_roles[0].role.id, 60 * 60 * 24) role_id: int = user_info.user_roles[0].role_id
set_value( role_name: str = user_info.user_roles[0].role.role_name
f'{user_info.id}_rn',
user_info.user_roles[0].role.role_name, set_value(f'{user_info.id}_rid', role_id, DEFAULT_EXPIRES)
60 * 60 * 24, set_value(f'{user_info.id}_rn', role_name, DEFAULT_EXPIRES)
) set_value(f'{user_info.id}_rt', user_info.role_type_id, DEFAULT_EXPIRES)
set_value(
f'{user_info.id}_rt',
user_info.user_roles[0].role.role_type.role_name,
60 * 60 * 24,
)
# TODO: fetch all api mapped with role id and set in redis
api_list: List[ORM.Api] = ( api_list: List[ORM.Api] = (
db.session.query(ORM.Api) db.session.query(ORM.Api)
.join(ORM.RoleApiMap, ORM.Api.id == ORM.RoleApiMap.api_id) .join(ORM.RoleApiMap, ORM.Api.id == ORM.RoleApiMap.api_id)
.filter( .filter(
ORM.RoleApiMap.role_id == user_info.user_roles[0].role.id, ORM.RoleApiMap.role_id == role_id,
ORM.Api.tag == 'RA', ORM.Api.tag == EndpointTagEnum.RA.value,
) )
.all() .all()
) )
for api in api_list: for api in api_list:
set_value( set_value(
f'r_{user_info.user_roles[0].role.id}_{api.id}_{api.method}', f'r_{role_id}_{api.id}_{api.method}',
1, 1,
60 * 60 * 24, DEFAULT_EXPIRES,
) )
CREATE TABLE IF NOT EXISTS role_type (
id BIGSERIAL PRIMARY KEY NOT NULL,
role_name VARCHAR(80) UNIQUE NOT NULL,
is_deleted BOOLEAN DEFAULT false,
created_at TIMESTAMP NOT NULL DEFAULT now(),
updated_at TIMESTAMP
);
CREATE TABLE IF NOT EXISTS webomagic_user ( CREATE TABLE IF NOT EXISTS webomagic_user (
id BIGSERIAL PRIMARY KEY NOT NULL, id BIGSERIAL PRIMARY KEY NOT NULL,
username VARCHAR(80) UNIQUE NOT NULL, username VARCHAR(80) UNIQUE NOT NULL,
...@@ -5,6 +13,7 @@ CREATE TABLE IF NOT EXISTS webomagic_user ( ...@@ -5,6 +13,7 @@ CREATE TABLE IF NOT EXISTS webomagic_user (
password VARCHAR(80) NOT NULL, password VARCHAR(80) NOT NULL,
organization VARCHAR(255), organization VARCHAR(255),
project_id BIGINT, project_id BIGINT,
role_type_id BIGINT NOT NULL,
gstin VARCHAR(50), gstin VARCHAR(50),
email VARCHAR(120) UNIQUE NOT NULL, email VARCHAR(120) UNIQUE NOT NULL,
phone_number VARCHAR(20) UNIQUE NOT NULL, phone_number VARCHAR(20) UNIQUE NOT NULL,
...@@ -16,6 +25,7 @@ CREATE TABLE IF NOT EXISTS webomagic_user ( ...@@ -16,6 +25,7 @@ CREATE TABLE IF NOT EXISTS webomagic_user (
created_by BIGINT, created_by BIGINT,
updated_at TIMESTAMP, updated_at TIMESTAMP,
updated_by BIGINT, updated_by BIGINT,
FOREIGN KEY (role_type_id) REFERENCES role_type (id),
FOREIGN KEY (created_by) REFERENCES webomagic_user (id), FOREIGN KEY (created_by) REFERENCES webomagic_user (id),
FOREIGN KEY (updated_by) REFERENCES webomagic_user (id) FOREIGN KEY (updated_by) REFERENCES webomagic_user (id)
); );
...@@ -34,14 +44,6 @@ CREATE TABLE IF NOT EXISTS webomagic_api ( ...@@ -34,14 +44,6 @@ CREATE TABLE IF NOT EXISTS webomagic_api (
UNIQUE (endpoint, method) UNIQUE (endpoint, method)
); );
CREATE TABLE IF NOT EXISTS role_type (
id BIGSERIAL PRIMARY KEY NOT NULL,
role_name VARCHAR(80) UNIQUE NOT NULL,
is_deleted BOOLEAN DEFAULT false,
created_at TIMESTAMP NOT NULL DEFAULT now(),
updated_at TIMESTAMP
);
CREATE TABLE IF NOT EXISTS role ( CREATE TABLE IF NOT EXISTS role (
id BIGSERIAL PRIMARY KEY NOT NULL, id BIGSERIAL PRIMARY KEY NOT NULL,
role_type_id BIGINT NOT NULL, role_type_id BIGINT NOT NULL,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment