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
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]
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
|