2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-10-31 20:02:26 +08:00
|
|
|
class Admin::GroupsController < Admin::StaffController
|
2018-04-06 17:11:00 +08:00
|
|
|
def create
|
2020-08-19 22:41:40 +08:00
|
|
|
guardian.ensure_can_create_group!
|
|
|
|
|
2018-04-06 17:11:00 +08:00
|
|
|
attributes = group_params.to_h.except(:owner_usernames, :usernames)
|
|
|
|
group = Group.new(attributes)
|
2017-04-21 03:47:25 +08:00
|
|
|
|
2018-04-06 17:11:00 +08:00
|
|
|
group.membership_request_template = nil unless group_params[:allow_membership_requests]
|
2016-08-17 00:34:04 +08:00
|
|
|
|
2017-07-27 14:39:47 +08:00
|
|
|
if group_params[:owner_usernames].present?
|
|
|
|
owner_ids = User.where(username: group_params[:owner_usernames].split(",")).pluck(:id)
|
|
|
|
|
|
|
|
owner_ids.each { |user_id| group.group_users.build(user_id: user_id, owner: true) }
|
|
|
|
end
|
|
|
|
|
|
|
|
if group_params[:usernames].present?
|
|
|
|
user_ids = User.where(username: group_params[:usernames].split(",")).pluck(:id)
|
|
|
|
user_ids -= owner_ids if owner_ids
|
|
|
|
|
|
|
|
user_ids.each { |user_id| group.group_users.build(user_id: user_id) }
|
|
|
|
end
|
|
|
|
|
2013-12-23 22:46:00 +08:00
|
|
|
if group.save
|
2017-07-27 14:39:47 +08:00
|
|
|
group.restore_user_count!
|
2015-01-24 01:25:43 +08:00
|
|
|
render_serialized(group, BasicGroupSerializer)
|
2013-12-23 22:46:00 +08:00
|
|
|
else
|
|
|
|
render_json_error group
|
2013-06-17 10:54:25 +08:00
|
|
|
end
|
2013-05-09 09:33:56 +08:00
|
|
|
end
|
|
|
|
|
2015-01-06 01:51:45 +08:00
|
|
|
def destroy
|
2018-04-06 17:11:00 +08:00
|
|
|
group = Group.find_by(id: params[:id])
|
|
|
|
raise Discourse::NotFound unless group
|
2014-11-21 01:29:56 +08:00
|
|
|
|
2015-01-06 01:51:45 +08:00
|
|
|
if group.automatic
|
|
|
|
can_not_modify_automatic
|
2014-11-21 01:29:56 +08:00
|
|
|
else
|
2023-06-29 18:23:28 +08:00
|
|
|
StaffActionLogger.new(current_user).log_group_deletion(group)
|
2021-01-14 02:53:18 +08:00
|
|
|
|
2018-04-06 17:11:00 +08:00
|
|
|
group.destroy!
|
2015-01-06 01:51:45 +08:00
|
|
|
render json: success_json
|
2014-11-21 01:29:56 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-11-09 21:52:04 +08:00
|
|
|
def remove_owner
|
2018-05-07 13:56:27 +08:00
|
|
|
group = Group.find_by(id: params.require(:id))
|
2018-04-06 17:11:00 +08:00
|
|
|
raise Discourse::NotFound unless group
|
|
|
|
|
2015-01-06 01:51:45 +08:00
|
|
|
return can_not_modify_automatic if group.automatic
|
2020-10-20 04:30:21 +08:00
|
|
|
guardian.ensure_can_edit_group!(group)
|
2015-01-06 01:51:45 +08:00
|
|
|
|
2021-02-03 22:11:08 +08:00
|
|
|
if params[:user_id].present?
|
|
|
|
users = [User.find_by(id: params[:user_id].to_i)]
|
|
|
|
elsif usernames = group_params[:usernames].presence
|
|
|
|
users = User.where(username: usernames.split(","))
|
|
|
|
else
|
|
|
|
raise Discourse::InvalidParameters.new(:user_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
users.each do |user|
|
|
|
|
group.group_users.where(user_id: user.id).update_all(owner: false)
|
|
|
|
GroupActionLogger.new(current_user, group).log_remove_user_as_group_owner(user)
|
|
|
|
end
|
2015-01-06 01:51:45 +08:00
|
|
|
|
2015-11-09 21:52:04 +08:00
|
|
|
render json: success_json
|
2013-06-17 10:54:25 +08:00
|
|
|
end
|
|
|
|
|
2021-02-03 22:11:08 +08:00
|
|
|
def set_primary
|
|
|
|
group = Group.find_by(id: params.require(:id))
|
|
|
|
raise Discourse::NotFound unless group
|
|
|
|
|
|
|
|
users = User.where(username: group_params[:usernames].split(","))
|
2022-11-11 19:06:05 +08:00
|
|
|
users.each { |user| guardian.ensure_can_change_primary_group!(user, group) }
|
2021-02-03 22:11:08 +08:00
|
|
|
users.update_all(primary_group_id: params[:primary] == "true" ? group.id : nil)
|
|
|
|
|
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
2020-04-23 00:37:39 +08:00
|
|
|
def automatic_membership_count
|
|
|
|
domains = Group.get_valid_email_domains(params.require(:automatic_membership_email_domains))
|
|
|
|
group_id = params[:id]
|
|
|
|
user_count = 0
|
|
|
|
|
|
|
|
if domains.present?
|
|
|
|
if group_id.present?
|
|
|
|
group = Group.find_by(id: group_id)
|
|
|
|
raise Discourse::NotFound unless group
|
|
|
|
|
|
|
|
return can_not_modify_automatic if group.automatic
|
|
|
|
|
2020-05-26 13:40:09 +08:00
|
|
|
existing_domains = group.automatic_membership_email_domains&.split("|") || []
|
2020-04-23 00:37:39 +08:00
|
|
|
domains -= existing_domains
|
|
|
|
end
|
|
|
|
|
|
|
|
user_count = Group.automatic_membership_users(domains.join("|")).count
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: { user_count: user_count }
|
|
|
|
end
|
|
|
|
|
2013-06-17 10:54:25 +08:00
|
|
|
protected
|
|
|
|
|
2016-11-29 16:25:02 +08:00
|
|
|
def can_not_modify_automatic
|
2020-04-21 09:50:20 +08:00
|
|
|
render_json_error(I18n.t("groups.errors.can_not_modify_automatic"))
|
2016-11-29 16:25:02 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def group_params
|
2019-06-06 10:05:33 +08:00
|
|
|
permitted = %i[
|
2017-07-28 10:37:10 +08:00
|
|
|
name
|
2017-08-29 00:32:08 +08:00
|
|
|
mentionable_level
|
|
|
|
messageable_level
|
2017-07-28 10:37:10 +08:00
|
|
|
visibility_level
|
2019-08-14 21:30:04 +08:00
|
|
|
members_visibility_level
|
2017-07-28 10:37:10 +08:00
|
|
|
automatic_membership_email_domains
|
|
|
|
title
|
|
|
|
primary_group
|
|
|
|
grant_trust_level
|
|
|
|
incoming_email
|
2020-05-25 13:38:47 +08:00
|
|
|
flair_icon
|
|
|
|
flair_upload_id
|
2017-07-28 10:37:10 +08:00
|
|
|
flair_bg_color
|
|
|
|
flair_color
|
|
|
|
bio_raw
|
|
|
|
public_admission
|
|
|
|
public_exit
|
|
|
|
allow_membership_requests
|
|
|
|
full_name
|
|
|
|
default_notification_level
|
2018-04-06 17:11:00 +08:00
|
|
|
membership_request_template
|
2017-08-08 17:53:02 +08:00
|
|
|
owner_usernames
|
2019-08-27 20:09:00 +08:00
|
|
|
usernames
|
2020-08-04 23:02:01 +08:00
|
|
|
publish_read_state
|
|
|
|
notify_users
|
2019-06-06 10:05:33 +08:00
|
|
|
]
|
2020-05-15 21:04:38 +08:00
|
|
|
custom_fields = DiscoursePluginRegistry.editable_group_custom_fields
|
2019-06-06 10:05:33 +08:00
|
|
|
permitted << { custom_fields: custom_fields } unless custom_fields.blank?
|
|
|
|
|
2021-12-09 20:30:27 +08:00
|
|
|
permitted << { associated_group_ids: [] } if guardian.can_associate_groups?
|
|
|
|
|
2021-09-06 08:18:51 +08:00
|
|
|
permitted = permitted | DiscoursePluginRegistry.group_params
|
|
|
|
|
2019-06-06 10:05:33 +08:00
|
|
|
params.require(:group).permit(permitted)
|
2016-11-29 16:25:02 +08:00
|
|
|
end
|
2013-04-17 15:08:21 +08:00
|
|
|
end
|