| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | #include "authn.hh" |
|---|
| 21 | #include "compose.hh" |
|---|
| 22 | #include "error_resource.hh" |
|---|
| 23 | #include "reauthn_resource.hh" |
|---|
| 24 | #include "redir_resource.hh" |
|---|
| 25 | #include "request.hh" |
|---|
| 26 | #include "resource.hh" |
|---|
| 27 | #include "session.hh" |
|---|
| 28 | |
|---|
| 29 | #include "../config.hh" |
|---|
| 30 | #include "../db/db.hh" |
|---|
| 31 | #include "../db/group.hh" |
|---|
| 32 | #include "../db/user.hh" |
|---|
| 33 | #include "../logger/logline.hh" |
|---|
| 34 | |
|---|
| 35 | namespace http |
|---|
| 36 | { |
|---|
| 37 | class sub : public resource |
|---|
| 38 | { |
|---|
| 39 | server::conn_cb cb; |
|---|
| 40 | public: |
|---|
| 41 | sub(server::conn_cb cb) |
|---|
| 42 | : cb(cb) |
|---|
| 43 | {} |
|---|
| 44 | protected: |
|---|
| 45 | boost::shared_ptr<response> operator() |
|---|
| 46 | (boost::shared_ptr<request>, response::factory); |
|---|
| 47 | }; |
|---|
| 48 | |
|---|
| 49 | boost::shared_ptr<response> sub::operator() |
|---|
| 50 | (boost::shared_ptr<request> req, response::factory) |
|---|
| 51 | { |
|---|
| 52 | std::string redir = req->get_form_field("redir"); |
|---|
| 53 | if (redir.empty()) |
|---|
| 54 | { |
|---|
| 55 | redir = "https://" + |
|---|
| 56 | config["canonical-name"].as<std::string>() + |
|---|
| 57 | (config["https-port"].as<std::string>() == "443" |
|---|
| 58 | ? "" |
|---|
| 59 | : ":" +config["https-port"].as<std::string>())+ |
|---|
| 60 | "/"; |
|---|
| 61 | } |
|---|
| 62 | boost::shared_ptr<resource> resp(new redir_resource |
|---|
| 63 | (cb, redir, "303 See other")); |
|---|
| 64 | bool sub; |
|---|
| 65 | if (req->get_path() == "/sub") |
|---|
| 66 | { |
|---|
| 67 | sub = true; |
|---|
| 68 | } |
|---|
| 69 | else if (req->get_path() == "/unsub") |
|---|
| 70 | { |
|---|
| 71 | sub = false; |
|---|
| 72 | } |
|---|
| 73 | else |
|---|
| 74 | { |
|---|
| 75 | resp.reset(new error_resource(cb, "404 Not found")); |
|---|
| 76 | throw resource_exception(resp); |
|---|
| 77 | } |
|---|
| 78 | db::group::ptr gr; |
|---|
| 79 | try |
|---|
| 80 | { |
|---|
| 81 | gr = cb.dbase().lookup_group(req->get_form_field("group")); |
|---|
| 82 | } |
|---|
| 83 | catch (db::no_such_group) |
|---|
| 84 | { |
|---|
| 85 | logger::logline ll; |
|---|
| 86 | ll << "no such group " << req->get_form_field("group"); |
|---|
| 87 | throw resource_exception(resp); |
|---|
| 88 | } |
|---|
| 89 | if (!req->is_authenticated()) |
|---|
| 90 | { |
|---|
| 91 | logger::logline ll; |
|---|
| 92 | ll << "not logged in "; |
|---|
| 93 | throw resource_exception(resp); |
|---|
| 94 | } |
|---|
| 95 | if (req->get_method() != "POST") throw resource_exception(resp); |
|---|
| 96 | db::user::ptr u = req->get_user(); |
|---|
| 97 | if (!u) throw resource_exception(resp); |
|---|
| 98 | if (sub) |
|---|
| 99 | { |
|---|
| 100 | gr->add_subscriber(u, cb, req->get_peer()); |
|---|
| 101 | } |
|---|
| 102 | else |
|---|
| 103 | { |
|---|
| 104 | gr->delete_subscriber(u, cb); |
|---|
| 105 | } |
|---|
| 106 | throw resource_exception(resp); |
|---|
| 107 | } |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | namespace |
|---|
| 111 | { |
|---|
| 112 | class factory : public server::http_resource_factory |
|---|
| 113 | { |
|---|
| 114 | public: |
|---|
| 115 | factory() { |
|---|
| 116 | server::register_http_resource("/sub", this); |
|---|
| 117 | server::register_http_resource("/unsub", this); |
|---|
| 118 | } |
|---|
| 119 | boost::shared_ptr<http::resource> operator() |
|---|
| 120 | (server::conn_cb cb,std::string) { |
|---|
| 121 | boost::shared_ptr<http::resource> rv |
|---|
| 122 | (new http::sub(cb)); |
|---|
| 123 | return rv; |
|---|
| 124 | } |
|---|
| 125 | }; |
|---|
| 126 | factory f; |
|---|
| 127 | } |
|---|