Updated CMS code.

Separated code to have a lib and an example.
Improved design, fixed a few issues related to folder location.

This is still experimental and require more work to be really friendly to use.
This commit is contained in:
Jocelyn Fiat
2013-01-31 15:33:24 +01:00
parent 40ea982293
commit ce469b6ede
136 changed files with 1841 additions and 299 deletions

View File

@@ -0,0 +1,59 @@
note
description: "[
]"
class
USER_ACCOUNT_CMS_EXECUTION
inherit
CMS_EXECUTION
create
make
feature -- Execution
process
-- Computed response message.
local
b: STRING_8
vars: detachable ARRAY [READABLE_STRING_32]
n: INTEGER
-- vars: detachable WSF_TABLE
do
if attached {WSF_TABLE} request.path_parameter ("vars") as tb then
vars := tb.as_array_of_string
end
if vars = Void or else vars.is_empty then
set_title ("Account")
create b.make_empty
b.append ("Account")
set_main_content (b)
else
n := vars.count
create b.make_empty
if n >= 1 then
if vars[1].same_string ("password") then
set_title ("Password")
if n >= 2 then
if vars[2].same_string ("reset") then
b.append ("Reset password")
else
b.append ("password ???")
end
end
elseif vars[1].same_string ("register") then
set_title ("Registration")
b.append ("Register new account")
else
b.append ("???")
end
else
set_title ("Account/")
b.append ("...")
end
set_main_content (b)
end
end
end

View File

@@ -0,0 +1,182 @@
note
description: "[
]"
class
USER_CMS_EXECUTION
inherit
CMS_EXECUTION
USER_MODULE_LIB
create
make
feature -- Execution
process
-- Computed response message.
local
b: STRING_8
u: detachable CMS_USER
do
if attached {WSF_STRING} request.path_parameter ("uid") as p_uid then
if p_uid.is_integer then
u := service.storage.user_by_id (p_uid.integer_value)
else
u := service.storage.user_by_name (p_uid.value)
end
else
u := user
end
initialize_primary_tabs (u)
if u /= Void then
if not u.same_as (user) and then not has_permission ("admin view users") then
set_main_content ("Access denied")
else
service.storage.fill_user_profile (u)
create b.make_empty
set_title ("User [" + u.name + "]")
b.append ("<ul>%N")
if attached u.email as l_email then
b.append ("<li>Email: <a mailto=%""+ l_email +"%">"+ l_email +"</a></li>")
end
b.append ("<li>Created: "+ u.creation_date.out +"</li>%N")
if attached u.last_login_date as dt then
b.append ("<li>Last signed: "+ dt.out +"</li>%N")
else
b.append ("<li>Never signed yet</li>%N")
end
if u = user and then attached last_user_access_date as dt then
b.append ("<li>Session date: "+ dt.out +"</li>%N")
end
service.storage.fill_user_profile (u)
if attached u.profile as prof then
across
prof as p
loop
b.append ("<li>" + p.key + "=" + p.item +"</li>%N")
end
end
b.append ("</ul>")
set_main_content (b)
end
else
process_login
end
end
process_login
local
l_url: detachable READABLE_STRING_8
b: STRING_8
f: CMS_FORM
fd: detachable CMS_FORM_DATA
do
if
attached {WSF_STRING} request.item ("destination") as s_dest
then
l_url := request.script_url (s_dest.value)
end
if l_url = Void then
l_url := request.script_url ("/user")
end
f := login_form (url ("/user", Void), "login-form", l_url)
service.call_form_alter_hooks (f, Current)
if request.is_request_method ("post") then
create fd.make (request, f)
if fd.is_valid then
on_form_submitted (fd)
if attached {WSF_STRING} fd.integer_item ("form-destination") as s_dest then
l_url := request.script_url (s_dest.value)
end
end
end
if authenticated then
set_redirection (l_url)
set_title ("Login")
create b.make_empty
set_main_content (b)
set_redirection (url ("/user", Void))
else
set_title ("Login")
create b.make_empty
if fd /= Void then
if not fd.is_valid then
report_form_errors (fd)
end
fd.apply_to_associated_form
end
b.append (f.to_html (theme))
set_main_content (b)
end
end
on_form_submitted (fd: CMS_FORM_DATA)
local
u: detachable CMS_USER
do
if attached {WSF_STRING} fd.item (form_username_or_email_name) as s_name and then not s_name.is_empty then
u := service.storage.user_by_name (s_name.value)
if u = Void then
u := service.storage.user_by_email (s_name.value)
end
end
if u = Void then
fd.report_error ("Sorry, unrecognized username/email or password. " + link ("Have you forgotten your password?", "/user/password", Void))
else
if attached {WSF_STRING} fd.item (form_password_name) as s_passwd and then not s_passwd.is_empty then
if service.auth_engine.valid_credential (u.name, s_passwd.value) then
login (u, request)
else
fd.report_error ("Sorry, unrecognized username/email or password. " + link ("Have you forgotten your password?", "/user/password", Void))
end
end
end
end
login_form (a_action: READABLE_STRING_8; a_form_name: READABLE_STRING_8; a_destination: READABLE_STRING_8): CMS_FORM
local
th: CMS_FORM_HIDDEN_INPUT
ti: CMS_FORM_TEXT_INPUT
tp: CMS_FORM_PASSWORD_INPUT
ts: CMS_FORM_SUBMIT_INPUT
do
create Result.make (a_action, a_form_name)
create th.make ("form-destination")
th.set_default_value (a_destination)
Result.extend (th)
create ti.make (form_username_or_email_name)
ti.set_label ("Username or email")
ti.set_is_required (True)
Result.extend (ti)
create tp.make (form_password_name)
tp.set_label ("Password")
tp.set_is_required (True)
tp.set_description (link ("Reset password", "/user/password", Void))
Result.extend (tp)
Result.extend_text ("[
<img alt="login" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAHiQAAB4kB+XNYvQAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAASmSURBVGiB1ZpbaBxVGMd/Mzu5bSRJY6LubmKJl4J9qShdwdpGGxGlharJSwpVVFrUUF+iT1VEUYRgaEGqsa2FGtu8xNtDsBQi5EFBS8UnsYWUtpsmaqppIdkkm+4eH77ZzWazl7lllv7gPOycmXP+/9kzM+d839GUUnjEOqAL2A7cCdSYJWjWx4F5s/wN/AgMAzNedK65NHI38CKwAwiZpcLitUvAlFlGgBPAFadCnBppAQ4DDwNhQHMqwEQBk8A5oAeYsNuAXSP1QD/wFNBqtzOLxIAzQC9ww/JVSimrZbdSalwplVJrT8rsa7dVfVZNHFRKTftgIJdps++SGksNrSDwLbAFqHUxXNwwB/wEPIe8+fJSzEi12cCDgO61OpukgN+RG7qQ74RiRkaApylg4sj4YZLqpgcalwloBvvu7SlUnQJOI6/6VRQy8jHwKkWG04aRMIlUwp7SElTqlVzYMVnslDlgAHgztyLf3e5GPnLleiaKUYto686tyDXSCHwANPkgyilNiMbG7INGzkn9QJvdltdVNnJg43uOVH34x7vMJP6ze1kbovWl9IFsI+uBJ3Ew3QgGaulqXfVvW+Lg+T5msG1EQ7SuBy7DyqH1GTKHulVoQTQDy0YiwKayyHHHJkR7xsgLyBTcFYupRbdN2CWEaM8Y2Yn7qTjHLw4w9s+o22bsoCHa0YEGZE3hmsXkInvP7vHbTBhoMJDlacTu1fs39JJMJQGoq6jLHE+kEuw9u4ejmwdpv6PDI61FiQBdBtCB9eVphv339xas89lMBdCh48FDno+0GZ+GWchAIh0F+f7q15y6fMJSaxPx2IrfPv4zNQbL4Zq8TM5P8Mu/PzvuwSczQZ0SRrzAh2EW9G3lp6Ghaa4/VQXRKbIO9ooqvYqj0UG2NW9fqy7iBiWMhGtaeOT2Ry21NhGPcXV+5QNfpVdxLPoVW5ufcKzSAnEDicUWZFekk12RTkutHTrfx6ELfZnfYuIkW5sfdyPSCvMGEnv1nOpANcc2n+Sx5va1aD6XKQMYBTqx+XXv//MjbqolAOoqGnjtvjcydT6bWAJGDSS0/w4SWbfM5+OfZKIokZrWjJHqQDVfRE+xpWmbt3ILMwUM68B1PBpeNUaQ49EhP02AaL+eXrN/A0RxuSZ5uW0fFXqlW2F2UIj2zMJqEMlPuMJnEyCaB2HZyBSSZLnVOIf5WGRPUXpwkfoqA1cQzcDKuNYEkil6BZvPSjw5x3BsyJGaeHLOyWUK0ZpJ0eUGseuB34B7SrVUpiB2movAQ2Sl5nJnvzeAt4Fpz9R5zzSicUV+Md80fghJFc/6IMous4i2VeM4N4id5i3gASTRE8h3woGN769JoqcISWDM1LaKUqm3MSSXnteMjySRV207BVJvxVaIC+aFpynvMJs1NRQ0AaWTnAtISHIAuOaZNOtcM/veSRETgK0NA91KqUvKvw0Dl8w+Pd0wkC71SqkjSqnYGpqImX3U29HmdFNNCPgUmTGH8GZTzRTwK/A6DpYVbrc53YXkJ55FouJh7G1zmjTLd8CXwF9Ohbg1ks1twPPAM8i/FCT/xrM4csd/QNYSnrwR/wfI5AekDWyX2QAAAABJRU5ErkJggg=="
style="float:right; margin: 5px;"/>
]")
create ts.make ("op")
ts.set_default_value ("Log in")
Result.extend (ts)
Result.extend_text ("<p>Need an account?<br/>" + link ("Sign up now!", "/user/register", Void) + "</p>")
end
form_username_or_email_name: STRING = "name"
form_password_name: STRING = "password"
end

View File

@@ -0,0 +1,168 @@
note
description: "[
]"
class
USER_EDIT_CMS_EXECUTION
inherit
CMS_EXECUTION
USER_MODULE_LIB
create
make
feature -- Execution
process
-- Computed response message.
local
b: STRING_8
f: CMS_FORM
fd: detachable CMS_FORM_DATA
u, fu: detachable CMS_USER
up: detachable CMS_USER_PROFILE
l_is_editing_current_user: BOOLEAN
do
if attached {WSF_STRING} request.path_parameter ("uid") as p_uid and then p_uid.is_integer then
u := service.storage.user_by_id (p_uid.integer_value)
if has_permission ("view users") then
else
if u /= Void and then u.same_as (user) then
else
u := Void
end
end
else
u := user
end
if attached user as l_active_user then
l_is_editing_current_user := l_active_user.same_as (u)
end
create b.make_empty
initialize_primary_tabs (u)
if u = Void then
b.append ("Access denied")
set_redirection (url ("/user/register", Void))
else
service.storage.fill_user_profile (u)
f := edit_form (u, request.path_info, "user-edit")
if request.is_post_request_method then
create fd.make (request, f)
if attached {WSF_STRING} fd.item ("username") as s_username then
fu := service.storage.user_by_name (s_username.value)
if fu = Void then
fd.report_invalid_field ("username", "User does not exist!")
end
end
if attached {WSF_STRING} fd.item ("email") as s_email then
fu := service.storage.user_by_email (s_email.value)
if fu /= Void and then fu.id /= u.id then
fd.report_invalid_field ("email", "Email is already used by another user!")
end
end
fu := Void
end
if fd /= Void and then fd.is_valid then
across
fd as c
loop
b.append ("<li>" + html_encoded (c.key) + "=")
if attached c.item as v then
b.append (html_encoded (v.string_representation))
end
b.append ("</li>")
end
if attached {WSF_STRING} fd.item ("password") as s_password then
u.set_password (s_password.value)
end
if attached {WSF_STRING} fd.item ("email") as s_email then
u.set_email (s_email.value)
end
if attached {WSF_STRING} fd.item ("note") as s_note then
up := u.profile
if up = Void then
create up.make
end
up.force (s_note.value, "note")
u.set_profile (up)
end
service.storage.save_user (u)
if l_is_editing_current_user and u /= user then
set_user (u)
end
set_redirection (url ("/user", Void))
set_main_content (b)
else
if fd /= Void then
if not fd.is_valid then
report_form_errors (fd)
end
fd.apply_to_associated_form
end
b.append (f.to_html (theme))
end
end
set_main_content (b)
end
edit_form (u: CMS_USER; a_url: READABLE_STRING_8; a_name: STRING): CMS_FORM
local
f: CMS_FORM
ti: CMS_FORM_TEXT_INPUT
tp: CMS_FORM_PASSWORD_INPUT
ta: CMS_FORM_TEXTAREA
ts: CMS_FORM_SUBMIT_INPUT
do
create f.make (a_url, a_name)
create ti.make ("username")
ti.set_label ("Username")
ti.set_default_value (u.name)
ti.set_is_required (False)
ti.set_is_readonly (True)
f.extend (ti)
f.extend (create {CMS_FORM_RAW_TEXT}.make ("<br/>"))
create tp.make ("password")
tp.set_label ("Password")
tp.set_is_required (False)
f.extend (tp)
f.extend (create {CMS_FORM_RAW_TEXT}.make ("<br/>"))
create ti.make ("email")
ti.set_label ("Valid email address")
if attached u.email as l_email then
ti.set_default_value (l_email)
end
ti.set_is_required (True)
f.extend (ti)
f.extend (create {CMS_FORM_RAW_TEXT}.make ("<br/>"))
create ta.make ("note")
ta.set_label ("Additional note about you")
ta.set_description ("You can use this input to tell us more about you")
if attached u.profile as p and then attached p.item ("note") as l_note then
ta.set_default_value (l_note)
end
ta.set_is_required (False)
f.extend (ta)
f.extend (create {CMS_FORM_RAW_TEXT}.make ("<br/>"))
create ts.make ("op")
ts.set_default_value ("Save")
f.extend (ts)
Result := f
end
end

View File

@@ -0,0 +1,114 @@
note
description: "[
]"
class
USER_LOGIN_CMS_EXECUTION
inherit
CMS_EXECUTION
CMS_AUTH_ENGINE
create
make
feature -- Status
valid_credential (u,p: READABLE_STRING_32): BOOLEAN
do
if attached service.storage.user_by_name (u) as l_user then
Result := attached l_user.encoded_password as l_pass and then l_pass.same_string (service.storage.encoded_password (p))
end
end
feature -- Execution
process
-- Computed response message.
local
auth_engine: CMS_AUTH_ENGINE
l_url: detachable READABLE_STRING_8
err: detachable STRING_8
b: STRING_8
u: detachable STRING_32
do
if request.is_request_method ("post") then
if
attached {WSF_STRING} request.form_parameter (form_login_name) as s_login and then not s_login.is_empty and
attached {WSF_STRING} request.form_parameter (form_password_name) as s_passwd and then not s_passwd.is_empty
then
auth_engine := Current
u := s_login.value
if attached service.storage.user_by_name (u) as l_user and auth_engine.valid_credential (u, s_passwd.value) then
login (l_user, request)
else
err := "Authentication failed for [" + html_encoded (u) + "]"
end
if attached {WSF_STRING} request.form_parameter ("form-destination") as s_dest then
l_url := request.script_url (s_dest.value)
end
end
else
if
attached {WSF_STRING} request.item ("destination") as s_dest
then
l_url := request.script_url (s_dest.value)
end
end
if l_url = Void then
l_url := request.script_url ("/user")
end
if authenticated then
set_redirection (l_url)
set_title ("Login")
create b.make_empty
b.append ("<h1>Login</h1>%N")
set_main_content (b)
else
set_title ("Login")
create b.make_empty
b.append ("<h1>Login</h1>%N")
if err /= Void then
b.append ("<div id=%"error-box%" style=%"background-color: #fcc; color:#f00;%">" + err + "</div>")
end
b.append ("<form action=%"" + request.path_info + "%" method=%"POST%" id=%"form-login%" style=%"border: dotted 1px #099; display: inline-block; padding: 10px; margin: 10px;%">%N")
-- b.append ("<div style=%"display:none%"><input type=%"hidden%" name=%"form-login-token%" value=%""+ cms.session.uuid +"%"></div>")
b.append ("<div style=%"display:none%"><input type=%"hidden%" name=%"form-destination%" value=%""+ l_url +"%"></div>")
b.append ("<div class=%"required username%">")
b.append ("<strong><label for=%"id_username%">Username or email</label></strong> <em>(required)</em><br/>")
b.append ("<input type=%"text%" id=%"id_username%" autofocus=%"autofocus%" name=%"" + form_login_name + "%" ")
if u /= Void then
b.append (" value=%""+ html_encoded (u) +"%" ")
end
b.append ("/>")
b.append ("</div>")
b.append ("<div class=%"required password%">")
b.append ("<strong><label for=%"id_password%">Password</label></strong> <em>(required)</em><br/>")
b.append ("<input type=%"password%" id=%"id_password%" name=%"" + form_password_name + "%" />")
b.append ("</div>")
b.append ("<p class=%"description%"><a href=%"" + url ("/user/password", Void) + "%" tabindex=%"-1%">Reset password</a></p>%N")
b.append ("<div class=%"submit%">")
b.append ("<input type=%"submit%" value=%"Log in%" name=%"submit%" >%N")
b.append ("[
<img alt="login" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAYAAAAeP4ixAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAHiQAAB4kB+XNYvQAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAASmSURBVGiB1ZpbaBxVGMd/Mzu5bSRJY6LubmKJl4J9qShdwdpGGxGlharJSwpVVFrUUF+iT1VEUYRgaEGqsa2FGtu8xNtDsBQi5EFBS8UnsYWUtpsmaqppIdkkm+4eH77ZzWazl7lllv7gPOycmXP+/9kzM+d839GUUnjEOqAL2A7cCdSYJWjWx4F5s/wN/AgMAzNedK65NHI38CKwAwiZpcLitUvAlFlGgBPAFadCnBppAQ4DDwNhQHMqwEQBk8A5oAeYsNuAXSP1QD/wFNBqtzOLxIAzQC9ww/JVSimrZbdSalwplVJrT8rsa7dVfVZNHFRKTftgIJdps++SGksNrSDwLbAFqHUxXNwwB/wEPIe8+fJSzEi12cCDgO61OpukgN+RG7qQ74RiRkaApylg4sj4YZLqpgcalwloBvvu7SlUnQJOI6/6VRQy8jHwKkWG04aRMIlUwp7SElTqlVzYMVnslDlgAHgztyLf3e5GPnLleiaKUYto686tyDXSCHwANPkgyilNiMbG7INGzkn9QJvdltdVNnJg43uOVH34x7vMJP6ze1kbovWl9IFsI+uBJ3Ew3QgGaulqXfVvW+Lg+T5msG1EQ7SuBy7DyqH1GTKHulVoQTQDy0YiwKayyHHHJkR7xsgLyBTcFYupRbdN2CWEaM8Y2Yn7qTjHLw4w9s+o22bsoCHa0YEGZE3hmsXkInvP7vHbTBhoMJDlacTu1fs39JJMJQGoq6jLHE+kEuw9u4ejmwdpv6PDI61FiQBdBtCB9eVphv339xas89lMBdCh48FDno+0GZ+GWchAIh0F+f7q15y6fMJSaxPx2IrfPv4zNQbL4Zq8TM5P8Mu/PzvuwSczQZ0SRrzAh2EW9G3lp6Ghaa4/VQXRKbIO9ooqvYqj0UG2NW9fqy7iBiWMhGtaeOT2Ry21NhGPcXV+5QNfpVdxLPoVW5ufcKzSAnEDicUWZFekk12RTkutHTrfx6ELfZnfYuIkW5sfdyPSCvMGEnv1nOpANcc2n+Sx5va1aD6XKQMYBTqx+XXv//MjbqolAOoqGnjtvjcydT6bWAJGDSS0/w4SWbfM5+OfZKIokZrWjJHqQDVfRE+xpWmbt3ILMwUM68B1PBpeNUaQ49EhP02AaL+eXrN/A0RxuSZ5uW0fFXqlW2F2UIj2zMJqEMlPuMJnEyCaB2HZyBSSZLnVOIf5WGRPUXpwkfoqA1cQzcDKuNYEkil6BZvPSjw5x3BsyJGaeHLOyWUK0ZpJ0eUGseuB34B7SrVUpiB2movAQ2Sl5nJnvzeAt4Fpz9R5zzSicUV+Md80fghJFc/6IMous4i2VeM4N4id5i3gASTRE8h3woGN769JoqcISWDM1LaKUqm3MSSXnteMjySRV207BVJvxVaIC+aFpynvMJs1NRQ0AaWTnAtISHIAuOaZNOtcM/veSRETgK0NA91KqUvKvw0Dl8w+Pd0wkC71SqkjSqnYGpqImX3U29HmdFNNCPgUmTGH8GZTzRTwK/A6DpYVbrc53YXkJ55FouJh7G1zmjTLd8CXwF9Ohbg1ks1twPPAM8i/FCT/xrM4csd/QNYSnrwR/wfI5AekDWyX2QAAAABJRU5ErkJggg=="
style="float:right; margin: 5px;"/>
]")
b.append ("</div>")
b.append ("<p>Need an account? <a href=%"" + url ("/user/register", Void) + "%">Sign up now!</a></p>%N")
b.append ("</form>%N")
set_main_content (b)
end
end
form_login_name: STRING = "login"
form_password_name: STRING = "password"
end

View File

@@ -0,0 +1,39 @@
note
description: "[
]"
class
USER_LOGOUT_CMS_EXECUTION
inherit
CMS_EXECUTION
create
make
feature -- Execution
process
-- Computed response message.
local
-- l_url: READABLE_STRING_8
b: STRING_8
do
logout (request)
if
attached {WSF_STRING} request.item ("destination") as s_dest
then
set_redirection (request.script_url (s_dest.value))
else
set_redirection (request.script_url ("/"))
end
set_title ("Logout")
create b.make_empty
set_main_content (b)
-- l_url := request.script_url ("/info/")
-- res.redirect_now_with_content (l_url, "Redirection to " + l_url, "text/html")
end
end

View File

@@ -0,0 +1,157 @@
note
description: "Summary description for {USER_MODULE}."
author: ""
date: "$Date$"
revision: "$Revision$"
class
USER_MODULE
inherit
CMS_MODULE
CMS_HOOK_MENU_ALTER
CMS_HOOK_BLOCK
create
make
feature {NONE} -- Initialization
make (a_service: like service)
do
service := a_service
name := "user"
version := "1.0"
description := "Users management"
package := "core"
enable
end
feature {CMS_SERVICE} -- Registration
service: CMS_SERVICE
register (a_service: CMS_SERVICE)
local
h: CMS_HANDLER
do
-- a_service.map_uri ("/user", agent handle_login)
a_service.map_uri ("/user/logout", agent handle_logout)
a_service.map_uri ("/user/register", agent handle_register)
a_service.map_uri ("/user/password", agent handle_request_new_password)
create {CMS_HANDLER} h.make (agent handle_user)
a_service.router.map (create {WSF_URI_TEMPLATE_MAPPING}.make ("/user/{uid}", h))
a_service.router.map (create {WSF_URI_MAPPING}.make_trailing_slash_ignored ("/user", h))
a_service.map_uri_template ("/user/{uid}/edit", agent handle_edit)
a_service.map_uri_template ("/user/reset/{uid}/{last-signed}/{extra}", agent handle_reset_password)
a_service.add_menu_alter_hook (Current)
a_service.add_block_hook (Current)
end
feature -- Hooks
block_list: ITERABLE [like {CMS_BLOCK}.name]
do
Result := <<"user-info">>
end
get_block_view (a_block_id: detachable READABLE_STRING_8; a_execution: CMS_EXECUTION)
local
b: CMS_CONTENT_BLOCK
do
if
a_execution.is_front and then
attached a_execution.user as u
then
create b.make ("user-info", "User", "Welcome " + a_execution.html_encoded (u.name), a_execution.formats.plain_text)
a_execution.add_block (b, Void)
end
end
menu_alter (a_menu_system: CMS_MENU_SYSTEM; a_execution: CMS_EXECUTION)
local
lnk: CMS_LOCAL_LINK
opts: CMS_API_OPTIONS
do
if attached a_execution.user as u then
create lnk.make ("Logout", "/user/logout")
a_execution.add_to_main_menu (lnk)
else
create lnk.make ("Login", "/user")
create opts.make_from_manifest (<<["query", <<["destination", a_execution.request.path_info]>> ]>>)
lnk.set_options (opts)
a_execution.add_to_main_menu (lnk)
create lnk.make ("Sign up", "/user/register")
lnk.set_options (opts)
a_execution.add_to_main_menu (lnk)
end
if a_execution.authenticated then
create lnk.make ("My Account", "/user")
a_menu_system.user_menu.extend (lnk)
create lnk.make ("Logout", "/user/logout")
a_menu_system.user_menu.extend (lnk)
else
create lnk.make ("Login", "/user")
a_menu_system.user_menu.extend (lnk)
end
end
links: HASH_TABLE [CMS_MODULE_LINK, STRING]
-- Link indexed by path
local
-- lnk: CMS_MODULE_LINK
do
create Result.make (3)
-- create lnk.make ("Date/time demo")
-- lnk.set_callback (agent process_date_time_demo, <<"arg">>)
-- Result["/demo/date/{arg}"] := lnk
end
-- handle_login (req: WSF_REQUEST; res: WSF_RESPONSE)
-- do
-- (create {USER_LOGIN_CMS_EXECUTION}.make (req, res, service)).execute
-- end
handle_logout (req: WSF_REQUEST; res: WSF_RESPONSE)
do
(create {USER_LOGOUT_CMS_EXECUTION}.make (req, res, service)).execute
end
handle_user (req: WSF_REQUEST; res: WSF_RESPONSE)
do
(create {USER_CMS_EXECUTION}.make (req, res, service)).execute
end
handle_edit (req: WSF_REQUEST; res: WSF_RESPONSE)
do
(create {USER_EDIT_CMS_EXECUTION}.make (req, res, service)).execute
end
-- handle_account (req: WSF_REQUEST; res: WSF_RESPONSE)
-- do
-- (create {USER_ACCOUNT_CMS_EXECUTION}.make (req, res, service)).execute
-- end
handle_register (req: WSF_REQUEST; res: WSF_RESPONSE)
do
(create {USER_REGISTER_CMS_EXECUTION}.make (req, res, service)).execute
end
handle_request_new_password (req: WSF_REQUEST; res: WSF_RESPONSE)
do
(create {USER_NEW_PASSWORD_CMS_EXECUTION}.make (req, res, service)).execute
end
handle_reset_password (req: WSF_REQUEST; res: WSF_RESPONSE)
do
(create {USER_RESET_PASSWORD_CMS_EXECUTION}.make (req, res, service)).execute
end
end

View File

@@ -0,0 +1,29 @@
note
description: "Summary description for {USER_MODULE_LIB}."
author: ""
date: "$Date$"
revision: "$Revision$"
deferred class
USER_MODULE_LIB
inherit
CMS_COMMON_API
CMS_EXECUTION
feature -- Initialization
initialize_primary_tabs (u: detachable CMS_USER)
do
if u /= Void then
primary_tabs.extend (create {CMS_LOCAL_LINK}.make ("View", "/user/" + u.id.out))
primary_tabs.extend (create {CMS_LOCAL_LINK}.make ("Edit", "/user/" + u.id.out + "/edit"))
else
primary_tabs.extend (create {CMS_LOCAL_LINK}.make ("Create new account", "/user/register"))
primary_tabs.extend (create {CMS_LOCAL_LINK}.make ("Log in", "/user"))
primary_tabs.extend (create {CMS_LOCAL_LINK}.make ("Request new password", "/user/password"))
end
end
end

View File

@@ -0,0 +1,154 @@
note
description: "[
]"
class
USER_NEW_PASSWORD_CMS_EXECUTION
inherit
CMS_EXECUTION
USER_MODULE_LIB
create
make
feature -- Execution
process
-- Computed response message.
local
b: STRING_8
f: CMS_FORM
u: detachable CMS_USER
fd: detachable CMS_FORM_DATA
e: detachable CMS_EMAIL
l_uuid: UUID
do
set_title ("Request new password")
create b.make_empty
if not request.is_post_request_method and authenticated then
u := user
initialize_primary_tabs (u)
if u /= Void then
if attached u.email as l_email then
f := new_password_form (request.path_info, "new-password")
b.append ("Password reset instructions will be mailed to <em>" + l_email + "</em>. You must " + link ("log out", "/user/logout", Void) + " to use the password reset link in the e-mail.")
b.append (f.to_html (theme))
else
b.append ("Your account does not have any email address set!")
set_redirection (url ("/user/"+ u.id.out +"/edit", Void))
end
else
b.append ("Unexpected issue")
end
else
f := new_password_form (request.path_info, "new-password")
if request.is_post_request_method then
create fd.make (request, f)
if attached {WSF_STRING} fd.item ("name") as s_name then
u := service.storage.user_by_name (s_name.value)
if u = Void then
u := service.storage.user_by_email (s_name.value)
if u = Void then
fd.report_invalid_field ("name", "Sorry, " + html_encoded (s_name.value)+ " is not recognized as a user name or an e-mail address.")
end
end
end
end
initialize_primary_tabs (u)
if fd /= Void and then fd.is_valid then
across
fd as c
loop
b.append ("<li>" + html_encoded (c.key) + "=")
if attached c.item as v then
b.append (html_encoded (v.string_representation))
end
b.append ("</li>")
end
if u /= Void and then attached u.email as l_mail_address then
l_uuid := (create {UUID_GENERATOR}).generate_uuid
e := new_password_email (u, l_mail_address, l_uuid.out)
u.set_data_item ("new_password_extra", l_uuid.out)
service.storage.save_user (u)
service.mailer.safe_process_email (e)
add_success_message ("Further instructions have been sent to your e-mail address.")
set_redirection (url ("/user", Void))
end
set_main_content (b)
else
if fd /= Void then
if not fd.is_valid then
report_form_errors (fd)
end
fd.apply_to_associated_form
end
b.append (f.to_html (theme))
end
end
set_main_content (b)
end
new_password_form (a_url: READABLE_STRING_8; a_name: STRING): CMS_FORM
require
attached user as l_auth_user implies l_auth_user.has_email
local
u: like user
f: CMS_FORM
ti: CMS_FORM_TEXT_INPUT
th: CMS_FORM_HIDDEN_INPUT
ts: CMS_FORM_SUBMIT_INPUT
err: BOOLEAN
do
create f.make (a_url, a_name)
u := user
if u = Void then
create ti.make ("name")
ti.set_label ("Username or e-mail address")
ti.set_is_required (True)
f.extend (ti)
elseif attached u.email as l_mail then
create th.make ("name")
th.set_default_value (l_mail)
th.set_is_required (True)
f.extend (th)
else
f.extend (create {CMS_FORM_RAW_TEXT}.make ("The associated account has no e-mail address."))
err := True
end
if not err then
create ts.make ("op")
ts.set_default_value ("E-mail new password")
f.extend (ts)
end
Result := f
end
new_password_email (u: CMS_USER; a_mail_address: STRING; a_extra: READABLE_STRING_8): CMS_EMAIL
local
b: STRING
opts: CMS_URL_API_OPTIONS
dt: detachable DATE_TIME
do
create b.make_empty
create opts.make_absolute
b.append ("A request to reset the password for your account has been made at " + service.site_name + ".%N")
b.append ("You may now log in by clicking this link or copying and pasting it to your browser:%N%N")
dt := u.last_login_date
if dt = Void then
dt := u.creation_date
end
b.append (url ("/user/reset/" + u.id.out + "/" + unix_timestamp (dt).out + "/" + a_extra, opts))
b.append ("%N")
b.append ("%N")
b.append ("This link can only be used once to log in and will lead you to a page where you can set your password. It expires after one day and nothing will happen if it's not used.%N")
b.append ("%N%N-- The %"" + service.site_name + "%" team")
create Result.make (service.site_email, a_mail_address, "Account details for " + u.name + " at " + service.site_name, b)
end
end

View File

@@ -0,0 +1,205 @@
note
description: "[
]"
class
USER_REGISTER_CMS_EXECUTION
inherit
CMS_EXECUTION
USER_MODULE_LIB
create
make
feature -- Execution
process
-- Computed response message.
local
b: STRING_8
f: CMS_FORM
fd: detachable CMS_FORM_DATA
u: detachable CMS_USER
up: detachable CMS_USER_PROFILE
e: detachable CMS_EMAIL
l_pass: detachable READABLE_STRING_32
l_uuid: UUID
do
set_title ("Create new account")
create b.make_empty
if authenticated then
initialize_primary_tabs (user)
b.append ("You are already " + link ("signed in", "/user", Void) + ", please " + link ("signout", "/user/logout", Void) + " before trying to " + link ("register a new account", "/account/register", Void) + ".")
set_redirection (url ("/user", Void))
else
f := registration_form (request.path_info, "reg")
if request.is_post_request_method then
create fd.make (request, f)
if attached {WSF_STRING} fd.item ("username") as s_username then
u := service.storage.user_by_name (s_username.value)
if u /= Void then
fd.report_invalid_field ("username", "User already exists!")
end
end
if attached {WSF_STRING} fd.item ("email") as s_email then
u := service.storage.user_by_email (s_email.value)
if u /= Void then
fd.report_invalid_field ("email", "Email is already used!")
end
end
u := Void
end
if fd /= Void and then fd.is_valid then
across
fd as c
loop
b.append ("<li>" + html_encoded (c.key) + "=")
if attached c.item as v then
b.append (html_encoded (v.string_representation))
end
b.append ("</li>")
end
if attached {WSF_STRING} fd.item ("username") as s_username then
u := service.storage.user_by_name (s_username.value)
create u.make_new (s_username.value)
if attached {WSF_STRING} fd.item ("password") as s_password then
u.set_password (s_password.value)
l_pass := u.password
end
if attached {WSF_STRING} fd.item ("email") as s_email then
u.set_email (s_email.value)
end
if attached {WSF_STRING} fd.item ("note") as s_note then
create up.make
up.force (s_note.value, "note")
u.set_profile (up)
end
l_uuid := (create {UUID_GENERATOR}).generate_uuid
u.set_data_item ("new_password_extra", l_uuid.out)
service.storage.save_user (u)
if attached u.email as l_mail_address then
e := new_registration_email (l_mail_address, u, l_pass, l_uuid.out)
service.mailer.safe_process_email (e)
end
e := new_user_account_email (service.site_email, u)
service.mailer.safe_process_email (e)
login (u, request)
set_redirection (url ("/user", Void))
end
set_main_content (b)
else
initialize_primary_tabs (user)
if fd /= Void then
if not fd.is_valid then
report_form_errors (fd)
end
fd.apply_to_associated_form
end
b.append (f.to_html (theme))
end
end
set_main_content (b)
end
registration_form (a_url: READABLE_STRING_8; a_name: STRING): CMS_FORM
local
f: CMS_FORM
ti: CMS_FORM_TEXT_INPUT
tp: CMS_FORM_PASSWORD_INPUT
ta: CMS_FORM_TEXTAREA
ts: CMS_FORM_SUBMIT_INPUT
do
create f.make (a_url, a_name)
create ti.make ("username")
ti.set_label ("Username")
ti.set_is_required (True)
ti.set_validation_action (agent (fd: CMS_FORM_DATA)
do
if attached {WSF_STRING} fd.item ("username") as f_username and then f_username.value.count >= 5 then
else
fd.report_invalid_field ("username", "Username should contain at least 5 characters!")
end
end)
f.extend (ti)
f.extend (create {CMS_FORM_RAW_TEXT}.make ("<br/>"))
create tp.make ("password")
tp.set_label ("Password")
tp.set_is_required (True)
f.extend (tp)
f.extend (create {CMS_FORM_RAW_TEXT}.make ("<br/>"))
create ti.make ("email")
ti.set_label ("Valid email address")
ti.set_is_required (True)
f.extend (ti)
f.extend (create {CMS_FORM_RAW_TEXT}.make ("<br/>"))
create ta.make ("note")
ta.set_label ("Additional note about you")
ta.set_description ("You can use this input to tell us more about you")
ta.set_is_required (False)
f.extend (ta)
f.extend (create {CMS_FORM_RAW_TEXT}.make ("<br/>"))
create ts.make ("Register")
ts.set_default_value ("Register")
f.extend (ts)
Result := f
end
new_registration_email (a_mail_address: STRING; u: CMS_USER; a_password: detachable like {CMS_USER}.password; a_extra: READABLE_STRING_8): CMS_EMAIL
require
has_clear_password: u.password /= Void or else a_password /= Void
local
p: detachable like {CMS_USER}.password
b: STRING
opts: CMS_URL_API_OPTIONS
do
p := a_password
if p = Void then
p := u.password
end
create b.make_from_string (u.name + "%N" + "Thank you for registering at " + service.site_name + ". ")
create opts.make_absolute
-- if p /= Void then
b.append ("You may now log in to " + url ("/user", opts) + " using your username %""+ u.name +"%" and password%N")
-- b.append ("%Nusername: " + u.name + "%Npassword: " + p + "%N%N")
-- end
b.append ("You may also log in by clicking on this link or copying and pasting it in your browser:%N%N")
b.append (url ("/user/reset/" + u.id.out + "/" + unix_timestamp (u.creation_date).out + "/" + a_extra, opts))
-- b.append (url ("/user/reset/" + u.id.out + "/" + unix_timestamp (u.creation_date).out + "/", opts))
b.append ("%N%NThis is a one-time login, so it can be used only once.%N%NAfter logging in, you will be redirected to " + url ("/user/" + u.id.out + "/edit", opts) + " so you can change your password.%N")
b.append ("%N%N-- The %"" + service.site_name + "%" team")
create Result.make (service.site_email, a_mail_address, "Account details for " + u.name + " at " + service.site_name, b)
end
new_user_account_email (a_mail_address: STRING; u: CMS_USER): CMS_EMAIL
local
b: STRING
opts: CMS_URL_API_OPTIONS
do
create b.make_from_string ("New user account %"" + u.name + "%" at " + service.site_name + ". ")
create opts.make_absolute
b.append ("See user account: " + user_url (u) + "%N")
b.append ("%N%N-- The %"" + service.site_name + "%" team")
create Result.make (service.site_email, a_mail_address, "New User Account %"" + u.name + "%" at " + service.site_name, b)
end
end

View File

@@ -0,0 +1,86 @@
note
description: "[
]"
class
USER_RESET_PASSWORD_CMS_EXECUTION
inherit
CMS_EXECUTION
create
make
feature -- Execution
process
-- Computed response message.
local
b: STRING_8
u: detachable CMS_USER
err: BOOLEAN
t: INTEGER_64
l_extra: detachable READABLE_STRING_8
do
create b.make_empty
u := user
if u /= Void then
add_success_message ("You are logged in as " + u.name + ". " + link ("Change your password", "/user/" + u.id.out + "/edit", Void))
set_redirection (front_page_url)
else
if attached {WSF_STRING} request.path_parameter ("uid") as p_uid and then p_uid.is_integer then
u := service.storage.user_by_id (p_uid.integer_value)
end
if u /= Void then
if attached non_empty_string_path_parameter ("last-signed") as p_last_signed then
if p_last_signed.is_integer_64 then
t := p_last_signed.to_integer_64
else
err := True
end
if t > 0 then
if attached u.last_login_date as l_last then
if t /= unix_timestamp (l_last) then
err := True
end
else
if t /= unix_timestamp (u.creation_date) then
err := True
end
end
end
else
err := True
end
if attached non_empty_string_path_parameter ("extra") as s_extra then
l_extra := s_extra
if l_extra /= Void then
if attached {READABLE_STRING_8} u.data_item ("new_password_extra") as u_extra and then u_extra.same_string (l_extra) then
else
err := True
end
else
err := True
end
else
err := True
end
if not err then
login (u, request)
u.remove_data_item ("new_password_extra")
service.storage.save_user (u)
set_redirection (url ("/user/" + u.id.out + "/edit", Void))
set_main_content (b)
end
else
err := True
end
if err then
add_warning_message ("The one-time login link you clicked is invalid.")
set_redirection (front_page_url)
end
end
set_main_content (b)
end
end