Django and angular6 with multitenancy - django

I am a newbie to Multi-Tenant architecture, developing SaaS product using django-tenant-schema
my requirement was something like clients would register for product. For every registered client, I was creating new schema by following single database and isolated schemas approach. Whenever client requested from a browser I was able to identify them by using subdomain and giving privilege to acces their specfic schema. While I was starting coding lot of questions were popup in my head. I am really sorry for asking here but stackoverflow is only my last hope. Database was like below
Database
Public_Schema
auth_user
Clients_List_Table
ClientA_Schema
auth_user
ClientA_User_List
ClientB_Schema
auth_user
ClientB_User_List
Q1.What kind of admin actions we can perform on every client?
I have rest api for example http://client.example.com/api/user_list/ here client maybe ClientA or ClientB
Q2. How can we implement api routing which gets client name dynamically when user requested at browser which let us to use corresponding schema to display current client's user_list.

Related

How to model django apps

i'm building a web app customer ticketing system using Django. i'm stuck and drawing blanks. accounts would create client, send email to sign up, and update tickets that clients create. clients would login and be able to create a ticket and view already created tickets. wondering if i should create my models all in app or so i create separate apps like accounts app, ticket app and client app. I've tried searching on google but unable to find a specific answer to my question. any help would be appreciated as i'm new to database modeling.
It's completely up to you - it's your web app after all. But my suggestion would be to keep everything separate. The rule of thumb is - every app should have only one main thing that it does. For every app, can you describe what it's main purpose is? If you are listing more than one, then it's an indication that it should perhaps be a new app.
This might be of interest:
https://docs.djangoproject.com/en/3.0/intro/reusable-apps/

Single table vs Table per user

I am creating backend for a messaging application. I want it to be mostly a web app so would have to store the list of people with whom a user chats and all the messages in the server. However, I also wanted to have the ability to extend it and use the same backend for a mobile app. So I was thinking of having a separate database or table for every user and only open the connection to it when a user connects to the backend using WebSocket. However, according to this post, it seems that in most cases, it is better to have a single table and have many to one relation. So what would be the best choice in my usage? Also, how can I go about implementing that in Django?

Multi-tenant Centralized Authentication Server

I am trying to create a centralized authentication server for multiple Django apps (APIs). I've seen posts/recommendations but none fit exactly what I am looking for.
Overview:
Users can be associated to one or multiple projects
Users have same credentials to all projects they are associated to
Use JSON Web Tokens - use payload to add user data, sub-domain (project) to route to, role, etc
Sub-domain will not be used for login. All users will login to same site and will be routed to project they are associated to (or given list if there are multiple). SSO is optional.
Questions/uncertainties:
Q: Should the authentication tokens be created on the authentication server or on each project? ie) Each user having one auth token for all projects or have one auth token for each project?
Q: Roles will be stored in each app. I would like to send the roles along with the authentication token in the JWT. Should this data be redundantly stored on the authentication server? Another other way would be for the authentication server to access the project databases. What is the best way to handle this? Users will have different roles for each project.
Q: Auth server will have basic user information (email/username, password, first/last name, etc). Since foreign keys can't be used between databases I can use a user proxy based on usernames to create the user on each project. Do the app servers need to have access to which authentication tokens are valid?
Taking advantage of pre-existing software:
Another approach I had in mind was to use django-tenant-schemas which takes advantage of Postgres schemas where each one of my projects would be a schema (currently using MYSQL databases). Does it make sense to take advantage of this?
Can I take advantage of an IdP service to offload some of the authentication? Does this easily tie into the Django auth layer?
Your question seems to be multiple so I would split the answer too:
ABOUT THE USERS
Since your users are not part of your "mutitencancy model" you have two options here:
Replicate your user data among the different tenant databases (via triggers and what not).
Write your own authentication middleware that verifies users in the right database (lets call it root database since now on). You can use user ids to from the root database and verify manually that they match, which is a bad idea.
That means your database schema will be something like this:
root database (all common data here)
project 1 database (with it's own user data or referencing root)
project 2 database (with it's own user data or referencing root)
Now for authentication tokens
You have the same options as above:
Keep them in the root database and write your own middleware.
Replicate them.
How to implement the whole thing
Since your use case is pretty particular, you may encounter some resistance from existing software. But creating your own multitenant solution is not that hard

oAuth2 and dead access_tokens in database

I am building web application on top of Django which primary role to serve as backend for mobile applications. For authentication I choosed oAuth2 protocol which is implemented by django-oauth-tookit app.
oAuth2 access_tokens are short-lived, so client apps will change them for new ones very often. As number of users will grow, this can end up in a huge database table with access_tokens which are actually dead.
The question is: should I be concerned about quantity of access_tokens? Do I have to clean them up manually (just by deleting the content of whole table) or do something else?
Thank you for help

Using Django with the Facebook API for a polling app

I randomly pick two friends of the user and ask him/her to pick who is the better friend. Now all I have is the friend ID which I then have to use to create a poll and store in the database accordingly. Using the Facebook graph API, I have the ID. All I need to do now is to pass it to Django.
I'm new to this so how exactly would I do that? Pass a javascript variable to Django?
I see two options.
At client side using Javascript SDK,
Fetch the friends' profile details along with ID.
Convert them to JSON.
Do a POST request to a django url/view which stores the data in database.
In this way, you don't need to do any graph API queries further from server side. But this won't help you updating the data at realtime. Consider, if one of the friends changing his name in FB, now what is stored in your database becomes obsolete. So, you need to make sure that some thing from client side implemented to do real time update posts to server side.
At server side using any django facebook graph API apps,
Get the IDs from client side.
Use the fb graph app to fetch the details at server side.
Store them in database.
In this way, you could be able to schedule a callback for real time updates. I prefer the second approach as it's always better to burden the server rather than client. And I found this app simple and do what you need. https://pypi.python.org/pypi/django-facebook-api/0.1.10