Commit c7275e73 authored by jan.koester's avatar jan.koester
Browse files

test

parent 2b105393
Loading
Loading
Loading
Loading
Loading
+56 −19
Changes for src/admin.cpp: 56 added lines, 19 removed lines.
Original line number Diff line number Diff line
@@ -2220,21 +2220,47 @@ namespace authdb {
            rep.send(curreq,out,0);
        }


        void editClient(libhttppp::HttpRequest& curreq, const int tid, ULONG_PTR args) {
            libhtmlpp::HtmlElement index = *_IndexElement;

            char cdid[255],cclid[255];
            uuid::uuid did, clid;
            const std::string& requestURL = curreq.getRequestURL(); // Use const reference

            std::string cdid;
            std::string cclid;

            static const std::string SETTINGS_PREFIX = "/settings/";
            static const std::string EDIT_CLIENT_MIDDLE = "/editclient/";


            if(sscanf(curreq.getRequestURL().c_str(),"/settings/%[^/]/editclient/%s",cdid,cclid)<0){
                throw AuthBackendError("editclient: wrong url!");
            const size_t startPos = requestURL.find(SETTINGS_PREFIX);
            if (startPos != 0) { // Check for prefix at the beginning (index 0)
                throw AuthBackendError("editclient: wrong url! (Missing prefix)");
            }

            DomainBackend dbackend(_AdminBackend);
            const size_t middlePos = requestURL.find(EDIT_CLIENT_MIDDLE, SETTINGS_PREFIX.length());
            if (middlePos == std::string::npos) {
                throw AuthBackendError("editclient: wrong url! (Missing separator)");
            }

            cdid = requestURL.substr(SETTINGS_PREFIX.length(), middlePos - SETTINGS_PREFIX.length());

            if (cdid.empty()) {
                throw AuthBackendError("editclient: wrong url! (Empty cdid)");
            }

            const size_t cclidStart = middlePos + EDIT_CLIENT_MIDDLE.length();
            cclid = requestURL.substr(cclidStart);

            if (cclid.empty()) {
                throw AuthBackendError("editclient: wrong url! (Empty cclid)");
            }

            DomainBackend dbackend(_AdminBackend);
            AuthBackend* backend = nullptr;

            if(strcmp(cdid,"admin") == 0 ){
            if (cdid == "admin") {
                backend = &_AdminBackend;
            } else if (did.parse(cdid) == 0) {
                backend = dbackend.data(did);
@@ -2242,29 +2268,36 @@ namespace authdb {
                throw AuthBackendError("editclient: could not parse domain uuid!");
            }

            if(clid.parse(cclid)!=0)
            if (clid.parse(cclid) != 0) {
                throw AuthBackendError("editclient: could not parse client uuid!");
            }

            class ClientData cldat(clid);
            class Client clt;
            Client clt;

            size_t pos = sizeof(AuthHeader);

            clt.info(*backend, cldat, pos);

            libhttppp::HttpForm cleformd;

            cleformd.parse(curreq);

            if (cleformd.UrlFormData.getFormData()) {
                std::string key, keyhash;
                for(libhttppp::HttpForm::UrlcodedForm::Data *curdat=cleformd.UrlFormData.getFormData(); curdat;

                for (libhttppp::HttpForm::UrlcodedForm::Data* curdat = cleformd.UrlFormData.getFormData();
                     curdat != nullptr;
                curdat = curdat->nextData()) {

                    if( curdat->getKey()=="clientname" )
                        cldat.setName(curdat->getValue().data());
                    else if (curdat->getKey() == "clientkey" && !curdat->getValue().empty() )
                        key=curdat->getValue();
                    const std::string& currentKey = curdat->getKey();
                    const std::string& currentValue = curdat->getValue();

                    if (currentKey == "clientname") {
                        // Use .data() only if setName requires a non-const char*
                        cldat.setName(currentValue.data());
                    } else if (currentKey == "clientkey" && !currentValue.empty()) {
                        key = currentValue;
                    }
                }

                if (!key.empty()) {
@@ -2276,20 +2309,24 @@ namespace authdb {
                clt.remove(*backend, clid);
                clt.create(*backend, &cldat);

                // --- Redirect Response ---

                libhttppp::HttpResponse rep;
                rep.setState(HTTP307);
                rep.setState(HTTP307); // Temporary Redirect
                rep.setVersion(HTTPVERSION(1.1));
                std::string rurl="/settings/"; rurl+=cdid; rurl+="/listclients";

                std::string rurl = "/settings/" + cdid + "/listclients";

                rep.setHeaderData("Location")->push_back(rurl.c_str());
                rep.setContentType("text/html");

                std::vector<char> out;
                rep.send(curreq, out, 0);

                return;
            }

            libhtmlpp::HtmlString cleform,out;

            libhtmlpp::HtmlString cleform;
            cleform << "<form method=\"post\" >"
            << "<ul>"
            << "<li><label for=\"clientname\" >Clientname:</label>"
@@ -2297,7 +2334,7 @@ namespace authdb {
            << "<li><label for=\"clientkey\" >Clientkey:</label>"
            << "<input name=\"clientkey\" type=\"password\" value=\"" << cldat.getKey() << "\"></li>"
            << "<li><input type=\"submit\" value=\"Save\"></li>"
                    << "<ul></form>";
            << "</ul></form>";

            libhtmlpp::HtmlElement* content = index.getElementbyID("content");

@@ -2305,6 +2342,7 @@ namespace authdb {
                content->appendChild(cleform.parse());
            }

            libhtmlpp::HtmlString out;
            libhtmlpp::print(index, out, false);

            libhttppp::HttpResponse rep;
@@ -2312,7 +2350,6 @@ namespace authdb {
            rep.setVersion(HTTPVERSION(1.1));
            rep.setContentType("text/html");
            rep.send(curreq, out.data(), out.length());

        }

        void SettingsController(libhttppp::HttpRequest &curreq, const int tid, ULONG_PTR args){