Leosac  0.7.0
OpenSourceAccessControl
CRUDResourceHandler.cpp
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 
21 #include "Exceptions.hpp"
22 #include "WSServer.hpp"
24 #include "tools/log.hpp"
25 #include <boost/algorithm/string/predicate.hpp>
26 
27 using namespace Leosac;
28 using namespace Leosac::Module;
29 using namespace Leosac::Module::WebSockAPI;
30 
33 {
34  if (boost::algorithm::ends_with(req, ".read"))
35  return Verb::READ;
36  else if (boost::algorithm::ends_with(req, ".create"))
37  return Verb::CREATE;
38  else if (boost::algorithm::ends_with(req, ".update"))
39  return Verb::UPDATE;
40  else if (boost::algorithm::ends_with(req, ".delete"))
41  return Verb::DELETE;
42  else
43  {
44  ASSERT_LOG(0, "Invalid request type {" << req
45  << "} for CRUD resource handler");
46  throw LEOSACException("Should not be here");
47  }
48 }
49 
51  const std::vector<ActionActionParam> &permissions)
52 {
53  auto &security_ctx = security_context();
54  for (const auto &action_and_param : permissions)
55  {
56  if (!security_ctx.check_permission(action_and_param.first,
57  action_and_param.second))
58  {
59  throw PermissionDenied();
60  }
61  }
62 }
63 
64 boost::optional<json> ICRUDResourceHandler::process(const ClientMessage &msg)
65 {
67  switch (verb_from_request_type(msg.type))
68  {
69  case Verb::READ:
70  enforce_permission(perms);
71  return read_impl(msg.content);
72  case Verb::CREATE:
73  enforce_permission(perms);
74  return create_impl(msg.content);
75  case Verb::UPDATE:
76  enforce_permission(perms);
77  return update_impl(msg.content);
78  case Verb::DELETE:
79  enforce_permission(perms);
80  return delete_impl(msg.content);
81  }
82  ASSERT_LOG(0, "Should not be here.");
83  throw LEOSACException("Should not be here");
84 }
85 
87  : ctx_(ctx)
88 {
89 }
90 
92 {
93  return nullptr;
94 }
95 
97 {
98  auto wsc =
99  dynamic_cast<UserSecurityContext *>(&ctx_.session->security_context());
100  ASSERT_LOG(wsc, "SecurityContext has unexpected type.");
101  return *wsc;
102 }
103 
105  : ctx_(ctx)
106 {
107 }
108 
110 {
111  return *ctx_.security_ctx;
112 }
virtual std::vector< ActionActionParam > required_permission(Verb verb, const json &req) const =0
This is the header file for a generated source file, GitSHA1.cpp.
virtual boost::optional< json > update_impl(const json &req)=0
virtual boost::optional< json > create_impl(const json &req)=0
A request context dedicated for websocket request that are handled by other modules.
virtual UserSecurityContext & security_context() const override
Helper function that returns the security context.
std::unique_ptr< CRUDResourceHandler > CRUDResourceHandlerUPtr
Definition: WebSockFwd.hpp:39
static Verb verb_from_request_type(const std::string &)
An exception that can be throw when the permission for a given operation is denied.
boost::optional< json > process(const ClientMessage &msg)
All modules that provides features to Leosac shall be in this namespace.
#define ASSERT_LOG(cond, msg)
Definition: log.hpp:221
virtual UserSecurityContext & security_context() const override
Helper function that returns the security context.
A base class for Leosac specific exception.
static CRUDResourceHandlerUPtr instanciate(RequestContext)
A message sent by a client to Leosac.
Definition: Messages.hpp:54
UserSecurityContext * security_ctx
The object lifetime will not expand past the current request processing.
virtual UserSecurityContext & security_context() const =0
Helper function that returns the security context.
virtual boost::optional< json > delete_impl(const json &req)=0
Holds valuable pointer to provide context to a request.
A SecurityContext object for users.
virtual boost::optional< json > read_impl(const json &req)=0
void enforce_permission(const std::vector< ActionActionParam > &)