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 +
16
17#include <Poco/ActiveResult.h>
18
19namespace Mantid::ICat {
20DECLARE_ALGORITHM(CatalogLogin)
21
22namespace {
23std::vector<std::string> namesOfFacilitiesWithICAT() {
24 const auto &config = Kernel::ConfigService::Instance();
25
26 const auto facilityDoesNotHaveICAT = [&](const std::string &name) {
27 return config.getFacility(name).catalogInfo().soapEndPoint().empty();
28 };
29
30 auto result = Kernel::ConfigService::Instance().getFacilityNames();
31 result.erase(std::remove_if(result.begin(), result.end(), facilityDoesNotHaveICAT), result.end());
32
33 return result;
34}
35
36std::string defaultFacility() {
37 // If the currently-set facility has ICAT, then we should use it, else
38 // we'll just pick the first facility that supports ICAT in the list,
39 // else if our Facilities.xml has no ICATs registered, then return an
40 // empty string.
41 const auto facility = Kernel::ConfigService::Instance().getFacility().name();
42 const auto facilitiesWithICAT = namesOfFacilitiesWithICAT();
43 if (std::find(facilitiesWithICAT.begin(), facilitiesWithICAT.end(), facility) != facilitiesWithICAT.end()) {
44 return facility;
45 }
46
47 if (facilitiesWithICAT.size() > 0) {
48 return facilitiesWithICAT[0];
49 }
50
51 return "";
52}
53} // namespace
54
57 auto requireValue = std::make_shared<Kernel::MandatoryValidator<std::string>>();
58 declareProperty("Username", "", requireValue, "The username to log into the catalog.");
59 declareProperty(std::make_unique<Kernel::MaskedProperty<std::string>>("Password", "", requireValue),
60 "The password of the related username to use.");
61 declareProperty("FacilityName", defaultFacility(),
62 std::make_shared<Kernel::StringListValidator>(namesOfFacilitiesWithICAT()),
63 "Select a facility to log in to.");
64 declareProperty("KeepSessionAlive", true, "Keeps the session of the catalog alive if login was successful.");
65 declareProperty(std::make_unique<API::AlgorithmProperty>("KeepAlive", std::make_shared<Kernel::NullValidator>(),
67 "A handle to the KeepAlive algorithm instance that continues "
68 "to keep the catalog alive after this algorithm completes.");
69}
70
73 auto catalogInfo = Kernel::ConfigService::Instance().getFacility(getProperty("FacilityName")).catalogInfo();
74 if (catalogInfo.soapEndPoint().empty())
75 throw std::runtime_error("There is no soap end-point for the facility you have selected.");
76
77 g_log.notice() << "Attempting to verify user credentials against " << catalogInfo.catalogName() << '\n';
78 progress(0.5, "Verifying user credentials...");
79
80 // Creates a new catalog and related session if the authentication is a
81 // success.
82 // This allows us to easily manage sessions alongside catalogs in the
83 // catalogmanager.
84 auto session = API::CatalogManager::Instance().login(getProperty("Username"), getProperty("Password"),
85 catalogInfo.soapEndPoint(), getProperty("FacilityName"));
86
87 progress(0, "Keeping current sessions alive.");
88
89 if (getProperty("KeepSessionAlive") && session) {
90 auto keepAliveAlgorithm = API::AlgorithmManager::Instance().create("CatalogKeepAlive");
91 keepAliveAlgorithm->initialize();
92 keepAliveAlgorithm->setPropertyValue("Session", session->getSessionId());
93 keepAliveAlgorithm->executeAsync();
94
95 // Set the output property to the keep alive algorithm
96 setProperty("KeepAlive", keepAliveAlgorithm);
97 }
98}
99} // namespace Mantid::ICat
#define DECLARE_ALGORITHM(classname)
Definition: Algorithm.h:576
void declareProperty(std::unique_ptr< Kernel::Property > p, const std::string &doc="") override
Add a property to the list of managed properties.
Definition: Algorithm.cpp:1913
TypedValue getProperty(const std::string &name) const override
Get the value of a property.
Definition: Algorithm.cpp:2076
Kernel::Logger & g_log
Definition: Algorithm.h:451
void progress(double p, const std::string &msg="", double estimatedTime=0.0, int progressPrecision=0)
Sends ProgressNotification.
Definition: Algorithm.cpp:231
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:95
A property class for masking the properties.
static T & Instance()
Return a reference to the Singleton instance, creating it if it does not already exist Creation is do...
@ Output
An output workspace.
Definition: Property.h:54