From 1543cd3d0c0e6b54440c6a895f2f283dc3688913 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Sat, 18 Apr 2015 21:08:01 +0200 Subject: rudimentary login available --- CMakeLists.txt | 4 ++++ LINKS | 7 +++++++ sql/sqlite3.sql | 2 ++ src/master.cpp | 4 ++-- src/master_content.hpp | 6 ++++++ src/strusCms.cpp | 12 ++++++----- src/strusCms.hpp | 10 ++++++---- src/user.cpp | 54 +++++++++++++++++++++++++++++++++++++++++++++++--- src/user.hpp | 3 ++- src/user_content.hpp | 8 +++++++- 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 +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( "strusCms.db_connection" ) ), intro( *this ), - user( *this ) + user( *this ), + conn( settings( ).get( "strusCms.db_connection" ) ) { locale_name = "en"; script = settings( ).get( "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 #include +#include 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 ); }; } -- cgit v1.2.3-54-g00ecf