Commit 6163c345 authored by jan.koester's avatar jan.koester
Browse files

test

parent 1a97c566
Loading
Loading
Loading
Loading
+1 −11
Changes for plugins/content/content.cpp: 1 added line, 11 removed lines.
Original line number Diff line number Diff line
@@ -895,7 +895,7 @@ namespace blogi {
                dbpp::SQL sql;
                dbpp::DBResult res;

                // Resolve author: use author_id from params if available, otherwise fall back to first user
                // Resolve author: use author_id from params (set by editor from authdb)
                int authorId = 0;
                if (params) {
                    json_object *aid_obj = nullptr;
@@ -903,16 +903,6 @@ namespace blogi {
                        authorId = json_object_get_int(aid_obj);
                    }
                }
                if (authorId <= 0) {
                    // Fallback: use the first available user
                    dbpp::SQL usql;
                    dbpp::DBResult ures;
                    usql << "SELECT id FROM users ORDER BY id LIMIT 1;";
                    int uc = Args->database[tid]->exec(usql, ures);
                    if (uc > 0) {
                        authorId = atoi(ures[0][0]);
                    }
                }

                if (authorId <= 0) {
                    json_object_object_add(response, "status", json_object_new_string("error"));
+57 −4
Changes for src/edit.cpp: 57 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -41,6 +41,7 @@

#include "blogi.h"
#include "theme.h"
#include "auth.h"

blogi::webedit::WebEditor::WebEditor(PluginArgs &plgargs) : Args (plgargs){
    EditPlugin::setPrefix(Args.config->getprefix());
@@ -2308,8 +2309,7 @@ void blogi::webedit::WebEditor::JsonApi(const int tid, json_object *request, jso
                            json_object *params = nullptr;
                            json_object_object_get_ex(single_request, "params", &params);

                            // Resolve author from authid in the request array
                            // The authid is sent as {"authid": "..."} in the request array
                            // Resolve author from authid via authdb
                            int authorId = 0;
                            size_t reqLen = json_object_array_length(request);
                            for (size_t ri = 0; ri < reqLen; ++ri) {
@@ -2318,15 +2318,68 @@ void blogi::webedit::WebEditor::JsonApi(const int tid, json_object *request, jso
                                if (json_object_object_get_ex(reqItem, "authid", &authObj)) {
                                    const char *authIdStr = json_object_get_string(authObj);
                                    if (authIdStr && authIdStr[0] != '\0') {
                                        // Look up user by sid (auth session id)
                                        try {
                                            // Connect to authdb and get session + user info
                                            uuid::uuid authSessionId(authIdStr);
                                            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(authSessionId);

                                                    authdb::client::Client clt(authcon);
                                                    if (!clt.ClientAuth()) continue;

                                                    // Get user uid from session
                                                    authdb::SessionData sdat;
                                                    clt.SessionInfo(sdat);
                                                    uuid::uuid uid;
                                                    sdat.getUid(uid);

                                                    // Get user details (firstname + lastname)
                                                    authdb::UserData udat{uid};
                                                    clt.UserInfo(udat);
                                                    std::string displayname = udat.getFirstname() + " " + udat.getLastname();
                                                    std::string username = udat.getUsername();
                                                    std::string email = udat.getMail();

                                                    // Find or create local user by authdb uid
                                                    std::vector<char> sbuf;
                                                    dbpp::SQL asql;
                                                    dbpp::DBResult ares;
                                        asql << "SELECT id FROM users WHERE sid='" << dbpp::SQL::escaped(sbuf, authIdStr) << "' LIMIT 1;";
                                                    asql << "SELECT id FROM users WHERE sid='" << dbpp::SQL::escaped(sbuf, uid.c_str()) << "' LIMIT 1;";
                                                    int ucount = Args.database[tid]->exec(asql, ares);
                                                    if (ucount > 0) {
                                                        authorId = atoi(ares[0][0]);
                                                        // Update displayname in case it changed
                                                        dbpp::SQL usql;
                                                        dbpp::DBResult ures;
                                                        usql << "UPDATE users SET displayname='" << dbpp::SQL::escaped(sbuf, displayname.c_str())
                                                             << "', username='" << dbpp::SQL::escaped(sbuf, username.c_str())
                                                             << "', email='" << dbpp::SQL::escaped(sbuf, email.c_str())
                                                             << "' WHERE id=" << authorId << ";";
                                                        try { Args.database[tid]->exec(usql, ures); } catch (...) {}
                                                    } else {
                                                        // Create new local user from authdb info
                                                        dbpp::SQL isql;
                                                        dbpp::DBResult ires;
                                                        isql << "INSERT INTO users (sid,username,email,displayname) VALUES ('"
                                                             << dbpp::SQL::escaped(sbuf, uid.c_str()) << "','"
                                                             << dbpp::SQL::escaped(sbuf, username.c_str()) << "','"
                                                             << dbpp::SQL::escaped(sbuf, email.c_str()) << "','"
                                                             << dbpp::SQL::escaped(sbuf, displayname.c_str()) << "') RETURNING id;";
                                                        Args.database[tid]->exec(isql, ires);
                                                        authorId = atoi(ires[0][0]);
                                                    }
                                                    break;
                                                } catch (...) {
                                                    continue;
                                                }
                                            }
                                        } catch (...) {}
                                    }
                                    break;
                                }