Class: Admin::GroupsController

Inherits:
StaffController
  • Object
show all
Defined in:
app/controllers/admin/groups_controller.rb

Instance Method Summary collapse

Instance Method Details

#automatic_membership_countObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/controllers/admin/groups_controller.rb', line 81

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

      existing_domains = group.automatic_membership_email_domains&.split("|") || []
      domains -= existing_domains
    end

    user_count = Group.automatic_membership_users(domains.join("|")).count
  end

  render json: { user_count: user_count }
end

#createObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/controllers/admin/groups_controller.rb', line 4

def create
  guardian.ensure_can_create_group!

  attributes = group_params.to_h.except(:owner_usernames, :usernames)
  group = Group.new(attributes)

  group.membership_request_template = nil unless group_params[:allow_membership_requests]

  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

  if group.save
    group.restore_user_count!
    render_serialized(group, BasicGroupSerializer)
  else
    render_json_error group
  end
end

#destroyObject



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/controllers/admin/groups_controller.rb', line 33

def destroy
  group = Group.find_by(id: params[:id])
  raise Discourse::NotFound unless group

  if group.automatic
    can_not_modify_automatic
  else
    StaffActionLogger.new(current_user).log_group_deletion(group)

    group.destroy!
    render json: success_json
  end
end

#remove_ownerObject



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/admin/groups_controller.rb', line 47

def remove_owner
  group = Group.find_by(id: params.require(:id))
  raise Discourse::NotFound unless group

  return can_not_modify_automatic if group.automatic
  guardian.ensure_can_edit_group!(group)

  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

  render json: success_json
end

#set_primaryObject



70
71
72
73
74
75
76
77
78
79
# File 'app/controllers/admin/groups_controller.rb', line 70

def set_primary
  group = Group.find_by(id: params.require(:id))
  raise Discourse::NotFound unless group

  users = User.where(username: group_params[:usernames].split(","))
  users.each { |user| guardian.ensure_can_change_primary_group!(user, group) }
  users.update_all(primary_group_id: params[:primary] == "true" ? group.id : nil)

  render json: success_json
end