FIX: prevents name collision by namespacing guardian method

This commit is contained in:
jjaffeux 2020-04-14 19:03:55 +02:00
parent 4e7c798971
commit cc193bf2ba
7 changed files with 24 additions and 24 deletions

View File

@ -24,7 +24,7 @@ module DiscoursePostEvent
def invite def invite
event = Event.find(params[:id]) event = Event.find(params[:id])
guardian.ensure_can_act_on_event!(event) guardian.ensure_can_act_on_discourse_post_event!(event)
invites = Array(params.permit(invites: [])[:invites]) invites = Array(params.permit(invites: [])[:invites])
users = Invitee.extract_uniq_usernames(invites) users = Invitee.extract_uniq_usernames(invites)
@ -44,7 +44,7 @@ module DiscoursePostEvent
def destroy def destroy
event = Event.find(params[:id]) event = Event.find(params[:id])
guardian.ensure_can_act_on_event!(event) guardian.ensure_can_act_on_discourse_post_event!(event)
event.publish_update! event.publish_update!
event.destroy event.destroy
render json: success_json render json: success_json
@ -54,7 +54,7 @@ module DiscoursePostEvent
DistributedMutex.synchronize("discourse-post-event[event-update]") do DistributedMutex.synchronize("discourse-post-event[event-update]") do
event = Event.find(params[:id]) event = Event.find(params[:id])
guardian.ensure_can_edit!(event.post) guardian.ensure_can_edit!(event.post)
guardian.ensure_can_act_on_event!(event) guardian.ensure_can_act_on_discourse_post_event!(event)
event.update_with_params!(event_params) event.update_with_params!(event_params)
serializer = EventSerializer.new(event, scope: guardian) serializer = EventSerializer.new(event, scope: guardian)
render_json_dump(serializer) render_json_dump(serializer)
@ -64,7 +64,7 @@ module DiscoursePostEvent
def create def create
event = Event.new(event_params) event = Event.new(event_params)
guardian.ensure_can_edit!(event.post) guardian.ensure_can_edit!(event.post)
guardian.ensure_can_create_event! guardian.ensure_can_create_discourse_post_event!
event.enforce_utc!(event_params) event.enforce_utc!(event_params)
case event_params[:status].to_i case event_params[:status].to_i

View File

@ -32,7 +32,7 @@ module DiscoursePostEvent
def create def create
status = Invitee.statuses[invitee_params[:status].to_sym] status = Invitee.statuses[invitee_params[:status].to_sym]
event = Event.find(invitee_params[:post_id]) event = Event.find(invitee_params[:post_id])
guardian.ensure_can_act_on_event!(event) guardian.ensure_can_act_on_discourse_post_event!(event)
invitee = Invitee.create!( invitee = Invitee.create!(
status: status, status: status,
post_id: invitee_params[:post_id], post_id: invitee_params[:post_id],

View File

@ -13,13 +13,13 @@ module DiscoursePostEvent
attributes :raw_invitees attributes :raw_invitees
attributes :post attributes :post
attributes :name attributes :name
attributes :can_act_on_event attributes :can_act_on_discourse_post_event
attributes :can_update_attendance attributes :can_update_attendance
attributes :is_expired attributes :is_expired
attributes :should_display_invitees attributes :should_display_invitees
def can_act_on_event def can_act_on_discourse_post_event
scope.can_act_on_event?(object) scope.can_act_on_discourse_post_event?(object)
end end
def is_expired def is_expired

View File

@ -150,7 +150,7 @@ export default createWidget("discourse-post-event", {
</div> </div>
</div> </div>
{{#if state.eventModel.can_act_on_event}} {{#if state.eventModel.can_act_on_discourse_post_event}}
<div class="actions"> <div class="actions">
{{attach {{attach
widget="button" widget="button"
@ -204,7 +204,7 @@ export default createWidget("discourse-post-event", {
}} }}
{{#unless state.eventModel.is_expired}} {{#unless state.eventModel.is_expired}}
{{#if state.eventModel.can_act_on_event}} {{#if state.eventModel.can_act_on_discourse_post_event}}
{{#if transformed.isPublicEvent}} {{#if transformed.isPublicEvent}}
{{attach {{attach
widget="button" widget="button"

View File

@ -7,7 +7,7 @@ function initializeEventBuilder(api) {
api.onToolbarCreate(toolbar => { api.onToolbarCreate(toolbar => {
if ( if (
!currentUser || !currentUser ||
!currentUser.can_create_event || !currentUser.can_create_discourse_post_event ||
!toolbar.context.outletArgs !toolbar.context.outletArgs
) { ) {
return; return;

View File

@ -26,12 +26,12 @@ module DiscoursePostEvent
extracted_event = extracted_events.first extracted_event = extracted_events.first
if @post.acting_user && @post.event if @post.acting_user && @post.event
if !@post.acting_user.can_act_on_event?(@post.event) if !@post.acting_user.can_act_on_discourse_post_event?(@post.event)
@post.errors.add(:base, I18n.t("discourse_post_event.errors.models.event.acting_user_not_allowed_to_act_on_this_event")) @post.errors.add(:base, I18n.t("discourse_post_event.errors.models.event.acting_user_not_allowed_to_act_on_this_event"))
return false return false
end end
else else
if !@post.acting_user || !@post.acting_user.can_create_event? if !@post.acting_user || !@post.acting_user.can_create_discourse_post_event?
@post.errors.add(:base, I18n.t("discourse_post_event.errors.models.event.acting_user_not_allowed_to_create_event")) @post.errors.add(:base, I18n.t("discourse_post_event.errors.models.event.acting_user_not_allowed_to_create_event"))
return false return false
end end

View File

@ -126,9 +126,9 @@ after_initialize do
end end
end end
add_to_class(:user, :can_create_event?) do add_to_class(:user, :can_create_discourse_post_event?) do
return @can_create_event if defined?(@can_create_event) return @can_create_discourse_post_event if defined?(@can_create_discourse_post_event)
@can_create_event = begin @can_create_discourse_post_event = begin
return true if staff? return true if staff?
allowed_groups = SiteSetting.discourse_post_event_allowed_on_groups.split('|').compact allowed_groups = SiteSetting.discourse_post_event_allowed_on_groups.split('|').compact
allowed_groups.present? && groups.where(id: allowed_groups).exists? allowed_groups.present? && groups.where(id: allowed_groups).exists?
@ -141,23 +141,23 @@ after_initialize do
user && (user.staff? || user.id == invitee.user_id) user && (user.staff? || user.id == invitee.user_id)
end end
add_to_class(:guardian, :can_create_event?) { user && user.can_create_event? } add_to_class(:guardian, :can_create_discourse_post_event?) { user && user.can_create_discourse_post_event? }
add_to_serializer(:current_user, :can_create_event) do add_to_serializer(:current_user, :can_create_discourse_post_event) do
object.can_create_event? object.can_create_discourse_post_event?
end end
add_to_class(:user, :can_act_on_event?) do |event| add_to_class(:user, :can_act_on_discourse_post_event?) do |event|
return @can_act_on_event if defined?(@can_act_on_event) return @can_act_on_discourse_post_event if defined?(@can_act_on_discourse_post_event)
@can_act_on_event = begin @can_act_on_discourse_post_event = begin
return true if admin? return true if admin?
can_create_event? && event.post.user_id == id can_create_discourse_post_event? && event.post.user_id == id
rescue rescue
false false
end end
end end
add_to_class(:guardian, :can_act_on_event?) { |event| user && user.can_act_on_event?(event) } add_to_class(:guardian, :can_act_on_discourse_post_event?) { |event| user && user.can_act_on_discourse_post_event?(event) }
add_class_method(:group, :discourse_post_event_allowed_groups) do add_class_method(:group, :discourse_post_event_allowed_groups) do
where(id: SiteSetting.discourse_post_event_allowed_on_groups.split('|').compact) where(id: SiteSetting.discourse_post_event_allowed_on_groups.split('|').compact)