2015-03-12 11:08:48 -04:00
|
|
|
class Groups::GroupMembersController < Groups::ApplicationController
|
2013-06-17 09:51:43 -04:00
|
|
|
# Authorize
|
2016-04-18 12:53:32 -04:00
|
|
|
before_action :authorize_admin_group_member!, except: [:index, :leave, :request_access]
|
2013-06-17 09:51:43 -04:00
|
|
|
|
2015-03-13 11:27:51 -04:00
|
|
|
def index
|
|
|
|
@project = @group.projects.find(params[:project_id]) if params[:project_id]
|
|
|
|
@members = @group.group_members
|
2016-04-18 12:53:32 -04:00
|
|
|
@members = @members.non_pending unless can?(current_user, :admin_group, @group)
|
2015-03-13 11:27:51 -04:00
|
|
|
|
|
|
|
if params[:search].present?
|
|
|
|
users = @group.users.search(params[:search]).to_a
|
|
|
|
@members = @members.where(user_id: users)
|
|
|
|
end
|
|
|
|
|
|
|
|
@members = @members.order('access_level DESC').page(params[:page]).per(50)
|
2015-11-17 09:51:40 -05:00
|
|
|
|
|
|
|
@group_member = @group.group_members.new
|
2015-03-13 11:27:51 -04:00
|
|
|
end
|
2013-06-17 09:51:43 -04:00
|
|
|
|
|
|
|
def create
|
2015-04-10 08:46:09 -04:00
|
|
|
@group.add_users(params[:user_ids].split(','), params[:access_level], current_user)
|
2013-06-17 09:51:43 -04:00
|
|
|
|
2015-03-13 11:27:51 -04:00
|
|
|
redirect_to group_group_members_path(@group), notice: 'Users were successfully added.'
|
2013-06-17 09:51:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2015-11-17 09:51:40 -05:00
|
|
|
@group_member = @group.group_members.find(params[:id])
|
2015-07-28 10:32:15 -04:00
|
|
|
|
2015-11-17 09:51:40 -05:00
|
|
|
return render_403 unless can?(current_user, :update_group_member, @group_member)
|
2015-07-28 10:32:15 -04:00
|
|
|
|
2015-11-17 09:51:40 -05:00
|
|
|
@group_member.update_attributes(member_params)
|
2013-06-17 09:51:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2015-03-13 11:16:51 -04:00
|
|
|
@group_member = @group.group_members.find(params[:id])
|
2014-10-20 06:18:07 -04:00
|
|
|
|
2015-11-17 09:51:40 -05:00
|
|
|
return render_403 unless can?(current_user, :destroy_group_member, @group_member)
|
|
|
|
|
2016-04-18 12:53:32 -04:00
|
|
|
@group_member.request? ? @group_member.decline_request : @group_member.destroy
|
2015-11-17 09:51:40 -05:00
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
format.html { redirect_to group_group_members_path(@group), notice: 'User was successfully removed from group.' }
|
2016-03-15 21:16:25 -04:00
|
|
|
format.js { head :ok }
|
2013-06-17 09:51:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-10 09:26:03 -04:00
|
|
|
def resend_invite
|
2015-04-14 09:16:00 -04:00
|
|
|
redirect_path = group_group_members_path(@group)
|
2015-04-16 08:03:37 -04:00
|
|
|
|
2015-04-10 09:26:03 -04:00
|
|
|
@group_member = @group.group_members.find(params[:id])
|
2015-04-14 09:16:00 -04:00
|
|
|
|
2015-04-10 09:26:03 -04:00
|
|
|
if @group_member.invite?
|
|
|
|
@group_member.resend_invite
|
|
|
|
|
2015-04-14 09:16:00 -04:00
|
|
|
redirect_to redirect_path, notice: 'The invitation was successfully resent.'
|
2015-04-10 09:26:03 -04:00
|
|
|
else
|
2015-04-14 09:16:00 -04:00
|
|
|
redirect_to redirect_path, alert: 'The invitation has already been accepted.'
|
2015-04-10 09:26:03 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-13 11:28:33 -04:00
|
|
|
def leave
|
2016-04-18 12:53:32 -04:00
|
|
|
@group_member =
|
|
|
|
@group.group_members.find_by(user_id: current_user.id) ||
|
|
|
|
@group.group_members.find_by(created_by_id: current_user.id)
|
2015-04-16 08:03:37 -04:00
|
|
|
|
2015-03-13 11:28:33 -04:00
|
|
|
if can?(current_user, :destroy_group_member, @group_member)
|
2016-04-18 12:53:32 -04:00
|
|
|
notice =
|
|
|
|
if @group_member.request?
|
|
|
|
'You withdrawn your access request to the group.'
|
|
|
|
else
|
|
|
|
"You left #{@group.name} group."
|
|
|
|
end
|
2015-03-13 11:28:33 -04:00
|
|
|
@group_member.destroy
|
2015-11-17 09:51:40 -05:00
|
|
|
|
2016-04-18 12:53:32 -04:00
|
|
|
redirect_to dashboard_groups_path, notice: notice
|
2015-03-13 11:28:33 -04:00
|
|
|
else
|
2015-05-29 08:11:37 -04:00
|
|
|
if @group.last_owner?(current_user)
|
|
|
|
redirect_to(dashboard_groups_path, alert: "You can not leave #{group.name} group because you're the last owner. Transfer or delete the group.")
|
|
|
|
else
|
|
|
|
return render_403
|
|
|
|
end
|
2015-03-13 11:28:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-18 12:53:32 -04:00
|
|
|
def request_access
|
|
|
|
@group.request_access(current_user)
|
|
|
|
|
|
|
|
redirect_to group_path(@group), notice: 'Your request for access has been queued for review.'
|
|
|
|
end
|
|
|
|
|
|
|
|
def approve
|
|
|
|
@group_member = @group.group_members.request.find(params[:id])
|
|
|
|
|
|
|
|
return render_403 unless can?(current_user, :update_group_member, @group_member)
|
|
|
|
|
|
|
|
@group_member.accept_request
|
|
|
|
|
|
|
|
redirect_to group_group_members_path(@group)
|
|
|
|
end
|
|
|
|
|
2013-06-17 09:51:43 -04:00
|
|
|
protected
|
|
|
|
|
2014-06-26 08:00:09 -04:00
|
|
|
def member_params
|
2014-09-14 14:52:54 -04:00
|
|
|
params.require(:group_member).permit(:access_level, :user_id)
|
2014-06-26 08:00:09 -04:00
|
|
|
end
|
2013-06-17 09:51:43 -04:00
|
|
|
end
|