Different storages for multiple users

Sometimes when you have multiple users on your website (on on your app) there are two ways to manage your storage:

  • All users have one folder they manage (usually on small websites).
  • Each user have individual directory it uploads to (used for isolating users on SaaS apps or on big websites).

This article describes the second approach.

Configure independent directories

In order to give to each user a separate directory, you need some code which will change configuration of Flmngr based on user ID.

For example each user has a login used in your system as a directory name, for example your structure can look like:

files/
   user1/
   user2/
   ...

There are two places where you need to specify user's folder: on backend (directory dirFiles - where files are placed) and on the frontend (parameter urlFiles - how to generate URLs of selected files).

Set a directory on the backend

Find the right place in a controller handling file manager and change configuration passed into flmngrRequest() call:

// Get string unique ID of the current user by a way your CMS/framework provides. 
$userId = ...   

// Substitute it into the configuration
FlmngrServer::flmngrRequest(
    array(
        // Directory of your files storage
        'dirFiles' => '/var/www/files/' . $userId,

        // Optionally: if you wish to use separate directory for cache files
        // This is handy when your "dirFiles" is slower a local disk,
        // for example this is a drive mounted over a network.
        //'dirCache' => '/var/www/cache' . $userId,
    )
);

Specify URL prefix of used directory

After editing this the file manager should browse independent user storages fine, but you still need to explain to UI how to generate URLs for selected files, because file manager dialog does not "know" which us folder location (URL) of browsed storage.

Pass the config depending of your integration:

JavaScript snippet

CKEditor 4

TinyMCE 5

Froala

// Get string unique ID of the current user by a way your CMS/framework provides. 
// Usually this can be found in Cookies.
// This value must be equal to "userId" you pass on the backend.
var userId = getUserId(); // your function

// Let's create a preconfigured instance of Flmngr
var flmngr = window.flmngr.create({
    urlFileManager: 'https://fm.flmngr.com/fileManager',
    urlFiles: 'https://fm.flmngr.com/files/' + userId
});
If you pass config into CKEditor directly on initialization:
// Get string unique ID of the current user by a way your CMS/framework provides. 
// Usually this can be found in Cookies.
// This value must be equal to "userId" you pass on the backend.
var userId = getUserId(); // your function

CKEDITOR.replace("#editor", {
    extraPlugins: "file-manager",
    Flmngr: {
        urlFileManager: 'https://fm.flmngr.com/fileManager',
        urlFiles: 'https://fm.flmngr.com/files/' + userId
    }
});  
Or if you edit config.js:
CKEDITOR.editorConfig = function(config) {
    config.extraPlugins = "file-manager";

    // Get string unique ID of the current user by a way your CMS/framework provides. 
    // Usually this can be found in Cookies.
    // This value must be equal to "userId" you pass on the backend.
    var userId = getUserId(); // your function

    config.Flmngr = {
        urlFileManager: 'https://fm.flmngr.com/fileManager',
        urlFiles: 'https://fm.flmngr.com/files/' + userId
    };
}; 
// Get string unique ID of the current user by a way your CMS/framework provides. 
// Usually this can be found in Cookies.
// This value must be equal to "userId" you pass on the backend.
var userId = getUserId(); // your function

tinymce.init({
    selector: "#editor",
    plugins: "file-manager",
    Flmngr: {
        urlFileManager: 'https://fm.flmngr.com/fileManager',
        urlFiles: 'https://fm.flmngr.com/files/' + userId
    }
});  
// Get string unique ID of the current user by a way your CMS/framework provides. 
// Usually this can be found in Cookies.
// This value must be equal to "userId" you pass on the backend.
var userId = getUserId(); // your function

FroalaEditor('#editor', {
    Flmngr: {
        urlFileManager: 'https://fm.flmngr.com/fileManager',
        urlFiles: 'https://fm.flmngr.com/files/' + userId
    }
});