Leosac  0.8.0
Open Source Access Control
CRUDResourceHandler.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 
25 #include <boost/optional.hpp>
26 #include <nlohmann/json.hpp>
27 #include <vector>
28 
29 namespace Leosac
30 {
31 namespace Module
32 {
33 namespace WebSockAPI
34 {
35 using json = nlohmann::json;
36 
41 {
42  public:
43  enum class Verb
44  {
45  CREATE,
46  READ,
47  UPDATE,
48  DELETE
49  };
50  boost::optional<json> process(const ClientMessage &msg);
51 
52  protected:
56  using ActionActionParam =
57  std::pair<SecurityContext::Action, SecurityContext::ActionParam>;
58 
62  virtual UserSecurityContext &security_context() const = 0;
63 
64  private:
65  virtual std::vector<ActionActionParam>
66  required_permission(Verb verb, const json &req) const = 0;
67 
68  virtual boost::optional<json> create_impl(const json &req) = 0;
69 
70  virtual boost::optional<json> read_impl(const json &req) = 0;
71 
72  virtual boost::optional<json> update_impl(const json &req) = 0;
73 
74  virtual boost::optional<json> delete_impl(const json &req) = 0;
75 
76  void enforce_permission(const std::vector<ActionActionParam> &);
77 
78  static Verb verb_from_request_type(const std::string &);
79 };
80 
85 {
86  protected:
88 
89  public:
91 
93 
94  protected:
96 
97  virtual UserSecurityContext &security_context() const override;
98 };
99 
104 {
105  public:
107 
108 
109  protected:
111 
113 
114  virtual UserSecurityContext &security_context() const override;
115 };
116 }
117 }
118 }
Leosac::Module::WebSockAPI::ExternalCRUDResourceHandler
For other module to use.
Definition: CRUDResourceHandler.hpp:103
Leosac::UserSecurityContext
A SecurityContext object for users.
Definition: UserSecurityContext.hpp:31
Leosac::Module::WebSockAPI::ICRUDResourceHandler::verb_from_request_type
static Verb verb_from_request_type(const std::string &)
Definition: CRUDResourceHandler.cpp:32
Leosac::Module::WebSockAPI::ICRUDResourceHandler::enforce_permission
void enforce_permission(const std::vector< ActionActionParam > &)
Definition: CRUDResourceHandler.cpp:50
Leosac::Module::WebSockAPI::ICRUDResourceHandler::read_impl
virtual boost::optional< json > read_impl(const json &req)=0
Leosac::Module::WebSockAPI::ExternalCRUDResourceHandlerUPtr
std::unique_ptr< ExternalCRUDResourceHandler > ExternalCRUDResourceHandlerUPtr
Definition: WebSockFwd.hpp:42
json
nlohmann::json json
Definition: WSServer.cpp:76
Leosac::Module::WebSockAPI::CRUDResourceHandlerUPtr
std::unique_ptr< CRUDResourceHandler > CRUDResourceHandlerUPtr
Definition: WebSockFwd.hpp:39
Leosac::Module::WebSockAPI::CRUDResourceHandler
Base CRUD handler for use within the websocket module.
Definition: CRUDResourceHandler.hpp:84
Leosac::Module::WebSockAPI::ClientMessage
A message sent by a client to Leosac.
Definition: Messages.hpp:54
Leosac::Module::WebSockAPI::ICRUDResourceHandler
A common interface for CRUD handler.
Definition: CRUDResourceHandler.hpp:40
Leosac::Module::WebSockAPI::ICRUDResourceHandler::Verb::DELETE
@ DELETE
Leosac::Module::WebSockAPI::CRUDResourceHandler::Factory
CRUDResourceHandlerUPtr(*)(RequestContext) Factory
Definition: CRUDResourceHandler.hpp:90
RequestContext.hpp
Leosac::Module::WebSockAPI::ICRUDResourceHandler::Verb::CREATE
@ CREATE
Leosac::Module::WebSockAPI::ActionActionParam
std::pair< SecurityContext::Action, SecurityContext::ActionParam > ActionActionParam
A pair of action and their parameters.
Definition: ActionActionParam.hpp:36
Leosac::Module::WebSockAPI::ICRUDResourceHandler::Verb::READ
@ READ
Leosac::Module::WebSockAPI::ICRUDResourceHandler::delete_impl
virtual boost::optional< json > delete_impl(const json &req)=0
Leosac::Module::WebSockAPI::ExternalCRUDResourceHandler::ExternalCRUDResourceHandler
ExternalCRUDResourceHandler(ModuleRequestContext ctx)
Definition: CRUDResourceHandler.cpp:104
Leosac::Module::WebSockAPI::ICRUDResourceHandler::Verb::UPDATE
@ UPDATE
Leosac::Module::WebSockAPI::CRUDResourceHandler::instanciate
static CRUDResourceHandlerUPtr instanciate(RequestContext)
Definition: CRUDResourceHandler.cpp:91
Leosac
This is the header file for a generated source file, GitSHA1.cpp.
Definition: APIStatusCode.hpp:22
Leosac::Module::WebSockAPI::ExternalCRUDResourceHandler::ctx_
ModuleRequestContext ctx_
Definition: CRUDResourceHandler.hpp:112
Leosac::Module::WebSockAPI::ICRUDResourceHandler::required_permission
virtual std::vector< ActionActionParam > required_permission(Verb verb, const json &req) const =0
Leosac::Module::WebSockAPI::CRUDResourceHandler::ctx_
RequestContext ctx_
Definition: CRUDResourceHandler.hpp:95
Leosac::Module::WebSockAPI::ModuleRequestContext
A request context dedicated for websocket request that are handled by other modules.
Definition: RequestContext.hpp:63
Leosac::Module::WebSockAPI::ExternalCRUDResourceHandler::Factory
ExternalCRUDResourceHandlerUPtr(*)(ModuleRequestContext) Factory
Definition: CRUDResourceHandler.hpp:106
Leosac::Module::WebSockAPI::ICRUDResourceHandler::update_impl
virtual boost::optional< json > update_impl(const json &req)=0
Leosac::Module::WebSockAPI::ExternalCRUDResourceHandler::security_context
virtual UserSecurityContext & security_context() const override
Helper function that returns the security context.
Definition: CRUDResourceHandler.cpp:109
Leosac::Module::WebSockAPI::ICRUDResourceHandler::security_context
virtual UserSecurityContext & security_context() const =0
Helper function that returns the security context.
WebSockFwd.hpp
Leosac::Module::WebSockAPI::ICRUDResourceHandler::create_impl
virtual boost::optional< json > create_impl(const json &req)=0
Leosac::Module::WebSockAPI::json
nlohmann::json json
Definition: AccessOverview.hpp:30
Leosac::Module::WebSockAPI::RequestContext
Holds valuable pointer to provide context to a request.
Definition: RequestContext.hpp:36
UserSecurityContext.hpp
Leosac::Module::WebSockAPI::ICRUDResourceHandler::Verb
Verb
Definition: CRUDResourceHandler.hpp:43
Leosac::Module::WebSockAPI::CRUDResourceHandler::CRUDResourceHandler
CRUDResourceHandler(RequestContext ctx)
Definition: CRUDResourceHandler.cpp:86
Leosac::Module::WebSockAPI::CRUDResourceHandler::security_context
virtual UserSecurityContext & security_context() const override
Helper function that returns the security context.
Definition: CRUDResourceHandler.cpp:96
Leosac::Module::WebSockAPI::ICRUDResourceHandler::process
boost::optional< json > process(const ClientMessage &msg)
Definition: CRUDResourceHandler.cpp:64