Commit 8061ad1d authored by jan.koester's avatar jan.koester
Browse files

tes

parent 2b9b14a4
Loading
Loading
Loading
Loading
+232 −30
Changes for plugins/content/content.cpp: 232 added lines, 30 removed lines.
Original line number Diff line number Diff line
@@ -902,49 +902,251 @@ namespace blogi {

        json_object* Settings(const int tid, libhttppp::HttpRequest &req, const std::string &sessionid){
            std::string _lang = getLang(*Args->database[tid]);
            std::string curl = req.getRequestURL();
            std::string base = Args->config->buildurl("settings/content/");

            // Resolve current user for GPO checks
            int currentAuthor = -1;
            std::string authid;
            try {
                Session sess(*Args->database[tid]);
                sess.getSessionData(sessionid,"authid",authid);
                std::string uid;
                sess.getSessionData(sessionid,"uid",uid);
                if(!uid.empty()){
                    dbpp::SQL asql;
                    dbpp::DBResult ares;
                    asql << "SELECT id FROM users WHERE sid='" << uid.c_str() << "' LIMIT 1;";
                    if(Args->database[tid]->exec(asql,ares) >= 1)
                        currentAuthor = atoi(ares[0][0]);
                }
            } catch(...) {}

            auto checkGPO = [&](const char *gpoid) -> bool {
                if(authid.empty()) return false;
                for(size_t si = 0; si < Args->config->getAuthSourceCount(); ++si){
                    const AuthSource &src = Args->config->getAuthSource(si);
                    try {
                        authdb::client::ClientConnection authcon;
                        authcon.setUrl(src.url);
                        authcon.setClientName(src.clientName);
                        authcon.setClientSecret(src.clientSecret);
                        authcon.setSessionID(uuid::uuid(authid.c_str()));
                        authdb::client::Client clt(authcon);
                        if(clt.ClientAuth() && clt.GPOcheck(uuid::uuid(gpoid)))
                            return true;
                    } catch(...) { continue; }
                }
                return false;
            };

            bool canEditOwn    = checkGPO(GPO_CONTENT_EDIT_OWN);
            bool canEditOthers = checkGPO(GPO_CONTENT_EDIT_OTHERS);
            bool canDelOwn     = checkGPO(GPO_CONTENT_DELETE_OWN);
            bool canDelOthers  = checkGPO(GPO_CONTENT_DELETE_OTHERS);

            // Sub-route: delete
            if(curl.length() > base.length()){
                std::string sub = curl.substr(base.length());

                if(sub.find("delete") == 0){
                    int cid = -1;
                    size_t idpos = sub.find("id=");
                    if(idpos != std::string::npos) cid = atoi(sub.c_str() + idpos + 3);

                    if(cid > 0){
                        // Check permission
                        dbpp::SQL psql; dbpp::DBResult pres;
                        psql << "SELECT author FROM content WHERE id='" << cid << "' LIMIT 1;";
                        bool allowed = false;
                        if(Args->database[tid]->exec(psql,pres) >= 1){
                            int postAuthor = atoi(pres[0][0]);
                            if(currentAuthor == postAuthor && canDelOwn) allowed = true;
                            else if(canDelOthers) allowed = true;
                        }
                        json_object *jroot = json_object_new_object();
            json_object_object_add(jroot, "title", json_object_new_string(tr(_lang,"Content Permissions").c_str()));
                        json_object_object_add(jroot, "title", json_object_new_string(tr(_lang,"Content").c_str()));
                        if(allowed){
                            dbpp::SQL dsql; dbpp::DBResult dres;
                            dsql << "DELETE FROM tags_content WHERE content_id='" << cid << "'; DELETE FROM content WHERE id='" << cid << "';";
                            Args->database[tid]->exec(dsql,dres);
                            cleartags(tid);
                            json_object_object_add(jroot, "message", json_object_new_string(tr(_lang,"Post deleted").c_str()));
                            json_object_object_add(jroot, "message_type", json_object_new_string("success"));
                        } else {
                            json_object_object_add(jroot, "message", json_object_new_string(tr(_lang,"Permission denied").c_str()));
                            json_object_object_add(jroot, "message_type", json_object_new_string("error"));
                        }
                        json_object *jnav = json_object_new_array();
                        json_object *jback = json_object_new_object();
                        json_object_object_add(jback, "label", json_object_new_string(tr(_lang,"Back to list").c_str()));
                        json_object_object_add(jback, "url", json_object_new_string(Args->config->buildurl("settings/content").c_str()));
                        json_object_array_add(jnav, jback);
                        json_object_object_add(jroot, "nav", jnav);
                        return jroot;
                    }
                }

                // Sub-route: edit
                if(sub.find("edit") == 0){
                    int cid = -1;
                    size_t idpos = sub.find("id=");
                    if(idpos != std::string::npos) cid = atoi(sub.c_str() + idpos + 3);

                    if(cid > 0){
                        // Check permission
                        dbpp::SQL psql; dbpp::DBResult pres;
                        psql << "SELECT author,title,descrition,text FROM content WHERE id='" << cid << "' LIMIT 1;";
                        bool allowed = false;
                        bool found = false;
                        int postAuthor = -1;
                        if(Args->database[tid]->exec(psql,pres) >= 1){
                            found = true;
                            postAuthor = atoi(pres[0][0]);
                            if(currentAuthor == postAuthor && canEditOwn) allowed = true;
                            else if(canEditOthers) allowed = true;
                        }

                        if(!found || !allowed){
                            json_object *jroot = json_object_new_object();
                            json_object_object_add(jroot, "title", json_object_new_string(tr(_lang,"Content").c_str()));
                            json_object_object_add(jroot, "message", json_object_new_string(
                                found ? tr(_lang,"Permission denied").c_str() : tr(_lang,"Post not found").c_str()));
                            json_object_object_add(jroot, "message_type", json_object_new_string("error"));
                            json_object *jnav = json_object_new_array();
                            json_object *jback = json_object_new_object();
                            json_object_object_add(jback, "label", json_object_new_string(tr(_lang,"Back to list").c_str()));
                            json_object_object_add(jback, "url", json_object_new_string(Args->config->buildurl("settings/content").c_str()));
                            json_object_array_add(jnav, jback);
                            json_object_object_add(jroot, "nav", jnav);
                            return jroot;
                        }

                        // Handle POST save
                        libhttppp::HttpForm form;
                        form.parse(req);
                        std::string newTitle, newDesc, newText;
                        bool saving = false;
                        for(const auto &cur : form.urlData()){
                            if(cur.key == "blog_title") { newTitle = cur.value; saving = true; }
                            else if(cur.key == "blog_description") newDesc = cur.value;
                            else if(cur.key == "blog_content") newText = cur.value;
                        }

                        json_object *jroot = json_object_new_object();
                        json_object_object_add(jroot, "title", json_object_new_string(tr(_lang,"Edit Post").c_str()));

                        if(saving && !newTitle.empty()){
                            std::vector<char> sbuf;
                            dbpp::SQL usql; dbpp::DBResult ures;
                            usql << "UPDATE content SET title='" << dbpp::SQL::escaped(sbuf,newTitle.c_str())
                                 << "',descrition='" << dbpp::SQL::escaped(sbuf,newDesc.c_str())
                                 << "',text='" << dbpp::SQL::escaped(sbuf,newText.c_str())
                                 << "' WHERE id='" << cid << "';";
                            Args->database[tid]->exec(usql,ures);
                            json_object_object_add(jroot, "message", json_object_new_string(tr(_lang,"Saved!").c_str()));
                            json_object_object_add(jroot, "message_type", json_object_new_string("success"));
                            // Re-read updated values
                            psql.clear();
                            psql << "SELECT author,title,descrition,text FROM content WHERE id='" << cid << "' LIMIT 1;";
                            Args->database[tid]->exec(psql,pres);
                        }

                        json_object *jnav = json_object_new_array();
                        json_object *jback = json_object_new_object();
                        json_object_object_add(jback, "label", json_object_new_string(tr(_lang,"Back to list").c_str()));
                        json_object_object_add(jback, "url", json_object_new_string(Args->config->buildurl("settings/content").c_str()));
                        json_object_array_add(jnav, jback);
                        json_object_object_add(jroot, "nav", jnav);

                        json_object *jform = json_object_new_object();
                        json_object_object_add(jform, "method", json_object_new_string("POST"));
            json_object_object_add(jform, "action", json_object_new_string(req.getRequestURL().c_str()));

            json_object *jtabs = json_object_new_array();
                        json_object_object_add(jform, "action", json_object_new_string(curl.c_str()));

            // GPO info tab
            {
                json_object *jtab = json_object_new_object();
                json_object_object_add(jtab, "label", json_object_new_string(tr(_lang,"Permissions (GPO)").c_str()));
                        json_object *jfields = json_object_new_array();

                struct GpoInfo { const char *name; const char *uuid; const char *desc; };
                GpoInfo gpos[] = {
                    {  "Edit Own Posts",      GPO_CONTENT_EDIT_OWN,
                       "Allows users to edit their own content posts (default: enabled)" },
                    {  "Edit Others Posts",   GPO_CONTENT_EDIT_OTHERS,
                       "Allows users to edit content posts from other authors (default: disabled)" },
                    {  "Delete Own Posts",    GPO_CONTENT_DELETE_OWN,
                       "Allows users to delete their own content posts (default: enabled)" },
                    {  "Delete Others Posts", GPO_CONTENT_DELETE_OTHERS,
                       "Allows users to delete content posts from other authors (default: disabled)" },
                };
                        json_object *jf1 = json_object_new_object();
                        json_object_object_add(jf1, "name", json_object_new_string("blog_title"));
                        json_object_object_add(jf1, "type", json_object_new_string("text"));
                        json_object_object_add(jf1, "label", json_object_new_string(tr(_lang,"Title").c_str()));
                        json_object_object_add(jf1, "value", json_object_new_string(pres[0][1] ? pres[0][1] : ""));
                        json_object_array_add(jfields, jf1);

                        json_object *jf2 = json_object_new_object();
                        json_object_object_add(jf2, "name", json_object_new_string("blog_description"));
                        json_object_object_add(jf2, "type", json_object_new_string("text"));
                        json_object_object_add(jf2, "label", json_object_new_string(tr(_lang,"Short description").c_str()));
                        json_object_object_add(jf2, "value", json_object_new_string(pres[0][2] ? pres[0][2] : ""));
                        json_object_array_add(jfields, jf2);

                        json_object *jf3 = json_object_new_object();
                        json_object_object_add(jf3, "name", json_object_new_string("blog_content"));
                        json_object_object_add(jf3, "type", json_object_new_string("textarea"));
                        json_object_object_add(jf3, "label", json_object_new_string(tr(_lang,"Text").c_str()));
                        json_object_object_add(jf3, "value", json_object_new_string(pres[0][3] ? pres[0][3] : ""));
                        json_object_array_add(jfields, jf3);

                        json_object_object_add(jform, "fields", jfields);
                        json_object_object_add(jform, "submit", json_object_new_string(tr(_lang,"Save").c_str()));
                        json_object_object_add(jroot, "form", jform);
                        return jroot;
                    }
                }
            }

                for(auto &g : gpos){
                    json_object *jf = json_object_new_object();
                    json_object_object_add(jf, "type", json_object_new_string("heading"));
                    std::string label = std::string(g.name) + " <small style=\"color:var(--blogi-text-muted,#aaa);\">[" + g.uuid + "]</small><br/>"
                                      + "<small>" + g.desc + "</small>";
                    json_object_object_add(jf, "label", json_object_new_string(label.c_str()));
                    json_object_array_add(jfields, jf);
            // Default: post list with edit/delete actions
            json_object *jroot = json_object_new_object();
            json_object_object_add(jroot, "title", json_object_new_string(tr(_lang,"Content").c_str()));

            json_object *jtable = json_object_new_object();
            json_object *jheaders = json_object_new_array();
            json_object_array_add(jheaders, json_object_new_string("ID"));
            json_object_array_add(jheaders, json_object_new_string(tr(_lang,"Title").c_str()));
            json_object_array_add(jheaders, json_object_new_string(tr(_lang,"Author").c_str()));
            json_object_array_add(jheaders, json_object_new_string(tr(_lang,"Date").c_str()));
            json_object_object_add(jtable, "headers", jheaders);

            dbpp::SQL sql; dbpp::DBResult res;
            sql << "SELECT content.id,content.title,users.displayname,content.created,content.author "
                << "FROM content LEFT JOIN users ON content.author=users.id ORDER BY content.id DESC LIMIT 100";
            int ncount = Args->database[tid]->exec(sql,res);

            json_object *jrows = json_object_new_array();
            for(int i = 0; i < ncount; ++i){
                json_object *jrow = json_object_new_object();
                json_object *jcells = json_object_new_array();
                json_object_array_add(jcells, json_object_new_string(res[i][0] ? res[i][0] : ""));
                json_object_array_add(jcells, json_object_new_string(res[i][1] ? res[i][1] : ""));
                json_object_array_add(jcells, json_object_new_string(res[i][2] ? res[i][2] : ""));
                json_object_array_add(jcells, json_object_new_string(res[i][3] ? res[i][3] : ""));
                json_object_object_add(jrow, "cells", jcells);

                int postAuthor = res[i][4] ? atoi(res[i][4]) : -2;
                bool isOwn = (currentAuthor == postAuthor);

                json_object *jactions = json_object_new_array();

                if((isOwn && canEditOwn) || canEditOthers){
                    json_object *jedit = json_object_new_object();
                    json_object_object_add(jedit, "label", json_object_new_string(tr(_lang,"Edit").c_str()));
                    json_object_object_add(jedit, "url", json_object_new_string(
                        (Args->config->buildurl("settings/content/edit?id=") + std::string(res[i][0])).c_str()));
                    json_object_array_add(jactions, jedit);
                }

                json_object_object_add(jtab, "fields", jfields);
                json_object_array_add(jtabs, jtab);
                if((isOwn && canDelOwn) || canDelOthers){
                    json_object *jdel = json_object_new_object();
                    json_object_object_add(jdel, "label", json_object_new_string(tr(_lang,"Delete").c_str()));
                    json_object_object_add(jdel, "url", json_object_new_string(
                        (Args->config->buildurl("settings/content/delete?id=") + std::string(res[i][0])).c_str()));
                    json_object_array_add(jactions, jdel);
                }

            json_object_object_add(jform, "tabs", jtabs);
            json_object_object_add(jroot, "form", jform);
                json_object_object_add(jrow, "actions", jactions);
                json_object_array_add(jrows, jrow);
            }
            json_object_object_add(jtable, "rows", jrows);
            json_object_object_add(jroot, "table", jtable);

            return jroot;
        }