Mantid
Loading...
Searching...
No Matches
CatalogLogin.cpp
Go to the documentation of this file.
1// Mantid Repository : https://github.com/mantidproject/mantid
2//
3// Copyright © 2018 ISIS Rutherford Appleton Laboratory UKRI,
4// NScD Oak Ridge National Laboratory, European Spallation Source,
5// Institut Laue - Langevin & CSNS, Institute of High Energy Physics, CAS
6// SPDX - License - Identifier: GPL - 3.0 +
17
18#include <Poco/ActiveResult.h>
19
20namespace Mantid::ICat {
21DECLARE_ALGORITHM(CatalogLogin)
22
23namespace {
24std::vector<std::string> namesOfFacilitiesWithICAT() {
25 const auto &config = Kernel::ConfigService::Instance();
26
27 const auto facilityDoesNotHaveICAT = [&](const std::string &name) {
28 return config.getFacility(name).catalogInfo().soapEndPoint().empty();
29 };
30
31 auto result = Kernel::ConfigService::Instance().getFacilityNames();
32 result.erase(std::remove_if(result.begin(), result.end(), facilityDoesNotHaveICAT), result.end());
33
34 return result;
35}
36
37std::string defaultFacility() {
38 // If the currently-set facility has ICAT, then we should use it, else
39 // we'll just pick the first facility that supports ICAT in the list,
40 // else if our Facilities.xml has no ICATs registered, then return an
41 // empty string.
42 const auto facility = Kernel::ConfigService::Instance().getFacility().name();
43 const auto facilitiesWithICAT = namesOfFacilitiesWithICAT();
44 if (std::find(facilitiesWithICAT.begin(), facilitiesWithICAT.end(), facility) != facilitiesWithICAT.end()) {
45 return facility;
46 }
47
48 if (facilitiesWithICAT.size() > 0) {
49 return facilitiesWithICAT[0];
50 }
51
52 return "";
53}
54} // namespace
55
58 auto requireValue = std::make_shared<Kernel::MandatoryValidator<std::string>>();
59 declareProperty("Username", "", requireValue, "The username to log into the catalog.");
60 declareProperty(std::make_unique<Kernel::MaskedProperty<std::string>>("Password", "", requireValue),
61 "The password of the related username to use.");
62 declareProperty("FacilityName", defaultFacility(),
63 std::make_shared<Kernel::StringListValidator>(namesOfFacilitiesWithICAT()),
64 "Select a facility to log in to.");
65 declareProperty("KeepSessionAlive", true, "Keeps the session of the catalog alive if login was successful.");
66 declareProperty(std::make_unique<API::AlgorithmProperty>("KeepAlive", std::make_shared<Kernel::NullValidator>(),
68 "A handle to the KeepAlive algorithm instance that continues "
69 "to keep the catalog alive after this algorithm completes.");
70}
71
74 auto catalogInfo = Kernel::ConfigService::Instance().getFacility(getProperty("FacilityName")).catalogInfo();
75 if (catalogInfo.soapEndPoint().empty())
76 throw std::runtime_error("There is no soap end-point for the facility you have selected.");
77
78 g_log.notice() << "Attempting to verify user credentials against " << catalogInfo.catalogName() << '\n';
79 progress(0.5, "Verifying user credentials...");
80
81 // Creates a new catalog and related session if the authentication is a
82 // success.
83 // This allows us to easily manage sessions alongside catalogs in the
84 // catalogmanager.
85 auto session = API::CatalogManager::Instance().login(getProperty("Username"), getProperty("Password"),
86 catalogInfo.soapEndPoint(), getProperty("FacilityName"));
87
88 progress(0, "Keeping current sessions alive.");
89
90 if (getProperty("KeepSessionAlive") && session) {
91 auto keepAliveAlgorithm = API::AlgorithmManager::Instance().create("CatalogKeepAlive");
92 keepAliveAlgorithm->initialize();
93 keepAliveAlgorithm->setPropertyValue("Session", session->getSessionId());
94 keepAliveAlgorithm->executeAsync();
95
96 // Set the output property to the keep alive algorithm
97 setProperty("KeepAlive", keepAliveAlgorithm);
98 }
99}
100} // namespace Mantid::ICat
std::string name
Definition Run.cpp:60
#define DECLARE_ALGORITHM(classname)
Definition Algorithm.h:547
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Kernel::Logger & g_log
Definition Algorithm.h:426
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
void exec() override
Overwrites Algorithm method.
void init() override
Overwrites Algorithm method.
IPropertyManager * setProperty(const std::string &name, const T &value)
Templated method to set the value of a PropertyWithValue.
void notice(const std::string &msg)
Logs at notice level.
Definition Logger.cpp:126
A property class for masking the properties.
@ Output
An output workspace.
Definition Property.h:55