Menu

[r542]: / wikipp / trunk / users.cpp  Maximize  Restore  History

Download this file

145 lines (129 with data), 2.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#include <cgicc/HTTPRedirectHeader.h>
#include "users.h"
#include "wiki.h"
using cgicc::HTTPRedirectHeader;
using cgicc::HTTPCookie;
using namespace dbixx;
namespace apps {
users::users(wiki &w) : master(w)
{
wi.url_next.add("^/login/?$",
boost::bind(&users::login,this));
disable_reg=app.config.lval("wikipp.disable_registration",1);
if(!disable_reg){
wi.url_next.add("^/register/?$",
boost::bind(&users::new_user,this));
}
on_start.connect(boost::bind(&users::reset,this));
reset();
}
void users::new_user()
{
data::new_user c(&wi);
if(env->getRequestMethod()=="POST") {
c.form.load(*cgi);
transaction tr(sql);
if(c.form.validate()) {
sql<< "INSERT INTO users(username,password) "
"VALUES(?,?)",
c.form.username.get(),
c.form.password1.get(),
exec();
tr.commit();
wi.page.redirect(locale);
wi.set_cookies(c.form.username.get(),c.form.password1.get(),3600*7*24);
return;
}
tr.commit();
}
ini(c);
render("new_user",c);
}
void users::reset()
{
auth_done=auth_ok=false;
}
string users::login_url()
{
return wi.root()+"/login/";
}
bool users::user_exists(string u)
{
sql<<"SELECT id FROM users WHERE username=?",u;
row r;
return sql.single(r);
}
void users::login()
{
data::login c(&wi);
int time=3600*24*7;
if(env->getRequestMethod()=="POST") {
c.form.load(*cgi);
if(c.form.validate()) {
wi.page.redirect(locale);
wi.set_cookies(c.form.username.get(),c.form.password.get(),time);
return;
}
}
else {
if(auth()) {
set_header(new HTTPRedirectHeader(env->getReferrer()));
add_header("Status: 302 Found");
wi.set_cookies("","",-1);
return;
}
}
ini(c);
if(!disable_reg)
c.new_user=wi.root()+"/register/";
render("login",c);
}
bool users::check_login(string u,string p)
{
if(u.empty() || p.empty())
return false;
sql<< "SELECT password FROM users "
"WHERE username=?",u;
row r;
if(!sql.single(r) ) {
return false;
}
string pass;
r>>pass;
if(p!=pass)
return false;
return true;
}
bool users::auth()
{
if(!auth_done)
do_auth();
return auth_ok;
}
void users::do_auth()
{
string tmp_username;
string tmp_password;
string cookie=app.config.sval("wikipp.cookie_id","");
const vector<HTTPCookie> &cookies = env->getCookieList();
unsigned i;
for(i=0;i!=cookies.size();i++) {
if(cookies[i].getName()==cookie + "username") {
tmp_username=cookies[i].getValue();
}
else if(cookies[i].getName()==cookie + "password") {
tmp_password=cookies[i].getValue();
}
}
auth_ok=check_login(tmp_username,tmp_password);
if(auth_ok)
username=tmp_username;
else
username=env->getRemoteAddr();
}
void users::error_forbidden()
{
set_header(new HTTPRedirectHeader(login_url()));
add_header("Status: 302 Found");
}
}
MongoDB Logo MongoDB