Leosac  0.8.0
Open Source Access Control
Service.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014-2016 Leosac
3 
4  This file is part of Leosac.
5 
6  Leosac is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Affero General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  Leosac is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Affero General Public License for more details.
15 
16  You should have received a copy of the GNU Affero General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 #pragma once
21 
22 #include "ActionActionParam.hpp"
23 #include "LeosacFwd.hpp"
27 #include "tools/JSONUtils.hpp"
28 #include "tools/log.hpp"
29 #include <boost/asio/io_service.hpp>
30 #include <boost/optional.hpp>
31 #include <future>
32 
33 namespace Leosac
34 {
35 namespace Module
36 {
37 namespace WebSockAPI
38 {
39 
45 class Service
46 {
47  public:
48  Service(WSServer &server)
49  : server_(server)
50  {
51  }
52 
53  using WSHandler = std::function<boost::optional<json>(const RequestContext &)>;
54 
61  template <typename HandlerT>
62  bool register_asio_handler(HandlerT &&handler, const std::string &type,
63  boost::asio::io_service &io)
64  {
65  // We want the `handler` callable to be executed in the current
66  // thread/io_service.
67 
68  // We wrap the user handler into a packaged task that will be `post()`ed
69  // onto the io_service (`io`) queue.
70 
71  WSHandler wrapped_handler =
72  [&, handler](const RequestContext &req_ctx) -> boost::optional<json> {
73  std::packaged_task<boost::optional<json>(const RequestContext &)> pt(
74  [&](const RequestContext &req_ctx) { return handler(req_ctx); });
75  auto future = pt.get_future();
76  io.post([&]() { pt(req_ctx); });
77  return future.get();
78  };
79  return register_typed_handler(wrapped_handler, type);
80  }
81 
87  template <typename HandlerT>
88  bool register_handler(HandlerT &&handler, const std::string &type)
89  {
90  return register_typed_handler(handler, type);
91  }
92 
93  void register_crud_handler(const std::string &resource_name,
95 
109  void unregister_handler(const std::string &name);
110 
111  template <typename HandlerT>
112  bool register_asio_handler_permission(HandlerT &&handler,
113  const std::string &type,
114  ActionActionParam permission,
115  boost::asio::io_service &io)
116  {
117  auto wrapped = [handler, permission](const RequestContext &req_ctx) {
118  req_ctx.security_ctx.enforce_permission(permission.first,
119  permission.second);
120  return handler(req_ctx);
121  };
122  return register_asio_handler(wrapped, type, io);
123  }
124 
125  template <typename HandlerT>
126  bool register_asio_handler_permission(HandlerT &&handler,
127  const std::string &type,
128  SecurityContext::Action permission,
129  boost::asio::io_service &io)
130  {
131  ActionActionParam aap;
132  aap.first = permission;
133  return register_asio_handler_permission(handler, type, aap, io);
134  }
135 
136  private:
141  bool register_typed_handler(const WSHandler &handler, const std::string &type);
142 
144 };
145 }
146 }
147 }
Leosac::Module::WebSockAPI::Service::register_typed_handler
bool register_typed_handler(const WSHandler &handler, const std::string &type)
Register an handler that is ready to be invoked in the websocket thread.
Definition: Service.cpp:30
Leosac::Module::WebSockAPI::Service::server_
WSServer & server_
Definition: Service.hpp:143
ActionActionParam.hpp
CRUDResourceHandler.hpp
LeosacFwd.hpp
Leosac::Module::WebSockAPI::Service::register_asio_handler_permission
bool register_asio_handler_permission(HandlerT &&handler, const std::string &type, SecurityContext::Action permission, boost::asio::io_service &io)
Definition: Service.hpp:126
Leosac::Module::WebSockAPI::Service::register_crud_handler
void register_crud_handler(const std::string &resource_name, CRUDResourceHandler::Factory factory)
Definition: Service.cpp:41
Leosac::Module::WebSockAPI::CRUDResourceHandler::Factory
CRUDResourceHandlerUPtr(*)(RequestContext) Factory
Definition: CRUDResourceHandler.hpp:90
RequestContext.hpp
Leosac::Module::WebSockAPI::Service::register_asio_handler
bool register_asio_handler(HandlerT &&handler, const std::string &type, boost::asio::io_service &io)
Register an handler that will be invoked by the io_service io.
Definition: Service.hpp:62
Leosac::Module::WebSockAPI::ActionActionParam
std::pair< SecurityContext::Action, SecurityContext::ActionParam > ActionActionParam
A pair of action and their parameters.
Definition: ActionActionParam.hpp:36
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Module::WebSockAPI::Service
A service object provided by the Websocket module.
Definition: Service.hpp:45
Leosac::Module::WebSockAPI::Service::WSHandler
std::function< boost::optional< json >(const RequestContext &)> WSHandler
Definition: Service.hpp:53
JSONUtils.hpp
Leosac::Module::WebSockAPI::WSServer
The implementation class that runs the websocket server.
Definition: WSServer.hpp:61
Leosac::SecurityContext::Action
Action
Definition: SecurityContext.hpp:45
Leosac::Module::WebSockAPI::Service::register_handler
bool register_handler(HandlerT &&handler, const std::string &type)
Register a handler for a websocket message.
Definition: Service.hpp:88
WebSockFwd.hpp
Leosac::Module::WebSockAPI::Service::Service
Service(WSServer &server)
Definition: Service.hpp:48
Leosac::Module::WebSockAPI::Service::unregister_handler
void unregister_handler(const std::string &name)
Remove an handler by name.
Definition: Service.cpp:36
log.hpp
Leosac::Module::WebSockAPI::RequestContext
Holds valuable pointer to provide context to a request.
Definition: RequestContext.hpp:36
Leosac::Module::WebSockAPI::Service::register_asio_handler_permission
bool register_asio_handler_permission(HandlerT &&handler, const std::string &type, ActionActionParam permission, boost::asio::io_service &io)
Definition: Service.hpp:112