2013-04-17 02:08:21 -05:00
|
|
|
class Admin::GroupsController < Admin::AdminController
|
2013-05-08 00:20:38 -05:00
|
|
|
def index
|
2014-05-09 03:22:15 -05:00
|
|
|
groups = Group.order(:name)
|
|
|
|
if search = params[:search]
|
|
|
|
search = search.to_s
|
|
|
|
groups = groups.where("name ilike ?", "%#{search}%")
|
|
|
|
end
|
|
|
|
if params[:ignore_automatic].to_s == "true"
|
|
|
|
groups = groups.where(automatic: false)
|
|
|
|
end
|
2013-05-09 02:37:34 -05:00
|
|
|
render_serialized(groups, BasicGroupSerializer)
|
2013-05-08 00:20:38 -05:00
|
|
|
end
|
|
|
|
|
2014-04-23 12:25:02 -05:00
|
|
|
def show
|
|
|
|
render nothing: true
|
|
|
|
end
|
|
|
|
|
2013-05-08 00:20:38 -05:00
|
|
|
def refresh_automatic_groups
|
|
|
|
Group.refresh_automatic_groups!
|
2013-07-26 18:46:42 -05:00
|
|
|
render json: success_json
|
2013-05-08 00:20:38 -05:00
|
|
|
end
|
|
|
|
|
2014-11-20 11:29:56 -06:00
|
|
|
def update_patch(group)
|
|
|
|
raise Discourse::InvalidAccess.new("automatic groups do not permit membership changes") if group.automatic
|
|
|
|
|
2014-11-21 12:03:29 -06:00
|
|
|
if actions = params[:changes]
|
|
|
|
Array(actions[:add]).each do |username|
|
|
|
|
if user = User.find_by_username(username)
|
|
|
|
group.add(user)
|
|
|
|
end
|
2014-11-20 11:29:56 -06:00
|
|
|
end
|
2014-11-21 12:03:29 -06:00
|
|
|
Array(actions[:delete]).each do |username|
|
|
|
|
if user = User.find_by_username(username)
|
|
|
|
group.remove(user)
|
|
|
|
end
|
2014-11-20 11:29:56 -06:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: success_json
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_put(group)
|
|
|
|
payload = params[:group]
|
2013-12-23 08:46:00 -06:00
|
|
|
|
2014-11-20 11:29:56 -06:00
|
|
|
group.alias_level = payload[:alias_level].to_i if payload[:alias_level].present?
|
|
|
|
group.visible = payload[:visible] == "true"
|
2014-08-17 23:40:54 -05:00
|
|
|
|
2013-06-16 21:54:25 -05:00
|
|
|
if group.automatic
|
2014-11-20 11:29:56 -06:00
|
|
|
# group rename & membership changes are ignored/prohibited for automatic groups
|
2013-06-16 21:54:25 -05:00
|
|
|
else
|
2014-11-20 11:29:56 -06:00
|
|
|
group.usernames = payload[:usernames] if payload[:usernames]
|
|
|
|
group.name = payload[:name] if payload[:name]
|
2013-12-23 08:46:00 -06:00
|
|
|
end
|
|
|
|
|
|
|
|
if group.save
|
|
|
|
render json: success_json
|
|
|
|
else
|
|
|
|
render_json_error group
|
2013-06-16 21:54:25 -05:00
|
|
|
end
|
2013-05-08 20:33:56 -05:00
|
|
|
end
|
|
|
|
|
2014-11-20 11:29:56 -06:00
|
|
|
def update
|
|
|
|
group = Group.find(params[:id].to_i)
|
|
|
|
|
|
|
|
if request.patch?
|
|
|
|
update_patch(group)
|
|
|
|
else
|
|
|
|
update_put(group)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-08 20:33:56 -05:00
|
|
|
def create
|
|
|
|
group = Group.new
|
2014-04-23 12:25:02 -05:00
|
|
|
group.name = (params[:group][:name] || '').strip
|
2013-05-08 20:33:56 -05:00
|
|
|
group.usernames = params[:group][:usernames] if params[:group][:usernames]
|
2014-04-22 15:43:46 -05:00
|
|
|
group.visible = params[:group][:visible] == "true"
|
2013-07-23 23:31:15 -05:00
|
|
|
if group.save
|
|
|
|
render_serialized(group, BasicGroupSerializer)
|
|
|
|
else
|
|
|
|
render_json_error group
|
|
|
|
end
|
2013-05-08 20:33:56 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
|
|
|
group = Group.find(params[:id].to_i)
|
2013-06-16 21:54:25 -05:00
|
|
|
if group.automatic
|
|
|
|
can_not_modify_automatic
|
|
|
|
else
|
|
|
|
group.destroy
|
2013-07-21 21:37:01 -05:00
|
|
|
render json: success_json
|
2013-06-16 21:54:25 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def can_not_modify_automatic
|
|
|
|
render json: {errors: I18n.t('groups.errors.can_not_modify_automatic')}, status: 422
|
2013-05-08 20:33:56 -05:00
|
|
|
end
|
2013-04-17 02:08:21 -05:00
|
|
|
end
|