newtx/sdk/sane_opt_json/user.cpp

168 lines
4.9 KiB
C++
Raw Normal View History

#include "user.h"
#include <huagao/hgscanner_error.h>
#include <string.h>
#include <base/ini_file.h>
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// json ...
static std::string device_opt_json[] = {
"{\"user-name\":{\"cat\":\"none\",\"group\":\"\\u7528\\u6237\",\"title\":\"\\u7528\\u6237\\u540d\",\"desc\":\"\\u767b\\u5f55\\u7528\\u6237\\u8d26\\u53f7\",\"type\":\"string\",\"fix-id\":39173,\"ui-pos\":10,\"auth\":0,\"size\":32,\"cur\":\"\",\"default\":\"\"},\"user-pwd\":{\"cat\":\"none\",\"group\":\"\\u7528\\u6237\",\"title\":\"\\u5bc6\\u7801\",\"desc\":\"\\u767b\\u5f55\\u7528\\u6237\\u8d26\\u53f7\\u5bc6\\u7801\",\"type\":\"string\",\"fix-id\":39174,\"ui-pos\":11,\"auth\":0,\"size\":32,\"cur\":\"\",\"default\":\"\"},\"login\":{\"cat\":\"none\",\"group\":\"\\u7528\\u6237\",\"title\":\"\\u767b\\u5f55\",\"desc\":\"\\u7528\\u6237\\u767b\\u5f55\",\"type\":\"button\",\"fix-id\":39168,\"ui-pos\":20,\"auth\":0,\"affect\":6,\"size\":4,\"auto\":false},\"logout\":{\"cat\":\"none\",\"group\":\"\\u7528\\u6237\",\"title\":\"\\u6ce8\\u9500\",\"desc\":\"\\u7528\\u6237\\u767b\\u51fa\",\"type\":\"button\",\"fix-id\":39169,\"ui-pos\":21,\"auth\":0,\"affect\":6,\"size\":4,\"auto\":false},\"dev-sn\":{\"cat\":\"base\",\"group\":\"\\u5173\\u4e8e\",\"title\":\"\\u5e8f\\u5217\\u53f7\",\"desc\":\"\\u8bbe\\u5907\\u5e8f\\u5217\\u53f7\",\"type\":\"string\",\"pos\":100,\"fix-id\":34902,\"ui-pos\":14,\"auth\":0,\"size\":32,\"auto\":false,\"cur\":\"\",\"default\":\"\"}}"
};
struct
{
std::string name;
int priv;
}g_user_priv[] = { {"user", USER_PRIVILEGE_COMMON}
, {"admin", USER_PRIVILEGE_LOCAL_MGR}
, {"owner", USER_PRIVILEGE_TECH_SUPPORTING}
, {"developer", USER_PRIVILEGE_DEVLOPER}
};
static std::string base64_table = "!@#$_~=;/+";
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// user_priv
user_priv::user_priv()
{
simple_ini ini;
std::string text("");
cfg_file_ = utils::get_local_data_path() + PATH_SEPARATOR + "config";
utils::create_folder(cfg_file_.c_str());
cfg_file_ += std::string(PATH_SEPARATOR) + "user.txt";
ini.load(cfg_file_.c_str());
for(auto& v: g_user_priv)
login_times_[v.name] = atoi(ini.get(v.name.c_str(), "login", "0").c_str());
set_where("user");
for(auto& v : device_opt_json)
text += v;
set_opt_json_text(&text[0]);
}
user_priv::~user_priv()
{
simple_ini ini;
ini.load(cfg_file_.c_str());
for(auto& v: login_times_)
{
if(v.second)
ini.set(v.first.c_str(), "login", std::to_string(v.second).c_str());
}
ini.save(cfg_file_.c_str());
}
bool user_priv::login(void)
{
bool ok = false;
for(auto& v: g_user_priv)
{
if(v.name == name_)
{
if(name_ == "user")
{
ok = true;
}
else
{
ok = true;
}
break;
}
}
return ok;
}
void user_priv::reset_privilege(void)
{
priv_ = USER_PRIVILEGE_COMMON;
for(auto& v : g_user_priv)
{
if(v.name == name_)
{
priv_ = v.priv;
break;
}
}
}
// sane_opt_provider
int user_priv::set_value(const char* name, void* val)
{
int ret = SCANNER_ERR_OK;
if(strcmp(name, SANE_OPT_NAME(USER_NAME)) == 0)
{
name_ = (char*)val;
priv_ = USER_PRIVILEGE_COMMON;
}
else if(strcmp(name, SANE_OPT_NAME(USER_PASSWORD)) == 0)
{
pwd_ = (char*)val;
priv_ = USER_PRIVILEGE_COMMON;
}
else if(strcmp(name, SANE_OPT_NAME(DEVICE_SERIAL_NO)) == 0)
{
dev_sn_ = (char*)val;
utils::to_log(LOG_LEVEL_DEBUG, "set user device SN: %s\n", dev_sn_.c_str());
priv_ = USER_PRIVILEGE_COMMON;
}
else if(strcmp(name, SANE_OPT_NAME(LOGIN)) == 0)
{
// login ...
if(login_times_.count(name) == 0)
{
ret = SCANNER_ERR_INVALID_USER_NAME;
}
else
{
if(login())
{
simple_ini ini;
ini.load(cfg_file_.c_str());
login_times_[name]++;
reset_privilege();
ini.set(name, "login", std::to_string(login_times_[name]).c_str());
}
else
{
ret = SCANNER_ERR_INVALID_PASSWORD;
}
}
}
else if(strcmp(name, SANE_OPT_NAME(LOGOUT)) == 0)
{
name_ = pwd_ = "";
reset_privilege();
}
else
{
ret = SCANNER_ERR_DEVICE_NOT_SUPPORT;
}
return ret;
}
bool user_priv::has_privilege(int priv)
{
return priv_ >= priv;
}
int user_priv::get_current_user_login_times(void)
{
if(login_times_.count(name_))
return login_times_[name_];
else
return 0;
}