1 #include "otsdaq/WebUsersUtilities/RemoteWebUsers.h"
3 #include "otsdaq/CgiDataUtilities/CgiDataUtilities.h"
4 #include "otsdaq/SOAPUtilities/SOAPCommand.h"
5 #include "otsdaq/SOAPUtilities/SOAPParameters.h"
6 #include "otsdaq/SOAPUtilities/SOAPUtilities.h"
7 #include "otsdaq/XmlUtilities/HttpXmlDocument.h"
14 #include "otsdaq/SupervisorInfo/AllSupervisorInfo.h"
19 #define __MF_SUBJECT__ "RemoteWebUsers"
31 xdaq::Application* application,
32 XDAQ_CONST_CALL xdaq::ApplicationDescriptor* gatewaySupervisorDescriptor)
34 , gatewaySupervisorDescriptor_(gatewaySupervisorDescriptor)
36 ActiveUserLastUpdateTime_ = 0;
45 std::ostringstream* out,
54 XDAQ_CONST_CALL xdaq::ApplicationDescriptor* gatewaySupervisor;
57 xoap::MessageReference retMsg;
65 if(allSupervisorInfo.
isWizardMode() || allSupervisorInfo.isMacroMakerMode())
68 std::string sequence =
71 if(!sequence.length())
73 __COUT_ERR__ <<
"Invalid access attempt (@" << userInfo.ip_ <<
")."
75 *out << WebUsers::REQ_NO_LOGIN_RESPONSE;
77 goto HANDLE_ACCESS_FAILURE;
83 gatewaySupervisor = allSupervisorInfo.getWizardInfo().
getDescriptor();
85 gatewaySupervisor = allSupervisorInfo.getAllMacroMakerTypeSupervisorInfo()
87 ->second.getDescriptor();
89 if(!gatewaySupervisor)
91 __COUT_ERR__ <<
"Missing gateway supervisor." << std::endl;
92 *out << WebUsers::REQ_NO_LOGIN_RESPONSE;
94 goto HANDLE_ACCESS_FAILURE;
97 parameters.addParameter(
"sequence", sequence);
98 parameters.addParameter(
"IPAddress", userInfo.ip_);
99 retMsg = SOAPMessenger::sendWithSOAPReply(
100 gatewaySupervisor,
"SupervisorSequenceCheck", parameters);
102 parameters.addParameter(
"Permissions");
103 SOAPUtilities::receive(retMsg, parameters);
108 cgi, out, xmldoc, userInfo,
true , sequence))
111 goto HANDLE_ACCESS_FAILURE;
116 gatewaySupervisor = allSupervisorInfo.getGatewayInfo().
getDescriptor();
118 if(!gatewaySupervisor)
120 __COUT_ERR__ <<
"Missing gateway supervisor." << std::endl;
121 *out << WebUsers::REQ_NO_LOGIN_RESPONSE;
122 goto HANDLE_ACCESS_FAILURE;
131 bool cacheHit =
false;
133 std::lock_guard<std::mutex> cacheLock(cookieCheckCacheMutex_);
134 auto cacheIt = cookieCheckCache_.find(userInfo.cookieCode_);
135 if(cacheIt != cookieCheckCache_.end() &&
136 (time(0) - cacheIt->second.cacheTime) < COOKIE_CHECK_CACHE_TTL)
138 if(!(userInfo.requireLock_ && cacheIt->second.userWithLock.empty()))
141 userInfo.cookieCode_ = cacheIt->second.cookieCode;
142 userInfo.username_ = cacheIt->second.username;
143 userInfo.displayName_ = cacheIt->second.displayName;
144 userInfo.usernameWithLock_ = cacheIt->second.userWithLock;
153 goto HANDLE_ACCESS_FAILURE;
161 const std::string originalCookieCode = userInfo.cookieCode_;
164 parameters.addParameter(
"CookieCode", userInfo.cookieCode_);
165 parameters.addParameter(
"RefreshOption", userInfo.automatedCommand_ ?
"0" :
"1");
166 parameters.addParameter(
"IPAddress", userInfo.ip_);
167 parameters.addParameter(
"RequireLock", userInfo.requireLock_ ?
"1" :
"0");
169 retMsg = SOAPMessenger::sendWithSOAPReply(
170 gatewaySupervisor,
"SupervisorCookieCheck", parameters);
173 parameters.addParameter(
"CookieCode");
174 parameters.addParameter(
"Permissions");
175 parameters.addParameter(
"UserGroups");
176 parameters.addParameter(
"UserWithLock");
177 parameters.addParameter(
"Username");
178 parameters.addParameter(
"DisplayName");
180 SOAPUtilities::receive(retMsg, parameters);
185 userInfo.cookieCode_ = parameters.getValue(
"CookieCode");
186 userInfo.username_ = parameters.getValue(
"Username");
187 userInfo.displayName_ = parameters.getValue(
"DisplayName");
188 userInfo.usernameWithLock_ = parameters.getValue(
"UserWithLock");
192 if(userInfo.cookieCode_.length() == WebUsers::COOKIE_CODE_LENGTH)
194 std::lock_guard<std::mutex> cacheLock(cookieCheckCacheMutex_);
195 cookieCheckCache_[originalCookieCode] = {time(0),
196 userInfo.cookieCode_,
197 parameters.getValue(
"Permissions"),
199 userInfo.displayName_,
200 userInfo.usernameWithLock_};
203 if(cookieCheckCache_.size() > 10)
205 time_t now = time(0);
206 for(
auto it = cookieCheckCache_.begin(); it != cookieCheckCache_.end();)
208 if(now - it->second.cacheTime > 2 * COOKIE_CHECK_CACHE_TTL)
209 it = cookieCheckCache_.erase(it);
219 goto HANDLE_ACCESS_FAILURE;
226 HANDLE_ACCESS_FAILURE:
229 if(!userInfo.automatedCommand_)
230 __COUT_ERR__ <<
"Failed request (requestType = " << userInfo.requestType_
231 <<
"): " << out->str() << __E__;
241 if(time(0) - ActiveUserLastUpdateTime_ >
242 ACTIVE_USERS_UPDATE_THRESHOLD)
244 __COUTS__(2) <<
"Need to update active user list" << std::endl;
246 xoap::MessageReference retMsg = ots::SOAPMessenger::sendWithSOAPReply(
250 SOAPUtilities::receive(retMsg, retParameters);
252 ActiveUserLastUpdateTime_ = time(0);
253 return (ActiveUserList_ = retParameters.getValue(
"UserList"));
256 return ActiveUserList_;
266 std::map<std::string ,
267 std::tuple<std::string ,
269 std::string >>& theGroups)
271 xoap::MessageReference retMsg =
273 "SupervisorLastTableGroupRequest",
277 retParameters.addParameter(
"GroupName");
278 retParameters.addParameter(
"GroupKey");
279 retParameters.addParameter(
"GroupAction");
280 retParameters.addParameter(
"GroupActionTime");
281 SOAPUtilities::receive(retMsg, retParameters);
284 std::vector<std::string> groupNames =
286 std::vector<std::string> groupKeys =
288 std::vector<std::string> groupActions =
291 retParameters.getValue(
"GroupActionTime"), {
','});
293 if(groupNames.size() < 2)
296 __SS__ <<
"Failure in handling request for recent config group activity. "
297 "Response received was this: \n"
298 << SOAPUtilities::translate(retMsg) << __E__;
302 if(groupNames.size() != groupKeys.size() ||
303 groupNames.size() != groupActions.size() || groupNames.size() != groupTimes.size())
305 __SS__ <<
"Illegal list size mismatch while retrieving recent config group info. "
306 "Should not be possible! Notify admins."
311 for(
size_t i = 0; i < groupNames.size(); ++i)
313 theGroups[groupActions[i]] = std::make_tuple(
314 groupNames[i], strtol(groupKeys[i].c_str(), 0, 0), groupTimes[i]);
317 __COUTT__ <<
"Done with getLastTableGroups()" << __E__;
327 const std::string& actionOfLastGroup, std::string& actionTimeString)
329 actionTimeString =
"";
330 xoap::MessageReference retMsg = ots::SOAPMessenger::sendWithSOAPReply(
332 "SupervisorLastTableGroupRequest",
336 retParameters.addParameter(
"GroupName");
337 retParameters.addParameter(
"GroupKey");
338 retParameters.addParameter(
"GroupAction");
339 retParameters.addParameter(
"GroupActionTime");
340 SOAPUtilities::receive(retMsg, retParameters);
343 if(retParameters.getValue(
"GroupAction") !=
346 __SS__ <<
"Returned group action '" << retParameters.getValue(
"GroupAction")
347 <<
"' does not match requested group action '" << actionOfLastGroup <<
".'"
353 theGroup.first = retParameters.getValue(
"GroupName");
354 theGroup.second = strtol(retParameters.getValue(
"GroupKey").c_str(), 0, 0);
355 actionTimeString = retParameters.getValue(
"GroupActionTime");
364 const std::string& message,
375 const std::string& subject,
376 const std::string& message,
380 parameters.addParameter(
"ToUser", toUser);
381 parameters.addParameter(
"Subject", subject);
382 parameters.addParameter(
"Message", message);
383 parameters.addParameter(
"DoEmail", doEmail ?
"1" :
"0");
385 xoap::MessageReference retMsg = SOAPMessenger::sendWithSOAPReply(
397 parameters.addParameter(
"EntryText", entryText);
399 xoap::MessageReference retMsg = SOAPMessenger::sendWithSOAPReply(
bool isWizardMode(void) const
BOOLs.
static std::string getOrPostData(cgicc::Cgicc &cgi, const std::string &needle)
XDAQ_CONST_CALL xdaq::ApplicationDescriptor * gatewaySupervisorDescriptor_
void getLastTableGroups(std::map< std::string, std::tuple< std::string, TableGroupKey, std::string >> &theGroups)
RemoteWebUsers(xdaq::Application *application, XDAQ_CONST_CALL xdaq::ApplicationDescriptor *gatewaySupervisorDescriptor)
std::pair< std::string, TableGroupKey > getLastTableGroup(const std::string &actionOfLastGroup, std::string &returnedActionTimeString)
actionOfLastGroup = "Configured" or "Started", for example
std::string getActiveUserList(void)
void sendSystemMessage(const std::string &toUser, const std::string &message, bool doEmail=false)
void makeSystemLogEntry(const std::string &entryText)
bool xmlRequestToGateway(cgicc::Cgicc &cgi, std::ostringstream *out, HttpXmlDocument *xmldoc, const AllSupervisorInfo &allSupervisorInfo, WebUsers::RequestUserInfo &userInfo)
XDAQ_CONST_CALL xdaq::ApplicationDescriptor * getDescriptor(void) const
Getters ----------------—.
static bool checkRequestAccess(cgicc::Cgicc &cgi, std::ostringstream *out, HttpXmlDocument *xmldoc, WebUsers::RequestUserInfo &userInfo, bool isWizardMode=false, const std::string &wizardModeSequence="")
static void initializeRequestUserInfo(cgicc::Cgicc &cgi, WebUsers::RequestUserInfo &userInfo)
used by gateway and other supervisors to verify requests consistently
defines used also by OtsConfigurationWizardSupervisor
static void getVectorFromString(const std::string &inputString, std::vector< std::string > &listToReturn, const std::set< char > &delimiter={',', '|', '&'}, const std::set< char > &whitespace={' ', '\t', '\n', '\r'}, std::vector< char > *listOfDelimiters=0, bool decodeURIComponents=false)
bool setGroupPermissionLevels(const std::string &groupPermissionLevelsString)
end setGroupPermissionLevels()