| 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 "redir_resource.hh" |
|---|
| 24 | #include "request.hh" |
|---|
| 25 | #include "templated_resource.hh" |
|---|
| 26 | #include "session.hh" |
|---|
| 27 | #include "token.hh" |
|---|
| 28 | |
|---|
| 29 | #include "../config.hh" |
|---|
| 30 | #include "../db/db.hh" |
|---|
| 31 | #include "../db/user.hh" |
|---|
| 32 | #include "../db/user_exists.hh" |
|---|
| 33 | #include "../html/util.hh" |
|---|
| 34 | #include "../logger/logline.hh" |
|---|
| 35 | #include "../msg/lexutils.hh" |
|---|
| 36 | #include "../server.hh" |
|---|
| 37 | #include "../smtp_client/smtp_client.hh" |
|---|
| 38 | #include "../tlate/tlate.hh" |
|---|
| 39 | #include "../util.hh" |
|---|
| 40 | |
|---|
| 41 | #include <boost/nondet_random.hpp> |
|---|
| 42 | #include <boost/shared_ptr.hpp> |
|---|
| 43 | |
|---|
| 44 | namespace http |
|---|
| 45 | { |
|---|
| 46 | class reguser : public templated_resource |
|---|
| 47 | { |
|---|
| 48 | public: |
|---|
| 49 | reguser(server::conn_cb cb) |
|---|
| 50 | : templated_resource(cb, "reguser.html") |
|---|
| 51 | {} |
|---|
| 52 | |
|---|
| 53 | void set_attributes(boost::shared_ptr<request>, |
|---|
| 54 | tlate::data_model::ptr); |
|---|
| 55 | |
|---|
| 56 | void reload_action(boost::shared_ptr<request> req, |
|---|
| 57 | tlate::data_model::ptr, |
|---|
| 58 | std::string error_message = ""); |
|---|
| 59 | |
|---|
| 60 | void post_action(boost::shared_ptr<request> req, |
|---|
| 61 | tlate::data_model::ptr); |
|---|
| 62 | }; |
|---|
| 63 | |
|---|
| 64 | void reguser::reload_action(boost::shared_ptr<request> req, |
|---|
| 65 | tlate::data_model::ptr am, |
|---|
| 66 | std::string error_message) |
|---|
| 67 | { |
|---|
| 68 | if (!error_message.empty()) |
|---|
| 69 | am->insert("error", html::quote(error_message, false)); |
|---|
| 70 | am->insert("userid", req->get_form_field("userid")); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | void reguser::post_action(boost::shared_ptr<request> req, |
|---|
| 74 | tlate::data_model::ptr am) |
|---|
| 75 | { |
|---|
| 76 | boost::random_device rand; |
|---|
| 77 | std::string (*const pd)(std::string) = uri::percent_decode; |
|---|
| 78 | |
|---|
| 79 | std::string pw1 = req->get_form_field("passwd"); |
|---|
| 80 | std::string pw2 = req->get_form_field("repass"); |
|---|
| 81 | |
|---|
| 82 | std::string err = db::user::passwd_problems(pw1); |
|---|
| 83 | if (pw1 != pw2) err = "Passwords did not match."; |
|---|
| 84 | |
|---|
| 85 | if (!err.empty()) |
|---|
| 86 | { |
|---|
| 87 | reload_action(req, am, err); |
|---|
| 88 | return; |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | std::string uid = pd(req->get_form_field("userid")); |
|---|
| 92 | if (uid.find_first_of("\t ") != std::string::npos) |
|---|
| 93 | { |
|---|
| 94 | reload_action(req, am, |
|---|
| 95 | "User id should not contain whitespace."); |
|---|
| 96 | return; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | boost::shared_ptr<db::user> u; |
|---|
| 100 | try |
|---|
| 101 | { |
|---|
| 102 | u = cb.dbase().create_user(pd(req->get_form_field("userid")), |
|---|
| 103 | pw1); |
|---|
| 104 | } |
|---|
| 105 | catch (db::user_exists) |
|---|
| 106 | { |
|---|
| 107 | reload_action(req, am, |
|---|
| 108 | "That user id is not available."); |
|---|
| 109 | return; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | boost::shared_ptr<session> sess(new session()); |
|---|
| 113 | sess->authenticate(u, req->get_form_field("passwd")); |
|---|
| 114 | |
|---|
| 115 | boost::shared_ptr<resource> rr |
|---|
| 116 | (new redir_resource(cb, "/editme", "303 See other")); |
|---|
| 117 | throw resource_exception(rr, sess); |
|---|
| 118 | } |
|---|
| 119 | |
|---|
| 120 | void reguser::set_attributes(boost::shared_ptr<request> req, |
|---|
| 121 | tlate::data_model::ptr am) |
|---|
| 122 | { |
|---|
| 123 | boost::shared_ptr<resource> resp; |
|---|
| 124 | if (req->get_path() != "/register") |
|---|
| 125 | { |
|---|
| 126 | resp.reset(new error_resource(cb, "404 Not found")); |
|---|
| 127 | throw resource_exception(resp); |
|---|
| 128 | } |
|---|
| 129 | if (req->is_authenticated()) |
|---|
| 130 | { |
|---|
| 131 | reload_action(req, am, |
|---|
| 132 | "You must log out before" |
|---|
| 133 | " you can continue registration"); |
|---|
| 134 | return; |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | if (req->has_form_field("regbut")) |
|---|
| 138 | { |
|---|
| 139 | if (req->get_method() == "POST") |
|---|
| 140 | post_action(req, am); |
|---|
| 141 | else |
|---|
| 142 | reload_action(req, am); |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | am->insert("action", "/register"); |
|---|
| 146 | am->insert("method", "post"); |
|---|
| 147 | am->insert("enctype", "application/x-www-form-urlencoded"); |
|---|
| 148 | am->insert("accept_charlist", "utf8"); |
|---|
| 149 | } |
|---|
| 150 | } |
|---|
| 151 | |
|---|
| 152 | namespace |
|---|
| 153 | { |
|---|
| 154 | class factory : public server::http_resource_factory |
|---|
| 155 | { |
|---|
| 156 | public: |
|---|
| 157 | factory() { |
|---|
| 158 | server::register_http_resource("/register", this); |
|---|
| 159 | } |
|---|
| 160 | boost::shared_ptr<http::resource> operator() |
|---|
| 161 | (server::conn_cb cb,std::string) { |
|---|
| 162 | boost::shared_ptr<http::resource> rv |
|---|
| 163 | (new http::reguser(cb)); |
|---|
| 164 | return rv; |
|---|
| 165 | } |
|---|
| 166 | }; |
|---|
| 167 | factory f; |
|---|
| 168 | } |
|---|