COREmanager Documentation

My first user

 

By default, only the local server administrator has access to the control panel. This article describes the authorization method for your own user.

For example, you have an etc/passwd file that stores colon-separated usernames and passwords. Passwords are stored in plain text.

The authenticate basic function is responsible for user authorization. Read more inAuthentication. Therefore, to authorize your own user, you need to create an authenticate event handler. Read more inHow the system behavior changes depending on user actions, events.

To simplify creating your own authorization method, use the isp_api::AuthMethod class from the C++ CORE API. At a minimum, you need to define two virtual methods AuthenByName and AuthenByPassword:

Note
For simplicity, the example contains no additional checks.
Example of myauth.cpp
#include <mgr/mgrstr.h>
#include <api/auth_method.h>
#include <api/module.h>
#include <fstream>
#include <sstream>
    
#define PASSWD  "etc/passwd"
namespace {
using namespace isp_api;
class MyAuth : public AuthMethod {
public:         
    MyAuth() : AuthMethod("myauth") {}
    string Lookup(const string &name) const {
        if (!name.empty()) {
            std::ifstream in(PASSWD);
            while (in.good()) {
                std::stringbuf buf; 
                in.get(buf);
                in.get();
                string pass = buf.str();
                string user = str::GetWord(pass, ':');
                if (user == name && !pass.empty())
                    return pass;
            }
        }
        return "";
    }

    void FillupParams(const string &user, mgr_xml::Xml &xml) const {
            xml.GetRoot().AppendChild("ok")
                .SetProp("level", str::Str(lvAdmin))
                .SetProp("name", user)
                .SetProp("method", "myauth");
    }

    virtual void AuthenByName(mgr_xml::Xml &res, const string &name) const {
        if (!Lookup(name).empty())
            FillupParams(name, res);
    }

    virtual void AuthenByPassword(mgr_xml::Xml &res, const string &name, const string &pass) const {
        string lppass = Lookup(name);
        if (!lppass.empty() && lppass == pass)
            FillupParams(name, res);
    }
};

MODULE_INIT(myauth, "") {
    new MyAuth();
}
} // end of private namespace

After creating the handler, build the components and load the library. Read more inHow to build custom components.