From 7fd19157644534d004a5ef4ca450fa7a3d3eb4b0 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Sun, 19 Apr 2015 13:29:08 +0200 Subject: completed login/logout --- CMakeLists.txt | 3 ++- FINDINGS | 4 ++++ media/basic-profile.css | 2 +- src/intro.cpp | 2 +- src/master.cpp | 10 ++++++++++ src/master.hpp | 3 +++ src/master_content.hpp | 9 ++++++++- src/user.cpp | 16 +++++++++++++++- src/user.hpp | 3 ++- templates/intro.tmpl | 9 +++++++-- templates/logout.tmpl | 24 ++++++++++++++++++++++++ templates/main.tmpl | 40 ---------------------------------------- templates/master.tmpl | 45 +++++++++++++++++++++++++++++++++++++++++++++ 13 files changed, 122 insertions(+), 48 deletions(-) create mode 100644 FINDINGS create mode 100644 templates/logout.tmpl delete mode 100644 templates/main.tmpl create mode 100644 templates/master.tmpl diff --git a/CMakeLists.txt b/CMakeLists.txt index 858369a..3d07a8e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -24,9 +24,10 @@ set( CMAKE_CXX_FLAGS "-std=c++98 -Wall -pedantic -g -Wfatal-errors -Werror -fPIC endif() set(TEMPLATES - ${CMAKE_CURRENT_SOURCE_DIR}/templates/main.tmpl + ${CMAKE_CURRENT_SOURCE_DIR}/templates/master.tmpl ${CMAKE_CURRENT_SOURCE_DIR}/templates/intro.tmpl ${CMAKE_CURRENT_SOURCE_DIR}/templates/login.tmpl + ${CMAKE_CURRENT_SOURCE_DIR}/templates/logout.tmpl ) set(SRC diff --git a/FINDINGS b/FINDINGS new file mode 100644 index 0000000..67d19b7 --- /dev/null +++ b/FINDINGS @@ -0,0 +1,4 @@ +- cmmcms: + - template parser: + - ugly: <%= session_username() %> works, <%= session_username( ) %> not, the same syntax quirk + doesn't exist for 'include' for example \ No newline at end of file diff --git a/media/basic-profile.css b/media/basic-profile.css index 1a26de8..5ebcaf3 100644 --- a/media/basic-profile.css +++ b/media/basic-profile.css @@ -4,7 +4,7 @@ For more information, see http://andreasviklund.com/templates/basic-profile/ */ *{margin:0; padding:0;} -body {background:#fff url(background.jpg) top center no-repeat; color:#303030; font:84% Arial,Helvetica,sans-serif; line-height:1.6em;} +body {background:#fff top center no-repeat; color:#303030; font:84% Arial,Helvetica,sans-serif; line-height:1.6em;} a {color:#0B0B61; text-decoration:none;} a:hover {text-decoration:underline;} p,ul,ol,dl {margin:0 0 30px 0;} diff --git a/src/intro.cpp b/src/intro.cpp index bf0331d..6b91bff 100644 --- a/src/intro.cpp +++ b/src/intro.cpp @@ -17,7 +17,7 @@ intro::intro( strusCms &cms ) void intro::show( ) { content::intro c; - c.title = "strusCms"; + ini( c ); render( "intro", c ); } diff --git a/src/master.cpp b/src/master.cpp index 751bb73..95383d8 100644 --- a/src/master.cpp +++ b/src/master.cpp @@ -11,4 +11,14 @@ master::master( strusCms &cms ) { } +void master::ini( content::master &c ) +{ + c.title = "strusCms"; + if( session( ).is_set( "username" ) ) { + c.username = session( )["username" ]; + } else { + c.username = ""; + } +} + } diff --git a/src/master.hpp b/src/master.hpp index 7ef46e9..d29073e 100644 --- a/src/master.hpp +++ b/src/master.hpp @@ -1,6 +1,8 @@ #ifndef MASTER_HPP #define MASTER_HPP +#include "master_content.hpp" + #include namespace apps { @@ -13,6 +15,7 @@ class master : public cppcms::application { public: master( strusCms &cms ); + virtual void ini( content::master &c ); }; } diff --git a/src/master_content.hpp b/src/master_content.hpp index 7d61dcc..ff40b09 100644 --- a/src/master_content.hpp +++ b/src/master_content.hpp @@ -17,9 +17,16 @@ namespace content { struct master : public cppcms::base_content { std::string title; std::string login_link; - master( ) : cppcms::base_content( ) { + std::string logout_link; + std::string username; + + public: + master( ) : cppcms::base_content( ) + { + // TODO: ini //~ c.login_link=wi.users.login_url(); login_link = "/strusCms/login"; + logout_link = "/strusCms/logout"; } }; diff --git a/src/user.cpp b/src/user.cpp index 36ad821..c77381d 100644 --- a/src/user.cpp +++ b/src/user.cpp @@ -5,6 +5,7 @@ #include #include #include +#include namespace apps { @@ -13,21 +14,34 @@ user::user( strusCms &cms ) { cms.dispatcher( ).assign( "/login", &user::login, this ); cms.mapper( ).assign( "login" ); + + cms.dispatcher( ).assign( "/logout", &user::logout, this ); + cms.mapper( ).assign( "logout" ); } void user::login( ) { content::user c( cms ); - c.title = "strusCms"; + ini( c ); if( request( ).request_method( ) == "POST" ) { c.login.load( context( ) ); if( c.login.validate( ) ) { response( ).set_redirect_header( cms.root( ) ); + session( )["username"] = c.login.username.value( ); + session( ).expose( "username" ); } } render( "login", c ); } +void user::logout( ) +{ + content::user c( cms ); + session( ).clear( ); + ini( c ); + render( "logout", c ); +} + // TODO: make this a salted hash bool user::check_login( std::string user, std::string password ) { diff --git a/src/user.hpp b/src/user.hpp index aecca71..9f35a0d 100644 --- a/src/user.hpp +++ b/src/user.hpp @@ -11,7 +11,8 @@ class user : public master { bool check_login( std::string user, std::string password ); private: - void login( ); + void login( ); + void logout( ); }; } diff --git a/templates/intro.tmpl b/templates/intro.tmpl index 99f5e92..0fd4c35 100644 --- a/templates/intro.tmpl +++ b/templates/intro.tmpl @@ -11,8 +11,13 @@ Welcome to StrusCms.
- If you want to edit pages, please log in - here. + <% if not empty username %> + After oyou finished editing pages, please log out + here. + <% else %> + If you want to edit pages, please log in + here. + <% end %>
<% end template %> diff --git a/templates/logout.tmpl b/templates/logout.tmpl new file mode 100644 index 0000000..2ce7816 --- /dev/null +++ b/templates/logout.tmpl @@ -0,0 +1,24 @@ +<% c++ #include "user_content.hpp" %> +<% skin view %> +<% view logout uses content::user extends master %> + +<% template title() %> + <% include master::title() %> :: Logout +<% end %> + +<% template page_content() %> +
+ You have been logged out of the system. Thanks for using strus CMS. +
+ +
+ If you found bugs, report them in Github. +
+ +
+ Otherwise contacting the developers is possible via email directly. +
+<% end template %> + +<% end view %> +<% end skin %> diff --git a/templates/main.tmpl b/templates/main.tmpl deleted file mode 100644 index 48b6984..0000000 --- a/templates/main.tmpl +++ /dev/null @@ -1,40 +0,0 @@ -<% c++ #include "content.hpp" %> -<% skin view %> -<% view master uses content::master %> - -<% template title( ) %><%= title %><% end %> -<% template page_content( ) %>Override Me<% end %> - -<% template render( ) %> - - - <% include title( ) %> - - - - -
- - - -
-

<% include title( ) %>

- <% include page_content( ) %> - -

© 2015 Patrick Frey
- Template design by Andreas Viklund / Best hosted at www.svenskadomaner.se

-
- - - -<% end template %> -<% end view %> -<% end skin %> diff --git a/templates/master.tmpl b/templates/master.tmpl new file mode 100644 index 0000000..7330ef1 --- /dev/null +++ b/templates/master.tmpl @@ -0,0 +1,45 @@ +<% c++ #include "content.hpp" %> +<% skin view %> +<% view master uses content::master %> + +<% template title( ) %><%= title %><% end %> +<% template page_content( ) %>Override Me<% end %> + +<% template render( ) %> + + + <% include title( ) %> + + + + +
+ + + +
+

<% include title( ) %>

+ <% if not empty username %> +

Logged in as <%= username %>

+ <% else %> +

Currently not logged in

+ <% end %> + <% include page_content( ) %> + +

© 2015 Patrick Frey
+ Template design by Andreas Viklund / Best hosted at www.svenskadomaner.se

+
+ + + +<% end template %> +<% end view %> +<% end skin %> -- cgit v1.2.3-54-g00ecf