Skip to content

Commit

Permalink
[feature] permissioned forum access
Browse files Browse the repository at this point in the history
  • Loading branch information
donrestarone and alis-khadka authored Jan 6, 2023
1 parent 6262211 commit 7c50d87
Show file tree
Hide file tree
Showing 11 changed files with 817 additions and 19 deletions.
1 change: 1 addition & 0 deletions app/controllers/comfy/admin/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ def update_params
:can_manage_analytics,
:can_manage_files,
:moderator,
:can_access_forum,
:name,
:can_view_restricted_pages,
:deliver_analytics_report,
Expand Down
19 changes: 18 additions & 1 deletion app/controllers/simple_discussion/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class SimpleDiscussion::ApplicationController < ::ApplicationController
before_action :redirect_if_forum_disabled

before_action :redirect_if_not_logged_in, if: -> { Subdomain.current.forum_is_private }
before_action :redirect_if_no_access_to_forum, if: -> { Subdomain.current.forum_is_private }

def page_number
page = params.fetch(:page, "").gsub(/[^0-9]/, "").to_i
Expand All @@ -12,7 +13,7 @@ def page_number
end

def is_moderator_or_owner?(object)
is_moderator? || object.user == current_user
is_moderator? || (object.user == current_user && current_user.can_access_forum)
end
helper_method :is_moderator_or_owner?

Expand All @@ -21,6 +22,11 @@ def is_moderator?
end
helper_method :is_moderator?

def is_moderator_or_has_forum_access?
is_moderator? || current_user&.can_access_forum
end
helper_method :is_moderator_or_has_forum_access?

def require_mod!
unless current_user.moderator
redirect_to_root
Expand All @@ -39,6 +45,13 @@ def require_mod_or_author_for_thread!
end
end

def require_mod_or_can_access_forum!
unless is_moderator_or_has_forum_access?
flash.alert = "You aren't allowed to do that."
redirect_back(fallback_location: root_path)
end
end

private

def redirect_if_not_logged_in
Expand All @@ -48,6 +61,10 @@ def redirect_if_not_logged_in
end
end

def redirect_if_no_access_to_forum
require_mod_or_can_access_forum!
end

def redirect_if_forum_disabled
unless Subdomain.current.forum_enabled
flash.alert = 'Forum is disabled'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class SimpleDiscussion::ForumPostsController < SimpleDiscussion::ApplicationCont
before_action :authenticate_user!
before_action :set_forum_thread
before_action :set_users_for_mention
before_action :require_mod_or_can_access_forum!, only: [:new, :create]
before_action :set_forum_post, only: [:edit, :update, :destroy]
before_action :require_mod_or_author_for_post!, only: [:edit, :update, :destroy]
before_action :require_mod_or_author_for_thread!, only: [:solved, :unsolved]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class SimpleDiscussion::ForumThreadsController < SimpleDiscussion::ApplicationController
before_action :authenticate_user!, only: [:mine, :participating, :new, :create]
before_action :require_mod_or_can_access_forum!, only: [ :new, :create]
before_action :set_forum_thread, only: [:show, :edit, :update, :destroy]
before_action :require_mod_or_author_for_thread!, only: [:edit, :update, :destroy]
before_action :set_users_for_mention
Expand Down
41 changes: 36 additions & 5 deletions app/views/comfy/admin/users/_form.haml
Original file line number Diff line number Diff line change
Expand Up @@ -79,13 +79,44 @@
= f.check_box :can_view_restricted_pages
%label
Can view restricted web pages
.form-group
= f.check_box :moderator
%label
Can manage forum
.card
.card-header
.d-inline
= f.check_box :moderator, id: 'moderator'
%label
%strong
Can manage forum
.card-body
.form-group
= f.check_box :can_access_forum, id: 'forum_access'
%label
Can access Forum

= f.submit "Update", class: 'btn btn-success'
- unless @user.is_last_admin?
- confirm_message = "Are you sure you want to remove this user? This cannot be undone."
- if @user.forum_threads.present? || @user.forum_posts.present? || @user.events.present?
- confirm_message += "\n\n Note: Deleting this user will affect its associated #{@user.forum_threads.size} forum-threads, #{@user.forum_posts.size} forum-posts and #{@user.events.size} analytics events and will show 'user deleted'."
= link_to "Remove", admin_user_path(id: @user.id), method: :delete, class: 'btn btn-sm btn-danger', data: { confirm: confirm_message.html_safe }
= link_to "Remove", admin_user_path(id: @user.id), method: :delete, class: 'btn btn-sm btn-danger', data: { confirm: confirm_message.html_safe }

:javascript
$(document).ready(function() {
const moderator_checkbox = $('#moderator');
const forum_access_checkbox = $('#forum_access');

// On initialization
if (moderator_checkbox.prop('checked')) {
forum_access_checkbox.prop('checked', true);
} else {
forum_access_checkbox.prop('checked', false);
}

// Add event listener
moderator_checkbox.on('change', function() {
if (this.checked) {
forum_access_checkbox.prop('checked', true);
} else {
forum_access_checkbox.prop('checked', false);
}
});
});
2 changes: 1 addition & 1 deletion app/views/simple_discussion/forum_threads/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,4 @@

<%= render partial: "simple_discussion/forum_posts/forum_post", collection: @forum_thread.forum_posts.includes(:user).sorted %>

<%= render partial: "simple_discussion/forum_posts/form" if user_signed_in? %>
<%= render partial: "simple_discussion/forum_posts/form" if is_moderator_or_has_forum_access? %>
12 changes: 12 additions & 0 deletions db/migrate/20221230035120_add_can_access_forum_to_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class AddCanAccessForumToUsers < ActiveRecord::Migration[6.1]
def up
add_column :users, :can_access_forum, :boolean, default: false

# Giving all existing users permission to access forum.
User.update_all(can_access_forum: true)
end

def down
remove_column :users, :can_access_forum
end
end
1 change: 1 addition & 0 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 7c50d87

Please sign in to comment.