Commit 6a2819d9 authored by jan.koester's avatar jan.koester
Browse files

editor: media only of the selected blog, only through the blog

- media plugin: the JSON API commands (media_list_albums with a store,
  media_list_media, media_create_preview, media_create_album,
  media_upload) only reach albums and media of the blog's own stores
  (MEDIADB_STORES); other ids are refused with "... does not belong to
  this blog", so the blog's mediadb login cannot be used for other
  blogs' media.
- editor: the media dialog, the HTML import and the ZIP template import
  use the selected project (blog) instead of offering every
  connection; without a selected project they say so.
- translations: I18N_SELECT_PROJECT_FIRST, I18N_MEDIA_OF_BLOG.

Co-Authored-By: default avatarClaude Opus 5.5 <noreply@anthropic.com>
parent 22c697fe
Loading
Loading
Loading
Loading
+2 −2
Changes for editor/html/index.html: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -508,8 +508,8 @@
    <dialog id="media-browser-dialog">
        <h3>Media Browser</h3>
        <div class="set-field">
            <label data-i18n="I18N_CONNECTION">Verbindung</label>
            <select id="media-conn-select"></select>
            <label data-i18n="I18N_MEDIA_OF_BLOG">Medien von</label>
            <select id="media-conn-select" disabled></select>
        </div>
        <div class="set-field">
            <label>Album</label>
+20 −26
Changes for editor/html/js/editor.js: 20 added lines, 26 removed lines.
Original line number Diff line number Diff line
@@ -22,6 +22,11 @@
    var currentProjectId = '';
    var currentProjectName = '';
    var CURRENT_PROJECT_STORAGE_KEY = 'blogi-current-project';
    // The selected blog (project) for the media dialogs: media are only fetched through
    // that blog and only its own media are shown (see media-browser.js)
    window.EditorProject = {
        current: function() { return { id: currentProjectId, name: currentProjectName }; }
    };
    var wizardTemplates = [];
    var wizardCsvRows = [];
    var wizardCsvColumns = [];
@@ -653,22 +658,22 @@
        albumSel.disabled = true;
    }

    // Media only go to the selected blog (project), never to another connection.
    function populateImportHtmlMediaConnections() {
        var connSel = document.getElementById('import-html-media-conn');
        connSel.innerHTML = '';
        EditorApi.listConnections().then(function(resp) {
            var conns = resp.connections || [];
            connSel.innerHTML = '';
            for (var i = 0; i < conns.length; i++) {
        connSel.disabled = true;
        var opt = document.createElement('option');
                opt.value = conns[i].id;
                opt.textContent = (conns[i].name || conns[i].id) + (connectedConnections[conns[i].id] ? ' ✓' : '');
        if (!currentProjectId) {
            opt.value = '';
            opt.textContent = I18n.t('I18N_SELECT_PROJECT_FIRST', 'Bitte zuerst ein Projekt (Blog) auswählen');
            connSel.appendChild(opt);
            return;
        }
            if (conns.length) populateImportHtmlMediaStores(connSel.value);
        }).catch(function() {
            connSel.innerHTML = '';
        });
        opt.value = currentProjectId;
        opt.textContent = (currentProjectName || currentProjectId) + (connectedConnections[currentProjectId] ? ' ✓' : '');
        connSel.appendChild(opt);
        populateImportHtmlMediaStores(currentProjectId);
    }

    function populateImportHtmlMediaStores(connId) {
@@ -1326,24 +1331,13 @@
                return;
            }

            EditorApi.listConnections().then(function(connResp) {
                var conns = connResp.connections || [];
                if (!conns.length) {
                    resolve(null);
            // media only go to the selected blog (project)
            if (!currentProjectId) {
                reject({error: I18n.t('I18N_SELECT_PROJECT_FIRST', 'Bitte zuerst ein Projekt (Blog) auswählen')});
                return;
            }

                var connIdx = chooseIndexByPrompt(
                    'Verbindung wählen (muss bereits verbunden sein):',
                    conns,
                    function(c) { return (c.name || c.id) + ' [' + c.id + ']'; }
                );
                if (connIdx < 0) {
                    resolve(null);
                    return;
                }

                var connId = conns[connIdx].id;
            var connId = currentProjectId;
            Promise.resolve().then(function() {
                EditorApi.mediaListStores(connId).then(function(storeResp) {
                    var stores = storeResp.stores || [];
                    if (!stores.length) {
+17 −20
Changes for editor/html/js/media-browser.js: 17 added lines, 20 removed lines.
Original line number Diff line number Diff line
/*******************************************************************************
 * blogi-webedit Media Browser
 * Lets the user browse media from connected blog servers (MediaDB).
 * Lets the user browse the media of the selected blog (project), fetched through that
 * blog's API -- only the albums of the blog's own MediaDB stores.
 *******************************************************************************/

var MediaBrowser = (function() {
@@ -104,29 +105,25 @@ var MediaBrowser = (function() {
        _albumSelect.innerHTML = '<option value="">-- Album --</option>';
        _status.textContent = '';

        // Load connections
        _connSelect.innerHTML = '<option value="">...</option>';
        EditorApi.listConnections().then(function(resp) {
            var conns = resp.connections || [];
        // Only the selected blog (project): media are fetched through that blog's API, and
        // the blog itself hands out only the albums and media of its own stores
        _connSelect.innerHTML = '';
            if (conns.length === 0) {
                _connSelect.innerHTML = '<option value="">No connections</option>';
                return;
            }
            for (var i = 0; i < conns.length; i++) {
        _connSelect.disabled = true;
        var project = (window.EditorProject && window.EditorProject.current()) || { id: '', name: '' };
        var opt = document.createElement('option');
                opt.value = conns[i].id;
                var label = conns[i].name;
                if (conns[i].group) label = conns[i].group + ' / ' + label;
                opt.textContent = label;
        if (!project.id) {
            opt.value = '';
            opt.textContent = '–';
            _connSelect.appendChild(opt);
            _status.textContent = I18n.t('I18N_SELECT_PROJECT_FIRST', 'Bitte zuerst ein Projekt (Blog) auswählen');
            _status.className = 'set-status error';
        } else {
            opt.value = project.id;
            opt.textContent = project.name || project.id;
            _connSelect.appendChild(opt);
            _status.className = 'set-status';
            _loadAlbums(project.id);
        }
            // Auto-load albums for first connection
            if (conns.length > 0) {
                _connSelect.value = conns[0].id;
                _loadAlbums(conns[0].id);
            }
        });

        _dialog.showModal();
    }
+42 −1
Changes for plugins/media/media.cpp: 42 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -1494,7 +1494,13 @@ namespace blogi {
                    std::vector<std::string> storesToQuery;
                    if(json_object_object_get_ex(request, "store_name", &jsn)){
                        const char *sn = json_object_get_string(jsn);
                        if(sn && sn[0] != '\0') storesToQuery.push_back(sn);
                        if(sn && sn[0] != '\0'){
                            if(!_ownStoreName(sn)){
                                json_object_put(jarr);
                                return _deny(response, "store does not belong to this blog");
                            }
                            storesToQuery.push_back(sn);
                        }
                    }
                    if(storesToQuery.empty())
                        storesToQuery = _mdbStores;
@@ -1534,6 +1540,9 @@ namespace blogi {
                const char *albumId = json_object_get_string(jaid);
                try {
                    _ensureMdbReady(tid, token);
                    if(!_ownAlbum(tid, albumId)){
                        return _deny(response, "album does not belong to this blog");
                    }
                    auto mediaList = _mdb[tid]->list_media(albumId);
                    json_object *jarr = json_object_new_array();
                    for(const auto &mi : mediaList){
@@ -1581,6 +1590,9 @@ namespace blogi {
                try {
                    _ensureMdbReady(tid, token);
                    auto mi = _mdb[tid]->get_media(mediaId);
                    if(!_ownStore(tid, mi.store_id)){
                        return _deny(response, "media does not belong to this blog");
                    }
                    std::string ext = "webp";
                    size_t dp = mi.filename.rfind('.');
                    if(dp != std::string::npos) ext = mi.filename.substr(dp + 1);
@@ -1665,6 +1677,9 @@ namespace blogi {
                    json_object_object_add(response, "message", json_object_new_string("store_name and album_name must not be empty"));
                    return;
                }
                if(!_ownStoreName(storeName)){
                    return _deny(response, "store does not belong to this blog");
                }
                try {
                    _ensureMdbReady(tid, token);
                    std::string storeId = _getStoreId(tid, storeName);
@@ -1729,6 +1744,9 @@ namespace blogi {
                    }

                    _ensureMdbReady(tid, token);
                    if(!_ownAlbum(tid, albumId)){
                        return _deny(response, "album does not belong to this blog");
                    }

                    // Check for duplicate filename in album
                    auto existingMedia = _mdb[tid]->list_media(albumId);
@@ -1883,6 +1901,29 @@ namespace blogi {
            return _mdbStores;
        }

        // Only this blog's own stores (MEDIADB_STORES): the JSON API -- and through it the
        // editor -- must not reach other blogs' albums and media through this blog's
        // mediadb login, whatever album or media id a request names.
        static void _deny(json_object *response, const char *message){
            json_object_object_add(response, "status", json_object_new_string("error"));
            json_object_object_add(response, "message", json_object_new_string(message));
        }

        bool _ownStoreName(const std::string &storeName) const {
            return std::find(_mdbStores.begin(), _mdbStores.end(), storeName) != _mdbStores.end();
        }

        bool _ownStore(int tid, const std::string &storeId){
            if(storeId.empty()) return false;
            for(const auto &name : _mdbStores)
                if(_getStoreId(tid, name) == storeId) return true;
            return false;
        }

        bool _ownAlbum(int tid, const std::string &albumId){
            return _ownStore(tid, _mdb[tid]->get_album(albumId).store_id);
        }

        void _loadStores(int tid){
            std::string json;
            try { getConfig(*Args->database[tid], "MEDIADB_STORES", json); } catch(...) {}
+4 −0
Changes for src/translations.h: 4 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -424,6 +424,8 @@ namespace blogi {
            {"I18N_COPY",                "Kopieren"},
            // Blog connection
            {"I18N_CONNECTION",          "Verbindung"},
            {"I18N_SELECT_PROJECT_FIRST", "Bitte zuerst ein Projekt (Blog) auswählen"},
            {"I18N_MEDIA_OF_BLOG",       "Medien von"},
            {"I18N_BLOG_NAME",           "Blog Name"},
            {"I18N_BLOG_URL",            "Blog URL"},
            {"I18N_CONNECTION_OK",       "Verbunden"},
@@ -1092,6 +1094,8 @@ namespace blogi {
            {"I18N_COPY",                "Copy"},
            // Blog connection
            {"I18N_CONNECTION",          "Connection"},
            {"I18N_SELECT_PROJECT_FIRST", "Please select a project (blog) first"},
            {"I18N_MEDIA_OF_BLOG",       "Media of"},
            {"I18N_BLOG_NAME",           "Blog Name"},
            {"I18N_BLOG_URL",            "Blog URL"},
            {"I18N_CONNECTION_OK",       "Connected"},