root/http/smanage.cc

Revision 751fac6b567d675afee10e627efb5cf786ce5c83, 7.2 KB (checked in by Antti-Juhani Kaijanaho <antti-juhani@…>, 21 months ago)

[http::request] Cleanup

Signed-off-by: Antti-Juhani Kaijanaho <antti-juhani@…>

  • Property mode set to 100644
Line 
1/*  This file is part of Alue, the multiprotocol Internet discussion daemon
2
3    Copyright © 2009, 2010 Antti-Juhani Kaijanaho
4
5    Alue is free software: you can redistribute it and/or modify it
6    under the terms of the GNU General Public License as published by
7    the Free Software Foundation, either version 3 of the License, or
8    (at your option) any later version.
9
10    Alue is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
15    You should have received a copy of the GNU General Public License
16    along with Alue.  If not, see <http://www.gnu.org/licenses/>.
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 "templated_resource.hh"
27#include "session.hh"
28
29#include "../db/user.hh"
30#include "../html/util.hh"
31#include "../logger/logline.hh"
32#include "../msg/lexutils.hh"
33#include "../server.hh"
34#include "../tlate/tlate.hh"
35#include "../util.hh"
36
37#include <boost/shared_ptr.hpp>
38
39namespace http
40{
41        class smanage : public templated_resource
42        {
43        public:
44                smanage(server::conn_cb cb)
45                        : templated_resource(cb, "smanage.html")
46                        {}
47        protected:
48                void set_attributes(boost::shared_ptr<request>,
49                                    tlate::data_model::ptr);
50
51                void reload_action(boost::shared_ptr<request>,
52                                   tlate::data_model::ptr,
53                                   std::string error_message = "");
54
55                void post_action(boost::shared_ptr<request> req,
56                                 tlate::data_model::ptr);
57        };
58
59        void smanage::reload_action(boost::shared_ptr<request> req,
60                                    tlate::data_model::ptr am,
61                                    std::string error_message)
62        {
63                if (!error_message.empty())
64                        am->insert("error", html::quote(error_message, false));
65                am->insert("diname", req->get_form_field("diname"));
66                am->insert("diaddr", req->get_form_field("diaddr"));
67                std::string deaddr = req->get_form_field("deaddr");
68                am->insert("deaddr", deaddr);
69                db::user::ptr u = req->get_session()->get_user();
70
71                if (u->is_delivery_email_verified())
72                        am->insert("verified");
73                else if (deaddr == u->get_delivery_email() && deaddr != "")
74                        am->insert("verify");
75
76                if (req->has_form_field("allow_clear"))
77                        am->insert("allow_clear",
78                                   req->get_form_field("allow_clear"));
79        }
80
81        void smanage::post_action(boost::shared_ptr<request> req,
82                                  tlate::data_model::ptr am)
83        {
84                std::string (*const pd)(std::string) = uri::percent_decode;
85
86                boost::shared_ptr<db::user> u = req->get_session()->get_user();
87
88                u->set_display_name(pd(req->get_form_field("diname")));
89                u->set_display_email(pd(req->get_form_field("diaddr")));
90                if (u->set_delivery_email(pd(req->get_form_field("deaddr")))) {
91                        u->send_delivery_email_cookie
92                                (cb, req->get_peer().to_string());
93                }
94                u->set_allow_cleartext_password
95                        (req->has_form_field("allow_clear"));
96                reload_action(req, am, "Saved.");
97        }
98
99        void smanage::set_attributes(boost::shared_ptr<request> req,
100                                     tlate::data_model::ptr am)
101        {
102                std::string (*const q)(std::string,bool) = html::quote;
103
104                boost::shared_ptr<resource> resp;
105                if (req->get_path() != "/editme")
106                {
107                        resp.reset(new error_resource(cb, "404 Not found"));
108                        throw resource_exception(resp);
109                }
110                if (!req->is_authenticated())
111                {
112                        resp.reset(new reauthn_resource(cb));
113                        throw resource_exception(resp);
114                }
115
116                if (req->has_form_field("change_button"))
117                {
118                        if (req->get_method() == "POST")
119                                post_action(req, am);
120                        else
121                                reload_action(req, am);
122                       
123                }
124                else if (req->has_form_field("verify_button"))
125                {
126                        boost::shared_ptr<db::user> u =
127                                req->get_session()->get_user();
128                        if (req->get_method() == "POST")
129                        {
130                                u->send_delivery_email_cookie
131                                        (cb, req->get_peer().to_string());
132                                reload_action(req, am,
133                                              "Verification email has been "
134                                              "sent to " +
135                                              u->get_delivery_email());
136                        }
137                        else
138                                reload_action(req, am);
139                }
140                else
141                {
142                        boost::shared_ptr<db::user> u =
143                                req->get_session()->get_user();
144                        am->insert("diname", q(u->get_display_name(),false));
145                        am->insert("diaddr", q(u->get_display_email(),false));
146                        am->insert("deaddr", q(u->get_delivery_email(),false));
147                        if (u->is_delivery_email_verified())
148                                am->insert("verified");
149                        else if (u->get_delivery_email() != "")
150                                am->insert("verify");
151                                           
152                        if (u->allow_cleartext_password())
153                                am->insert("allow_clear",
154                                           req->get_form_field("allow_clear"));
155                }
156
157                boost::shared_ptr<db::user> usr= req->get_session()->get_user();
158                am->insert("userid", q(usr->get_userid(), false));
159                am->insert("action", "/editme");
160                am->insert("method", "post");
161                am->insert("enctype", "application/x-www-form-urlencoded");
162                am->insert("accept_charlist", "utf8");
163       }
164}
165
166namespace
167{
168        class factory : public server::http_resource_factory
169        {
170        public:
171                factory() {
172                        server::register_http_resource("/editme", this);
173                }
174                boost::shared_ptr<http::resource> operator()
175                (server::conn_cb cb,std::string) {
176                        boost::shared_ptr<http::resource> rv
177                                (new http::smanage(cb));
178                        return rv;
179                }
180        };
181        factory f;
182}
Note: See TracBrowser for help on using the browser.