From 4e8f32752f7333308e6cfa54f7b958a9426d1320 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20=C3=85dahl?= Date: Fri, 15 Oct 2010 22:58:04 +0200 Subject: [PATCH] Display dialog when unauthorized requests are performed. Changed more name parts of dyn_dialog to dialog. --- include/ui.hrl | 8 +++--- include/utils.hrl | 2 +- res/translations/en_US.res | 5 +++- res/translations/sv_SE.res | 5 +++- src/actions/action_dialog.erl | 16 +++++------ src/cms/cms_admin_view.erl | 2 +- src/dialog.erl | 1 - src/elements/element_dialog.erl | 10 +++++-- src/session.erl | 33 ++++++++++++----------- src/session_view.erl | 47 ++++++++++++++++++++++++++++++++- wwwroot/jabber.se/jabber.se.css | 4 +++ wwwroot/jabber.se/jabber.se.js | 6 ++++- 12 files changed, 102 insertions(+), 37 deletions(-) diff --git a/include/ui.hrl b/include/ui.hrl index 97a83d6..ebae2e3 100644 --- a/include/ui.hrl +++ b/include/ui.hrl @@ -27,7 +27,7 @@ -record(strong, {?ELEMENT_BASE(element_typeface), body = "", text = "", html_encode = true}). -record(state_panel, {?ELEMENT_BASE(element_state_panel), bodies, inline = false, visible = false, init_state}). --record(dialog, {?ELEMENT_BASE(element_dialog), corner = close, title = "", body = ""}). +-record(dialog, {?ELEMENT_BASE(element_dialog), corner = close, title = "", title_class = [], body = ""}). -record(ext_link, {?ELEMENT_BASE(element_ext_link), url, type, rel, title}). @@ -41,9 +41,9 @@ -record(state_panel_show, {?ACTION_BASE(action_state_panel), key}). -record(state_panel_hide, {?ACTION_BASE(action_state_panel)}). --record(dyn_dialog_back, {?ACTION_BASE(action_dialog)}). --record(dyn_dialog_set, {?ACTION_BASE(action_dialog), title, body}). --record(dyn_dialog_corner, {?ACTION_BASE(action_dialog), corner}). +-record(dialog_back, {?ACTION_BASE(action_dialog)}). +-record(dialog_set, {?ACTION_BASE(action_dialog), title, body}). +-record(dialog_corner, {?ACTION_BASE(action_dialog), corner}). -record(dialog_show, {?ACTION_BASE(action_dialog)}). -record(dialog_hide, {?ACTION_BASE(action_dialog)}). diff --git a/include/utils.hrl b/include/utils.hrl index 847d13a..c1aa463 100644 --- a/include/utils.hrl +++ b/include/utils.hrl @@ -95,7 +95,7 @@ % % AUTH(Statement) -> Statement % --define(AUTH(Statement), ?WHEN(session:authenticated(), (fun() -> session:env(), Statement end)())). +-define(AUTH(Statement), ?EITHER(session:authenticated(), (fun() -> session:env(), Statement end)(), session:unauthorized_request())). % % Logging diff --git a/res/translations/en_US.res b/res/translations/en_US.res index ac062db..b21528b 100644 --- a/res/translations/en_US.res +++ b/res/translations/en_US.res @@ -40,5 +40,8 @@ {msg_id_post_remove, "Remove post"}, {msg_id_post_title, "Post content"}, {msg_id_post_untitled, "(untitled)"}, - {msg_id_posts_empty, "No posts..."} + {msg_id_posts_empty, "No posts..."}, + {msg_id_reload, "Reload"}, + {msg_id_reload_page, "Page needs to be reloaded"}, + {msg_id_unauthorized_request, "Unauthorized request"} ]. diff --git a/res/translations/sv_SE.res b/res/translations/sv_SE.res index 6f3efa0..866c169 100644 --- a/res/translations/sv_SE.res +++ b/res/translations/sv_SE.res @@ -40,5 +40,8 @@ {msg_id_post_remove, "Ta bort"}, {msg_id_post_title, "Titel"}, {msg_id_post_untitled, "(ingen titel)"}, - {msg_id_posts_empty, "Inga inlägg"} + {msg_id_posts_empty, "Inga inlägg"}, + {msg_id_reload, "Sidan behöver laddas om"}, + {msg_id_reload_page, "Ladda om"}, + {msg_id_unauthorized_request, "Åtkomst förbjuden"} ]. diff --git a/src/actions/action_dialog.erl b/src/actions/action_dialog.erl index 6bb8d11..3532166 100644 --- a/src/actions/action_dialog.erl +++ b/src/actions/action_dialog.erl @@ -34,17 +34,17 @@ render_action(#dialog_show{target = Target}) -> render_action(#dialog_hide{target = Target}) -> #js_call{fname = "$Site.$dialog_hide", args = [Target]}; -render_action(#dyn_dialog_back{target = Id}) -> +render_action(#dialog_back{target = Id}) -> #jquery_cast{anchor = Id, target = corner_link, cast = click}; -render_action(#dyn_dialog_corner{ +render_action(#dialog_corner{ target = Target, corner = Corner, actions = Actions }) -> #update{anchor = Target, target = corner, elements = element_dialog:render_corner(Corner, Actions)}; -render_action(#dyn_dialog_set{ +render_action(#dialog_set{ target = Target, title = Title, body = Body}) -> @@ -61,16 +61,16 @@ hide(Id) -> reset_actions(Id, Title, Body, Corner, Actions) -> [ - #dyn_dialog_corner{target = Id, corner = Corner, actions = Actions}, - #dyn_dialog_set{target = Id, title = Title, body = Body} + #dialog_corner{target = Id, corner = Corner, actions = Actions}, + #dialog_set{target = Id, title = Title, body = Body} ]. set(Id, Title, Body) -> - wf:wire(#dyn_dialog_set{target = Id, title = Title, body = Body}). + wf:wire(#dialog_set{target = Id, title = Title, body = Body}). corner(Id, Corner, Actions) -> - wf:wire(#dyn_dialog_corner{target = Id, corner = Corner, actions = Actions}). + wf:wire(#dialog_corner{target = Id, corner = Corner, actions = Actions}). back(Id) -> - wf:wire(#dyn_dialog_back{target = Id}). + wf:wire(#dialog_back{target = Id}). diff --git a/src/cms/cms_admin_view.erl b/src/cms/cms_admin_view.erl index ded6087..c3d42bc 100644 --- a/src/cms/cms_admin_view.erl +++ b/src/cms/cms_admin_view.erl @@ -34,7 +34,7 @@ set_body_close(Title, Body) -> action_dialog:corner(?DIALOG_ID, close, #dialog_hide{target = ?DIALOG_ID}). back_action() -> - #dyn_dialog_back{target = ?DIALOG_ID}. + #dialog_back{target = ?DIALOG_ID}. back() -> action_dialog:back(?DIALOG_ID). diff --git a/src/dialog.erl b/src/dialog.erl index b8f84f3..a9dfcda 100644 --- a/src/dialog.erl +++ b/src/dialog.erl @@ -31,6 +31,5 @@ show(Dialog, ContentFun) -> Class = Dialog#dialog.class, wf:update(dialogs, Dialog#dialog{class = lists:flatten([top_dialog, Class])}), ContentFun(), - io:format("show ing dialog ~p~n", [Id]), wf:wire(Id, #dialog_show{}). diff --git a/src/elements/element_dialog.erl b/src/elements/element_dialog.erl index f93753b..c10c80a 100644 --- a/src/elements/element_dialog.erl +++ b/src/elements/element_dialog.erl @@ -43,15 +43,21 @@ render_corner(Corner, Actions) -> }. -spec render_element(dialog()) -> term(). -render_element(#dialog{id = Id, class = Class, title = Title, body = Body}) -> +render_element(#dialog{ + id = Id, + class = Class, + title = Title, + title_class = TitleClass, + body = Body}) -> Corner = #panel{id = corner}, + FinalTitleClass = lists:flatten([TitleClass, dyn_dialog_title]), #panel{ class = [dialog, Class], id = Id, style = ?HIDDEN, body = [ - #panel{class = dyn_dialog_title, body = [#h2{id = title, text = Title}, Corner]}, + #panel{class = FinalTitleClass, body = [#h2{id = title, text = Title}, Corner]}, #hr{}, #panel{id = body, body = Body} ] diff --git a/src/session.erl b/src/session.erl index 11de339..22dc28a 100644 --- a/src/session.erl +++ b/src/session.erl @@ -18,12 +18,14 @@ -module(session). -include_lib("nitrogen/include/wf.inc"). --export([authenticated/0, event/1, env/0, language/1, page_init/0]). +-export([authenticated/0, event/1, env/0, language/1, page_init/0, unauthorized_request/0]). -include("include/config.hrl"). -include("include/utils.hrl"). -include("include/ui.hrl"). +-include_lib("nitrogen/include/wf.hrl"). + % % Constants % @@ -112,8 +114,7 @@ event(do_logout) -> session:env(), wf:clear_session(), - wf:wire(admin_panel, #fade{actions = #update{type = remove}}), - wf:wire(#state_panel_set{target = login_link, key = anonymous}); + session_view:logged_out(); event(Event) -> ?LOG_WARNING("Unhandled event \"~p\".~n", [Event]). @@ -123,13 +124,8 @@ event(Event) -> % page_init() -> - case wf:user() of - undefined -> - ok; - _ -> - wf:insert_top(menu_bar_center, session_view:admin_panel()), - wf:wire(#show{target = admin_panel}) - end. + User = wf:user(), + session_view:page_init(User). % % Login @@ -144,13 +140,10 @@ login() -> % Set session user value wf:user(Username), - % Update elements - wf:wire(#state_panel_set{target = login_link, key = authenticated}), - wf:wire(#state_panel_set{target = login_dialog, animate = true, key = success, actions = #focus{target = login_dialog_close_button}}), - wf:insert_bottom(menu_bar_center, session_view:admin_panel()), - wf:wire(#appear{target = admin_panel, speed = "slow"}); + % Update view + session_view:logged_in(); _ -> - wf:wire(#state_panel_set{target = login_dialog, animate = true, key = fail}) + session_view:login_failed() end. @@ -182,3 +175,11 @@ authenticate1(Username, Password) -> _ -> denied end. + +% +% Timeout +% + +unauthorized_request() -> + session_view:unauthorized_request(). + diff --git a/src/session_view.erl b/src/session_view.erl index 3505ea0..64ec6e4 100644 --- a/src/session_view.erl +++ b/src/session_view.erl @@ -17,7 +17,11 @@ % -module(session_view). --export([login_dialog/0, login_panel/0, admin_panel/0]). +-export([ + login_dialog/0, login_panel/0, admin_panel/0, + logged_out/0, logged_in/0, login_failed/0, page_init/1, + unauthorized_request/0 + ]). -include_lib("nitrogen/include/wf.hrl"). -include("include/utils.hrl"). @@ -177,3 +181,44 @@ admin_panel() -> ] }. +logged_out() -> + wf:wire(admin_panel, #fade{actions = #update{type = remove}}), + wf:wire(#state_panel_set{target = login_link, key = anonymous}). + +logged_in() -> + % Update elements + wf:wire(#state_panel_set{target = login_link, key = authenticated}), + wf:wire(#state_panel_set{target = login_dialog, animate = true, key = success, actions = #focus{target = login_dialog_close_button}}), + wf:insert_bottom(menu_bar_center, admin_panel()), + wf:wire(#appear{target = admin_panel, speed = "slow"}). + +login_failed() -> + wf:wire(#state_panel_set{target = login_dialog, animate = true, key = fail}). + +page_init(undefined) -> + ok; +page_init(_User) -> + wf:insert_top(menu_bar_center, admin_panel()), + wf:wire(#show{target = admin_panel}). + +unauthorized_request() -> + % display logged out dialog + Dialog = #dialog{ + id = unauthorized_request_dialog, + title = ?T(msg_id_unauthorized_request), + title_class = [center, warning], + body = #panel{ + class = center, + body = [ + % TODO add ability to login + % TODO add reason: session timed out/other (?) + #p{body = ?T(msg_id_reload_page)}, + #button{ + text = ?T(msg_id_reload), + actions = #event{type = click, actions = #script{script = "location.reload();"}} + } + ] + } + }, + wf:wire(#site_cast{cast = overlay_inhibit_hide}), + dialog:show(Dialog). diff --git a/wwwroot/jabber.se/jabber.se.css b/wwwroot/jabber.se/jabber.se.css index f263de2..4bf460c 100644 --- a/wwwroot/jabber.se/jabber.se.css +++ b/wwwroot/jabber.se/jabber.se.css @@ -72,6 +72,10 @@ ul.contact { text-align: center; } +.warning { + color: rgb(245, 100, 100); +} + /* * Basic elements */ diff --git a/wwwroot/jabber.se/jabber.se.js b/wwwroot/jabber.se/jabber.se.js index 8bf60d3..f4ebf21 100644 --- a/wwwroot/jabber.se/jabber.se.js +++ b/wwwroot/jabber.se/jabber.se.js @@ -227,7 +227,7 @@ Site.prototype.$dialog_show = function(dialog_id, callback) { if (this.active_dialog) { this.active_dialog = null; - dialog.fadeIn('fast', function() { $Site.show_dialog(dialog_id, callback); }); + dialog.fadeIn('fast', function() { $Site.$dialog_show(dialog_id, callback); }); } else { @@ -247,6 +247,10 @@ Site.prototype.$overlay_hide = function(callback) { } } +Site.prototype.$overlay_inhibit_hide = function() { + this.overlay.unbind('click'); +} + Site.prototype.$set_overlay_size = function() { this.overlay.height($(document).height()); this.overlay.width($(document).width());