summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2015-04-18 21:08:01 +0200
committerAndreas Baumann <mail@andreasbaumann.cc>2015-04-18 21:08:01 +0200
commit1543cd3d0c0e6b54440c6a895f2f283dc3688913 (patch)
tree30947c4c52b3692371f7a6e71654e189dd0e85ee
parent0e980c64a91db883a252c1695b0a3639b4489bfc (diff)
downloadaCms-1543cd3d0c0e6b54440c6a895f2f283dc3688913.tar.gz
aCms-1543cd3d0c0e6b54440c6a895f2f283dc3688913.tar.bz2
rudimentary login available
-rw-r--r--CMakeLists.txt4
-rw-r--r--LINKS7
-rw-r--r--sql/sqlite3.sql2
-rw-r--r--src/master.cpp4
-rw-r--r--src/master_content.hpp6
-rw-r--r--src/strusCms.cpp12
-rw-r--r--src/strusCms.hpp10
-rw-r--r--src/user.cpp54
-rw-r--r--src/user.hpp3
-rw-r--r--src/user_content.hpp8
10 files changed, 94 insertions, 16 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index a2c0a2b..858369a 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -19,6 +19,10 @@ find_program(XGETTEXT xgettext)
find_program(MSGFMT msgfmt)
find_program(MSGMERGE msgmerge)
+if(CMAKE_COMPILER_IS_GNUCXX)
+set( CMAKE_CXX_FLAGS "-std=c++98 -Wall -pedantic -g -Wfatal-errors -Werror -fPIC -O0" )
+endif()
+
set(TEMPLATES
${CMAKE_CURRENT_SOURCE_DIR}/templates/main.tmpl
${CMAKE_CURRENT_SOURCE_DIR}/templates/intro.tmpl
diff --git a/LINKS b/LINKS
index d79fdd2..e0c13fc 100644
--- a/LINKS
+++ b/LINKS
@@ -1,2 +1,9 @@
+links about CMS and web programming:
+
http://www.devarticles.com/c/a/JavaScript/Building-a-CHAP-Login-System-Encrypting-Data-in-the-Client/2/
http://www.techrepublic.com/article/two-ways-to-design-a-database-for-a-net-based-cms/
+
+other projects using CppCms:
+
+https://github.com/allan-simon/tatoebacpp
+11z-zpr-netspy
diff --git a/sql/sqlite3.sql b/sql/sqlite3.sql
index e524ce6..9aaa818 100644
--- a/sql/sqlite3.sql
+++ b/sql/sqlite3.sql
@@ -5,3 +5,5 @@ create table users(
username varchar(32) unique not null,
password varchar(32) not null
);
+
+insert into users(username, password) values('admin','admin');
diff --git a/src/master.cpp b/src/master.cpp
index f05b79a..751bb73 100644
--- a/src/master.cpp
+++ b/src/master.cpp
@@ -6,8 +6,8 @@
namespace apps {
master::master( strusCms &cms )
- : cms( cms ),
- application( cms.service( ) )
+ : application( cms.service( ) ),
+ cms( cms )
{
}
diff --git a/src/master_content.hpp b/src/master_content.hpp
index f924c94..7d61dcc 100644
--- a/src/master_content.hpp
+++ b/src/master_content.hpp
@@ -6,6 +6,12 @@
#include <string>
+namespace apps {
+
+class strusCms;
+
+}
+
namespace content {
struct master : public cppcms::base_content {
diff --git a/src/strusCms.cpp b/src/strusCms.cpp
index db715f4..e5792ec 100644
--- a/src/strusCms.cpp
+++ b/src/strusCms.cpp
@@ -8,9 +8,9 @@ namespace apps {
strusCms::strusCms( cppcms::service &srv )
: cppcms::application( srv ),
- conn( settings( ).get<std::string>( "strusCms.db_connection" ) ),
intro( *this ),
- user( *this )
+ user( *this ),
+ conn( settings( ).get<std::string>( "strusCms.db_connection" ) )
{
locale_name = "en";
script = settings( ).get<std::string>( "strusCms.script" );
@@ -18,13 +18,15 @@ strusCms::strusCms( cppcms::service &srv )
add( intro );
add( user );
- mapper( ).root( "/strusCms" );
+ mapper( ).root( root( ) );
}
std::string strusCms::root( std::string l )
{
- if( l.empty( ) ) l = locale_name;
- return script + "/" + l;
+ return script;
+ // TODO: localization later
+ //~ if( l.empty( ) ) l = locale_name;
+ //~ return script + "/" + l;
}
}
diff --git a/src/strusCms.hpp b/src/strusCms.hpp
index 539833e..9a93e99 100644
--- a/src/strusCms.hpp
+++ b/src/strusCms.hpp
@@ -12,14 +12,16 @@ class strusCms : public cppcms::application {
public:
strusCms( cppcms::service &srv );
std::string root( std::string locale_name = "" );
-
+
+ public:
+ apps::intro intro;
+ apps::user user;
+ std::string conn;
+
private:
std::string script;
std::string locale_name;
- std::string conn;
- apps::intro intro;
- apps::user user;
};
}
diff --git a/src/user.cpp b/src/user.cpp
index cc2af65..36ad821 100644
--- a/src/user.cpp
+++ b/src/user.cpp
@@ -4,6 +4,7 @@
#include <cppcms/url_dispatcher.h>
#include <cppcms/url_mapper.h>
+#include <cppdb/frontend.h>
namespace apps {
@@ -16,24 +17,51 @@ user::user( strusCms &cms )
void user::login( )
{
- content::user c;
+ content::user c( cms );
c.title = "strusCms";
if( request( ).request_method( ) == "POST" ) {
c.login.load( context( ) );
if( c.login.validate( ) ) {
+ response( ).set_redirect_header( cms.root( ) );
}
}
render( "login", c );
}
+// TODO: make this a salted hash
+bool user::check_login( std::string user, std::string password )
+{
+ if( user.empty( ) || password.empty( ) ) {
+ return false;
+ }
+
+ cppdb::session sql( cms.conn );
+ cppdb::result r;
+ r = sql << "SELECT password FROM users WHERE username=?" << user << cppdb::row;
+ if( r.empty( ) ) {
+ return false;
+ }
+
+ std::string pass;
+ r >> pass;
+
+ if( password != pass ) {
+ return false;
+ }
+
+ return true;
}
+} // namespace apps
+
namespace content {
-login_form::login_form( ) : cppcms::form( )
+login_form::login_form( apps::strusCms &cms )
+ : cppcms::form( ),
+ cms( cms )
{
username.message( "Your login" );
- username.error_message( "The login name can't be empty" );
+ username.error_message( "The login is illegal" );
password.message( "Your password" );
password.error_message( "Your password is illegal" );
submit.value( "Log in" );
@@ -46,4 +74,24 @@ login_form::login_form( ) : cppcms::form( )
password.non_empty( );
}
+bool login_form::validate( )
+{
+ if( !form::validate( ) ) {
+ return false;
+ }
+
+ if( !cms.user.check_login( username.value( ), password.value( ) ) ) {
+ username.valid( false );
+ password.valid( false );
+ return false;
+ }
+
+ return true;
}
+
+user::user( apps::strusCms &cms )
+ : login( cms )
+{
+}
+
+} // namespace content
diff --git a/src/user.hpp b/src/user.hpp
index 041585e..aecca71 100644
--- a/src/user.hpp
+++ b/src/user.hpp
@@ -8,9 +8,10 @@ namespace apps {
class user : public master {
public:
user( strusCms &cms );
+ bool check_login( std::string user, std::string password );
private:
- void login( );
+ void login( );
};
}
diff --git a/src/user_content.hpp b/src/user_content.hpp
index b52d2ea..4b260f5 100644
--- a/src/user_content.hpp
+++ b/src/user_content.hpp
@@ -8,16 +8,22 @@
namespace content {
struct login_form : public cppcms::form {
+ apps::strusCms &cms;
cppcms::widgets::text username;
cppcms::widgets::password password;
cppcms::widgets::submit submit;
public:
- login_form( );
+ login_form( apps::strusCms &cms );
+ virtual bool validate( );
+
};
struct user : public master {
login_form login;
+
+ public:
+ user( apps::strusCms &cms );
};
}