CakePHP 3 - Implementing SaaS (Multi Tenant) Model - cakephp

Our CakePHP 3 app (a POS System) is going to use subdomain approach where clients will share a common code base but run on individual databases.
2 things from the config (app.php) file will be unique to each instance : database details and security salt. These details will be stored in a table of main site database (wordpress) and I want to load them based on the subdomain opened. (abc.maindomain.com or xyz.maindomain.com)
I am not sure where should I implement this stuff in the code. bootstrap.php seems to be the only viable solution for now. What are experts' thoughts over this? I plan to store those details in a session once loaded so that system doesn't need to call main database everytime.

Related

Switch from using one database to having one for auth and one for everything else

Within my Django app I currently only have one DB, under settings it is default. What I want to do is port over all of the Auth tables created by Django to a separate database, Ill say SiteAuth for now.
I have copied all of the auth tables over to the SiteAuth db, but I am not sure how to integrate those changes within the site. I know I am going to have to add another db the databases section of settings, But I am not sure how to do the migrations, or how to specify which database I want to query.
How would I do the migrations? how would I specify which database I want to query? how would my models.py change?
Thanks for all the help!
you need to write a router. Check this:
https://docs.djangoproject.com/en/2.2/topics/db/multi-db/
I would recommend to keep the auth in the default db, and move the other stuff in the new one. On the other questions the docs are very descriptive, but to summarize you need to add stuff to the Meta class of your models.

Managing user accounts for different sites with one database

We now have one site running but we will need to build a branded site for our client soon. The client site will have exactly the same data set as our current site expect for the user data. The client site must have totally separated user info which allows only the client to use the site.
I don't see the need for setting up a new database or creating a new user table for the client. My tentative solution is add a "Company" column for the user table so that I can differ which site the user data row is on.
I do not know if this approach will work or not or if it is the best practice. Could anyone with experience like this shed some light on this question?
Thanks,
Nigong
P.S. I use LAMP with AWS.
Using an extra column to store a company / entity id is a common approach for multitenant system. In general you will want to abstract the part that that verifies you can only retrieve data you're allowed to a piece that all queries go through, like your ORM. This will prevent people new to the project from exposing/using data that shouldn't be exposed/used.

Is it possible to import a form straight from a drupal website into my access database?

This might be a very stupid question but I'll try it anyway. We currently have our company website TeamDeals Energie Collectief running on Drupal and next to that we have an access database with all our customer info. We would like to automatically import submitted forms from the website into our database. The problem is we have no idea if and how this could be accomplished.
You can setup a view exporting your form results in CSV (you can configure your view so it fetches only last days submissions or something like that) with drupal.org/project/views_data_export, set a URL to this view, and have a daily cron job on your remote system (hosting your access database) like "wget http://www.teamdeals.nl/path_to_your_csv_view". Maybe not very clean but it works (I saw this once).
You might have problems trying to get form submissions from a view. Assuming you're using webforms, have a look to this post.
You can (should ?) restrain your CSV view URL access to a IP array (on your view : Page settings -> Access permission -> PHP code :
return in_array($_SERVER['REMOTE_ADDR'], array('your.remote.server.ip'));
).
I repeat, there is certainly a better / more secure solution, but it might be a starting point.

CakePHP One Core, One App, One Database, Multiple Domains & Multiple Themes

I am a new addition to the world of CakePHP, and have only created / followed the blog tutorial, and extended it with users. I have been reading the 2.0 book to become more familiar with the framework.
Before I commit to moving to CakePHP, I have a couple of questions.
Currently, I develop a multi domain / multi theme CMS system which is procedural PHP. By doing this I have been able to create a single system on a single database with multiple domains and themes.
I take the $_SERVER['SERVER_NAME'] and look it up in a table of "brands" and then return a brand_id and theme directory. I then suffix all my SQL queries with "and brand_id = X".
I am looking to move over to Cake as the current platform has become overwhelming in size and complexity.
So in short...
Can I create an application with One Core, One App, One Database that can serve Multiple Domains and Themes?
Would I use $_SERVER['SERVER_NAME'] to look up the domain and return back a brand_id and theme?
I don't really want to have multiple databases, as if we had a large number of customers making changes across all databases could prove time consuming.
I certainly don't want to have multiple controllers and multiple models, but in some cases we would want a bespoke view if a customer had a specific requirement.
Any guidance on a basic configuration to get me started would be great. Would each Model "belong to" a "brand"? How would you "serve" up a brand?
Name: Brand1 Domain: www.brand1.com ThemeDir: Brand1
Name: Brand2 Domain: www.brand2.com ThemeDir: Brand2
I have a similar situation in which I am using wildcard subdomains. I accomplished this by doing a combination of things.
In my bootstrap file I am reading in my subdomain and assigning it a value to the cakePHP cache. In my case I am calling:
Configure::write('SubdomainHTTP', $myvalue);
This would be your domain name in your case.
I am loading in my routes.php file a filename called subdomainRoute.php. This file checks to make sure that the route or subdomain in my case exists in the database. It concludes by writing the subdomain name and theme name using Configure::write like step 1.
App::uses('SubdomainRoute', 'Routes');
I glue it all together here, in my AppController.php I read the information I write in step 2. Specifically the theme name and set it using:
$this->theme = $yourtheme;
This should allow you to have different domain name themes. Given you will have to have a database to hold a list of domain names and there theme names.

one authentification for different applications

I'm building four websites. They all should have the same login-datas (user can registrate on website 1 and also can use website 2 and 3 by using the same Login-Name).
My idea was to use the MS SQL Membershipprovider (good idea?).
Now I don't know where to place the SQL-Mebershipprovider (in an extra databse? or together with the websites? -> sound like getting chaos^^)
A other idea I've read was to create a webservice to the authentification?
But I think I'm getting problems with the data consitency, because I think there is no way to point from one database to an other (linking for example the usertable in database one to the texttable in databse 2).
I want to use MVC3 and a MS SQL-database.
Any experiences or ideas?
Thanks a lot!
You can use a separate membership database to do this and just point the providers of each site at this database.
If you wanted to use the role provider you would have to have the same roles in all four websites which may not be what you want. You could use a central database to just handle authentication and then create a local user record in each website that links back to your central user database (you will have to do this linking manually i.e. no relationship). This will then let you role your own role provider for each site.

Resources