/
CatalogLogin.cpp
76 lines (67 loc) · 3.03 KB
/
CatalogLogin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "MantidICat/CatalogLogin.h"
#include "MantidAPI/AlgorithmManager.h"
#include "MantidAPI/CatalogManager.h"
#include "MantidAPI/AlgorithmProperty.h"
#include "MantidKernel/ConfigService.h"
#include "MantidKernel/FacilityInfo.h"
#include "MantidKernel/ListValidator.h"
#include "MantidKernel/MandatoryValidator.h"
#include "MantidKernel/MaskedProperty.h"
#include <Poco/ActiveResult.h>
namespace Mantid {
namespace ICat {
DECLARE_ALGORITHM(CatalogLogin)
/// Init method to declare algorithm properties
void CatalogLogin::init() {
auto requireValue =
boost::make_shared<Kernel::MandatoryValidator<std::string>>();
declareProperty("Username", "", requireValue,
"The username to log into the catalog.");
declareProperty(Kernel::make_unique<Kernel::MaskedProperty<std::string>>(
"Password", "", requireValue),
"The password of the related username to use.");
declareProperty("FacilityName",
Kernel::ConfigService::Instance().getFacility().name(),
boost::make_shared<Kernel::StringListValidator>(
Kernel::ConfigService::Instance().getFacilityNames()),
"Select a facility to log in to.");
declareProperty(
"KeepSessionAlive", true,
"Keeps the session of the catalog alive if login was successful.");
declareProperty(Kernel::make_unique<API::AlgorithmProperty>(
"KeepAlive", boost::make_shared<Kernel::NullValidator>(),
Kernel::Direction::Output),
"A handle to the KeepAlive algorithm instance that continues "
"to keep the catalog alive after this algorithm completes.");
}
/// execute the algorithm
void CatalogLogin::exec() {
auto catalogInfo = Kernel::ConfigService::Instance()
.getFacility(getProperty("FacilityName"))
.catalogInfo();
if (catalogInfo.soapEndPoint().empty())
throw std::runtime_error(
"There is no soap end-point for the facility you have selected.");
g_log.notice() << "Attempting to verify user credentials against "
<< catalogInfo.catalogName() << std::endl;
progress(0.5, "Verifying user credentials...");
// Creates a new catalog and related session if the authentication is a
// success.
// This allows us to easily manage sessions alongside catalogs in the
// catalogmanager.
auto session = API::CatalogManager::Instance().login(
getProperty("Username"), getProperty("Password"),
catalogInfo.soapEndPoint(), getProperty("FacilityName"));
progress(0, "Keeping current sessions alive.");
if (getProperty("KeepSessionAlive") && session) {
auto keepAliveAlgorithm =
API::AlgorithmManager::Instance().create("CatalogKeepAlive");
keepAliveAlgorithm->initialize();
keepAliveAlgorithm->setPropertyValue("Session", session->getSessionId());
keepAliveAlgorithm->executeAsync();
// Set the output property to the keep alive algorithm
setProperty("KeepAlive", keepAliveAlgorithm);
}
}
}
}