From bd34b803f13565a47ce22028d0904ddbff961bb0 Mon Sep 17 00:00:00 2001 From: Alex Ling Date: Wed, 30 Dec 2020 11:12:56 +0000 Subject: [PATCH 01/14] Tokens take precedence over default user setting --- src/handlers/auth_handler.cr | 13 ++++++++++--- src/util/web.cr | 9 ++++++--- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/handlers/auth_handler.cr b/src/handlers/auth_handler.cr index 1db4376..3f094eb 100644 --- a/src/handlers/auth_handler.cr +++ b/src/handlers/auth_handler.cr @@ -74,10 +74,17 @@ class AuthHandler < Kemal::Handler end if request_path_startswith env, ["/admin", "/api/admin", "/download"] - unless validate_token_admin(env) || - Storage.default.username_is_admin Config.current.default_username - env.response.status_code = 403 + # The token (if exists) takes precedence over the default user option. + # this is why we check the default username first before checking the + # token. + should_reject = true + if Storage.default.username_is_admin Config.current.default_username + should_reject = false end + if env.session.string? "token" + should_reject = !validate_token_admin(env) + end + env.response.status_code = 403 if should_reject end call_next env diff --git a/src/util/web.cr b/src/util/web.cr index 647a536..504bbaa 100644 --- a/src/util/web.cr +++ b/src/util/web.cr @@ -4,13 +4,16 @@ macro layout(name) base_url = Config.current.base_url begin is_admin = false - if token = env.session.string? "token" - is_admin = @context.storage.verify_admin token - end + # The token (if exists) takes precedence over the default user option. + # this is why we check the default username first before checking the + # token. if Config.current.disable_login is_admin = @context.storage. username_is_admin Config.current.default_username end + if token = env.session.string? "token" + is_admin = @context.storage.verify_admin token + end page = {{name}} render "src/views/#{{{name}}}.html.ecr", "src/views/layout.html.ecr" rescue e From ce88acb9e5b3ff564d5e6d91c64449522115e092 Mon Sep 17 00:00:00 2001 From: Alex Ling Date: Mon, 28 Dec 2020 16:29:29 +0000 Subject: [PATCH 02/14] Simplify the request_path_startswith helper method --- src/util/web.cr | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/util/web.cr b/src/util/web.cr index 504bbaa..03af6a2 100644 --- a/src/util/web.cr +++ b/src/util/web.cr @@ -59,12 +59,7 @@ def hash_to_query(hash) end def request_path_startswith(env, ary) - ary.each do |prefix| - if env.request.path.starts_with? prefix - return true - end - end - false + ary.any? { |prefix| env.request.path.starts_with? prefix } end def requesting_static_file(env) From 45a81ad5f6f6ae334fc6d890cb366811eea8dca1 Mon Sep 17 00:00:00 2001 From: Alex Ling Date: Tue, 29 Dec 2020 04:33:55 +0000 Subject: [PATCH 03/14] Display the entries and sub-titles count --- src/library/title.cr | 14 ++++++++++++-- src/views/components/card.html.ecr | 2 +- src/views/title.html.ecr | 2 +- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/library/title.cr b/src/library/title.cr index 52e9838..d72bb48 100644 --- a/src/library/title.cr +++ b/src/library/title.cr @@ -108,8 +108,18 @@ class Title ary.reverse end - def size - @entries.size + @title_ids.size + # Returns a string the describes the content of the title + # e.g., - 3 titles and 1 entry + # - 4 entries + # - 1 title + def content_label + ary = [] of String + tsize = titles.size + esize = entries.size + + ary << "#{tsize} #{tsize > 1 ? "titles" : "title"}" if tsize > 0 + ary << "#{esize} #{esize > 1 ? "entries" : "entry"}" if esize > 0 + ary.join " and " end def get_entry(eid) diff --git a/src/views/components/card.html.ecr b/src/views/components/card.html.ecr index a116fee..b85d39e 100644 --- a/src/views/components/card.html.ecr +++ b/src/views/components/card.html.ecr @@ -76,7 +76,7 @@ <% end %> <% if item.is_a? Title %> <% if grouped_count == 1 %> -

<%= item.size %> entries

+

<%= item.content_label %>

<% else %>

<%= grouped_count %> new entries

<% end %> diff --git a/src/views/title.html.ecr b/src/views/title.html.ecr index 9d22f22..53db6ab 100644 --- a/src/views/title.html.ecr +++ b/src/views/title.html.ecr @@ -32,7 +32,7 @@ <%- end -%>
  • <%= title.display_name %>
  • -

    <%= title.size %> entries found

    +

    <%= title.content_label %> found