Menu

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

Download this file

102 lines (88 with data), 1.8 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
#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));
on_start.connect(boost::bind(&users::reset,this));
reset();
}
void users::reset()
{
auth_done=auth_ok=false;
}
string users::login_url()
{
return wi.root()+"/login/";
}
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()));
wi.set_cookies("","",-1);
return;
}
}
ini(c);
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);
}
void users::error_forbidden()
{
set_header(new HTTPRedirectHeader(login_url()));
}
}
MongoDB Logo MongoDB