Class: Collavre::CreativeSharesController

Inherits:
ApplicationController show all
Defined in:
app/controllers/collavre/creative_shares_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'app/controllers/collavre/creative_shares_controller.rb', line 22

def create
  @creative = Creative.find(params[:creative_id]).effective_origin

  unless @creative.has_permission?(Current.user, :admin)
    respond_to do |format|
      format.html { redirect_back fallback_location: creatives_path, alert: t("collavre.creatives.errors.no_permission") }
      format.json { render json: { error: t("collavre.creatives.errors.no_permission") }, status: :forbidden }
    end
    return
  end

  user = nil
  if params[:user_email].present?
    user = User.find_by(email: params[:user_email])
    unless user
      invitation = Invitation.create!(email: params[:user_email], inviter: Current.user, creative: @creative, permission: params[:permission])
      InvitationMailer.with(invitation: invitation).invite.deliver_later
      respond_to do |format|
        format.html { redirect_back fallback_location: creatives_path, notice: t("collavre.invites.invite_sent") }
        format.json { render json: { notice: t("collavre.invites.invite_sent") }, status: :created }
      end
      return
    end

    # Restrict sharing non-searchable AI agents to their owners only
    if user.ai_user? && !user.searchable? && user.created_by_id != Current.user.id
      respond_to do |format|
        format.html { redirect_back fallback_location: creatives_path, alert: t("collavre.creatives.share.cannot_share_private_ai_agent") }
        format.json { render json: { error: t("collavre.creatives.share.cannot_share_private_ai_agent") }, status: :forbidden }
      end
      return
    end
  end

  permission = params[:permission]

  # Enforce read-only for public shares (no user email provided)
  if params[:user_email].blank? && permission != "no_access" && permission != "read"
    permission = "read"
  end

  ancestor_ids = @creative.ancestors.pluck(:id)
  ancestor_shares = CreativeShare.where(creative_id: ancestor_ids, user: user)
                                 .where("permission >= ? or permission = ?", CreativeShare.permissions[permission], CreativeShare.permissions[:no_access])
  closest_parent_share = CreativeShare.closest_parent_share(ancestor_ids, ancestor_shares)

  is_param_no_access = permission == :no_access.to_s
  Rails.logger.debug "### closest_parent_share = #{closest_parent_share.inspect}, is_param_no_access: #{is_param_no_access}"
  if closest_parent_share.present?
    if closest_parent_share.permission == :no_access.to_s
      respond_to do |format|
        format.html { redirect_back fallback_location: creatives_path, alert: t("collavre.creatives.share.can_not_share_by_no_access_in_parent") }
        format.json { render json: { error: t("collavre.creatives.share.can_not_share_by_no_access_in_parent") }, status: :unprocessable_entity }
      end
      return
    else
      unless is_param_no_access
        respond_to do |format|
          format.html { redirect_back fallback_location: creatives_path, alert: t("collavre.creatives.share.already_shared_in_parent") }
          format.json { render json: { error: t("collavre.creatives.share.already_shared_in_parent") }, status: :unprocessable_entity }
        end
        return
      end
    end
  end

  share = CreativeShare.find_or_initialize_by(creative: @creative, user: user)
  share.shared_by ||= Current.user
  share.permission = permission
  if share.save and not is_param_no_access
    if user
      @creative.create_linked_creative_for_user(user)
      Contact.ensure(user: Current.user, contact_user: user)
      Contact.ensure(user: @creative.user, contact_user: user)
    end
    respond_to do |format|
      format.html { redirect_back fallback_location: creatives_path, notice: t("collavre.creatives.share.shared") }
      format.json { render json: { notice: t("collavre.creatives.share.shared") }, status: :created }
    end
  else
    respond_to do |format|
      format.html { redirect_back fallback_location: creatives_path, alert: share.errors.full_messages.to_sentence }
      format.json { render json: { error: share.errors.full_messages.to_sentence }, status: :unprocessable_entity }
    end
  end
end

#destroyObject



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/controllers/collavre/creative_shares_controller.rb', line 132

def destroy
  @creative_share = CreativeShare.find(params[:id])
  unless @creative_share.creative.has_permission?(Current.user, :admin)
    respond_to do |format|
      format.html { redirect_back fallback_location: main_app.root_path, alert: t("collavre.creatives.errors.no_permission") }
      format.json { render json: { error: t("collavre.creatives.errors.no_permission") }, status: :forbidden }
    end
    return
  end

  @creative_share.destroy
  # remove linked creative if it exists
  linked_creative = Creative.find_by(origin_id: @creative_share.creative_id, user_id: @creative_share.user_id)
  linked_creative&.destroy
  respond_to do |format|
    format.html { redirect_back fallback_location: main_app.root_path, notice: t("collavre.creatives.index.share_deleted") }
    format.json { head :no_content }
  end
end

#indexObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/controllers/collavre/creative_shares_controller.rb', line 3

def index
  @creative = Creative.find(params[:creative_id])
  unless @creative.has_permission?(Current.user, :admin)
    head :forbidden and return
  end

  @shared_list = CreativeShare.where(creative: @creative)
                              .includes(user: [ avatar_attachment: :blob ])

  # Build inherited shares from ancestor creatives
  @inherited_shares = build_inherited_shares(@creative)

  @pending_invitations = Invitation.where(creative: @creative, accepted_at: nil)
                                   .where("expires_at > ?", Time.current)
                                   .order(created_at: :desc)

  render partial: "collavre/creatives/share_modal", layout: false
end

#updateObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'app/controllers/collavre/creative_shares_controller.rb', line 109

def update
  @creative_share = CreativeShare.find(params[:id])
  unless @creative_share.creative.has_permission?(Current.user, :admin)
    respond_to do |format|
      format.html { redirect_back fallback_location: main_app.root_path, alert: t("collavre.creatives.errors.no_permission") }
      format.json { render json: { error: t("collavre.creatives.errors.no_permission") }, status: :forbidden }
    end
    return
  end

  if @creative_share.update(permission: params[:permission])
    respond_to do |format|
      format.html { redirect_back fallback_location: main_app.root_path, notice: t("collavre.creatives.share.permission_updated") }
      format.json { render json: { permission: @creative_share.permission }, status: :ok }
    end
  else
    respond_to do |format|
      format.html { redirect_back fallback_location: main_app.root_path, alert: @creative_share.errors.full_messages.to_sentence }
      format.json { render json: { error: @creative_share.errors.full_messages.to_sentence }, status: :unprocessable_entity }
    end
  end
end